Merge branch 'tizen_gst_1.19.3' into tizen_gst_1.20.0
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtpmanager / gstrtpbin.c
index 1713d18..03f24b1 100644 (file)
@@ -353,6 +353,9 @@ enum
 #define DEFAULT_MAX_STREAMS          G_MAXUINT
 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
 #define DEFAULT_MAX_TS_OFFSET        G_GINT64_CONSTANT(3000000000)
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+#define DEFAULT_RTSP_USE_BUFFERING  FALSE
+#endif
 
 enum
 {
@@ -382,6 +385,9 @@ enum
   PROP_MAX_TS_OFFSET,
   PROP_FEC_DECODERS,
   PROP_FEC_ENCODERS,
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  PROP_USE_RTSP_BUFFERING       /* use for player RTSP buffering */
+#endif
 };
 
 #define GST_RTP_BIN_RTCP_SYNC_TYPE (gst_rtp_bin_rtcp_sync_get_type())
@@ -456,7 +462,9 @@ struct _GstRtpBinStream
   gulong buffer_ptreq_sig;
   gulong buffer_ntpstop_sig;
   gint percent;
-
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  gint prev_percent;
+#endif
   /* the PT demuxer of the SSRC */
   GstElement *demux;
   gulong demux_newpad_sig;
@@ -1795,6 +1803,9 @@ static GstRtpBinStream *
 create_stream (GstRtpBinSession * session, guint32 ssrc)
 {
   GstElement *buffer, *demux = NULL;
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  GstElement *queue2 = NULL;
+#endif
   GstRtpBinStream *stream;
   GstRtpBin *rtpbin;
   GstState target;
@@ -1813,6 +1824,13 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
     if (!(demux = gst_element_factory_make ("rtpptdemux", NULL)))
       goto no_demux;
   }
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  if (rtpbin->use_rtsp_buffering &&
+      rtpbin->buffer_mode == RTP_JITTER_BUFFER_MODE_SLAVE) {
+    if (!(queue2 = gst_element_factory_make ("queue2", NULL)))
+      goto no_queue2;
+  }
+#endif
 
   stream = g_new0 (GstRtpBinStream, 1);
   stream->ssrc = ssrc;
@@ -1825,6 +1843,9 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
   stream->rt_delta = 0;
   stream->rtp_delta = 0;
   stream->percent = 100;
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  stream->prev_percent = 0;
+#endif
   stream->clock_base = -100 * GST_SECOND;
   session->streams = g_slist_prepend (session->streams, stream);
 
@@ -1867,17 +1888,43 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
     g_object_set (buffer, "max-ts-offset-adjustment",
         rtpbin->max_ts_offset_adjustment, NULL);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  /* configure queue2 to use live buffering */
+  if (queue2) {
+    g_object_set_data (G_OBJECT (queue2), "GstRTPBin.stream", stream);
+    g_object_set (queue2, "use-buffering", TRUE, NULL);
+    g_object_set (queue2, "buffer-mode", GST_BUFFERING_LIVE, NULL);
+  }
+#endif
   g_signal_emit (rtpbin, gst_rtp_bin_signals[SIGNAL_NEW_JITTERBUFFER], 0,
       buffer, session->id, ssrc);
 
   if (!rtpbin->ignore_pt)
     gst_bin_add (GST_BIN_CAST (rtpbin), demux);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  if (queue2)
+    gst_bin_add (GST_BIN_CAST (rtpbin), queue2);
+#endif
+
   /* link stuff */
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  if (queue2) {
+    gst_element_link_pads_full (buffer, "src", queue2, "sink",
+        GST_PAD_LINK_CHECK_NOTHING);
+    if (demux) {
+      gst_element_link_pads_full (queue2, "src", demux, "sink",
+          GST_PAD_LINK_CHECK_NOTHING);
+    }
+  } else if (demux) {
+    gst_element_link_pads_full (buffer, "src", demux, "sink",
+        GST_PAD_LINK_CHECK_NOTHING);
+  }
+#else
   if (demux)
     gst_element_link_pads_full (buffer, "src", demux, "sink",
         GST_PAD_LINK_CHECK_NOTHING);
-
+#endif
   if (rtpbin->buffering) {
     guint64 last_out;
 
@@ -1900,6 +1947,11 @@ create_stream (GstRtpBinSession * session, guint32 ssrc)
 
   gst_element_set_state (buffer, target);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  if (queue2)
+    gst_element_set_state (queue2, target);
+#endif
+
   return stream;
 
   /* ERRORS */
@@ -1920,6 +1972,15 @@ no_demux:
     g_warning ("rtpbin: could not create rtpptdemux element");
     return NULL;
   }
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+no_queue2:
+  {
+    gst_object_unref (buffer);
+    gst_object_unref (demux);
+    g_warning ("rtpbin: could not create queue2 element");
+    return NULL;
+  }
+#endif
 }
 
 /* called with RTP_BIN_LOCK */
@@ -2797,6 +2858,14 @@ gst_rtp_bin_class_init (GstRtpBinClass * klass)
           "fec-encoders='fec,0=\"rtpst2022-1-fecenc\\ rows\\=5\\ columns\\=5\";'",
           GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  g_object_class_install_property (gobject_class, PROP_USE_RTSP_BUFFERING,
+      g_param_spec_boolean ("use-rtsp-buffering", "Use RTSP buffering",
+          "Use RTSP buffering in RTP_JITTER_BUFFER_MODE_SLAVE buffer mode",
+          DEFAULT_RTSP_USE_BUFFERING,
+          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+#endif
+
   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rtp_bin_change_state);
   gstelement_class->request_new_pad =
       GST_DEBUG_FUNCPTR (gst_rtp_bin_request_new_pad);
@@ -2883,6 +2952,9 @@ gst_rtp_bin_init (GstRtpBin * rtpbin)
   rtpbin->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
   rtpbin->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
   rtpbin->max_ts_offset_is_set = FALSE;
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+  rtpbin->use_rtsp_buffering = FALSE;
+#endif
 
   /* some default SDES entries */
   cname = g_strdup_printf ("user%u@host-%x", g_random_int (), g_random_int ());
@@ -3043,6 +3115,13 @@ gst_rtp_bin_set_property (GObject * object, guint prop_id,
   rtpbin = GST_RTP_BIN (object);
 
   switch (prop_id) {
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+    case PROP_USE_RTSP_BUFFERING:
+      GST_RTP_BIN_LOCK (rtpbin);
+      rtpbin->use_rtsp_buffering = g_value_get_boolean (value);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      break;
+#endif
     case PROP_LATENCY:
       GST_RTP_BIN_LOCK (rtpbin);
       rtpbin->latency_ms = g_value_get_uint (value);
@@ -3218,6 +3297,13 @@ gst_rtp_bin_get_property (GObject * object, guint prop_id,
   rtpbin = GST_RTP_BIN (object);
 
   switch (prop_id) {
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+    case PROP_USE_RTSP_BUFFERING:
+      GST_RTP_BIN_LOCK (rtpbin);
+      g_value_set_boolean (value, rtpbin->use_rtsp_buffering);
+      GST_RTP_BIN_UNLOCK (rtpbin);
+      break;
+#endif
     case PROP_LATENCY:
       GST_RTP_BIN_LOCK (rtpbin);
       g_value_set_uint (value, rtpbin->latency_ms);
@@ -3345,6 +3431,9 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
       gint min_percent = 100;
       GSList *sessions, *streams;
       GstRtpBinStream *stream;
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+      gboolean buffering_flag = FALSE, update_buffering_status = TRUE;
+#endif
       gboolean change = FALSE, active = FALSE;
       GstClockTime min_out_time;
       GstBufferingMode mode;
@@ -3378,9 +3467,54 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
                 streams = g_slist_next (streams)) {
               GstRtpBinStream *stream = (GstRtpBinStream *) streams->data;
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+              if (rtpbin->use_rtsp_buffering &&
+                  rtpbin->buffer_mode == RTP_JITTER_BUFFER_MODE_SLAVE) {
+                GstPad *temp_pad_src = NULL;
+                GstCaps *temp_caps_src = NULL;
+                GstStructure *caps_structure;
+                const gchar *caps_str_media = NULL;
+                temp_pad_src = gst_element_get_static_pad (stream->buffer, "src");
+                temp_caps_src = gst_pad_get_current_caps (temp_pad_src);
+                GST_DEBUG_OBJECT (bin,
+                    "stream %p percent %d : temp_caps_src=%" GST_PTR_FORMAT,
+                    stream, stream->percent, temp_caps_src);
+                if (temp_caps_src) {
+                  caps_structure = gst_caps_get_structure (temp_caps_src, 0);
+                  caps_str_media =
+                      gst_structure_get_string (caps_structure, "media");
+                  if (caps_str_media != NULL) {
+                    if ((strcmp (caps_str_media, "video") != 0)
+                        && (strcmp (caps_str_media, "audio") != 0)) {
+                      GST_DEBUG_OBJECT (bin,
+                          "Non Audio/Video Stream.. ignoring the same !!");
+                      gst_caps_unref (temp_caps_src);
+                      gst_object_unref (temp_pad_src);
+                      continue;
+                    } else if (stream->percent >= 100) {
+                      /* Most of the time buffering icon displays in rtsp playback.
+                         Optimizing the buffering updation code. Whenever any stream percentage
+                         reaches 100 do not post buffering messages. */
+                      if (stream->prev_percent < 100)
+                        buffering_flag = TRUE;
+                      else
+                        update_buffering_status = FALSE;
+                    }
+                  }
+                  gst_caps_unref (temp_caps_src);
+                }
+                gst_object_unref (temp_pad_src);
+                /* Updating prev stream percentage */
+                stream->prev_percent = stream->percent;
+              } else {
+                GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
+                    stream->percent);
+              }
+#else
               GST_DEBUG_OBJECT (bin, "stream %p percent %d", stream,
                   stream->percent);
 
+#endif
               /* find min percent */
               if (min_percent > stream->percent)
                 min_percent = stream->percent;
@@ -3394,6 +3528,10 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
         }
         GST_DEBUG_OBJECT (bin, "min percent %d", min_percent);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+        if (!(rtpbin->use_rtsp_buffering &&
+            rtpbin->buffer_mode == RTP_JITTER_BUFFER_MODE_SLAVE)) {
+#endif
         if (rtpbin->buffering) {
           if (min_percent == 100) {
             rtpbin->buffering = FALSE;
@@ -3408,16 +3546,35 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
             change = TRUE;
           }
         }
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+        }
+#endif
         GST_RTP_BIN_UNLOCK (rtpbin);
 
         gst_message_unref (message);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+          if (rtpbin->use_rtsp_buffering &&
+              rtpbin->buffer_mode == RTP_JITTER_BUFFER_MODE_SLAVE) {
+          if (update_buffering_status == FALSE)
+            break;
+          if (buffering_flag) {
+            min_percent = 100;
+            GST_DEBUG_OBJECT (bin, "forcefully change min_percent to 100!!!");
+          }
+        }
+#endif
         /* make a new buffering message with the min value */
         message =
             gst_message_new_buffering (GST_OBJECT_CAST (bin), min_percent);
         gst_message_set_buffering_stats (message, mode, avg_in, avg_out,
             buffering_left);
 
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+        if (rtpbin->use_rtsp_buffering &&
+            rtpbin->buffer_mode == RTP_JITTER_BUFFER_MODE_SLAVE)
+          goto slave_buffering;
+#endif
         if (G_UNLIKELY (change)) {
           GstClock *clock;
           guint64 running_time = 0;
@@ -3497,6 +3654,9 @@ gst_rtp_bin_handle_message (GstBin * bin, GstMessage * message)
           GST_RTP_BIN_UNLOCK (rtpbin);
         }
       }
+#ifdef TIZEN_FEATURE_RTSP_MODIFICATION
+slave_buffering:
+#endif
       GST_BIN_CLASS (parent_class)->handle_message (bin, message);
       break;
     }