summaryrefslogtreecommitdiff
path: root/src/MediaTransporterSenderToServerRtsp.cpp
blob: 2b14e1c883e6a9d64e1502443fa147990bdc6a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gst/rtsp/rtsp.h>

#include "MediaTransporterBase.h"
#include "MediaTransporterException.h"
#include "MediaTransporterSenderToServerRtsp.h"
#include "MediaTransporterGst.h"
#include "MediaTransporterLog.h"
#include "MediaTransporterUtil.h"
#include "MediaTransporterParseIni.h"

using namespace tizen_media_transporter;
using namespace tizen_media_transporter::param::rtsp;

MediaTransporterSenderToServerRtsp::MediaTransporterSenderToServerRtsp()
{
	LOG_DEBUG("ctor: %p", this);
}

MediaTransporterSenderToServerRtsp::~MediaTransporterSenderToServerRtsp()
{
	LOG_DEBUG("dtor: %p", this);
}

void MediaTransporterSenderToServerRtsp::buildPipeline()
{
	GstElement *mux = NULL;
	GstElement *sink = NULL;

	if (_receiverAddress.empty())
		throw MediaTransporterException(MTPR_ERROR_INVALID_OPERATION, "address is empty");

	if (_mediaSources.empty())
		throw MediaTransporterException(MTPR_ERROR_INVALID_OPERATION, "source is empty");

	try {
		/* create mux to sink */
		mux = gst::_createElement(gst::DEFAULT_ELEMENT_TSMUX);
		g_object_set(G_OBJECT(mux), "alignment", 7, NULL);

		sink = gst::_createElement(gst::DEFAULT_ELEMENT_RTSPSINK);
		g_object_set(G_OBJECT(sink),
			"location", util::addProtocolPrefix(_receiverAddress, "rtsp").c_str(), NULL);

		if (!_connectionParam.certPath.empty()) {
			GError* error = NULL;
			GTlsDatabase* tlsDatabase = NULL;

			SECURE_LOGD("Cert Path = %s", _connectionParam.certPath.c_str());

			tlsDatabase = g_tls_file_database_new(_connectionParam.certPath.c_str(), &error);
			if (error) {
				LOG_ERROR("failed setting tls database: %s", error->message);
				g_error_free (error);

				throw MediaTransporterException(MTPR_ERROR_INVALID_OPERATION, "failed setting tls db");
			}

			g_object_set(G_OBJECT(sink),
				"protocols", (GST_RTSP_LOWER_TRANS_TCP | GST_RTSP_LOWER_TRANS_TLS),
				"tls-database", tlsDatabase, NULL);

			g_clear_object(&tlsDatabase);
		}

		gst_bin_add_many(GST_BIN(_gst.pipeline), mux, sink, NULL);
		if (!gst_element_link(mux, sink)) {
			LOG_ERROR("failed to gst_element_link()");
			throw MediaTransporterException(MTPR_ERROR_INVALID_OPERATION, "failed to gst_element_link()");
		}

		linkMediaSourceToMuxer(mux);

		_rtspSink = sink;
		LOG_INFO("linked mux and sink");

	} catch (const MediaTransporterException& e) {
		LOG_ERROR("%s", e.what());

		gst::_destroyElementFromParent(mux);
		gst::_destroyElementFromParent(sink);

		throw;
	}
}

void MediaTransporterSenderToServerRtsp::startPipeline()
{
	gst::_setPipelineState(_gst.pipeline, GST_STATE_PLAYING,
							MediaTransporterIni::get().general().timeout, _asyncStart);
	_canSendPacket = true;
}

void MediaTransporterSenderToServerRtsp::stopPipeline()
{
	_canSendPacket = false;
	gst::_setPipelineState(_gst.pipeline, GST_STATE_NULL,
							MediaTransporterIni::get().general().timeout);
	_rtspSink = nullptr;
}

void MediaTransporterSenderToServerRtsp::setConnection(std::string name, std::string val)
{
	setConnectionParam(name, val, &_connectionParam);
}

std::string MediaTransporterSenderToServerRtsp::getConnection(std::string name)
{
	return getConnectionParam(_connectionParam, name);
}

void MediaTransporterSenderToServerRtsp::setReceiverAddress(std::string address)
{
	_receiverAddress = address;
}