18 #include <gst/app/gstappsink.h>
27 #define DBG_TIME_PERIOD_PRINTS 10 //10 sec
38 std::mutex *m{
nullptr};
105 caps = gst_caps_new_simple(
"application/x-rtp",
106 "media", G_TYPE_STRING,
"video",
107 "encoding-name", G_TYPE_STRING,
"H264",
108 "payload", G_TYPE_INT, 96,
112 link_ok = gst_element_link_filtered(e1, e2, caps);
132 caps = gst_caps_new_simple(
"video/x-raw",
133 "format", G_TYPE_STRING,
"RGB",
137 link_ok = gst_element_link_filtered(e1, e2, caps);
152 GstFlowReturn
new_sample(GstAppSink *appsink, gpointer user_data)
155 static bool isFirst =
true;
159 static double last_call;
160 static double sumOf_timeBetweenCalls = 0;
161 static double sumOf_timeOfNewSampleFunc = 0;
162 static uint32_t count=0;
163 #define MAX_COUNT 100
167 sumOf_timeBetweenCalls+=(start_time -last_call);
169 last_call = start_time;
176 GstSample *sample =
nullptr;
177 g_signal_emit_by_name (appsink,
"pull-sample", &sample, NULL);
184 GstCaps *caps = gst_sample_get_caps (sample);
188 return GST_FLOW_ERROR;
190 GstStructure *struc = gst_caps_get_structure(caps, 0);
194 return GST_FLOW_ERROR;
198 res = gst_structure_get_int(struc,
"width", &width);
202 return GST_FLOW_ERROR;
205 res = gst_structure_get_int(struc,
"height", &height);
209 return GST_FLOW_ERROR;
213 GstBuffer *
buffer = gst_sample_get_buffer(sample);
215 if(!gst_buffer_map(
buffer, &map, GST_MAP_READ))
218 return GST_FLOW_ERROR;
224 dec_data->isNew =
true;
225 dec_data->img->resize(width, height);
227 unsigned char *ydata_ptr = dec_data->img->getRawImage();
228 memcpy(ydata_ptr, map.data, width*height*3);
230 dec_data->m->unlock();
231 gst_buffer_unmap(
buffer, &map);
233 gst_sample_unref(sample);
240 sumOf_timeOfNewSampleFunc += (end_time-start_time);
247 "On %d times: NewSampleFunc is long %.6f sec and sleeps %.6f sec",
249 (sumOf_timeOfNewSampleFunc/MAX_COUNT),
250 (sumOf_timeBetweenCalls/MAX_COUNT) );
253 sumOf_timeBetweenCalls = 0;
254 sumOf_timeOfNewSampleFunc = 0;
311 sizeChanger(nullptr),
315 gst_cbk_data.
m = m_ptr;
316 gst_cbk_data.
img = &myframe;
317 gst_cbk_data.
s = s_ptr;
324 gst_init(
nullptr,
nullptr);
325 pipeline = gst_pipeline_new (
"video-player");
326 source = gst_element_factory_make (
"udpsrc",
"video-source");
327 rtpDepay = gst_element_factory_make (
"rtph264depay",
"rtp-depay");
328 parser = gst_element_factory_make (
"h264parse",
"parser");
329 decoder = gst_element_factory_make (
"avdec_h264",
"decoder");
330 sizeChanger = gst_element_factory_make (
"videocrop",
"cropper");
331 convert = gst_element_factory_make (
"videoconvert",
"convert");
332 sink = gst_element_factory_make (
"appsink",
"video-output");
334 if (!pipeline || !source || !rtpDepay || !parser || !decoder || !convert || !sink || !sizeChanger)
336 yCError(
H264CARRIER) <<
"H264Decoder-GSTREAMER: one element could not be created. Exiting.";
341 jitterBuff = gst_element_factory_make(
"rtpjitterbuffer",
"jitterBuffer");
344 yCError(
H264CARRIER) <<
"H264Decoder-GSTREAMER: rtpjitterbuffer could not be created. Exiting.";
358 g_object_set(source,
"port", cfgParams.
remotePort, NULL);
364 g_object_set( sink,
"emit-signals",
false, NULL );
366 GstAppSinkCallbacks cbs;
370 cbs.new_preroll =
nullptr;
372 gst_app_sink_set_callbacks( GST_APP_SINK( sink ), &cbs, &gst_cbk_data,
nullptr );
396 yCTrace(
H264CARRIER) <<
"H264Decoder-GSTREAMER: try to add elements to pipeline..... ";
398 gst_bin_add_many (GST_BIN (pipeline),
399 source, rtpDepay, parser, decoder, sizeChanger, convert, sink, NULL);
403 if (jitterBuff !=
nullptr)
405 result = gst_bin_add(GST_BIN(pipeline), jitterBuff);
406 if (!result) {
yCError(
H264CARRIER) <<
"H264Decoder: Error adding jitterBuff to the bin";
return false; }
409 yCTrace(
H264CARRIER) <<
"H264Decoder-GSTREAMER: elements have been added in pipeline!";
413 if (!result) {
yCError(
H264CARRIER) <<
"H264Decoder: Error linking converter to sink ";
return false; }
419 yCTrace(
H264CARRIER) <<
"H264Decoder-GSTREAMER: try to link videosrc to rtpjitterBuffer.....";
421 if (!result){
yCError(
H264CARRIER) <<
"H264Decoder: Error linking videosrc to rtpjitterBuffer ";
return false;}
423 yCTrace(
H264CARRIER) <<
"H264Decoder-GSTREAMER: try to link jitterBuff to rtpDapay.....";
424 result = gst_element_link(jitterBuff, rtpDepay);
425 if (!result) {
yCError(
H264CARRIER) <<
"H264Decoder: Error linking jitterBuff to rtpDapay ";
return false; }
432 if (!result) {
yCError(
H264CARRIER) <<
"H264Decoder: Error linking videosrc to rtpDepay ";
return false; }
437 gst_element_link_many(rtpDepay, parser, decoder, sizeChanger, convert, NULL);
448 #define GET_HELPER(x) (*((H264DecoderHelper*)(x)))
486 gst_element_set_state (helper.
pipeline, GST_STATE_PLAYING);
496 gst_element_set_state (helper.
pipeline, GST_STATE_NULL);
497 gst_bus_set_sync_handler(gst_pipeline_get_bus (GST_PIPELINE (helper.
pipeline)),
nullptr,
nullptr,
nullptr);
499 gst_object_unref (GST_OBJECT (helper.
pipeline));