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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/pbutils/pbutils.h>
30 /* Included to not duplicate gst_rtp_h264_add_sps_pps () */
31 #include "gstrtph264depay.h"
33 #include "gstrtph264pay.h"
40 GST_DEBUG_CATEGORY_STATIC (rtph264pay_debug);
41 #define GST_CAT_DEFAULT (rtph264pay_debug)
48 static GstStaticPadTemplate gst_rtp_h264_pay_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
52 GST_STATIC_CAPS ("video/x-h264, "
53 "stream-format = (string) avc, alignment = (string) au;"
55 "stream-format = (string) byte-stream, alignment = (string) { nal, au }")
58 static GstStaticPadTemplate gst_rtp_h264_pay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
62 GST_STATIC_CAPS ("application/x-rtp, "
63 "media = (string) \"video\", "
64 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
65 "clock-rate = (int) 90000, " "encoding-name = (string) \"H264\"")
68 #define DEFAULT_SPROP_PARAMETER_SETS NULL
69 #define DEFAULT_CONFIG_INTERVAL 0
74 PROP_SPROP_PARAMETER_SETS,
79 #define IS_ACCESS_UNIT(x) (((x) > 0x00) && ((x) < 0x06))
81 static void gst_rtp_h264_pay_finalize (GObject * object);
83 static void gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
84 const GValue * value, GParamSpec * pspec);
85 static void gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
86 GValue * value, GParamSpec * pspec);
88 static GstCaps *gst_rtp_h264_pay_getcaps (GstRTPBasePayload * payload,
89 GstPad * pad, GstCaps * filter);
90 static gboolean gst_rtp_h264_pay_setcaps (GstRTPBasePayload * basepayload,
92 static GstFlowReturn gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * pad,
94 static gboolean gst_rtp_h264_pay_sink_event (GstRTPBasePayload * payload,
96 static GstStateChangeReturn gst_rtp_h264_pay_change_state (GstElement *
97 element, GstStateChange transition);
99 #define gst_rtp_h264_pay_parent_class parent_class
100 G_DEFINE_TYPE (GstRtpH264Pay, gst_rtp_h264_pay, GST_TYPE_RTP_BASE_PAYLOAD);
103 gst_rtp_h264_pay_class_init (GstRtpH264PayClass * klass)
105 GObjectClass *gobject_class;
106 GstElementClass *gstelement_class;
107 GstRTPBasePayloadClass *gstrtpbasepayload_class;
109 gobject_class = (GObjectClass *) klass;
110 gstelement_class = (GstElementClass *) klass;
111 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
113 gobject_class->set_property = gst_rtp_h264_pay_set_property;
114 gobject_class->get_property = gst_rtp_h264_pay_get_property;
116 g_object_class_install_property (G_OBJECT_CLASS (klass),
117 PROP_SPROP_PARAMETER_SETS, g_param_spec_string ("sprop-parameter-sets",
118 "sprop-parameter-sets",
119 "The base64 sprop-parameter-sets to set in out caps (set to NULL to "
120 "extract from stream)",
121 DEFAULT_SPROP_PARAMETER_SETS,
122 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 g_object_class_install_property (G_OBJECT_CLASS (klass),
125 PROP_CONFIG_INTERVAL,
126 g_param_spec_uint ("config-interval",
127 "SPS PPS Send Interval",
128 "Send SPS and PPS Insertion Interval in seconds (sprop parameter sets "
129 "will be multiplexed in the data stream when detected.) (0 = disabled)",
130 0, 3600, DEFAULT_CONFIG_INTERVAL,
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
134 gobject_class->finalize = gst_rtp_h264_pay_finalize;
136 gst_element_class_add_pad_template (gstelement_class,
137 gst_static_pad_template_get (&gst_rtp_h264_pay_src_template));
138 gst_element_class_add_pad_template (gstelement_class,
139 gst_static_pad_template_get (&gst_rtp_h264_pay_sink_template));
141 gst_element_class_set_static_metadata (gstelement_class, "RTP H264 payloader",
142 "Codec/Payloader/Network/RTP",
143 "Payload-encode H264 video into RTP packets (RFC 3984)",
144 "Laurent Glayal <spglegle@yahoo.fr>");
146 gstelement_class->change_state =
147 GST_DEBUG_FUNCPTR (gst_rtp_h264_pay_change_state);
149 gstrtpbasepayload_class->get_caps = gst_rtp_h264_pay_getcaps;
150 gstrtpbasepayload_class->set_caps = gst_rtp_h264_pay_setcaps;
151 gstrtpbasepayload_class->handle_buffer = gst_rtp_h264_pay_handle_buffer;
152 gstrtpbasepayload_class->sink_event = gst_rtp_h264_pay_sink_event;
154 GST_DEBUG_CATEGORY_INIT (rtph264pay_debug, "rtph264pay", 0,
155 "H264 RTP Payloader");
159 gst_rtp_h264_pay_init (GstRtpH264Pay * rtph264pay)
161 rtph264pay->queue = g_array_new (FALSE, FALSE, sizeof (guint));
162 rtph264pay->profile = 0;
163 rtph264pay->sps = g_ptr_array_new_with_free_func (
164 (GDestroyNotify) gst_buffer_unref);
165 rtph264pay->pps = g_ptr_array_new_with_free_func (
166 (GDestroyNotify) gst_buffer_unref);
167 rtph264pay->last_spspps = -1;
168 rtph264pay->spspps_interval = DEFAULT_CONFIG_INTERVAL;
170 rtph264pay->adapter = gst_adapter_new ();
174 gst_rtp_h264_pay_clear_sps_pps (GstRtpH264Pay * rtph264pay)
176 g_ptr_array_set_size (rtph264pay->sps, 0);
177 g_ptr_array_set_size (rtph264pay->pps, 0);
181 gst_rtp_h264_pay_finalize (GObject * object)
183 GstRtpH264Pay *rtph264pay;
185 rtph264pay = GST_RTP_H264_PAY (object);
187 g_array_free (rtph264pay->queue, TRUE);
189 g_ptr_array_free (rtph264pay->sps, TRUE);
190 g_ptr_array_free (rtph264pay->pps, TRUE);
192 g_free (rtph264pay->sprop_parameter_sets);
194 g_object_unref (rtph264pay->adapter);
196 G_OBJECT_CLASS (parent_class)->finalize (object);
199 static const gchar all_levels[][4] = {
219 gst_rtp_h264_pay_getcaps (GstRTPBasePayload * payload, GstPad * pad,
222 GstCaps *template_caps;
223 GstCaps *allowed_caps;
224 GstCaps *caps, *icaps;
225 gboolean append_unrestricted;
229 gst_pad_peer_query_caps (GST_RTP_BASE_PAYLOAD_SRCPAD (payload), NULL);
231 if (allowed_caps == NULL)
235 gst_static_pad_template_get_caps (&gst_rtp_h264_pay_sink_template);
237 if (gst_caps_is_any (allowed_caps)) {
238 caps = gst_caps_ref (template_caps);
242 if (gst_caps_is_empty (allowed_caps)) {
243 caps = gst_caps_ref (allowed_caps);
247 caps = gst_caps_new_empty ();
249 append_unrestricted = FALSE;
250 for (i = 0; i < gst_caps_get_size (allowed_caps); i++) {
251 GstStructure *s = gst_caps_get_structure (allowed_caps, i);
252 GstStructure *new_s = gst_structure_new_empty ("video/x-h264");
253 const gchar *profile_level_id;
255 profile_level_id = gst_structure_get_string (s, "profile-level-id");
257 if (profile_level_id && strlen (profile_level_id) == 6) {
258 const gchar *profile;
263 spsint = strtol (profile_level_id, NULL, 16);
264 sps[0] = spsint >> 16;
265 sps[1] = spsint >> 8;
268 profile = gst_codec_utils_h264_get_profile (sps, 3);
269 level = gst_codec_utils_h264_get_level (sps, 3);
271 if (profile && level) {
272 GST_LOG_OBJECT (payload, "In caps, have profile %s and level %s",
275 if (!strcmp (profile, "constrained-baseline"))
276 gst_structure_set (new_s, "profile", G_TYPE_STRING, profile, NULL);
279 GValue profiles = { 0, };
281 g_value_init (&profiles, GST_TYPE_LIST);
282 g_value_init (&val, G_TYPE_STRING);
284 g_value_set_static_string (&val, profile);
285 gst_value_list_append_value (&profiles, &val);
287 g_value_set_static_string (&val, "constrained-baseline");
288 gst_value_list_append_value (&profiles, &val);
290 gst_structure_take_value (new_s, "profile", &profiles);
293 if (!strcmp (level, "1"))
294 gst_structure_set (new_s, "level", G_TYPE_STRING, level, NULL);
296 GValue levels = { 0, };
300 g_value_init (&levels, GST_TYPE_LIST);
301 g_value_init (&val, G_TYPE_STRING);
303 for (j = 0; j < G_N_ELEMENTS (all_levels); j++) {
304 g_value_set_static_string (&val, all_levels[j]);
305 gst_value_list_prepend_value (&levels, &val);
306 if (!strcmp (level, all_levels[j]))
309 gst_structure_take_value (new_s, "level", &levels);
312 /* Invalid profile-level-id means baseline */
314 gst_structure_set (new_s,
315 "profile", G_TYPE_STRING, "constrained-baseline", NULL);
318 /* No profile-level-id means baseline or unrestricted */
320 gst_structure_set (new_s,
321 "profile", G_TYPE_STRING, "constrained-baseline", NULL);
322 append_unrestricted = TRUE;
325 caps = gst_caps_merge_structure (caps, new_s);
328 if (append_unrestricted) {
330 gst_caps_merge_structure (caps, gst_structure_new ("video/x-h264", NULL,
334 icaps = gst_caps_intersect (caps, template_caps);
335 gst_caps_unref (caps);
340 gst_caps_unref (template_caps);
341 gst_caps_unref (allowed_caps);
343 GST_LOG_OBJECT (payload, "returning caps %" GST_PTR_FORMAT, caps);
347 /* take the currently configured SPS and PPS lists and set them on the caps as
348 * sprop-parameter-sets */
350 gst_rtp_h264_pay_set_sps_pps (GstRTPBasePayload * basepayload)
352 GstRtpH264Pay *payloader = GST_RTP_H264_PAY (basepayload);
361 sprops = g_string_new ("");
364 /* build the sprop-parameter-sets */
365 for (i = 0; i < payloader->sps->len; i++) {
367 GST_BUFFER_CAST (g_ptr_array_index (payloader->sps, i));
369 gst_buffer_map (sps_buf, &map, GST_MAP_READ);
370 set = g_base64_encode (map.data, map.size);
371 gst_buffer_unmap (sps_buf, &map);
373 g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
377 for (i = 0; i < payloader->pps->len; i++) {
379 GST_BUFFER_CAST (g_ptr_array_index (payloader->pps, i));
381 gst_buffer_map (pps_buf, &map, GST_MAP_READ);
382 set = g_base64_encode (map.data, map.size);
383 gst_buffer_unmap (pps_buf, &map);
385 g_string_append_printf (sprops, "%s%s", count ? "," : "", set);
390 if (G_LIKELY (count)) {
391 /* profile is 24 bit. Force it to respect the limit */
392 profile = g_strdup_printf ("%06x", payloader->profile & 0xffffff);
393 /* combine into output caps */
394 res = gst_rtp_base_payload_set_outcaps (basepayload,
395 "sprop-parameter-sets", G_TYPE_STRING, sprops->str, NULL);
398 res = gst_rtp_base_payload_set_outcaps (basepayload, NULL);
400 g_string_free (sprops, TRUE);
407 gst_rtp_h264_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
409 GstRtpH264Pay *rtph264pay;
416 const gchar *alignment, *stream_format;
418 rtph264pay = GST_RTP_H264_PAY (basepayload);
420 str = gst_caps_get_structure (caps, 0);
422 /* we can only set the output caps when we found the sprops and profile
424 gst_rtp_base_payload_set_options (basepayload, "video", TRUE, "H264", 90000);
426 rtph264pay->alignment = GST_H264_ALIGNMENT_UNKNOWN;
427 alignment = gst_structure_get_string (str, "alignment");
429 if (g_str_equal (alignment, "au"))
430 rtph264pay->alignment = GST_H264_ALIGNMENT_AU;
431 if (g_str_equal (alignment, "nal"))
432 rtph264pay->alignment = GST_H264_ALIGNMENT_NAL;
435 rtph264pay->stream_format = GST_H264_STREAM_FORMAT_UNKNOWN;
436 stream_format = gst_structure_get_string (str, "stream-format");
438 if (g_str_equal (stream_format, "avc"))
439 rtph264pay->stream_format = GST_H264_STREAM_FORMAT_AVC;
440 if (g_str_equal (stream_format, "byte-stream"))
441 rtph264pay->stream_format = GST_H264_STREAM_FORMAT_BYTESTREAM;
444 /* packetized AVC video has a codec_data */
445 if ((value = gst_structure_get_value (str, "codec_data"))) {
446 guint num_sps, num_pps;
449 GST_DEBUG_OBJECT (rtph264pay, "have packetized h264");
451 buffer = gst_value_get_buffer (value);
453 gst_buffer_map (buffer, &map, GST_MAP_READ);
457 /* parse the avcC data */
460 /* parse the version, this must be 1 */
464 /* AVCProfileIndication */
466 /* AVCLevelIndication */
467 rtph264pay->profile = (data[1] << 16) | (data[2] << 8) | data[3];
468 GST_DEBUG_OBJECT (rtph264pay, "profile %06x", rtph264pay->profile);
470 /* 6 bits reserved | 2 bits lengthSizeMinusOne */
471 /* this is the number of bytes in front of the NAL units to mark their
473 rtph264pay->nal_length_size = (data[4] & 0x03) + 1;
474 GST_DEBUG_OBJECT (rtph264pay, "nal length %u", rtph264pay->nal_length_size);
475 /* 3 bits reserved | 5 bits numOfSequenceParameterSets */
476 num_sps = data[5] & 0x1f;
477 GST_DEBUG_OBJECT (rtph264pay, "num SPS %u", num_sps);
482 /* create the sprop-parameter-sets */
483 for (i = 0; i < num_sps; i++) {
489 nal_size = (data[0] << 8) | data[1];
493 GST_LOG_OBJECT (rtph264pay, "SPS %d size %d", i, nal_size);
498 /* make a buffer out of it and add to SPS list */
499 sps_buf = gst_buffer_new_and_alloc (nal_size);
500 gst_buffer_fill (sps_buf, 0, data, nal_size);
501 gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264pay), rtph264pay->sps,
502 rtph264pay->pps, sps_buf);
509 /* 8 bits numOfPictureParameterSets */
514 GST_DEBUG_OBJECT (rtph264pay, "num PPS %u", num_pps);
515 for (i = 0; i < num_pps; i++) {
521 nal_size = (data[0] << 8) | data[1];
525 GST_LOG_OBJECT (rtph264pay, "PPS %d size %d", i, nal_size);
530 /* make a buffer out of it and add to PPS list */
531 pps_buf = gst_buffer_new_and_alloc (nal_size);
532 gst_buffer_fill (pps_buf, 0, data, nal_size);
533 gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264pay), rtph264pay->sps,
534 rtph264pay->pps, pps_buf);
540 /* and update the caps with the collected data */
541 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
542 goto set_sps_pps_failed;
544 gst_buffer_unmap (buffer, &map);
546 GST_DEBUG_OBJECT (rtph264pay, "have bytestream h264");
553 GST_ERROR_OBJECT (rtph264pay, "avcC size %" G_GSIZE_FORMAT " < 7", size);
558 GST_ERROR_OBJECT (rtph264pay, "wrong avcC version");
563 GST_ERROR_OBJECT (rtph264pay, "avcC too small ");
568 GST_ERROR_OBJECT (rtph264pay, "failed to set sps/pps");
573 gst_buffer_unmap (buffer, &map);
579 gst_rtp_h264_pay_parse_sprop_parameter_sets (GstRtpH264Pay * rtph264pay)
587 ps = rtph264pay->sprop_parameter_sets;
591 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
593 params = g_strsplit (ps, ",", 0);
594 len = g_strv_length (params);
596 GST_DEBUG_OBJECT (rtph264pay, "we have %d params", len);
598 for (i = 0; params[i]; i++) {
605 nal_len = strlen (params[i]);
606 buf = gst_buffer_new_and_alloc (nal_len);
608 gst_buffer_map (buf, &map, GST_MAP_WRITE);
610 nal_len = g_base64_decode_step (params[i], nal_len, nalp, &state, &save);
611 gst_buffer_unmap (buf, &map);
612 gst_buffer_resize (buf, 0, nal_len);
615 gst_buffer_unref (buf);
619 gst_rtp_h264_add_sps_pps (GST_ELEMENT (rtph264pay), rtph264pay->sps,
620 rtph264pay->pps, buf);
626 next_start_code (const guint8 * data, guint size)
628 /* Boyer-Moore string matching algorithm, in a degenerative
629 * sense because our search 'alphabet' is binary - 0 & 1 only.
630 * This allow us to simplify the general BM algorithm to a very
632 /* assume 1 is in the 3th byte */
635 while (offset < size) {
636 if (1 == data[offset]) {
637 unsigned int shift = offset;
639 if (0 == data[--shift]) {
640 if (0 == data[--shift]) {
644 /* The jump is always 3 because of the 1 previously matched.
645 * All the 0's must be after this '1' matched at offset */
647 } else if (0 == data[offset]) {
648 /* maybe next byte is 1? */
651 /* can jump 3 bytes forward */
654 /* at each iteration, we rescan in a backward manner until
655 * we match 0.0.1 in reverse order. Since our search string
656 * has only 2 'alpabets' (i.e. 0 & 1), we know that any
657 * mismatch will force us to shift a fixed number of steps */
659 GST_DEBUG ("Cannot find next NAL start code. returning %u", size);
665 gst_rtp_h264_pay_decode_nal (GstRtpH264Pay * payloader,
666 const guint8 * data, guint size, GstClockTime dts, GstClockTime pts)
671 /* default is no update */
674 GST_DEBUG ("NAL payload len=%u", size);
677 type = header & 0x1f;
679 /* We record the timestamp of the last SPS/PPS so
680 * that we can insert them at regular intervals and when needed. */
681 if (SPS_TYPE_ID == type || PPS_TYPE_ID == type) {
684 /* encode the entire SPS NAL in base64 */
685 GST_DEBUG ("Found %s %x %x %x Len=%u", type == SPS_TYPE_ID ? "SPS" : "PPS",
686 (header >> 7), (header >> 5) & 3, type, size);
688 nal = gst_buffer_new_allocate (NULL, size, NULL);
689 gst_buffer_fill (nal, 0, data, size);
691 updated = gst_rtp_h264_add_sps_pps (GST_ELEMENT (payloader),
692 payloader->sps, payloader->pps, nal);
694 /* remember when we last saw SPS */
695 if (updated && pts != -1)
696 payloader->last_spspps = pts;
698 GST_DEBUG ("NAL: %x %x %x Len = %u", (header >> 7),
699 (header >> 5) & 3, type, size);
706 gst_rtp_h264_pay_payload_nal (GstRTPBasePayload * basepayload,
707 GstBuffer * paybuf, GstClockTime dts, GstClockTime pts, gboolean end_of_au);
710 gst_rtp_h264_pay_send_sps_pps (GstRTPBasePayload * basepayload,
711 GstRtpH264Pay * rtph264pay, GstClockTime dts, GstClockTime pts)
713 GstFlowReturn ret = GST_FLOW_OK;
714 gboolean sent_all_sps_pps = TRUE;
717 for (i = 0; i < rtph264pay->sps->len; i++) {
719 GST_BUFFER_CAST (g_ptr_array_index (rtph264pay->sps, i));
721 GST_DEBUG_OBJECT (rtph264pay, "inserting SPS in the stream");
723 ret = gst_rtp_h264_pay_payload_nal (basepayload, gst_buffer_ref (sps_buf),
725 /* Not critical here; but throw a warning */
726 if (ret != GST_FLOW_OK) {
727 sent_all_sps_pps = FALSE;
728 GST_WARNING ("Problem pushing SPS");
731 for (i = 0; i < rtph264pay->pps->len; i++) {
733 GST_BUFFER_CAST (g_ptr_array_index (rtph264pay->pps, i));
735 GST_DEBUG_OBJECT (rtph264pay, "inserting PPS in the stream");
737 ret = gst_rtp_h264_pay_payload_nal (basepayload, gst_buffer_ref (pps_buf),
739 /* Not critical here; but throw a warning */
740 if (ret != GST_FLOW_OK) {
741 sent_all_sps_pps = FALSE;
742 GST_WARNING ("Problem pushing PPS");
746 if (pts != -1 && sent_all_sps_pps)
747 rtph264pay->last_spspps = pts;
753 gst_rtp_h264_pay_payload_nal (GstRTPBasePayload * basepayload,
754 GstBuffer * paybuf, GstClockTime dts, GstClockTime pts, gboolean end_of_au)
756 GstRtpH264Pay *rtph264pay;
760 guint packet_len, payload_len, mtu;
763 GstBufferList *list = NULL;
764 gboolean send_spspps;
765 GstRTPBuffer rtp = { NULL };
766 guint size = gst_buffer_get_size (paybuf);
768 rtph264pay = GST_RTP_H264_PAY (basepayload);
769 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtph264pay);
771 gst_buffer_extract (paybuf, 0, &nalHeader, 1);
772 nalType = nalHeader & 0x1f;
774 GST_DEBUG_OBJECT (rtph264pay, "Processing Buffer with NAL TYPE=%d", nalType);
776 /* should set src caps before pushing stuff,
777 * and if we did not see enough SPS/PPS, that may not be the case */
778 if (G_UNLIKELY (!gst_pad_has_current_caps (GST_RTP_BASE_PAYLOAD_SRCPAD
780 gst_rtp_h264_pay_set_sps_pps (basepayload);
784 /* check if we need to emit an SPS/PPS now */
785 if (nalType == IDR_TYPE_ID && rtph264pay->spspps_interval > 0) {
786 if (rtph264pay->last_spspps != -1) {
789 GST_LOG_OBJECT (rtph264pay,
790 "now %" GST_TIME_FORMAT ", last SPS/PPS %" GST_TIME_FORMAT,
791 GST_TIME_ARGS (pts), GST_TIME_ARGS (rtph264pay->last_spspps));
793 /* calculate diff between last SPS/PPS in milliseconds */
794 if (pts > rtph264pay->last_spspps)
795 diff = pts - rtph264pay->last_spspps;
799 GST_DEBUG_OBJECT (rtph264pay,
800 "interval since last SPS/PPS %" GST_TIME_FORMAT,
801 GST_TIME_ARGS (diff));
803 /* bigger than interval, queue SPS/PPS */
804 if (GST_TIME_AS_SECONDS (diff) >= rtph264pay->spspps_interval) {
805 GST_DEBUG_OBJECT (rtph264pay, "time to send SPS/PPS");
809 /* no know previous SPS/PPS time, send now */
810 GST_DEBUG_OBJECT (rtph264pay, "no previous SPS/PPS time, send now");
815 if (send_spspps || rtph264pay->send_spspps) {
816 /* we need to send SPS/PPS now first. FIXME, don't use the pts for
817 * checking when we need to send SPS/PPS but convert to running_time first. */
818 rtph264pay->send_spspps = FALSE;
819 ret = gst_rtp_h264_pay_send_sps_pps (basepayload, rtph264pay, dts, pts);
820 if (ret != GST_FLOW_OK)
824 packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
826 if (packet_len < mtu) {
827 /* will fit in one packet */
828 GST_DEBUG_OBJECT (basepayload,
829 "NAL Unit fit in one packet datasize=%d mtu=%d", size, mtu);
831 /* create buffer without payload containing only the RTP header
832 * (memory block at index 0) */
833 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
835 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
837 /* only set the marker bit on packets containing access units */
838 if (IS_ACCESS_UNIT (nalType) && end_of_au) {
839 gst_rtp_buffer_set_marker (&rtp, 1);
842 /* timestamp the outbuffer */
843 GST_BUFFER_PTS (outbuf) = pts;
844 GST_BUFFER_DTS (outbuf) = dts;
846 gst_rtp_buffer_unmap (&rtp);
848 /* insert payload memory block */
849 outbuf = gst_buffer_append (outbuf, paybuf);
851 /* push the buffer to the next element */
852 ret = gst_rtp_base_payload_push (basepayload, outbuf);
854 /* fragmentation Units FU-A */
856 int ii = 0, start = 1, end = 0, pos = 0;
858 GST_DEBUG_OBJECT (basepayload,
859 "NAL Unit DOES NOT fit in one packet datasize=%d mtu=%d", size, mtu);
866 GST_DEBUG_OBJECT (basepayload, "Using FU-A fragmentation for data size=%d",
869 /* We keep 2 bytes for FU indicator and FU Header */
870 payload_len = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
872 list = gst_buffer_list_new_sized ((size / payload_len) + 1);
875 limitedSize = size < payload_len ? size : payload_len;
876 GST_DEBUG_OBJECT (basepayload,
877 "Inside FU-A fragmentation limitedSize=%d iteration=%d", limitedSize,
881 * create buffer without payload containing only the RTP header
882 * (memory block at index 0) */
883 outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
885 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
887 GST_BUFFER_DTS (outbuf) = dts;
888 GST_BUFFER_PTS (outbuf) = pts;
889 payload = gst_rtp_buffer_get_payload (&rtp);
891 if (limitedSize == size) {
892 GST_DEBUG_OBJECT (basepayload, "end size=%d iteration=%d", size, ii);
895 if (IS_ACCESS_UNIT (nalType)) {
896 gst_rtp_buffer_set_marker (&rtp, end && end_of_au);
900 payload[0] = (nalHeader & 0x60) | 28;
903 payload[1] = (start << 7) | (end << 6) | (nalHeader & 0x1f);
905 gst_rtp_buffer_unmap (&rtp);
907 /* insert payload memory block */
908 gst_buffer_append (outbuf,
909 gst_buffer_copy_region (paybuf, GST_BUFFER_COPY_MEMORY, pos,
912 /* add the buffer to the buffer list */
913 gst_buffer_list_add (list, outbuf);
922 ret = gst_rtp_base_payload_push_list (basepayload, list);
923 gst_buffer_unref (paybuf);
929 gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload,
932 GstRtpH264Pay *rtph264pay;
938 GstClockTime dts, pts;
941 GstBuffer *paybuf = NULL;
944 rtph264pay = GST_RTP_H264_PAY (basepayload);
946 /* the input buffer contains one or more NAL units */
948 avc = rtph264pay->stream_format == GST_H264_STREAM_FORMAT_AVC;
951 /* In AVC mode, there is no adapter, so nothign to flush */
954 gst_buffer_map (buffer, &map, GST_MAP_READ);
957 pts = GST_BUFFER_PTS (buffer);
958 dts = GST_BUFFER_DTS (buffer);
959 GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", size);
961 dts = gst_adapter_prev_dts (rtph264pay->adapter, NULL);
962 pts = gst_adapter_prev_pts (rtph264pay->adapter, NULL);
964 if (!GST_CLOCK_TIME_IS_VALID (dts))
965 dts = GST_BUFFER_DTS (buffer);
966 if (!GST_CLOCK_TIME_IS_VALID (pts))
967 pts = GST_BUFFER_PTS (buffer);
969 gst_adapter_push (rtph264pay->adapter, buffer);
971 size = gst_adapter_available (rtph264pay->adapter);
972 /* Nothing to do here if the adapter is empty, e.g. on EOS */
975 data = gst_adapter_map (rtph264pay->adapter, size);
976 GST_DEBUG_OBJECT (basepayload,
977 "got %" G_GSIZE_FORMAT " bytes (%" G_GSIZE_FORMAT ")", size,
978 buffer ? gst_buffer_get_size (buffer) : 0);
983 /* now loop over all NAL units and put them in a packet
984 * FIXME, we should really try to pack multiple NAL units into one RTP packet
985 * if we can, especially for the config packets that wont't cause decoder
988 guint nal_length_size;
991 nal_length_size = rtph264pay->nal_length_size;
993 while (size > nal_length_size) {
995 gboolean end_of_au = FALSE;
998 for (i = 0; i < nal_length_size; i++) {
999 nal_len = ((nal_len << 8) + data[i]);
1002 /* skip the length bytes, make sure we don't run past the buffer size */
1003 data += nal_length_size;
1004 offset += nal_length_size;
1005 size -= nal_length_size;
1007 if (size >= nal_len) {
1008 GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len);
1011 GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u",
1015 /* If we're at the end of the buffer, then we're at the end of the
1018 if (rtph264pay->alignment == GST_H264_ALIGNMENT_AU
1019 && size - nal_len <= nal_length_size) {
1023 paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, offset,
1026 gst_rtp_h264_pay_payload_nal (basepayload, paybuf, dts, pts,
1028 if (ret != GST_FLOW_OK)
1037 gboolean update = FALSE;
1039 /* get offset of first start code */
1040 next = next_start_code (data, size);
1042 /* skip to start code, if no start code is found, next will be size and we
1043 * will not collect data. */
1046 nal_queue = rtph264pay->queue;
1049 /* array must be empty when we get here */
1050 g_assert (nal_queue->len == 0);
1052 GST_DEBUG_OBJECT (basepayload,
1053 "found first start at %u, bytes left %" G_GSIZE_FORMAT, next, size);
1055 /* first pass to locate NALs and parse SPS/PPS */
1057 /* skip start code */
1061 /* use next_start_code() to scan buffer.
1062 * next_start_code() returns the offset in data,
1063 * starting from zero to the first byte of 0.0.0.1
1064 * If no start code is found, it returns the value of the
1066 * data is unchanged by the call to next_start_code()
1068 next = next_start_code (data, size);
1070 if (next == size && buffer != NULL) {
1071 /* Didn't find the start of next NAL and it's not EOS,
1072 * handle it next time */
1076 /* nal length is distance to next start code */
1079 GST_DEBUG_OBJECT (basepayload, "found next start at %u of size %u", next,
1082 if (rtph264pay->sprop_parameter_sets != NULL) {
1083 /* explicitly set profile and sprop, use those */
1084 if (rtph264pay->update_caps) {
1085 if (!gst_rtp_base_payload_set_outcaps (basepayload,
1086 "sprop-parameter-sets", G_TYPE_STRING,
1087 rtph264pay->sprop_parameter_sets, NULL))
1090 /* parse SPS and PPS from provided parameter set (for insertion) */
1091 gst_rtp_h264_pay_parse_sprop_parameter_sets (rtph264pay);
1093 rtph264pay->update_caps = FALSE;
1095 GST_DEBUG ("outcaps update: sprop-parameter-sets=%s",
1096 rtph264pay->sprop_parameter_sets);
1099 /* We know our stream is a valid H264 NAL packet,
1100 * go parse it for SPS/PPS to enrich the caps */
1101 /* order: make sure to check nal */
1103 gst_rtp_h264_pay_decode_nal (rtph264pay, data, nal_len, dts, pts)
1106 /* move to next NAL packet */
1110 g_array_append_val (nal_queue, nal_len);
1113 /* if has new SPS & PPS, update the output caps */
1114 if (G_UNLIKELY (update))
1115 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
1118 /* second pass to payload and push */
1120 if (nal_queue->len != 0)
1121 gst_adapter_flush (rtph264pay->adapter, skip);
1123 for (i = 0; i < nal_queue->len; i++) {
1125 gboolean end_of_au = FALSE;
1127 nal_len = g_array_index (nal_queue, guint, i);
1128 /* skip start code */
1129 gst_adapter_flush (rtph264pay->adapter, 3);
1131 /* Trim the end unless we're the last NAL in the stream.
1132 * In case we're not at the end of the buffer we know the next block
1133 * starts with 0x000001 so all the 0x00 bytes at the end of this one are
1134 * trailing 0x0 that can be discarded */
1136 data = gst_adapter_map (rtph264pay->adapter, size);
1137 if (i + 1 != nal_queue->len || buffer != NULL)
1138 for (; size > 1 && data[size - 1] == 0x0; size--)
1142 /* If it's the last nal unit we have in non-bytestream mode, we can
1143 * assume it's the end of an access-unit
1145 * FIXME: We need to wait until the next packet or EOS to
1146 * actually payload the NAL so we can know if the current NAL is
1147 * the last one of an access unit or not if we are in bytestream mode
1149 if ((rtph264pay->alignment == GST_H264_ALIGNMENT_AU || buffer == NULL) &&
1150 i == nal_queue->len - 1)
1152 paybuf = gst_adapter_take_buffer (rtph264pay->adapter, size);
1155 /* put the data in one or more RTP packets */
1157 gst_rtp_h264_pay_payload_nal (basepayload, paybuf, dts, pts,
1159 if (ret != GST_FLOW_OK) {
1163 /* move to next NAL packet */
1164 /* Skips the trailing zeros */
1165 gst_adapter_flush (rtph264pay->adapter, nal_len - size);
1167 g_array_set_size (nal_queue, 0);
1172 gst_buffer_unmap (buffer, &map);
1173 gst_buffer_unref (buffer);
1175 gst_adapter_unmap (rtph264pay->adapter);
1182 GST_WARNING_OBJECT (basepayload, "Could not set outcaps");
1183 g_array_set_size (nal_queue, 0);
1184 ret = GST_FLOW_NOT_NEGOTIATED;
1190 gst_rtp_h264_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
1193 const GstStructure *s;
1194 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (payload);
1196 switch (GST_EVENT_TYPE (event)) {
1197 case GST_EVENT_FLUSH_STOP:
1198 gst_adapter_clear (rtph264pay->adapter);
1200 case GST_EVENT_CUSTOM_DOWNSTREAM:
1201 s = gst_event_get_structure (event);
1202 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1203 gboolean resend_codec_data;
1205 if (gst_structure_get_boolean (s, "all-headers",
1206 &resend_codec_data) && resend_codec_data)
1207 rtph264pay->send_spspps = TRUE;
1212 /* call handle_buffer with NULL to flush last NAL from adapter
1213 * in byte-stream mode
1215 gst_rtp_h264_pay_handle_buffer (payload, NULL);
1218 case GST_EVENT_STREAM_START:
1219 GST_DEBUG_OBJECT (rtph264pay, "New stream detected => Clear SPS and PPS");
1220 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
1226 res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
1231 static GstStateChangeReturn
1232 gst_rtp_h264_pay_change_state (GstElement * element, GstStateChange transition)
1234 GstStateChangeReturn ret;
1235 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (element);
1237 switch (transition) {
1238 case GST_STATE_CHANGE_READY_TO_PAUSED:
1239 rtph264pay->send_spspps = FALSE;
1240 gst_adapter_clear (rtph264pay->adapter);
1242 case GST_STATE_CHANGE_PAUSED_TO_READY:
1243 rtph264pay->last_spspps = -1;
1244 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
1250 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1256 gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
1257 const GValue * value, GParamSpec * pspec)
1259 GstRtpH264Pay *rtph264pay;
1261 rtph264pay = GST_RTP_H264_PAY (object);
1264 case PROP_SPROP_PARAMETER_SETS:
1265 g_free (rtph264pay->sprop_parameter_sets);
1266 rtph264pay->sprop_parameter_sets = g_value_dup_string (value);
1267 rtph264pay->update_caps = TRUE;
1269 case PROP_CONFIG_INTERVAL:
1270 rtph264pay->spspps_interval = g_value_get_uint (value);
1273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1279 gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
1280 GValue * value, GParamSpec * pspec)
1282 GstRtpH264Pay *rtph264pay;
1284 rtph264pay = GST_RTP_H264_PAY (object);
1287 case PROP_SPROP_PARAMETER_SETS:
1288 g_value_set_string (value, rtph264pay->sprop_parameter_sets);
1290 case PROP_CONFIG_INTERVAL:
1291 g_value_set_uint (value, rtph264pay->spspps_interval);
1294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1300 gst_rtp_h264_pay_plugin_init (GstPlugin * plugin)
1302 return gst_element_register (plugin, "rtph264pay",
1303 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_PAY);