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;
716 for (i = 0; i < rtph264pay->sps->len; i++) {
718 GST_BUFFER_CAST (g_ptr_array_index (rtph264pay->sps, i));
720 GST_DEBUG_OBJECT (rtph264pay, "inserting SPS in the stream");
722 ret = gst_rtp_h264_pay_payload_nal (basepayload, gst_buffer_ref (sps_buf),
724 /* Not critical here; but throw a warning */
725 if (ret != GST_FLOW_OK)
726 GST_WARNING ("Problem pushing SPS");
728 for (i = 0; i < rtph264pay->pps->len; i++) {
730 GST_BUFFER_CAST (g_ptr_array_index (rtph264pay->pps, i));
732 GST_DEBUG_OBJECT (rtph264pay, "inserting PPS in the stream");
734 ret = gst_rtp_h264_pay_payload_nal (basepayload, gst_buffer_ref (pps_buf),
736 /* Not critical here; but throw a warning */
737 if (ret != GST_FLOW_OK)
738 GST_WARNING ("Problem pushing PPS");
742 rtph264pay->last_spspps = pts;
748 gst_rtp_h264_pay_payload_nal (GstRTPBasePayload * basepayload,
749 GstBuffer * paybuf, GstClockTime dts, GstClockTime pts, gboolean end_of_au)
751 GstRtpH264Pay *rtph264pay;
755 guint packet_len, payload_len, mtu;
758 GstBufferList *list = NULL;
759 gboolean send_spspps;
760 GstRTPBuffer rtp = { NULL };
761 guint size = gst_buffer_get_size (paybuf);
763 rtph264pay = GST_RTP_H264_PAY (basepayload);
764 mtu = GST_RTP_BASE_PAYLOAD_MTU (rtph264pay);
766 gst_buffer_extract (paybuf, 0, &nalHeader, 1);
767 nalType = nalHeader & 0x1f;
769 GST_DEBUG_OBJECT (rtph264pay, "Processing Buffer with NAL TYPE=%d", nalType);
771 /* should set src caps before pushing stuff,
772 * and if we did not see enough SPS/PPS, that may not be the case */
773 if (G_UNLIKELY (!gst_pad_has_current_caps (GST_RTP_BASE_PAYLOAD_SRCPAD
775 gst_rtp_h264_pay_set_sps_pps (basepayload);
779 /* check if we need to emit an SPS/PPS now */
780 if (nalType == IDR_TYPE_ID && rtph264pay->spspps_interval > 0) {
781 if (rtph264pay->last_spspps != -1) {
784 GST_LOG_OBJECT (rtph264pay,
785 "now %" GST_TIME_FORMAT ", last SPS/PPS %" GST_TIME_FORMAT,
786 GST_TIME_ARGS (pts), GST_TIME_ARGS (rtph264pay->last_spspps));
788 /* calculate diff between last SPS/PPS in milliseconds */
789 if (pts > rtph264pay->last_spspps)
790 diff = pts - rtph264pay->last_spspps;
794 GST_DEBUG_OBJECT (rtph264pay,
795 "interval since last SPS/PPS %" GST_TIME_FORMAT,
796 GST_TIME_ARGS (diff));
798 /* bigger than interval, queue SPS/PPS */
799 if (GST_TIME_AS_SECONDS (diff) >= rtph264pay->spspps_interval) {
800 GST_DEBUG_OBJECT (rtph264pay, "time to send SPS/PPS");
804 /* no know previous SPS/PPS time, send now */
805 GST_DEBUG_OBJECT (rtph264pay, "no previous SPS/PPS time, send now");
810 if (send_spspps || rtph264pay->send_spspps) {
811 /* we need to send SPS/PPS now first. FIXME, don't use the pts for
812 * checking when we need to send SPS/PPS but convert to running_time first. */
813 rtph264pay->send_spspps = FALSE;
814 ret = gst_rtp_h264_pay_send_sps_pps (basepayload, rtph264pay, dts, pts);
815 if (ret != GST_FLOW_OK)
819 packet_len = gst_rtp_buffer_calc_packet_len (size, 0, 0);
821 if (packet_len < mtu) {
822 GST_DEBUG_OBJECT (basepayload,
823 "NAL Unit fit in one packet datasize=%d mtu=%d", size, mtu);
824 /* will fit in one packet */
827 * create buffer without payload containing only the RTP header
828 * (memory block at index 0) */
829 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
831 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
833 /* only set the marker bit on packets containing access units */
834 if (IS_ACCESS_UNIT (nalType) && end_of_au) {
835 gst_rtp_buffer_set_marker (&rtp, 1);
838 /* timestamp the outbuffer */
839 GST_BUFFER_PTS (outbuf) = pts;
840 GST_BUFFER_DTS (outbuf) = dts;
842 /* insert payload memory block */
843 outbuf = gst_buffer_append (outbuf, paybuf);
845 list = gst_buffer_list_new ();
847 /* add the buffer to the buffer list */
848 gst_buffer_list_add (list, outbuf);
850 gst_rtp_buffer_unmap (&rtp);
852 /* push the list to the next element in the pipe */
853 ret = gst_rtp_base_payload_push_list (basepayload, list);
855 /* fragmentation Units FU-A */
857 int ii = 0, start = 1, end = 0, pos = 0;
859 GST_DEBUG_OBJECT (basepayload,
860 "NAL Unit DOES NOT fit in one packet datasize=%d mtu=%d", size, mtu);
867 GST_DEBUG_OBJECT (basepayload, "Using FU-A fragmentation for data size=%d",
870 /* We keep 2 bytes for FU indicator and FU Header */
871 payload_len = gst_rtp_buffer_calc_payload_len (mtu - 2, 0, 0);
873 list = gst_buffer_list_new ();
876 limitedSize = size < payload_len ? size : payload_len;
877 GST_DEBUG_OBJECT (basepayload,
878 "Inside FU-A fragmentation limitedSize=%d iteration=%d", limitedSize,
882 * create buffer without payload containing only the RTP header
883 * (memory block at index 0) */
884 outbuf = gst_rtp_buffer_new_allocate (2, 0, 0);
886 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
888 GST_BUFFER_DTS (outbuf) = dts;
889 GST_BUFFER_PTS (outbuf) = pts;
890 payload = gst_rtp_buffer_get_payload (&rtp);
892 if (limitedSize == size) {
893 GST_DEBUG_OBJECT (basepayload, "end size=%d iteration=%d", size, ii);
896 if (IS_ACCESS_UNIT (nalType)) {
897 gst_rtp_buffer_set_marker (&rtp, end && end_of_au);
901 payload[0] = (nalHeader & 0x60) | 28;
904 payload[1] = (start << 7) | (end << 6) | (nalHeader & 0x1f);
906 gst_rtp_buffer_unmap (&rtp);
908 /* insert payload memory block */
909 gst_buffer_append (outbuf,
910 gst_buffer_copy_region (paybuf, GST_BUFFER_COPY_MEMORY, pos,
913 /* add the buffer to the buffer list */
914 gst_buffer_list_add (list, outbuf);
923 ret = gst_rtp_base_payload_push_list (basepayload, list);
924 gst_buffer_unref (paybuf);
930 gst_rtp_h264_pay_handle_buffer (GstRTPBasePayload * basepayload,
933 GstRtpH264Pay *rtph264pay;
939 GstClockTime dts, pts;
942 GstBuffer *paybuf = NULL;
945 rtph264pay = GST_RTP_H264_PAY (basepayload);
947 /* the input buffer contains one or more NAL units */
949 avc = rtph264pay->stream_format == GST_H264_STREAM_FORMAT_AVC;
952 /* In AVC mode, there is no adapter, so nothign to flush */
955 gst_buffer_map (buffer, &map, GST_MAP_READ);
958 pts = GST_BUFFER_PTS (buffer);
959 dts = GST_BUFFER_DTS (buffer);
960 GST_DEBUG_OBJECT (basepayload, "got %" G_GSIZE_FORMAT " bytes", size);
962 dts = gst_adapter_prev_dts (rtph264pay->adapter, NULL);
963 pts = gst_adapter_prev_pts (rtph264pay->adapter, NULL);
965 if (!GST_CLOCK_TIME_IS_VALID (dts))
966 dts = GST_BUFFER_DTS (buffer);
967 if (!GST_CLOCK_TIME_IS_VALID (pts))
968 pts = GST_BUFFER_PTS (buffer);
970 gst_adapter_push (rtph264pay->adapter, buffer);
972 size = gst_adapter_available (rtph264pay->adapter);
973 /* Nothing to do here if the adapter is empty, e.g. on EOS */
976 data = gst_adapter_map (rtph264pay->adapter, size);
977 GST_DEBUG_OBJECT (basepayload,
978 "got %" G_GSIZE_FORMAT " bytes (%" G_GSIZE_FORMAT ")", size,
979 buffer ? gst_buffer_get_size (buffer) : 0);
984 /* now loop over all NAL units and put them in a packet
985 * FIXME, we should really try to pack multiple NAL units into one RTP packet
986 * if we can, especially for the config packets that wont't cause decoder
989 guint nal_length_size;
992 nal_length_size = rtph264pay->nal_length_size;
994 while (size > nal_length_size) {
996 gboolean end_of_au = FALSE;
999 for (i = 0; i < nal_length_size; i++) {
1000 nal_len = ((nal_len << 8) + data[i]);
1003 /* skip the length bytes, make sure we don't run past the buffer size */
1004 data += nal_length_size;
1005 offset += nal_length_size;
1006 size -= nal_length_size;
1008 if (size >= nal_len) {
1009 GST_DEBUG_OBJECT (basepayload, "got NAL of size %u", nal_len);
1012 GST_DEBUG_OBJECT (basepayload, "got incomplete NAL of size %u",
1016 /* If we're at the end of the buffer, then we're at the end of the
1019 if (rtph264pay->alignment == GST_H264_ALIGNMENT_AU
1020 && size - nal_len <= nal_length_size) {
1024 paybuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, offset,
1027 gst_rtp_h264_pay_payload_nal (basepayload, paybuf, dts, pts,
1029 if (ret != GST_FLOW_OK)
1038 gboolean update = FALSE;
1040 /* get offset of first start code */
1041 next = next_start_code (data, size);
1043 /* skip to start code, if no start code is found, next will be size and we
1044 * will not collect data. */
1047 nal_queue = rtph264pay->queue;
1050 /* array must be empty when we get here */
1051 g_assert (nal_queue->len == 0);
1053 GST_DEBUG_OBJECT (basepayload,
1054 "found first start at %u, bytes left %" G_GSIZE_FORMAT, next, size);
1056 /* first pass to locate NALs and parse SPS/PPS */
1058 /* skip start code */
1062 /* use next_start_code() to scan buffer.
1063 * next_start_code() returns the offset in data,
1064 * starting from zero to the first byte of 0.0.0.1
1065 * If no start code is found, it returns the value of the
1067 * data is unchanged by the call to next_start_code()
1069 next = next_start_code (data, size);
1071 if (next == size && buffer != NULL) {
1072 /* Didn't find the start of next NAL and it's not EOS,
1073 * handle it next time */
1077 /* nal length is distance to next start code */
1080 GST_DEBUG_OBJECT (basepayload, "found next start at %u of size %u", next,
1083 if (rtph264pay->sprop_parameter_sets != NULL) {
1084 /* explicitly set profile and sprop, use those */
1085 if (rtph264pay->update_caps) {
1086 if (!gst_rtp_base_payload_set_outcaps (basepayload,
1087 "sprop-parameter-sets", G_TYPE_STRING,
1088 rtph264pay->sprop_parameter_sets, NULL))
1091 /* parse SPS and PPS from provided parameter set (for insertion) */
1092 gst_rtp_h264_pay_parse_sprop_parameter_sets (rtph264pay);
1094 rtph264pay->update_caps = FALSE;
1096 GST_DEBUG ("outcaps update: sprop-parameter-sets=%s",
1097 rtph264pay->sprop_parameter_sets);
1100 /* We know our stream is a valid H264 NAL packet,
1101 * go parse it for SPS/PPS to enrich the caps */
1102 /* order: make sure to check nal */
1104 gst_rtp_h264_pay_decode_nal (rtph264pay, data, nal_len, dts, pts)
1107 /* move to next NAL packet */
1111 g_array_append_val (nal_queue, nal_len);
1114 /* if has new SPS & PPS, update the output caps */
1115 if (G_UNLIKELY (update))
1116 if (!gst_rtp_h264_pay_set_sps_pps (basepayload))
1119 /* second pass to payload and push */
1121 if (nal_queue->len != 0)
1122 gst_adapter_flush (rtph264pay->adapter, skip);
1124 for (i = 0; i < nal_queue->len; i++) {
1126 gboolean end_of_au = FALSE;
1128 nal_len = g_array_index (nal_queue, guint, i);
1129 /* skip start code */
1130 gst_adapter_flush (rtph264pay->adapter, 3);
1132 /* Trim the end unless we're the last NAL in the stream.
1133 * In case we're not at the end of the buffer we know the next block
1134 * starts with 0x000001 so all the 0x00 bytes at the end of this one are
1135 * trailing 0x0 that can be discarded */
1137 data = gst_adapter_map (rtph264pay->adapter, size);
1138 if (i + 1 != nal_queue->len || buffer != NULL)
1139 for (; size > 1 && data[size - 1] == 0x0; size--)
1143 /* If it's the last nal unit we have in non-bytestream mode, we can
1144 * assume it's the end of an access-unit
1146 * FIXME: We need to wait until the next packet or EOS to
1147 * actually payload the NAL so we can know if the current NAL is
1148 * the last one of an access unit or not if we are in bytestream mode
1150 if ((rtph264pay->alignment == GST_H264_ALIGNMENT_AU || buffer == NULL) &&
1151 i == nal_queue->len - 1)
1153 paybuf = gst_adapter_take_buffer (rtph264pay->adapter, size);
1156 /* put the data in one or more RTP packets */
1158 gst_rtp_h264_pay_payload_nal (basepayload, paybuf, dts, pts,
1160 if (ret != GST_FLOW_OK) {
1164 /* move to next NAL packet */
1165 /* Skips the trailing zeros */
1166 gst_adapter_flush (rtph264pay->adapter, nal_len - size);
1168 g_array_set_size (nal_queue, 0);
1173 gst_buffer_unmap (buffer, &map);
1174 gst_buffer_unref (buffer);
1176 gst_adapter_unmap (rtph264pay->adapter);
1183 GST_WARNING_OBJECT (basepayload, "Could not set outcaps");
1184 g_array_set_size (nal_queue, 0);
1185 ret = GST_FLOW_NOT_NEGOTIATED;
1191 gst_rtp_h264_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
1194 const GstStructure *s;
1195 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (payload);
1197 switch (GST_EVENT_TYPE (event)) {
1198 case GST_EVENT_FLUSH_STOP:
1199 gst_adapter_clear (rtph264pay->adapter);
1201 case GST_EVENT_CUSTOM_DOWNSTREAM:
1202 s = gst_event_get_structure (event);
1203 if (gst_structure_has_name (s, "GstForceKeyUnit")) {
1204 gboolean resend_codec_data;
1206 if (gst_structure_get_boolean (s, "all-headers",
1207 &resend_codec_data) && resend_codec_data)
1208 rtph264pay->send_spspps = TRUE;
1213 /* call handle_buffer with NULL to flush last NAL from adapter
1214 * in byte-stream mode
1216 gst_rtp_h264_pay_handle_buffer (payload, NULL);
1219 case GST_EVENT_STREAM_START:
1220 GST_DEBUG_OBJECT (rtph264pay, "New stream detected => Clear SPS and PPS");
1221 gst_rtp_h264_pay_clear_sps_pps (rtph264pay);
1227 res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
1232 static GstStateChangeReturn
1233 gst_rtp_h264_pay_change_state (GstElement * element, GstStateChange transition)
1235 GstStateChangeReturn ret;
1236 GstRtpH264Pay *rtph264pay = GST_RTP_H264_PAY (element);
1238 switch (transition) {
1239 case GST_STATE_CHANGE_READY_TO_PAUSED:
1240 rtph264pay->send_spspps = FALSE;
1241 gst_adapter_clear (rtph264pay->adapter);
1247 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1253 gst_rtp_h264_pay_set_property (GObject * object, guint prop_id,
1254 const GValue * value, GParamSpec * pspec)
1256 GstRtpH264Pay *rtph264pay;
1258 rtph264pay = GST_RTP_H264_PAY (object);
1261 case PROP_SPROP_PARAMETER_SETS:
1262 g_free (rtph264pay->sprop_parameter_sets);
1263 rtph264pay->sprop_parameter_sets = g_value_dup_string (value);
1264 rtph264pay->update_caps = TRUE;
1266 case PROP_CONFIG_INTERVAL:
1267 rtph264pay->spspps_interval = g_value_get_uint (value);
1270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1276 gst_rtp_h264_pay_get_property (GObject * object, guint prop_id,
1277 GValue * value, GParamSpec * pspec)
1279 GstRtpH264Pay *rtph264pay;
1281 rtph264pay = GST_RTP_H264_PAY (object);
1284 case PROP_SPROP_PARAMETER_SETS:
1285 g_value_set_string (value, rtph264pay->sprop_parameter_sets);
1287 case PROP_CONFIG_INTERVAL:
1288 g_value_set_uint (value, rtph264pay->spspps_interval);
1291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1297 gst_rtp_h264_pay_plugin_init (GstPlugin * plugin)
1299 return gst_element_register (plugin, "rtph264pay",
1300 GST_RANK_SECONDARY, GST_TYPE_RTP_H264_PAY);