1 /* ex: set tabstop=2 shiftwidth=2 expandtab: */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/pbutils/pbutils.h>
30 #include "gstrtph264pay.h"
39 GST_DEBUG_CATEGORY_STATIC (rtph264pay_debug);
40 #define GST_CAT_DEFAULT (rtph264pay_debug)
47 static GstStaticPadTemplate gst_rtp_h264_pay_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_STATIC_CAPS ("video/x-h264")
54 static GstStaticPadTemplate gst_rtp_h264_pay_src_template =
55 GST_STATIC_PAD_TEMPLATE ("src",
58 GST_STATIC_CAPS ("application/x-rtp, "
59 "media = (string) \"video\", "
60 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
61 "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
64 #define GST_TYPE_H264_SCAN_MODE (gst_h264_scan_mode_get_type())
66 gst_h264_scan_mode_get_type (void)
68 static GType h264_scan_mode_type = 0;
69 static const GEnumValue h264_scan_modes[] = {
70 {GST_H264_SCAN_MODE_BYTESTREAM,
71 "Scan complete bytestream for NALUs",
73 {GST_H264_SCAN_MODE_MULTI_NAL, "Buffers contain multiple complete NALUs",
75 {GST_H264_SCAN_MODE_SINGLE_NAL, "Buffers contain a single complete NALU",
80 if (!h264_scan_mode_type) {
82 g_enum_register_static ("GstH264PayScanMode", h264_scan_modes);
84 return h264_scan_mode_type;
87 #define DEFAULT_PROFILE_LEVEL_ID NULL
88 #define DEFAULT_SPROP_PARAMETER_SETS NULL
89 #define DEFAULT_SCAN_MODE GST_H264_SCAN_MODE_MULTI_NAL
90 #define DEFAULT_BUFFER_LIST FALSE
91 #define DEFAULT_CONFIG_INTERVAL 0
96 PROP_PROFILE_LEVEL_ID,
97 PROP_SPROP_PARAMETER_SETS,
100 PROP_CONFIG_INTERVAL,
104 #define IS_ACCESS_UNIT(x) (((x) > 0x00) && ((x) < 0x06))
106 static void gst_rtp_h264_pay_finalize (GObject * object);
108 static void gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
109 const GValue * value, GParamSpec * pspec);
110 static void gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
111 GValue * value, GParamSpec * pspec);
113 static GstCaps *gst_rtp_h264_pay_getcaps (GstBaseRTPPayload * payload,
114 GstPad * pad, GstCaps * filter);
115 static gboolean gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload,
117 static GstFlowReturn gst_rtp_h264_pay_handle_buffer (GstBaseRTPPayload * pad,
119 static gboolean gst_rtp_h264_pay_handle_event (GstBaseRTPPayload * payload,
121 static GstStateChangeReturn gst_basertppayload_change_state (GstElement *
122 element, GstStateChange transition);
124 #define gst_rtp_h264_pay_parent_class parent_class
125 G_DEFINE_TYPE (GstRtpH264Pay, gst_rtp_h264_pay, GST_TYPE_BASE_RTP_PAYLOAD);
128 gst_rtp_h264_pay_class_init (GstRtpH264PayClass * klass)
130 GObjectClass *gobject_class;
131 GstElementClass *gstelement_class;
132 GstBaseRTPPayloadClass *gstbasertppayload_class;
134 gobject_class = (GObjectClass *) klass;
135 gstelement_class = (GstElementClass *) klass;
136 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
138 gobject_class->set_property = gst_rtp_h264_pay_set_property;
139 gobject_class->get_property = gst_rtp_h264_pay_get_property;
141 g_object_class_install_property (G_OBJECT_CLASS (klass),
142 PROP_PROFILE_LEVEL_ID, g_param_spec_string ("profile-level-id",
144 "The base64 profile-level-id to set in the sink caps (deprecated)",
145 DEFAULT_PROFILE_LEVEL_ID,
146 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (G_OBJECT_CLASS (klass),
149 PROP_SPROP_PARAMETER_SETS, g_param_spec_string ("sprop-parameter-sets",
150 "sprop-parameter-sets",
151 "The base64 sprop-parameter-sets to set in out caps (set to NULL to "
152 "extract from stream)",
153 DEFAULT_SPROP_PARAMETER_SETS,
154 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
156 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCAN_MODE,
157 g_param_spec_enum ("scan-mode", "Scan Mode",
158 "How to scan the input buffers for NAL units. Performance can be "
159 "increased when certain assumptions are made about the input buffers",
160 GST_TYPE_H264_SCAN_MODE, DEFAULT_SCAN_MODE,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
164 g_param_spec_boolean ("buffer-list", "Buffer List",
166 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (G_OBJECT_CLASS (klass),
169 PROP_CONFIG_INTERVAL,
170 g_param_spec_uint ("config-interval",
171 "SPS PPS Send Interval",
172 "Send SPS and PPS Insertion Interval in seconds (sprop parameter sets "
173 "will be multiplexed in the data stream when detected.) (0 = disabled)",
174 0, 3600, DEFAULT_CONFIG_INTERVAL,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
178 gobject_class->finalize = gst_rtp_h264_pay_finalize;
180 gst_element_class_add_pad_template (gstelement_class,
181 gst_static_pad_template_get (&gst_rtp_h264_pay_src_template));
182 gst_element_class_add_pad_template (gstelement_class,
183 gst_static_pad_template_get (&gst_rtp_h264_pay_sink_template));
185 gst_element_class_set_details_simple (gstelement_class, "RTP H264 payloader",
186 "Codec/Payloader/Network/RTP",
187 "Payload-encode H264 video into RTP packets (RFC 3984)",
188 "Laurent Glayal <spglegle@yahoo.fr>");
190 gstelement_class->change_state =
191 GST_DEBUG_FUNCPTR (gst_basertppayload_change_state);
193 gstbasertppayload_class->get_caps = gst_rtp_h264_pay_getcaps;
194 gstbasertppayload_class->set_caps = gst_rtp_h264_pay_setcaps;
195 gstbasertppayload_class->handle_buffer = gst_rtp_h264_pay_handle_buffer;
196 gstbasertppayload_class->handle_event = gst_rtp_h264_pay_handle_event;
198 GST_DEBUG_CATEGORY_INIT (rtph264pay_debug, "rtph264pay", 0,
199 "H264 RTP Payloader");
203 gst_rtp_h264_pay_init (GstRtpH264Pay * rtph264pay)
205 rtph264pay->queue = g_array_new (FALSE, FALSE, sizeof (guint));
206 rtph264pay->profile = 0;
207 rtph264pay->sps = NULL;
208 rtph264pay->pps = NULL;
209 rtph264pay->last_spspps = -1;
210 rtph264pay->scan_mode = GST_H264_SCAN_MODE_MULTI_NAL;
211 rtph264pay->buffer_list = DEFAULT_BUFFER_LIST;
212 rtph264pay->spspps_interval = DEFAULT_CONFIG_INTERVAL;
214 rtph264pay->adapter = gst_adapter_new ();
218 gst_rtp_h264_pay_clear_sps_pps (GstRtpH264Pay * rtph264pay)
220 g_list_foreach (rtph264pay->sps, (GFunc) gst_mini_object_unref, NULL);
221 g_list_free (rtph264pay->sps);
222 rtph264pay->sps = NULL;
223 g_list_foreach (rtph264pay->pps, (GFunc) gst_mini_object_unref, NULL);
224 g_list_free (rtph264pay->pps);
225 rtph264pay->pps = NULL;
229 gst_rtp_h264_pay_finalize (GObject * object)
231 GstRtpH264Pay *rtph264pay;
233 rtph264pay = GST_RTP_H264_PAY (object);
235 g_array_free (rtph264pay->queue, TRUE);
237 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
239 g_free (rtph264pay->sprop_parameter_sets);
241 g_object_unref (rtph264pay->adapter);
243 G_OBJECT_CLASS (parent_class)->finalize (object);
246 static const gchar *all_levels[] = {
267 gst_rtp_h264_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad,
270 GstCaps *allowed_caps;
273 gst_pad_peer_get_caps (GST_BASE_RTP_PAYLOAD_SRCPAD (payload), filter);
276 GstCaps *caps = NULL;
279 if (gst_caps_is_any (allowed_caps)) {
280 gst_caps_unref (allowed_caps);
284 if (gst_caps_is_empty (allowed_caps))
287 caps = gst_caps_new_empty ();
289 for (i = 0; i < gst_caps_get_size (allowed_caps); i++) {
290 GstStructure *s = gst_caps_get_structure (allowed_caps, i);
291 GstStructure *new_s = gst_structure_new ("video/x-h264", NULL);
292 const gchar *profile_level_id;
294 profile_level_id = gst_structure_get_string (s, "profile-level-id");
296 if (profile_level_id && strlen (profile_level_id) == 6) {
297 const gchar *profile;
302 spsint = strtol (profile_level_id, NULL, 16);
303 sps[0] = spsint >> 16;
304 sps[1] = spsint >> 8;
307 profile = gst_codec_utils_h264_get_profile (sps, 3);
308 level = gst_codec_utils_h264_get_level (sps, 3);
310 if (profile && level) {
311 GST_LOG_OBJECT (payload, "In caps, have profile %s and level %s",
314 if (!strcmp (profile, "constrained-baseline"))
315 gst_structure_set (new_s, "profile", G_TYPE_STRING, profile, NULL);
318 GValue profiles = { 0, };
320 g_value_init (&profiles, GST_TYPE_LIST);
321 g_value_init (&val, G_TYPE_STRING);
323 g_value_set_static_string (&val, profile);
324 gst_value_list_append_value (&profiles, &val);
326 g_value_set_static_string (&val, "constrained-baseline");
327 gst_value_list_append_value (&profiles, &val);
329 gst_structure_take_value (new_s, "profile", &profiles);
332 if (!strcmp (level, "1"))
333 gst_structure_set (new_s, "level", G_TYPE_STRING, level, NULL);
335 GValue levels = { 0, };
339 g_value_init (&levels, GST_TYPE_LIST);
340 g_value_init (&val, G_TYPE_STRING);
342 for (j = 0; all_levels[j]; j++) {
343 g_value_set_static_string (&val, all_levels[j]);
344 gst_value_list_prepend_value (&levels, &val);
345 if (!strcmp (level, all_levels[j]))
348 gst_structure_take_value (new_s, "level", &levels);
353 gst_caps_merge_structure (caps, new_s);
356 gst_caps_unref (allowed_caps);
361 return gst_caps_new_simple ("video/x-h264", NULL);
364 /* take the currently configured SPS and PPS lists and set them on the caps as
365 * sprop-parameter-sets */
367 gst_rtp_h264_pay_set_sps_pps (GstBaseRTPPayload * basepayload)
369 GstRtpH264Pay *payloader = GST_RTP_H264_PAY (basepayload);
379 sprops = g_string_new ("");
382 /* build the sprop-parameter-sets */
383 for (walk = payloader->sps; walk; walk = g_list_next (walk)) {
384 GstBuffer *sps_buf = GST_BUFFER_CAST (walk->data);
386 data = gst_buffer_map (sps_buf, &size, NULL, GST_MAP_READ);
387 set = g_base64_encode (data, size);
388 gst_buffer_unmap (sps_buf, data, size);
390 g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
394 for (walk = payloader->pps; walk; walk = g_list_next (walk)) {
395 GstBuffer *pps_buf = GST_BUFFER_CAST (walk->data);
397 data = gst_buffer_map (pps_buf, &size, NULL, GST_MAP_READ);
398 set = g_base64_encode (data, size);
399 gst_buffer_unmap (pps_buf, data, size);
401 g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
406 /* profile is 24 bit. Force it to respect the limit */
407 profile = g_strdup_printf ("%06x", payloader->profile & 0xffffff);
408 /* combine into output caps */
409 res = gst_basertppayload_set_outcaps (basepayload,
410 "sprop-parameter-sets", G_TYPE_STRING, sprops->str, NULL);
411 g_string_free (sprops, TRUE);
418 gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
420 GstRtpH264Pay *rtph264pay;
423 guint8 *data, *bdata;
426 const gchar *alignment;
428 rtph264pay = GST_RTP_H264_PAY (basepayload);
430 str = gst_caps_get_structure (caps, 0);
432 /* we can only set the output caps when we found the sprops and profile
434 gst_basertppayload_set_options (basepayload, "video", TRUE, "H264", 90000);
436 alignment = gst_structure_get_string (str, "alignment");
437 if (alignment && !strcmp (alignment, "au"))
438 rtph264pay->au_alignment = TRUE;
440 rtph264pay->au_alignment = FALSE;
442 /* packetized AVC video has a codec_data */
443 if ((value = gst_structure_get_value (str, "codec_data"))) {
444 guint num_sps, num_pps;
447 GST_DEBUG_OBJECT (rtph264pay, "have packetized h264");
448 rtph264pay->packetized = TRUE;
450 buffer = gst_value_get_buffer (value);
452 bdata = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
456 /* parse the avcC data */
459 /* parse the version, this must be 1 */
463 /* AVCProfileIndication */
465 /* AVCLevelIndication */
466 rtph264pay->profile = (data[1] << 16) | (data[2] << 8) | data[3];
467 GST_DEBUG_OBJECT (rtph264pay, "profile %06x", rtph264pay->profile);
469 /* 6 bits reserved | 2 bits lengthSizeMinusOne */
470 /* this is the number of bytes in front of the NAL units to mark their
472 rtph264pay->nal_length_size = (data[4] & 0x03) + 1;
473 GST_DEBUG_OBJECT (rtph264pay, "nal length %u", rtph264pay->nal_length_size);
474 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
475 num_sps = data[5] & 0x1f;
476 GST_DEBUG_OBJECT (rtph264pay, "num SPS %u", num_sps);
481 /* create the sprop-parameter-sets */
482 for (i = 0; i < num_sps; i++) {
488 nal_size = (data[0] << 8) | data[1];
492 GST_LOG_OBJECT (rtph264pay, "SPS %d size %d", i, nal_size);
497 /* make a buffer out of it and add to SPS list */
498 sps_buf = gst_buffer_new_and_alloc (nal_size);
499 gst_buffer_fill (sps_buf, 0, data, nal_size);
500 rtph264pay->sps = g_list_append (rtph264pay->sps, sps_buf);
508 /* 8 bits numOfPictureParameterSets */
513 GST_DEBUG_OBJECT (rtph264pay, "num PPS %u", num_pps);
514 for (i = 0; i < num_pps; i++) {
520 nal_size = (data[0] << 8) | data[1];
524 GST_LOG_OBJECT (rtph264pay, "PPS %d size %d", i, nal_size);
529 /* make a buffer out of it and add to PPS list */
530 pps_buf = gst_buffer_new_and_alloc (nal_size);
531 gst_buffer_fill (pps_buf, 0, data, nal_size);
532 rtph264pay->pps = g_list_append (rtph264pay->pps, pps_buf);
537 /* and update the caps with the collected data */
538 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
539 goto set_sps_pps_failed;
541 GST_DEBUG_OBJECT (rtph264pay, "have bytestream h264");
542 rtph264pay->packetized = FALSE;
549 GST_ERROR_OBJECT (rtph264pay, "avcC size %u < 7", size);
554 GST_ERROR_OBJECT (rtph264pay, "wrong avcC version");
559 GST_ERROR_OBJECT (rtph264pay, "avcC too small ");
564 GST_ERROR_OBJECT (rtph264pay, "failed to set sps/pps");
569 gst_buffer_unmap (buffer, bdata, bsize);
575 gst_rtp_h264_pay_parse_sprop_parameter_sets (GstRtpH264Pay * rtph264pay)
579 guint len, num_sps, num_pps;
583 ps = rtph264pay->sprop_parameter_sets;
587 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
589 params = g_strsplit (ps, ",", 0);
590 len = g_strv_length (params);
592 GST_DEBUG_OBJECT (rtph264pay, "we have %d params", len);
594 num_sps = num_pps = 0;
596 for (i = 0; params[i]; i++) {
603 nal_len = strlen (params[i]);
604 buf = gst_buffer_new_and_alloc (nal_len);
606 nalp = gst_buffer_map (buf, NULL, NULL, GST_MAP_WRITE);
607 nal_len = g_base64_decode_step (params[i], nal_len, nalp, &state, &save);
609 gst_buffer_unmap (buf, nalp, nal_len);
612 gst_buffer_unref (buf);
616 /* append to the right list */
617 if ((nal_type & 0x1f) == 7) {
618 GST_DEBUG_OBJECT (rtph264pay, "adding param %d as SPS %d", i, num_sps);
619 rtph264pay->sps = g_list_append (rtph264pay->sps, buf);
622 GST_DEBUG_OBJECT (rtph264pay, "adding param %d as PPS %d", i, num_pps);
623 rtph264pay->pps = g_list_append (rtph264pay->pps, buf);
631 next_start_code (const guint8 * data, guint size)
633 /* Boyer-Moore string matching algorithm, in a degenerative
634 * sense because our search 'alphabet' is binary - 0 & 1 only.
635 * This allow us to simplify the general BM algorithm to a very
637 /* assume 1 is in the 3th byte */
640 while (offset < size) {
641 if (1 == data[offset]) {
642 unsigned int shift = offset;
644 if (0 == data[--shift]) {
645 if (0 == data[--shift]) {
649 /* The jump is always 3 because of the 1 previously matched.
650 * All the 0's must be after this '1' matched at offset */
652 } else if (0 == data[offset]) {
653 /* maybe next byte is 1? */
656 /* can jump 3 bytes forward */
659 /* at each iteration, we rescan in a backward manner until
660 * we match 0.0.1 in reverse order. Since our search string
661 * has only 2 'alpabets' (i.e. 0 & 1), we know that any
662 * mismatch will force us to shift a fixed number of steps */
664 GST_DEBUG ("Cannot find next NAL start code. returning %u", size);
670 gst_rtp_h264_pay_decode_nal (GstRtpH264Pay * payloader,
671 const guint8 * data, guint size, GstClockTime timestamp)
673 const guint8 *sps = NULL, *pps = NULL;
674 guint sps_len = 0, pps_len = 0;
679 /* default is no update */
682 GST_DEBUG ("NAL payload len=%u", size);
686 type = header & 0x1f;
688 /* keep sps & pps separately so that we can update either one
689 * independently. We also record the timestamp of the last SPS/PPS so
690 * that we can insert them at regular intervals and when needed. */
691 if (SPS_TYPE_ID == type) {
692 /* encode the entire SPS NAL in base64 */
693 GST_DEBUG ("Found SPS %x %x %x Len=%u", (header >> 7),
694 (header >> 5) & 3, type, len);
698 /* remember when we last saw SPS */
700 payloader->last_spspps = timestamp;
701 } else if (PPS_TYPE_ID == type) {
702 /* encoder the entire PPS NAL in base64 */
703 GST_DEBUG ("Found PPS %x %x %x Len = %u",
704 (header >> 7), (header >> 5) & 3, type, len);
708 /* remember when we last saw PPS */
710 payloader->last_spspps = timestamp;
712 GST_DEBUG ("NAL: %x %x %x Len = %u", (header >> 7),
713 (header >> 5) & 3, type, len);
716 /* If we encountered an SPS and/or a PPS, check if it's the
717 * same as the one we have. If not, update our version and
718 * set updated to TRUE
723 if (payloader->sps != NULL) {
724 sps_buf = GST_BUFFER_CAST (payloader->sps->data);
726 if (gst_buffer_memcmp (sps_buf, 0, sps, sps_len)) {
727 /* something changed, update */
728 payloader->profile = (sps[1] << 16) + (sps[2] << 8) + sps[3];
729 GST_DEBUG ("Profile level IDC = %06x", payloader->profile);
733 /* no previous SPS, update */
738 sps_buf = gst_buffer_new_and_alloc (sps_len);
739 gst_buffer_fill (sps_buf, 0, sps, sps_len);
741 if (payloader->sps) {
742 /* replace old buffer */
743 gst_buffer_unref (payloader->sps->data);
744 payloader->sps->data = sps_buf;
747 payloader->sps = g_list_prepend (payloader->sps, sps_buf);
755 if (payloader->pps != NULL) {
756 pps_buf = GST_BUFFER_CAST (payloader->pps->data);
758 if (gst_buffer_memcmp (pps_buf, 0, pps, pps_len)) {
759 /* something changed, update */
763 /* no previous SPS, update */
768 pps_buf = gst_buffer_new_and_alloc (pps_len);
769 gst_buffer_fill (pps_buf, 0, pps, pps_len);
771 if (payloader->pps) {
772 /* replace old buffer */
773 gst_buffer_unref (payloader->pps->data);
774 payloader->pps->data = pps_buf;
777 payloader->pps = g_list_prepend (payloader->pps, pps_buf);
785 gst_rtp_h264_pay_payload_nal (GstBaseRTPPayload * basepayload,
786 const guint8 * data, guint size, GstClockTime timestamp,
787 GstBuffer * buffer_orig, gboolean end_of_au);
790 gst_rtp_h264_pay_send_sps_pps (GstBaseRTPPayload * basepayload,
791 GstRtpH264Pay * rtph264pay, GstClockTime timestamp)
793 GstFlowReturn ret = GST_FLOW_OK;
798 for (walk = rtph264pay->sps; walk; walk = g_list_next (walk)) {
799 GstBuffer *sps_buf = GST_BUFFER_CAST (walk->data);
801 GST_DEBUG_OBJECT (rtph264pay, "inserting SPS in the stream");
803 data = gst_buffer_map (sps_buf, &size, NULL, GST_MAP_READ);
804 ret = gst_rtp_h264_pay_payload_nal (basepayload,
805 data, size, timestamp, sps_buf, FALSE);
806 gst_buffer_unmap (sps_buf, data, size);
807 /* Not critical here; but throw a warning */
808 if (ret != GST_FLOW_OK)
809 GST_WARNING ("Problem pushing SPS");
811 for (walk = rtph264pay->pps; walk; walk = g_list_next (walk)) {
812 GstBuffer *pps_buf = GST_BUFFER_CAST (walk->data);
814 GST_DEBUG_OBJECT (rtph264pay, "inserting PPS in the stream");
816 data = gst_buffer_map (pps_buf, &size, NULL, GST_MAP_READ);
817 ret = gst_rtp_h264_pay_payload_nal (basepayload,
818 data, size, timestamp, pps_buf, FALSE);
819 gst_buffer_unmap (pps_buf, data, size);
820 /* Not critical here; but throw a warning */
821 if (ret != GST_FLOW_OK)
822 GST_WARNING ("Problem pushing PPS");
826 rtph264pay->last_spspps = timestamp;
832 gst_rtp_h264_pay_payload_nal (GstBaseRTPPayload * basepayload,
833 const guint8 * data, guint size, GstClockTime timestamp,
834 GstBuffer * buffer_orig, gboolean end_of_au)
836 GstRtpH264Pay *rtph264pay;
839 guint packet_len, payload_len, mtu;
843 GstBufferList *list = NULL;
845 gboolean send_spspps;
846 GstRTPBuffer rtp = { NULL };
848 rtph264pay = GST_RTP_H264_PAY (basepayload);
849 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtph264pay);
851 nalType = data[0] & 0x1f;
852 GST_DEBUG_OBJECT (rtph264pay, "Processing Buffer with NAL TYPE=%d", nalType);
856 /* check if we need to emit an SPS/PPS now */
857 if (nalType == IDR_TYPE_ID && rtph264pay->spspps_interval > 0) {
858 if (rtph264pay->last_spspps != -1) {
861 GST_LOG_OBJECT (rtph264pay,
862 "now %" GST_TIME_FORMAT ", last SPS/PPS %" GST_TIME_FORMAT,
863 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtph264pay->last_spspps));
865 /* calculate diff between last SPS/PPS in milliseconds */
866 if (timestamp > rtph264pay->last_spspps)
867 diff = timestamp - rtph264pay->last_spspps;
871 GST_DEBUG_OBJECT (rtph264pay,
872 "interval since last SPS/PPS %" GST_TIME_FORMAT,
873 GST_TIME_ARGS (diff));
875 /* bigger than interval, queue SPS/PPS */
876 if (GST_TIME_AS_SECONDS (diff) >= rtph264pay->spspps_interval) {
877 GST_DEBUG_OBJECT (rtph264pay, "time to send SPS/PPS");
881 /* no know previous SPS/PPS time, send now */
882 GST_DEBUG_OBJECT (rtph264pay, "no previous SPS/PPS time, send now");
887 if (send_spspps || rtph264pay->send_spspps) {
888 /* we need to send SPS/PPS now first. FIXME, don't use the timestamp for
889 * checking when we need to send SPS/PPS but convert to running_time first. */
890 rtph264pay->send_spspps = FALSE;
891 ret = gst_rtp_h264_pay_send_sps_pps (basepayload, rtph264pay, timestamp);
892 if (ret != GST_FLOW_OK)
896 packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
898 if (packet_len < mtu) {
899 GST_DEBUG_OBJECT (basepayload,
900 "NAL Unit fit in one packet datasize=%d mtu=%d", size, mtu);
901 /* will fit in one packet */
904 if (rtph264pay->buffer_list) {
906 * first create buffer without payload containing only the RTP header
907 * and then another buffer containing the payload. both buffers will
908 * be then added to the list */
909 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
913 /* use the old-fashioned way with a single buffer and memcpy */
914 outbuf = gst_rtp_buffer_new_allocate (size, 0, 0);
917 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
919 /* only set the marker bit on packets containing access units */
920 if (IS_ACCESS_UNIT (nalType) && end_of_au) {
921 gst_rtp_buffer_set_marker (&rtp, 1);
924 /* timestamp the outbuffer */
925 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
928 if (rtph264pay->buffer_list) {
931 /* create another buffer with the payload. */
933 paybuf = gst_buffer_create_sub (buffer_orig, data -
934 GST_BUFFER_DATA (buffer_orig), size);
936 paybuf = gst_buffer_new_and_alloc (size);
937 gst_buffer_fill (paybuf, 0, data, size);
940 list = gst_buffer_list_new ();
942 /* add both buffers to the buffer list */
943 gst_buffer_list_add (list, outbuf);
944 gst_buffer_list_add (list, paybuf);
946 /* push the list to the next element in the pipe */
947 ret = gst_basertppayload_push_list (basepayload, list);
951 payload = gst_rtp_buffer_get_payload (&rtp);
952 GST_DEBUG_OBJECT (basepayload, "Copying %d bytes to outbuf", size);
953 memcpy (payload, data, size);
954 gst_rtp_buffer_unmap (&rtp);
956 ret = gst_basertppayload_push (basepayload, outbuf);
959 /* fragmentation Units FU-A */
962 int ii = 0, start = 1, end = 0, pos = 0;
964 GST_DEBUG_OBJECT (basepayload,
965 "NAL Unit DOES NOT fit in one packet datasize=%d mtu=%d", size, mtu);
973 GST_DEBUG_OBJECT (basepayload, "Using FU-A fragmentation for data size=%d",
976 /* We keep 2 bytes for FU indicator and FU Header */
977 payload_len = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
980 if (rtph264pay->buffer_list) {
981 list = gst_buffer_list_new ();
982 it = gst_buffer_list_iterate (list);
987 limitedSize = size < payload_len ? size : payload_len;
988 GST_DEBUG_OBJECT (basepayload,
989 "Inside FU-A fragmentation limitedSize=%d iteration=%d", limitedSize,
993 if (rtph264pay->buffer_list) {
995 * first create buffer without payload containing only the RTP header
996 * and then another buffer containing the payload. both buffers will
997 * be then added to the list */
998 outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
1002 /* use the old-fashioned way with a single buffer and memcpy
1003 * first create buffer to hold the payload */
1004 outbuf = gst_rtp_buffer_new_allocate (limitedSize + 2, 0, 0);
1007 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
1009 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
1010 payload = gst_rtp_buffer_get_payload (&rtp);
1012 if (limitedSize == size) {
1013 GST_DEBUG_OBJECT (basepayload, "end size=%d iteration=%d", size, ii);
1016 if (IS_ACCESS_UNIT (nalType)) {
1017 gst_rtp_buffer_set_marker (&rtp, end && end_of_au);
1021 payload[0] = (nalHeader & 0x60) | 28;
1024 payload[1] = (start << 7) | (end << 6) | (nalHeader & 0x1f);
1027 if (rtph264pay->buffer_list) {
1030 /* create another buffer to hold the payload */
1032 paybuf = gst_buffer_create_sub (buffer_orig, data -
1033 GST_BUFFER_DATA (buffer_orig) + pos, limitedSize);
1035 paybuf = gst_buffer_new_and_alloc (limitedSize);
1036 memcpy (GST_BUFFER_DATA (paybuf), data + pos, limitedSize);
1039 /* create a new group to hold the header and the payload */
1040 gst_buffer_list_iterator_add_group (it);
1042 /* add both buffers to the buffer list */
1043 gst_buffer_list_iterator_add (it, outbuf);
1044 gst_buffer_list_iterator_add (it, paybuf);
1049 memcpy (&payload[2], data + pos, limitedSize);
1050 gst_rtp_buffer_unmap (&rtp);
1051 GST_DEBUG_OBJECT (basepayload,
1052 "recorded %d payload bytes into packet iteration=%d",
1053 limitedSize + 2, ii);
1055 ret = gst_basertppayload_push (basepayload, outbuf);
1056 if (ret != GST_FLOW_OK)
1060 size -= limitedSize;
1067 if (rtph264pay->buffer_list) {
1068 /* free iterator and push the whole buffer list at once */
1069 gst_buffer_list_iterator_free (it);
1070 ret = gst_basertppayload_push_list (basepayload, list);
1077 static GstFlowReturn
1078 gst_rtp_h264_pay_handle_buffer (GstBaseRTPPayload * basepayload,
1081 GstRtpH264Pay *rtph264pay;
1085 guint8 *bdata = NULL;
1087 const guint8 *data, *nal_data;
1088 GstClockTime timestamp;
1091 gboolean bytestream;
1093 rtph264pay = GST_RTP_H264_PAY (basepayload);
1095 /* the input buffer contains one or more NAL units */
1097 bytestream = (rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM);
1100 timestamp = gst_adapter_prev_timestamp (rtph264pay->adapter, NULL);
1101 gst_adapter_push (rtph264pay->adapter, buffer);
1102 size = gst_adapter_available (rtph264pay->adapter);
1103 data = gst_adapter_map (rtph264pay->adapter, size);
1104 GST_DEBUG_OBJECT (basepayload, "got %d bytes (%d)", size,
1105 gst_buffer_get_size (buffer));
1107 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
1108 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1110 bdata = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
1113 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1114 GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
1119 /* now loop over all NAL units and put them in a packet
1120 * FIXME, we should really try to pack multiple NAL units into one RTP packet
1121 * if we can, especially for the config packets that wont't cause decoder
1123 if (rtph264pay->packetized) {
1124 guint nal_length_size;
1126 nal_length_size = rtph264pay->nal_length_size;
1128 while (size > nal_length_size) {
1130 gboolean end_of_au = FALSE;
1133 for (i = 0; i < nal_length_size; i++) {
1134 nal_len = ((nal_len << 8) + data[i]);
1137 /* skip the length bytes, make sure we don't run past the buffer size */
1138 data += nal_length_size;
1139 size -= nal_length_size;
1141 if (size >= nal_len) {
1142 GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len);
1145 GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u",
1149 /* If we're at the end of the buffer, then we're at the end of the
1152 if (rtph264pay->au_alignment && size - nal_len <= nal_length_size) {
1157 gst_rtp_h264_pay_payload_nal (basepayload, data, nal_len, timestamp,
1159 if (ret != GST_FLOW_OK)
1167 gboolean update = FALSE;
1169 /* get offset of first start code */
1170 next = next_start_code (data, size);
1172 /* skip to start code, if no start code is found, next will be size and we
1173 * will not collect data. */
1177 nal_queue = rtph264pay->queue;
1179 /* array must be empty when we get here */
1180 g_assert (nal_queue->len == 0);
1182 GST_DEBUG_OBJECT (basepayload, "found first start at %u, bytes left %u",
1185 /* first pass to locate NALs and parse SPS/PPS */
1187 /* skip start code */
1191 if (rtph264pay->scan_mode == GST_H264_SCAN_MODE_SINGLE_NAL) {
1192 /* we are told that there is only a single NAL in this packet so that we
1193 * can avoid scanning for the next NAL. */
1196 /* use next_start_code() to scan buffer.
1197 * next_start_code() returns the offset in data,
1198 * starting from zero to the first byte of 0.0.0.1
1199 * If no start code is found, it returns the value of the
1201 * data is unchanged by the call to next_start_code()
1203 next = next_start_code (data, size);
1206 && rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM) {
1207 /* Didn't find the start of next NAL, handle it next time */
1212 /* nal length is distance to next start code */
1215 GST_DEBUG_OBJECT (basepayload, "found next start at %u of size %u", next,
1218 if (rtph264pay->sprop_parameter_sets != NULL) {
1219 /* explicitly set profile and sprop, use those */
1220 if (rtph264pay->update_caps) {
1221 if (!gst_basertppayload_set_outcaps (basepayload,
1222 "sprop-parameter-sets", G_TYPE_STRING,
1223 rtph264pay->sprop_parameter_sets, NULL))
1226 /* parse SPS and PPS from provided parameter set (for insertion) */
1227 gst_rtp_h264_pay_parse_sprop_parameter_sets (rtph264pay);
1229 rtph264pay->update_caps = FALSE;
1231 GST_DEBUG ("outcaps update: sprop-parameter-sets=%s",
1232 rtph264pay->sprop_parameter_sets);
1235 /* We know our stream is a valid H264 NAL packet,
1236 * go parse it for SPS/PPS to enrich the caps */
1237 /* order: make sure to check nal */
1239 gst_rtp_h264_pay_decode_nal (rtph264pay, data, nal_len, timestamp)
1242 /* move to next NAL packet */
1246 g_array_append_val (nal_queue, nal_len);
1249 /* if has new SPS & PPS, update the output caps */
1250 if (G_UNLIKELY (update))
1251 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
1254 /* second pass to payload and push */
1258 for (i = 0; i < nal_queue->len; i++) {
1260 gboolean end_of_au = FALSE;
1262 nal_len = g_array_index (nal_queue, guint, i);
1263 /* skip start code */
1266 /* Trim the end unless we're the last NAL in the buffer.
1267 * In case we're not at the end of the buffer we know the next block
1268 * starts with 0x000001 so all the 0x00 bytes at the end of this one are
1269 * trailing 0x0 that can be discarded */
1271 if (i + 1 != nal_queue->len
1272 || rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM)
1273 for (; size > 1 && data[size - 1] == 0x0; size--)
1276 /* If it's the last nal unit we have in non-bytestream mode, we can
1277 * assume it's the end of an access-unit
1279 * FIXME: We need to wait until the next packet or EOS to
1280 * actually payload the NAL so we can know if the current NAL is
1281 * the last one of an access unit or not if we are in bytestream mode
1283 if (rtph264pay->au_alignment &&
1284 rtph264pay->scan_mode != GST_H264_SCAN_MODE_BYTESTREAM &&
1285 i == nal_queue->len - 1)
1288 /* put the data in one or more RTP packets */
1290 gst_rtp_h264_pay_payload_nal (basepayload, data, size, timestamp,
1292 if (ret != GST_FLOW_OK) {
1296 /* move to next NAL packet */
1299 pushed += nal_len + 3;
1301 g_array_set_size (nal_queue, 0);
1306 gst_adapter_unmap (rtph264pay->adapter, pushed);
1308 gst_buffer_unmap (buffer, bdata, bsize);
1309 gst_buffer_unref (buffer);
1316 GST_WARNING_OBJECT (basepayload, "Could not set outcaps");
1317 g_array_set_size (nal_queue, 0);
1318 ret = GST_FLOW_NOT_NEGOTIATED;
1324 gst_rtp_h264_pay_handle_event (GstBaseRTPPayload * payload, GstEvent * event)
1327 const GstStructure *s;
1328 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (payload);
1330 switch (GST_EVENT_TYPE (event)) {
1331 case GST_EVENT_FLUSH_STOP:
1332 gst_adapter_clear (rtph264pay->adapter);
1334 case GST_EVENT_CUSTOM_DOWNSTREAM:
1335 s = gst_event_get_structure (event);
1336 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1337 gboolean resend_codec_data;
1339 if (gst_structure_get_boolean (s, "all-headers",
1340 &resend_codec_data) && resend_codec_data)
1341 rtph264pay->send_spspps = TRUE;
1349 GST_BASE_RTP_PAYLOAD_CLASS (parent_class)->handle_event (payload, event);
1354 static GstStateChangeReturn
1355 gst_basertppayload_change_state (GstElement * element,
1356 GstStateChange transition)
1358 GstStateChangeReturn ret;
1359 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (element);
1361 switch (transition) {
1362 case GST_STATE_CHANGE_READY_TO_PAUSED:
1363 rtph264pay->send_spspps = FALSE;
1364 gst_adapter_clear (rtph264pay->adapter);
1370 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1376 gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
1377 const GValue * value, GParamSpec * pspec)
1379 GstRtpH264Pay *rtph264pay;
1381 rtph264pay = GST_RTP_H264_PAY (object);
1384 case PROP_PROFILE_LEVEL_ID:
1386 case PROP_SPROP_PARAMETER_SETS:
1387 g_free (rtph264pay->sprop_parameter_sets);
1388 rtph264pay->sprop_parameter_sets = g_value_dup_string (value);
1389 rtph264pay->update_caps = TRUE;
1391 case PROP_SCAN_MODE:
1392 rtph264pay->scan_mode = g_value_get_enum (value);
1394 case PROP_BUFFER_LIST:
1395 rtph264pay->buffer_list = g_value_get_boolean (value);
1397 case PROP_CONFIG_INTERVAL:
1398 rtph264pay->spspps_interval = g_value_get_uint (value);
1401 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1407 gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
1408 GValue * value, GParamSpec * pspec)
1410 GstRtpH264Pay *rtph264pay;
1412 rtph264pay = GST_RTP_H264_PAY (object);
1415 case PROP_PROFILE_LEVEL_ID:
1417 case PROP_SPROP_PARAMETER_SETS:
1418 g_value_set_string (value, rtph264pay->sprop_parameter_sets);
1420 case PROP_SCAN_MODE:
1421 g_value_set_enum (value, rtph264pay->scan_mode);
1423 case PROP_BUFFER_LIST:
1424 g_value_set_boolean (value, rtph264pay->buffer_list);
1426 case PROP_CONFIG_INTERVAL:
1427 g_value_set_uint (value, rtph264pay->spspps_interval);
1430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1436 gst_rtp_h264_pay_plugin_init (GstPlugin * plugin)
1438 return gst_element_register (plugin, "rtph264pay",
1439 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_PAY);