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,
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 (GstPad * pad, GstEvent * event);
120 static GstStateChangeReturn gst_basertppayload_change_state (GstElement *
121 element, GstStateChange transition);
123 GST_BOILERPLATE (GstRtpH264Pay, gst_rtp_h264_pay, GstBaseRTPPayload,
124 GST_TYPE_BASE_RTP_PAYLOAD);
127 gst_rtp_h264_pay_base_init (gpointer klass)
129 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
131 gst_element_class_add_static_pad_template (element_class,
132 &gst_rtp_h264_pay_src_template);
133 gst_element_class_add_static_pad_template (element_class,
134 &gst_rtp_h264_pay_sink_template);
136 gst_element_class_set_details_simple (element_class, "RTP H264 payloader",
137 "Codec/Payloader/Network/RTP",
138 "Payload-encode H264 video into RTP packets (RFC 3984)",
139 "Laurent Glayal <spglegle@yahoo.fr>");
143 gst_rtp_h264_pay_class_init (GstRtpH264PayClass * klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
147 GstBaseRTPPayloadClass *gstbasertppayload_class;
149 gobject_class = (GObjectClass *) klass;
150 gstelement_class = (GstElementClass *) klass;
151 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
153 gobject_class->set_property = gst_rtp_h264_pay_set_property;
154 gobject_class->get_property = gst_rtp_h264_pay_get_property;
156 g_object_class_install_property (G_OBJECT_CLASS (klass),
157 PROP_PROFILE_LEVEL_ID, g_param_spec_string ("profile-level-id",
159 "The base64 profile-level-id to set in the sink caps (deprecated)",
160 DEFAULT_PROFILE_LEVEL_ID,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 g_object_class_install_property (G_OBJECT_CLASS (klass),
164 PROP_SPROP_PARAMETER_SETS, g_param_spec_string ("sprop-parameter-sets",
165 "sprop-parameter-sets",
166 "The base64 sprop-parameter-sets to set in out caps (set to NULL to "
167 "extract from stream)",
168 DEFAULT_SPROP_PARAMETER_SETS,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCAN_MODE,
172 g_param_spec_enum ("scan-mode", "Scan Mode",
173 "How to scan the input buffers for NAL units. Performance can be "
174 "increased when certain assumptions are made about the input buffers",
175 GST_TYPE_H264_SCAN_MODE, DEFAULT_SCAN_MODE,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
179 g_param_spec_boolean ("buffer-list", "Buffer List",
181 DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (G_OBJECT_CLASS (klass),
184 PROP_CONFIG_INTERVAL,
185 g_param_spec_uint ("config-interval",
186 "SPS PPS Send Interval",
187 "Send SPS and PPS Insertion Interval in seconds (sprop parameter sets "
188 "will be multiplexed in the data stream when detected.) (0 = disabled)",
189 0, 3600, DEFAULT_CONFIG_INTERVAL,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
193 gobject_class->finalize = gst_rtp_h264_pay_finalize;
195 gstelement_class->change_state =
196 GST_DEBUG_FUNCPTR (gst_basertppayload_change_state);
198 gstbasertppayload_class->get_caps = gst_rtp_h264_pay_getcaps;
199 gstbasertppayload_class->set_caps = gst_rtp_h264_pay_setcaps;
200 gstbasertppayload_class->handle_buffer = gst_rtp_h264_pay_handle_buffer;
201 gstbasertppayload_class->handle_event = gst_rtp_h264_pay_handle_event;
203 GST_DEBUG_CATEGORY_INIT (rtph264pay_debug, "rtph264pay", 0,
204 "H264 RTP Payloader");
208 gst_rtp_h264_pay_init (GstRtpH264Pay * rtph264pay, GstRtpH264PayClass * klass)
210 rtph264pay->queue = g_array_new (FALSE, FALSE, sizeof (guint));
211 rtph264pay->profile = 0;
212 rtph264pay->sps = NULL;
213 rtph264pay->pps = NULL;
214 rtph264pay->last_spspps = -1;
215 rtph264pay->scan_mode = GST_H264_SCAN_MODE_MULTI_NAL;
216 rtph264pay->buffer_list = DEFAULT_BUFFER_LIST;
217 rtph264pay->spspps_interval = DEFAULT_CONFIG_INTERVAL;
219 rtph264pay->adapter = gst_adapter_new ();
223 gst_rtp_h264_pay_clear_sps_pps (GstRtpH264Pay * rtph264pay)
225 g_list_foreach (rtph264pay->sps, (GFunc) gst_mini_object_unref, NULL);
226 g_list_free (rtph264pay->sps);
227 rtph264pay->sps = NULL;
228 g_list_foreach (rtph264pay->pps, (GFunc) gst_mini_object_unref, NULL);
229 g_list_free (rtph264pay->pps);
230 rtph264pay->pps = NULL;
234 gst_rtp_h264_pay_finalize (GObject * object)
236 GstRtpH264Pay *rtph264pay;
238 rtph264pay = GST_RTP_H264_PAY (object);
240 g_array_free (rtph264pay->queue, TRUE);
242 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
244 g_free (rtph264pay->sprop_parameter_sets);
246 g_object_unref (rtph264pay->adapter);
248 G_OBJECT_CLASS (parent_class)->finalize (object);
251 static const gchar *all_levels[] = {
272 gst_rtp_h264_pay_getcaps (GstBaseRTPPayload * payload, GstPad * pad)
274 GstCaps *allowed_caps;
277 gst_pad_peer_get_caps_reffed (GST_BASE_RTP_PAYLOAD_SRCPAD (payload));
280 GstCaps *caps = NULL;
283 if (gst_caps_is_any (allowed_caps)) {
284 gst_caps_unref (allowed_caps);
288 if (gst_caps_is_empty (allowed_caps))
291 caps = gst_caps_new_empty ();
293 for (i = 0; i < gst_caps_get_size (allowed_caps); i++) {
294 GstStructure *s = gst_caps_get_structure (allowed_caps, i);
295 GstStructure *new_s = gst_structure_new ("video/x-h264", NULL);
296 const gchar *profile_level_id;
298 profile_level_id = gst_structure_get_string (s, "profile-level-id");
300 if (profile_level_id && strlen (profile_level_id) == 6) {
301 const gchar *profile;
306 spsint = strtol (profile_level_id, NULL, 16);
307 sps[0] = spsint >> 16;
308 sps[1] = spsint >> 8;
311 profile = gst_codec_utils_h264_get_profile (sps, 3);
312 level = gst_codec_utils_h264_get_level (sps, 3);
314 if (profile && level) {
315 GST_LOG_OBJECT (payload, "In caps, have profile %s and level %s",
318 if (!strcmp (profile, "constrained-baseline"))
319 gst_structure_set (new_s, "profile", G_TYPE_STRING, profile, NULL);
322 GValue profiles = { 0, };
324 g_value_init (&profiles, GST_TYPE_LIST);
325 g_value_init (&val, G_TYPE_STRING);
327 g_value_set_static_string (&val, profile);
328 gst_value_list_append_value (&profiles, &val);
330 g_value_set_static_string (&val, "constrained-baseline");
331 gst_value_list_append_value (&profiles, &val);
333 gst_structure_take_value (new_s, "profile", &profiles);
336 if (!strcmp (level, "1"))
337 gst_structure_set (new_s, "level", G_TYPE_STRING, level, NULL);
339 GValue levels = { 0, };
343 g_value_init (&levels, GST_TYPE_LIST);
344 g_value_init (&val, G_TYPE_STRING);
346 for (j = 0; all_levels[j]; j++) {
347 g_value_set_static_string (&val, all_levels[j]);
348 gst_value_list_prepend_value (&levels, &val);
349 if (!strcmp (level, all_levels[j]))
352 gst_structure_take_value (new_s, "level", &levels);
357 gst_caps_merge_structure (caps, new_s);
360 gst_caps_unref (allowed_caps);
365 return gst_caps_new_simple ("video/x-h264", NULL);
368 /* take the currently configured SPS and PPS lists and set them on the caps as
369 * sprop-parameter-sets */
371 gst_rtp_h264_pay_set_sps_pps (GstBaseRTPPayload * basepayload)
373 GstRtpH264Pay *payloader = GST_RTP_H264_PAY (basepayload);
381 sprops = g_string_new ("");
384 /* build the sprop-parameter-sets */
385 for (walk = payloader->sps; walk; walk = g_list_next (walk)) {
386 GstBuffer *sps_buf = GST_BUFFER_CAST (walk->data);
389 g_base64_encode (GST_BUFFER_DATA (sps_buf), GST_BUFFER_SIZE (sps_buf));
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);
398 g_base64_encode (GST_BUFFER_DATA (pps_buf), GST_BUFFER_SIZE (pps_buf));
399 g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
404 /* profile is 24 bit. Force it to respect the limit */
405 profile = g_strdup_printf ("%06x", payloader->profile & 0xffffff);
406 /* combine into output caps */
407 res = gst_basertppayload_set_outcaps (basepayload,
408 "sprop-parameter-sets", G_TYPE_STRING, sprops->str, NULL);
409 g_string_free (sprops, TRUE);
416 gst_rtp_h264_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
418 GstRtpH264Pay *rtph264pay;
423 const gchar *alignment;
425 rtph264pay = GST_RTP_H264_PAY (basepayload);
427 str = gst_caps_get_structure (caps, 0);
429 /* we can only set the output caps when we found the sprops and profile
431 gst_basertppayload_set_options (basepayload, "video", TRUE, "H264", 90000);
433 alignment = gst_structure_get_string (str, "alignment");
434 if (alignment && !strcmp (alignment, "au"))
435 rtph264pay->au_alignment = TRUE;
437 rtph264pay->au_alignment = FALSE;
439 /* packetized AVC video has a codec_data */
440 if ((value = gst_structure_get_value (str, "codec_data"))) {
442 guint num_sps, num_pps;
445 GST_DEBUG_OBJECT (rtph264pay, "have packetized h264");
446 rtph264pay->packetized = TRUE;
448 buffer = gst_value_get_buffer (value);
449 data = GST_BUFFER_DATA (buffer);
450 size = GST_BUFFER_SIZE (buffer);
452 /* parse the avcC data */
455 /* parse the version, this must be 1 */
459 /* AVCProfileIndication */
461 /* AVCLevelIndication */
462 rtph264pay->profile = (data[1] << 16) | (data[2] << 8) | data[3];
463 GST_DEBUG_OBJECT (rtph264pay, "profile %06x", rtph264pay->profile);
465 /* 6 bits reserved | 2 bits lengthSizeMinusOne */
466 /* this is the number of bytes in front of the NAL units to mark their
468 rtph264pay->nal_length_size = (data[4] & 0x03) + 1;
469 GST_DEBUG_OBJECT (rtph264pay, "nal length %u", rtph264pay->nal_length_size);
470 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
471 num_sps = data[5] & 0x1f;
472 GST_DEBUG_OBJECT (rtph264pay, "num SPS %u", num_sps);
477 /* create the sprop-parameter-sets */
478 for (i = 0; i < num_sps; i++) {
484 nal_size = (data[0] << 8) | data[1];
488 GST_LOG_OBJECT (rtph264pay, "SPS %d size %d", i, nal_size);
493 /* make a buffer out of it and add to SPS list */
494 sps_buf = gst_buffer_new_and_alloc (nal_size);
495 memcpy (GST_BUFFER_DATA (sps_buf), data, nal_size);
496 rtph264pay->sps = g_list_append (rtph264pay->sps, sps_buf);
504 /* 8 bits numOfPictureParameterSets */
509 GST_DEBUG_OBJECT (rtph264pay, "num PPS %u", num_pps);
510 for (i = 0; i < num_pps; i++) {
516 nal_size = (data[0] << 8) | data[1];
520 GST_LOG_OBJECT (rtph264pay, "PPS %d size %d", i, nal_size);
525 /* make a buffer out of it and add to PPS list */
526 pps_buf = gst_buffer_new_and_alloc (nal_size);
527 memcpy (GST_BUFFER_DATA (pps_buf), data, nal_size);
528 rtph264pay->pps = g_list_append (rtph264pay->pps, pps_buf);
533 /* and update the caps with the collected data */
534 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
537 GST_DEBUG_OBJECT (rtph264pay, "have bytestream h264");
538 rtph264pay->packetized = FALSE;
545 GST_ERROR_OBJECT (rtph264pay, "avcC size %u < 7", size);
550 GST_ERROR_OBJECT (rtph264pay, "wrong avcC version");
555 GST_ERROR_OBJECT (rtph264pay, "avcC too small ");
561 gst_rtp_h264_pay_parse_sprop_parameter_sets (GstRtpH264Pay * rtph264pay)
565 guint len, num_sps, num_pps;
569 ps = rtph264pay->sprop_parameter_sets;
573 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
575 params = g_strsplit (ps, ",", 0);
576 len = g_strv_length (params);
578 GST_DEBUG_OBJECT (rtph264pay, "we have %d params", len);
580 num_sps = num_pps = 0;
582 for (i = 0; params[i]; i++) {
588 nal_len = strlen (params[i]);
589 buf = gst_buffer_new_and_alloc (nal_len);
590 nalp = GST_BUFFER_DATA (buf);
592 nal_len = g_base64_decode_step (params[i], nal_len, nalp, &state, &save);
593 GST_BUFFER_SIZE (buf) = nal_len;
596 gst_buffer_unref (buf);
600 /* append to the right list */
601 if ((nalp[0] & 0x1f) == 7) {
602 GST_DEBUG_OBJECT (rtph264pay, "adding param %d as SPS %d", i, num_sps);
603 rtph264pay->sps = g_list_append (rtph264pay->sps, buf);
606 GST_DEBUG_OBJECT (rtph264pay, "adding param %d as PPS %d", i, num_pps);
607 rtph264pay->pps = g_list_append (rtph264pay->pps, buf);
615 next_start_code (const guint8 * data, guint size)
617 /* Boyer-Moore string matching algorithm, in a degenerative
618 * sense because our search 'alphabet' is binary - 0 & 1 only.
619 * This allow us to simplify the general BM algorithm to a very
621 /* assume 1 is in the 3th byte */
624 while (offset < size) {
625 if (1 == data[offset]) {
626 unsigned int shift = offset;
628 if (0 == data[--shift]) {
629 if (0 == data[--shift]) {
633 /* The jump is always 3 because of the 1 previously matched.
634 * All the 0's must be after this '1' matched at offset */
636 } else if (0 == data[offset]) {
637 /* maybe next byte is 1? */
640 /* can jump 3 bytes forward */
643 /* at each iteration, we rescan in a backward manner until
644 * we match 0.0.1 in reverse order. Since our search string
645 * has only 2 'alpabets' (i.e. 0 & 1), we know that any
646 * mismatch will force us to shift a fixed number of steps */
648 GST_DEBUG ("Cannot find next NAL start code. returning %u", size);
654 gst_rtp_h264_pay_decode_nal (GstRtpH264Pay * payloader,
655 const guint8 * data, guint size, GstClockTime timestamp)
657 const guint8 *sps = NULL, *pps = NULL;
658 guint sps_len = 0, pps_len = 0;
663 /* default is no update */
666 GST_DEBUG ("NAL payload len=%u", size);
670 type = header & 0x1f;
672 /* keep sps & pps separately so that we can update either one
673 * independently. We also record the timestamp of the last SPS/PPS so
674 * that we can insert them at regular intervals and when needed. */
675 if (SPS_TYPE_ID == type) {
676 /* encode the entire SPS NAL in base64 */
677 GST_DEBUG ("Found SPS %x %x %x Len=%u", (header >> 7),
678 (header >> 5) & 3, type, len);
682 /* remember when we last saw SPS */
684 payloader->last_spspps = timestamp;
685 } else if (PPS_TYPE_ID == type) {
686 /* encoder the entire PPS NAL in base64 */
687 GST_DEBUG ("Found PPS %x %x %x Len = %u",
688 (header >> 7), (header >> 5) & 3, type, len);
692 /* remember when we last saw PPS */
694 payloader->last_spspps = timestamp;
696 GST_DEBUG ("NAL: %x %x %x Len = %u", (header >> 7),
697 (header >> 5) & 3, type, len);
700 /* If we encountered an SPS and/or a PPS, check if it's the
701 * same as the one we have. If not, update our version and
702 * set updated to TRUE
707 if (payloader->sps != NULL) {
708 sps_buf = GST_BUFFER_CAST (payloader->sps->data);
710 if ((GST_BUFFER_SIZE (sps_buf) != sps_len)
711 || memcmp (GST_BUFFER_DATA (sps_buf), sps, sps_len)) {
712 /* something changed, update */
713 payloader->profile = (sps[1] << 16) + (sps[2] << 8) + sps[3];
714 GST_DEBUG ("Profile level IDC = %06x", payloader->profile);
718 /* no previous SPS, update */
723 sps_buf = gst_buffer_new_and_alloc (sps_len);
724 memcpy (GST_BUFFER_DATA (sps_buf), sps, sps_len);
726 if (payloader->sps) {
727 /* replace old buffer */
728 gst_buffer_unref (payloader->sps->data);
729 payloader->sps->data = sps_buf;
732 payloader->sps = g_list_prepend (payloader->sps, sps_buf);
740 if (payloader->pps != NULL) {
741 pps_buf = GST_BUFFER_CAST (payloader->pps->data);
743 if ((GST_BUFFER_SIZE (pps_buf) != pps_len)
744 || memcmp (GST_BUFFER_DATA (pps_buf), pps, pps_len)) {
745 /* something changed, update */
749 /* no previous SPS, update */
754 pps_buf = gst_buffer_new_and_alloc (pps_len);
755 memcpy (GST_BUFFER_DATA (pps_buf), pps, pps_len);
757 if (payloader->pps) {
758 /* replace old buffer */
759 gst_buffer_unref (payloader->pps->data);
760 payloader->pps->data = pps_buf;
763 payloader->pps = g_list_prepend (payloader->pps, pps_buf);
771 gst_rtp_h264_pay_payload_nal (GstBaseRTPPayload * basepayload,
772 const guint8 * data, guint size, GstClockTime timestamp,
773 GstBuffer * buffer_orig, gboolean end_of_au);
776 gst_rtp_h264_pay_send_sps_pps (GstBaseRTPPayload * basepayload,
777 GstRtpH264Pay * rtph264pay, GstClockTime timestamp)
779 GstFlowReturn ret = GST_FLOW_OK;
782 for (walk = rtph264pay->sps; walk; walk = g_list_next (walk)) {
783 GstBuffer *sps_buf = GST_BUFFER_CAST (walk->data);
785 GST_DEBUG_OBJECT (rtph264pay, "inserting SPS in the stream");
787 ret = gst_rtp_h264_pay_payload_nal (basepayload,
788 GST_BUFFER_DATA (sps_buf), GST_BUFFER_SIZE (sps_buf), timestamp,
790 /* Not critical here; but throw a warning */
791 if (ret != GST_FLOW_OK)
792 GST_WARNING ("Problem pushing SPS");
794 for (walk = rtph264pay->pps; walk; walk = g_list_next (walk)) {
795 GstBuffer *pps_buf = GST_BUFFER_CAST (walk->data);
797 GST_DEBUG_OBJECT (rtph264pay, "inserting PPS in the stream");
799 ret = gst_rtp_h264_pay_payload_nal (basepayload,
800 GST_BUFFER_DATA (pps_buf), GST_BUFFER_SIZE (pps_buf), timestamp,
802 /* Not critical here; but throw a warning */
803 if (ret != GST_FLOW_OK)
804 GST_WARNING ("Problem pushing PPS");
808 rtph264pay->last_spspps = timestamp;
814 gst_rtp_h264_pay_payload_nal (GstBaseRTPPayload * basepayload,
815 const guint8 * data, guint size, GstClockTime timestamp,
816 GstBuffer * buffer_orig, gboolean end_of_au)
818 GstRtpH264Pay *rtph264pay;
821 guint packet_len, payload_len, mtu;
824 GstBufferList *list = NULL;
825 GstBufferListIterator *it = NULL;
826 gboolean send_spspps;
828 rtph264pay = GST_RTP_H264_PAY (basepayload);
829 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtph264pay);
831 nalType = data[0] & 0x1f;
832 GST_DEBUG_OBJECT (rtph264pay, "Processing Buffer with NAL TYPE=%d", nalType);
836 /* check if we need to emit an SPS/PPS now */
837 if (nalType == IDR_TYPE_ID && rtph264pay->spspps_interval > 0) {
838 if (rtph264pay->last_spspps != -1) {
841 GST_LOG_OBJECT (rtph264pay,
842 "now %" GST_TIME_FORMAT ", last SPS/PPS %" GST_TIME_FORMAT,
843 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtph264pay->last_spspps));
845 /* calculate diff between last SPS/PPS in milliseconds */
846 if (timestamp > rtph264pay->last_spspps)
847 diff = timestamp - rtph264pay->last_spspps;
851 GST_DEBUG_OBJECT (rtph264pay,
852 "interval since last SPS/PPS %" GST_TIME_FORMAT,
853 GST_TIME_ARGS (diff));
855 /* bigger than interval, queue SPS/PPS */
856 if (GST_TIME_AS_SECONDS (diff) >= rtph264pay->spspps_interval) {
857 GST_DEBUG_OBJECT (rtph264pay, "time to send SPS/PPS");
861 /* no know previous SPS/PPS time, send now */
862 GST_DEBUG_OBJECT (rtph264pay, "no previous SPS/PPS time, send now");
867 if (send_spspps || rtph264pay->send_spspps) {
868 /* we need to send SPS/PPS now first. FIXME, don't use the timestamp for
869 * checking when we need to send SPS/PPS but convert to running_time first. */
870 rtph264pay->send_spspps = FALSE;
871 ret = gst_rtp_h264_pay_send_sps_pps (basepayload, rtph264pay, timestamp);
872 if (ret != GST_FLOW_OK)
876 packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
878 if (packet_len < mtu) {
879 GST_DEBUG_OBJECT (basepayload,
880 "NAL Unit fit in one packet datasize=%d mtu=%d", size, mtu);
881 /* will fit in one packet */
883 if (rtph264pay->buffer_list) {
885 * first create buffer without payload containing only the RTP header
886 * and then another buffer containing the payload. both buffers will
887 * be then added to the list */
888 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
890 /* use the old-fashioned way with a single buffer and memcpy */
891 outbuf = gst_rtp_buffer_new_allocate (size, 0, 0);
894 /* only set the marker bit on packets containing access units */
895 if (IS_ACCESS_UNIT (nalType) && end_of_au) {
896 gst_rtp_buffer_set_marker (outbuf, 1);
899 /* timestamp the outbuffer */
900 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
902 if (rtph264pay->buffer_list) {
905 /* create another buffer with the payload. */
907 paybuf = gst_buffer_create_sub (buffer_orig, data -
908 GST_BUFFER_DATA (buffer_orig), size);
910 paybuf = gst_buffer_new_and_alloc (size);
911 memcpy (GST_BUFFER_DATA (paybuf), data, size);
914 list = gst_buffer_list_new ();
915 it = gst_buffer_list_iterate (list);
917 /* add both buffers to the buffer list */
918 gst_buffer_list_iterator_add_group (it);
919 gst_buffer_list_iterator_add (it, outbuf);
920 gst_buffer_list_iterator_add (it, paybuf);
922 gst_buffer_list_iterator_free (it);
924 /* push the list to the next element in the pipe */
925 ret = gst_basertppayload_push_list (basepayload, list);
927 payload = gst_rtp_buffer_get_payload (outbuf);
928 GST_DEBUG_OBJECT (basepayload, "Copying %d bytes to outbuf", size);
929 memcpy (payload, data, size);
931 ret = gst_basertppayload_push (basepayload, outbuf);
934 /* fragmentation Units FU-A */
937 int ii = 0, start = 1, end = 0, pos = 0;
939 GST_DEBUG_OBJECT (basepayload,
940 "NAL Unit DOES NOT fit in one packet datasize=%d mtu=%d", size, mtu);
948 GST_DEBUG_OBJECT (basepayload, "Using FU-A fragmentation for data size=%d",
951 /* We keep 2 bytes for FU indicator and FU Header */
952 payload_len = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
954 if (rtph264pay->buffer_list) {
955 list = gst_buffer_list_new ();
956 it = gst_buffer_list_iterate (list);
960 limitedSize = size < payload_len ? size : payload_len;
961 GST_DEBUG_OBJECT (basepayload,
962 "Inside FU-A fragmentation limitedSize=%d iteration=%d", limitedSize,
965 if (rtph264pay->buffer_list) {
967 * first create buffer without payload containing only the RTP header
968 * and then another buffer containing the payload. both buffers will
969 * be then added to the list */
970 outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
972 /* use the old-fashioned way with a single buffer and memcpy
973 * first create buffer to hold the payload */
974 outbuf = gst_rtp_buffer_new_allocate (limitedSize + 2, 0, 0);
977 GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
978 payload = gst_rtp_buffer_get_payload (outbuf);
980 if (limitedSize == size) {
981 GST_DEBUG_OBJECT (basepayload, "end size=%d iteration=%d", size, ii);
984 if (IS_ACCESS_UNIT (nalType)) {
985 gst_rtp_buffer_set_marker (outbuf, end && end_of_au);
989 payload[0] = (nalHeader & 0x60) | 28;
992 payload[1] = (start << 7) | (end << 6) | (nalHeader & 0x1f);
994 if (rtph264pay->buffer_list) {
997 /* create another buffer to hold the payload */
999 paybuf = gst_buffer_create_sub (buffer_orig, data -
1000 GST_BUFFER_DATA (buffer_orig) + pos, limitedSize);
1002 paybuf = gst_buffer_new_and_alloc (limitedSize);
1003 memcpy (GST_BUFFER_DATA (paybuf), data + pos, limitedSize);
1006 /* create a new group to hold the header and the payload */
1007 gst_buffer_list_iterator_add_group (it);
1009 /* add both buffers to the buffer list */
1010 gst_buffer_list_iterator_add (it, outbuf);
1011 gst_buffer_list_iterator_add (it, paybuf);
1014 memcpy (&payload[2], data + pos, limitedSize);
1015 GST_DEBUG_OBJECT (basepayload,
1016 "recorded %d payload bytes into packet iteration=%d",
1017 limitedSize + 2, ii);
1019 ret = gst_basertppayload_push (basepayload, outbuf);
1020 if (ret != GST_FLOW_OK)
1024 size -= limitedSize;
1030 if (rtph264pay->buffer_list) {
1031 /* free iterator and push the whole buffer list at once */
1032 gst_buffer_list_iterator_free (it);
1033 ret = gst_basertppayload_push_list (basepayload, list);
1039 static GstFlowReturn
1040 gst_rtp_h264_pay_handle_buffer (GstBaseRTPPayload * basepayload,
1043 GstRtpH264Pay *rtph264pay;
1045 guint size, nal_len, i;
1046 const guint8 *data, *nal_data;
1047 GstClockTime timestamp;
1051 rtph264pay = GST_RTP_H264_PAY (basepayload);
1053 /* the input buffer contains one or more NAL units */
1055 if (rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM) {
1056 timestamp = gst_adapter_prev_timestamp (rtph264pay->adapter, NULL);
1057 gst_adapter_push (rtph264pay->adapter, buffer);
1058 size = gst_adapter_available (rtph264pay->adapter);
1059 data = gst_adapter_peek (rtph264pay->adapter, size);
1060 GST_DEBUG_OBJECT (basepayload, "got %d bytes (%d)", size,
1061 GST_BUFFER_SIZE (buffer));
1063 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
1064 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1066 size = GST_BUFFER_SIZE (buffer);
1067 data = GST_BUFFER_DATA (buffer);
1068 timestamp = GST_BUFFER_TIMESTAMP (buffer);
1069 GST_DEBUG_OBJECT (basepayload, "got %d bytes", size);
1074 /* now loop over all NAL units and put them in a packet
1075 * FIXME, we should really try to pack multiple NAL units into one RTP packet
1076 * if we can, especially for the config packets that wont't cause decoder
1078 if (rtph264pay->packetized) {
1079 guint nal_length_size;
1081 nal_length_size = rtph264pay->nal_length_size;
1083 while (size > nal_length_size) {
1085 gboolean end_of_au = FALSE;
1088 for (i = 0; i < nal_length_size; i++) {
1089 nal_len = ((nal_len << 8) + data[i]);
1092 /* skip the length bytes, make sure we don't run past the buffer size */
1093 data += nal_length_size;
1094 size -= nal_length_size;
1096 if (size >= nal_len) {
1097 GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len);
1100 GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u",
1104 /* If we're at the end of the buffer, then we're at the end of the
1107 if (rtph264pay->au_alignment && size - nal_len <= nal_length_size) {
1112 gst_rtp_h264_pay_payload_nal (basepayload, data, nal_len, timestamp,
1114 if (ret != GST_FLOW_OK)
1122 gboolean update = FALSE;
1124 /* get offset of first start code */
1125 next = next_start_code (data, size);
1127 /* skip to start code, if no start code is found, next will be size and we
1128 * will not collect data. */
1132 nal_queue = rtph264pay->queue;
1134 /* array must be empty when we get here */
1135 g_assert (nal_queue->len == 0);
1137 GST_DEBUG_OBJECT (basepayload, "found first start at %u, bytes left %u",
1140 /* first pass to locate NALs and parse SPS/PPS */
1142 /* skip start code */
1146 if (rtph264pay->scan_mode == GST_H264_SCAN_MODE_SINGLE_NAL) {
1147 /* we are told that there is only a single NAL in this packet so that we
1148 * can avoid scanning for the next NAL. */
1151 /* use next_start_code() to scan buffer.
1152 * next_start_code() returns the offset in data,
1153 * starting from zero to the first byte of 0.0.0.1
1154 * If no start code is found, it returns the value of the
1156 * data is unchanged by the call to next_start_code()
1158 next = next_start_code (data, size);
1161 && rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM) {
1162 /* Didn't find the start of next NAL, handle it next time */
1167 /* nal length is distance to next start code */
1170 GST_DEBUG_OBJECT (basepayload, "found next start at %u of size %u", next,
1173 if (rtph264pay->sprop_parameter_sets != NULL) {
1174 /* explicitly set profile and sprop, use those */
1175 if (rtph264pay->update_caps) {
1176 if (!gst_basertppayload_set_outcaps (basepayload,
1177 "sprop-parameter-sets", G_TYPE_STRING,
1178 rtph264pay->sprop_parameter_sets, NULL))
1181 /* parse SPS and PPS from provided parameter set (for insertion) */
1182 gst_rtp_h264_pay_parse_sprop_parameter_sets (rtph264pay);
1184 rtph264pay->update_caps = FALSE;
1186 GST_DEBUG ("outcaps update: sprop-parameter-sets=%s",
1187 rtph264pay->sprop_parameter_sets);
1190 /* We know our stream is a valid H264 NAL packet,
1191 * go parse it for SPS/PPS to enrich the caps */
1192 /* order: make sure to check nal */
1194 gst_rtp_h264_pay_decode_nal (rtph264pay, data, nal_len, timestamp)
1197 /* move to next NAL packet */
1201 g_array_append_val (nal_queue, nal_len);
1204 /* if has new SPS & PPS, update the output caps */
1205 if (G_UNLIKELY (update))
1206 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
1209 /* second pass to payload and push */
1213 for (i = 0; i < nal_queue->len; i++) {
1215 gboolean end_of_au = FALSE;
1217 nal_len = g_array_index (nal_queue, guint, i);
1218 /* skip start code */
1221 /* Trim the end unless we're the last NAL in the buffer.
1222 * In case we're not at the end of the buffer we know the next block
1223 * starts with 0x000001 so all the 0x00 bytes at the end of this one are
1224 * trailing 0x0 that can be discarded */
1226 if (i + 1 != nal_queue->len
1227 || rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM)
1228 for (; size > 1 && data[size - 1] == 0x0; size--)
1231 /* If it's the last nal unit we have in non-bytestream mode, we can
1232 * assume it's the end of an access-unit
1234 * FIXME: We need to wait until the next packet or EOS to
1235 * actually payload the NAL so we can know if the current NAL is
1236 * the last one of an access unit or not if we are in bytestream mode
1238 if (rtph264pay->au_alignment &&
1239 rtph264pay->scan_mode != GST_H264_SCAN_MODE_BYTESTREAM &&
1240 i == nal_queue->len - 1)
1243 /* put the data in one or more RTP packets */
1245 gst_rtp_h264_pay_payload_nal (basepayload, data, size, timestamp,
1247 if (ret != GST_FLOW_OK) {
1251 /* move to next NAL packet */
1254 pushed += nal_len + 3;
1256 g_array_set_size (nal_queue, 0);
1259 if (rtph264pay->scan_mode == GST_H264_SCAN_MODE_BYTESTREAM)
1260 gst_adapter_flush (rtph264pay->adapter, pushed);
1262 gst_buffer_unref (buffer);
1268 GST_WARNING_OBJECT (basepayload, "Could not set outcaps");
1269 g_array_set_size (nal_queue, 0);
1270 gst_buffer_unref (buffer);
1271 return GST_FLOW_NOT_NEGOTIATED;
1276 gst_rtp_h264_pay_handle_event (GstPad * pad, GstEvent * event)
1278 const GstStructure *s;
1279 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (GST_PAD_PARENT (pad));
1281 switch (GST_EVENT_TYPE (event)) {
1282 case GST_EVENT_FLUSH_STOP:
1283 gst_adapter_clear (rtph264pay->adapter);
1285 case GST_EVENT_CUSTOM_DOWNSTREAM:
1286 s = gst_event_get_structure (event);
1287 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1288 gboolean resend_codec_data;
1290 if (gst_structure_get_boolean (s, "all-headers",
1291 &resend_codec_data) && resend_codec_data)
1292 rtph264pay->send_spspps = TRUE;
1302 static GstStateChangeReturn
1303 gst_basertppayload_change_state (GstElement * element,
1304 GstStateChange transition)
1306 GstStateChangeReturn ret;
1307 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (element);
1309 switch (transition) {
1310 case GST_STATE_CHANGE_READY_TO_PAUSED:
1311 rtph264pay->send_spspps = FALSE;
1312 gst_adapter_clear (rtph264pay->adapter);
1318 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1324 gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
1325 const GValue * value, GParamSpec * pspec)
1327 GstRtpH264Pay *rtph264pay;
1329 rtph264pay = GST_RTP_H264_PAY (object);
1332 case PROP_PROFILE_LEVEL_ID:
1334 case PROP_SPROP_PARAMETER_SETS:
1335 g_free (rtph264pay->sprop_parameter_sets);
1336 rtph264pay->sprop_parameter_sets = g_value_dup_string (value);
1337 rtph264pay->update_caps = TRUE;
1339 case PROP_SCAN_MODE:
1340 rtph264pay->scan_mode = g_value_get_enum (value);
1342 case PROP_BUFFER_LIST:
1343 rtph264pay->buffer_list = g_value_get_boolean (value);
1345 case PROP_CONFIG_INTERVAL:
1346 rtph264pay->spspps_interval = g_value_get_uint (value);
1349 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1355 gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
1356 GValue * value, GParamSpec * pspec)
1358 GstRtpH264Pay *rtph264pay;
1360 rtph264pay = GST_RTP_H264_PAY (object);
1363 case PROP_PROFILE_LEVEL_ID:
1365 case PROP_SPROP_PARAMETER_SETS:
1366 g_value_set_string (value, rtph264pay->sprop_parameter_sets);
1368 case PROP_SCAN_MODE:
1369 g_value_set_enum (value, rtph264pay->scan_mode);
1371 case PROP_BUFFER_LIST:
1372 g_value_set_boolean (value, rtph264pay->buffer_list);
1374 case PROP_CONFIG_INTERVAL:
1375 g_value_set_uint (value, rtph264pay->spspps_interval);
1378 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1384 gst_rtp_h264_pay_plugin_init (GstPlugin * plugin)
1386 return gst_element_register (plugin, "rtph264pay",
1387 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_PAY);