-
Notifications
You must be signed in to change notification settings - Fork 0
/
livevideopipelinebuilder.cpp
231 lines (176 loc) · 8.99 KB
/
livevideopipelinebuilder.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "livevideopipelinebuilder.h"
LiveVideoPipelineBuilder::LiveVideoPipelineBuilder(){
qDebug()<<"LiveVideoPipelineBuilder::LiveVideoPipelineBuilder";
gst_init(NULL, NULL);
mPipeline.bin = NULL;
}
LiveVideoPipelineBuilder::~LiveVideoPipelineBuilder(){
destroyPipeline();
}
void LiveVideoPipelineBuilder::setBin(std::string pipelineName){
mPipeline.bin = gst_pipeline_new(pipelineName.data());
}
void LiveVideoPipelineBuilder::setSource(std::string sourceType, std::string sourceName){
mPipeline.source = gst_element_factory_make(sourceType.data(), sourceName.data());
}
void LiveVideoPipelineBuilder::setVolume(std::string volumeType, std::string volumeName){
mPipeline.volume = gst_element_factory_make(volumeType.data(), volumeName.data());
}
void LiveVideoPipelineBuilder::setLevel(std::string levelType, std::string levelName){
mPipeline.level = gst_element_factory_make(levelType.data(), levelName.data());
}
void LiveVideoPipelineBuilder::setVideoQueue(std::string videoqueueType, std::string videoqueueName){
mPipeline.videoqueue = gst_element_factory_make(videoqueueType.data(), videoqueueName.data());
}
void LiveVideoPipelineBuilder::setVideoDepayloader(std::string depayloaderType, std::string depayloaderName){
mPipeline.videodepayloader = gst_element_factory_make(depayloaderType.data(), depayloaderName.data());
}
void LiveVideoPipelineBuilder::setVideoDecoder(std::string decoderType, std::string decoderName){
mPipeline.videodecoder = gst_element_factory_make(decoderType.data(), decoderName.data());
}
void LiveVideoPipelineBuilder::setVideoSink(std::string sinkType, std::string sinkName){
mPipeline.videosink = gst_element_factory_make(sinkType.data(), sinkName.data());
}
void LiveVideoPipelineBuilder::setAudioQueue(std::string audioqueueType, std::string audioqueueName){
mPipeline.audioqueue = gst_element_factory_make(audioqueueType.data(), audioqueueName.data());
}
void LiveVideoPipelineBuilder::setAudioDepayloader(std::string audiodepayloaderType, std::string audiodepayloaderName){
mPipeline.audiodepayloader = gst_element_factory_make(audiodepayloaderType.data(), audiodepayloaderName.data());
}
void LiveVideoPipelineBuilder::setAudioDecoder(std::string audiodecoderType, std::string audiodecoderName){
mPipeline.audiodecoder = gst_element_factory_make(audiodecoderType.data(), audiodecoderName.data());
}
void LiveVideoPipelineBuilder::setAudioConvertor(std::string audioconvertorType, std::string audioconvertorName){
mPipeline.audioconvertor = gst_element_factory_make(audioconvertorType.data(), audioconvertorName.data());
}
void LiveVideoPipelineBuilder::setAudioSink(std::string audiosinkType, std::string audiosinkName){
mPipeline.audiosink = gst_element_factory_make(audiosinkType.data(), audiosinkName.data());
}
void LiveVideoPipelineBuilder::setState(GstState){
mPipeline.state = GstState::GST_STATE_PAUSED;
}
void LiveVideoPipelineBuilder::setLiveness(bool livenessState){
mPipeline.isLive = livenessState;
}
void LiveVideoPipelineBuilder::setPropertiesOfGstElement(std::string pVideoSource, long long int pWindId){
g_object_set(mPipeline.source , "location", pVideoSource.data(), NULL );
g_object_set(mPipeline.volume , "volume", 0.5, NULL );
gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY (mPipeline.videosink), pWindId);
}
void LiveVideoPipelineBuilder::addElements(){
if(!mPipeline.bin || !mPipeline.source || !mPipeline.videoqueue || !mPipeline.videodepayloader || !mPipeline.videodecoder || !mPipeline.videosink ||
!mPipeline.audioqueue || !mPipeline.audiodepayloader || !mPipeline.audiodecoder || !mPipeline.audioconvertor || !mPipeline.audiosink){
g_printerr("All element could not created\n");
}
gst_bin_add_many (GST_BIN (mPipeline.bin), mPipeline.source, mPipeline.videoqueue, mPipeline.videodepayloader, mPipeline.videodecoder, mPipeline.videosink, mPipeline.audioqueue,
mPipeline.audiodepayloader, mPipeline.audiodecoder, mPipeline.audioconvertor, mPipeline.volume, mPipeline.level, mPipeline.audiosink, NULL);
}
void onPadAddedForLiveVideo(GstElement *src, GstPad *newPad, gpointer sink){
GstPad *sink_pad = gst_element_get_static_pad ((GstElement *)sink, "sink");
GstPadLinkReturn ret;
g_print ("Received new pad '%s' from '%s':\n", GST_PAD_NAME (newPad), GST_ELEMENT_NAME (src));
if (gst_pad_is_linked (sink_pad)){
g_print ("We are already linked. Ignoring.\n");
return;
}
ret = gst_pad_link(newPad, sink_pad);
if (GST_PAD_LINK_FAILED (ret)){
g_print ("Link failed.\n");
}
else{
g_print ("Link succeeded.\n");
}
gst_object_unref (sink_pad);
}
gboolean getMessageFromBusForLiveVideo(GstBus * bus, GstMessage * message, gpointer data){
//g_print ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message) );
switch (GST_MESSAGE_TYPE(message) ){
case GST_MESSAGE_ERROR:
{
GError *err;
gchar *debug;
gst_message_parse_error (message, &err, &debug);
g_print ("Error: %s\n", err->message);
g_error_free (err);
g_free (debug);
break;
}
case GST_MESSAGE_STATE_CHANGED:
//
break;
case GST_MESSAGE_EOS:
//
break;
default:
break;
}
return TRUE;
}
void LiveVideoPipelineBuilder::linkElements(){
if( !gst_element_link(mPipeline.source, mPipeline.videoqueue) ){
g_printerr ("Source and Videoqueue could not be linked.\n");
}
if(!gst_element_link(mPipeline.videoqueue, mPipeline.videodepayloader) ){
g_printerr ("Videoqueue and VideoDepayloader could not be linked.\n");
}
if(!gst_element_link(mPipeline.videodepayloader, mPipeline.videodecoder) ){
g_printerr ("VideoDepayloader and VideoDecoder could not be linked.\n");
}
if( !gst_element_link(mPipeline.videodecoder, mPipeline.videosink) ){
g_printerr ("VideoDecoder and VideoSink could not be linked.\n");
}
if(!gst_element_link(mPipeline.source, mPipeline.audioqueue) ){
g_printerr ("Source and AudioQueue could not be linked.\n");
}
if(!gst_element_link(mPipeline.audioqueue, mPipeline.audiodepayloader) ){
g_printerr ("AudioQueue and AudioDepayloader could not be linked.\n");
}
if( !gst_element_link(mPipeline.audiodepayloader, mPipeline.audiodecoder) ){
g_printerr ("AudioDepayloader and AudioDecoder could not be linked.\n");
}
if(!gst_element_link(mPipeline.audiodecoder, mPipeline.audioconvertor) ){
g_printerr ("AudioDecoder and AudioConvertor could not be linked.\n");
}
if( !gst_element_link(mPipeline.audioconvertor, mPipeline.volume) ){
g_printerr ("AudioConvertor and Volume could not be linked.\n");
}
if(!gst_element_link(mPipeline.volume, mPipeline.level) ){
g_printerr ("Volume and Level could not be linked.\n");
}
if(!gst_element_link(mPipeline.level, mPipeline.audiosink) ){
g_printerr ("Level and AudioSink could not be linked.\n");
}
g_signal_connect(mPipeline.source, "pad-added", G_CALLBACK (onPadAddedForLiveVideo), mPipeline.videoqueue);
g_signal_connect(mPipeline.source, "pad-added", G_CALLBACK (onPadAddedForLiveVideo), mPipeline.audioqueue);
}
void LiveVideoPipelineBuilder::buildPipeline(std::string pResourcePath, long long int pWindowID){
this->setBin(std::string("pipeline"));
this->setSource(std::string("rtspsrc"), std::string("source"));
this->setVideoQueue(std::string("queue"), std::string("videoqueue"));
this->setVideoDepayloader(std::string("rtph264depay"), std::string("videodepayloader"));
this->setVideoDecoder(std::string("avdec_h264"), std::string("videodecoder"));
this->setVideoSink(std::string("glimagesink"), std::string("videosink"));
this->setAudioQueue(std::string("queue"), std::string("audioqueue"));
this->setAudioDepayloader(std::string("rtpmp4gdepay"), std::string("audiodepayloader"));
this->setAudioDecoder(std::string("avdec_aac"), std::string("audiodecoder"));
this->setAudioConvertor(std::string("audioconvert"), std::string("audioconvertor"));
this->setVolume(std::string("volume"), std::string("volume"));
this->setLevel(std::string("level"), std::string("level"));
this->setAudioSink(std::string("autoaudiosink"), std::string("audiosink"));
this->setLiveness(true);
this->setPropertiesOfGstElement(pResourcePath, pWindowID);
this->addElements();
this->linkElements();
this->setBus();
this->setState(GstState::GST_STATE_READY);
}
void LiveVideoPipelineBuilder::setBus(){
mPipeline.bus = gst_pipeline_get_bus (GST_PIPELINE (mPipeline.bin));
//gst_bus_add_watch (mPipeline.bus, getMessageFromBusForLiveVideo, this);
gst_bus_set_sync_handler(mPipeline.bus, (GstBusSyncHandler)getMessageFromBusForLiveVideo, this, NULL);
}
void LiveVideoPipelineBuilder::destroyPipeline(){
g_object_unref(mPipeline.bus);
gst_element_set_state(mPipeline.bin, GST_STATE_NULL);
g_object_unref(mPipeline.bin);
}