rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vpay.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include <gst/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpmp4vpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmp4vpay_debug);
31 #define GST_CAT_DEFAULT (rtpmp4vpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
34     GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("video/mpeg,"
38         "mpegversion=(int) 4, systemstream=(boolean)false;" "video/x-divx")
39     );
40
41 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
42 GST_STATIC_PAD_TEMPLATE ("src",
43     GST_PAD_SRC,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"video\", "
47         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
48         "clock-rate = (int) [1, MAX ], " "encoding-name = (string) \"MP4V-ES\""
49         /* two string params
50          *
51          "profile-level-id = (string) [1,MAX]"
52          "config = (string) [1,MAX]"
53          */
54     )
55     );
56
57 #define DEFAULT_CONFIG_INTERVAL 0
58
59 enum
60 {
61   ARG_0,
62   ARG_CONFIG_INTERVAL
63 };
64
65
66 static void gst_rtp_mp4v_pay_finalize (GObject * object);
67
68 static void gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
69     const GValue * value, GParamSpec * pspec);
70 static void gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
71     GValue * value, GParamSpec * pspec);
72
73 static gboolean gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload,
74     GstCaps * caps);
75 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload *
76     payload, GstBuffer * buffer);
77 static gboolean gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay,
78     GstEvent * event);
79
80 #define gst_rtp_mp4v_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
82
83      static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
84 {
85   GObjectClass *gobject_class;
86   GstElementClass *gstelement_class;
87   GstRTPBasePayloadClass *gstrtpbasepayload_class;
88
89   gobject_class = (GObjectClass *) klass;
90   gstelement_class = (GstElementClass *) klass;
91   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
92
93   gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
94   gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
95
96   gst_element_class_add_pad_template (gstelement_class,
97       gst_static_pad_template_get (&gst_rtp_mp4v_pay_src_template));
98   gst_element_class_add_pad_template (gstelement_class,
99       gst_static_pad_template_get (&gst_rtp_mp4v_pay_sink_template));
100
101   gst_element_class_set_static_metadata (gstelement_class,
102       "RTP MPEG4 Video payloader", "Codec/Payloader/Network/RTP",
103       "Payload MPEG-4 video as RTP packets (RFC 3016)",
104       "Wim Taymans <wim.taymans@gmail.com>");
105
106   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CONFIG_INTERVAL,
107       g_param_spec_uint ("config-interval", "Config Send Interval",
108           "Send Config Insertion Interval in seconds (configuration headers "
109           "will be multiplexed in the data stream when detected.) (0 = disabled)",
110           0, 3600, DEFAULT_CONFIG_INTERVAL,
111           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
112       );
113
114   gobject_class->finalize = gst_rtp_mp4v_pay_finalize;
115
116   gstrtpbasepayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
117   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
118   gstrtpbasepayload_class->sink_event = gst_rtp_mp4v_pay_sink_event;
119
120   GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
121       "MP4 video RTP Payloader");
122 }
123
124 static void
125 gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
126 {
127   rtpmp4vpay->adapter = gst_adapter_new ();
128   rtpmp4vpay->rate = 90000;
129   rtpmp4vpay->profile = 1;
130   rtpmp4vpay->need_config = TRUE;
131   rtpmp4vpay->config_interval = DEFAULT_CONFIG_INTERVAL;
132   rtpmp4vpay->last_config = -1;
133
134   rtpmp4vpay->config = NULL;
135 }
136
137 static void
138 gst_rtp_mp4v_pay_finalize (GObject * object)
139 {
140   GstRtpMP4VPay *rtpmp4vpay;
141
142   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
143
144   if (rtpmp4vpay->config) {
145     gst_buffer_unref (rtpmp4vpay->config);
146     rtpmp4vpay->config = NULL;
147   }
148   g_object_unref (rtpmp4vpay->adapter);
149   rtpmp4vpay->adapter = NULL;
150
151   G_OBJECT_CLASS (parent_class)->finalize (object);
152 }
153
154 static gboolean
155 gst_rtp_mp4v_pay_new_caps (GstRtpMP4VPay * rtpmp4vpay)
156 {
157   gchar *profile, *config;
158   GValue v = { 0 };
159   gboolean res;
160
161   profile = g_strdup_printf ("%d", rtpmp4vpay->profile);
162   g_value_init (&v, GST_TYPE_BUFFER);
163   gst_value_set_buffer (&v, rtpmp4vpay->config);
164   config = gst_value_serialize (&v);
165
166   res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
167       "profile-level-id", G_TYPE_STRING, profile,
168       "config", G_TYPE_STRING, config, NULL);
169
170   g_value_unset (&v);
171
172   g_free (profile);
173   g_free (config);
174
175   return res;
176 }
177
178 static gboolean
179 gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
180 {
181   GstRtpMP4VPay *rtpmp4vpay;
182   GstStructure *structure;
183   const GValue *codec_data;
184   gboolean res;
185
186   rtpmp4vpay = GST_RTP_MP4V_PAY (payload);
187
188   gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP4V-ES",
189       rtpmp4vpay->rate);
190
191   res = TRUE;
192
193   structure = gst_caps_get_structure (caps, 0);
194   codec_data = gst_structure_get_value (structure, "codec_data");
195   if (codec_data) {
196     GST_LOG_OBJECT (rtpmp4vpay, "got codec_data");
197     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
198       GstBuffer *buffer;
199
200       buffer = gst_value_get_buffer (codec_data);
201
202       if (gst_buffer_get_size (buffer) < 5)
203         goto done;
204
205       gst_buffer_extract (buffer, 4, &rtpmp4vpay->profile, 1);
206       GST_LOG_OBJECT (rtpmp4vpay, "configuring codec_data, profile %d",
207           rtpmp4vpay->profile);
208
209       if (rtpmp4vpay->config)
210         gst_buffer_unref (rtpmp4vpay->config);
211       rtpmp4vpay->config = gst_buffer_copy (buffer);
212       res = gst_rtp_mp4v_pay_new_caps (rtpmp4vpay);
213     }
214   }
215
216 done:
217   return res;
218 }
219
220 static void
221 gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
222 {
223   gst_adapter_clear (rtpmp4vpay->adapter);
224 }
225
226 static GstFlowReturn
227 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
228 {
229   guint avail;
230   GstBuffer *outbuf;
231   GstBuffer *outbuf_data = NULL;
232   GstFlowReturn ret;
233   GstBufferList *list = NULL;
234
235   /* the data available in the adapter is either smaller
236    * than the MTU or bigger. In the case it is smaller, the complete
237    * adapter contents can be put in one packet. In the case the
238    * adapter has more than one MTU, we need to split the MP4V data
239    * over multiple packets. */
240   avail = gst_adapter_available (rtpmp4vpay->adapter);
241
242   if (rtpmp4vpay->config == NULL && rtpmp4vpay->need_config) {
243     /* when we don't have a config yet, flush things out */
244     gst_adapter_flush (rtpmp4vpay->adapter, avail);
245     avail = 0;
246   }
247
248   if (!avail)
249     return GST_FLOW_OK;
250
251   ret = GST_FLOW_OK;
252
253   /* Use buffer lists. Each frame will be put into a list
254    * of buffers and the whole list will be pushed downstream
255    * at once */
256   list = gst_buffer_list_new ();
257
258   while (avail > 0) {
259     guint towrite;
260     guint payload_len;
261     guint packet_len;
262     GstRTPBuffer rtp = { NULL };
263
264     /* this will be the total lenght of the packet */
265     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
266
267     /* fill one MTU or all available bytes */
268     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay));
269
270     /* this is the payload length */
271     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
272
273     /* create buffer without payload. The payload will be put
274      * in next buffer instead. Both buffers will be merged */
275     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
276
277     /* Take buffer with the payload from the adapter */
278     outbuf_data = gst_adapter_take_buffer_fast (rtpmp4vpay->adapter,
279         payload_len);
280
281     avail -= payload_len;
282
283     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
284     gst_rtp_buffer_set_marker (&rtp, avail == 0);
285     gst_rtp_buffer_unmap (&rtp);
286
287     outbuf = gst_buffer_append (outbuf, outbuf_data);
288
289     GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4vpay->first_timestamp;
290
291     /* add to list */
292     gst_buffer_list_insert (list, -1, outbuf);
293   }
294
295   /* push the whole buffer list at once */
296   ret =
297       gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), list);
298
299   return ret;
300 }
301
302 #define VOS_STARTCODE                   0x000001B0
303 #define VOS_ENDCODE                     0x000001B1
304 #define USER_DATA_STARTCODE             0x000001B2
305 #define GOP_STARTCODE                   0x000001B3
306 #define VISUAL_OBJECT_STARTCODE         0x000001B5
307 #define VOP_STARTCODE                   0x000001B6
308
309 static gboolean
310 gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
311     gint * strip, gboolean * vopi)
312 {
313   guint32 code;
314   gboolean result;
315   *vopi = FALSE;
316
317   *strip = 0;
318
319   if (size < 5)
320     return FALSE;
321
322   code = GST_READ_UINT32_BE (data);
323   GST_DEBUG_OBJECT (enc, "start code 0x%08x", code);
324
325   switch (code) {
326     case VOS_STARTCODE:
327     case 0x00000101:
328     {
329       gint i;
330       guint8 profile;
331       gboolean newprofile = FALSE;
332       gboolean equal;
333
334       if (code == VOS_STARTCODE) {
335         /* profile_and_level_indication */
336         profile = data[4];
337
338         GST_DEBUG_OBJECT (enc, "VOS profile 0x%08x", profile);
339
340         if (profile != enc->profile) {
341           newprofile = TRUE;
342           enc->profile = profile;
343         }
344       }
345
346       /* up to the next GOP_STARTCODE or VOP_STARTCODE is
347        * the config information */
348       code = 0xffffffff;
349       for (i = 5; i < size - 4; i++) {
350         code = (code << 8) | data[i];
351         if (code == GOP_STARTCODE || code == VOP_STARTCODE)
352           break;
353       }
354       i -= 3;
355       /* see if config changed */
356       equal = FALSE;
357       if (enc->config) {
358         if (gst_buffer_get_size (enc->config) == i) {
359           equal = gst_buffer_memcmp (enc->config, 0, data, i) == 0;
360         }
361       }
362       /* if config string changed or new profile, make new caps */
363       if (!equal || newprofile) {
364         if (enc->config)
365           gst_buffer_unref (enc->config);
366         enc->config = gst_buffer_new_and_alloc (i);
367
368         gst_buffer_fill (enc->config, 0, data, i);
369
370         gst_rtp_mp4v_pay_new_caps (enc);
371       }
372       *strip = i;
373       /* we need to flush out the current packet. */
374       result = TRUE;
375       break;
376     }
377     case VOP_STARTCODE:
378       GST_DEBUG_OBJECT (enc, "VOP");
379       /* VOP startcode, we don't have to flush the packet */
380       result = FALSE;
381       /* vop-coding-type == I-frame */
382       if (size > 4 && (data[4] >> 6 == 0)) {
383         GST_DEBUG_OBJECT (enc, "VOP-I");
384         *vopi = TRUE;
385       }
386       break;
387     case GOP_STARTCODE:
388       GST_DEBUG_OBJECT (enc, "GOP");
389       *vopi = TRUE;
390       result = TRUE;
391       break;
392     case 0x00000100:
393       enc->need_config = FALSE;
394       result = TRUE;
395       break;
396     default:
397       if (code >= 0x20 && code <= 0x2f) {
398         GST_DEBUG_OBJECT (enc, "short header");
399         result = FALSE;
400       } else {
401         GST_DEBUG_OBJECT (enc, "other startcode");
402         /* all other startcodes need a flush */
403         result = TRUE;
404       }
405       break;
406   }
407   return result;
408 }
409
410 /* we expect buffers starting on startcodes. 
411  */
412 static GstFlowReturn
413 gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
414     GstBuffer * buffer)
415 {
416   GstRtpMP4VPay *rtpmp4vpay;
417   GstFlowReturn ret;
418   guint avail;
419   guint packet_len;
420   GstMapInfo map;
421   gsize size;
422   gboolean flush;
423   gint strip;
424   GstClockTime timestamp, duration;
425   gboolean vopi;
426   gboolean send_config;
427
428   ret = GST_FLOW_OK;
429   send_config = FALSE;
430
431   rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
432
433   gst_buffer_map (buffer, &map, GST_MAP_READ);
434   size = map.size;
435   timestamp = GST_BUFFER_TIMESTAMP (buffer);
436   duration = GST_BUFFER_DURATION (buffer);
437   avail = gst_adapter_available (rtpmp4vpay->adapter);
438
439   if (duration == -1)
440     duration = 0;
441
442   /* empty buffer, take timestamp */
443   if (avail == 0) {
444     rtpmp4vpay->first_timestamp = timestamp;
445     rtpmp4vpay->duration = 0;
446   }
447
448   /* depay incomming data and see if we need to start a new RTP
449    * packet */
450   flush =
451       gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, map.data, size, &strip, &vopi);
452   gst_buffer_unmap (buffer, &map);
453
454   if (strip) {
455     /* strip off config if requested */
456     if (!(rtpmp4vpay->config_interval > 0)) {
457       GstBuffer *subbuf;
458
459       GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
460           (gint) size - strip);
461
462       /* strip off header */
463       subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, strip,
464           size - strip);
465       GST_BUFFER_TIMESTAMP (subbuf) = timestamp;
466       gst_buffer_unref (buffer);
467       buffer = subbuf;
468
469       size = gst_buffer_get_size (buffer);
470     } else {
471       GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
472       rtpmp4vpay->last_config = timestamp;
473     }
474   }
475
476   /* there is a config request, see if we need to insert it */
477   if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
478     if (rtpmp4vpay->last_config != -1) {
479       guint64 diff;
480
481       GST_LOG_OBJECT (rtpmp4vpay,
482           "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
483           GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
484
485       /* calculate diff between last config in milliseconds */
486       if (timestamp > rtpmp4vpay->last_config) {
487         diff = timestamp - rtpmp4vpay->last_config;
488       } else {
489         diff = 0;
490       }
491
492       GST_DEBUG_OBJECT (rtpmp4vpay,
493           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
494
495       /* bigger than interval, queue config */
496       /* FIXME should convert timestamps to running time */
497       if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
498         GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
499         send_config = TRUE;
500       }
501     } else {
502       /* no known previous config time, send now */
503       GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
504       send_config = TRUE;
505     }
506
507     if (send_config) {
508       /* we need to send config now first */
509       GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
510
511       /* insert header */
512       buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
513
514       GST_BUFFER_TIMESTAMP (buffer) = timestamp;
515       size = gst_buffer_get_size (buffer);
516
517       if (timestamp != -1) {
518         rtpmp4vpay->last_config = timestamp;
519       }
520     }
521   }
522
523   /* if we need to flush, do so now */
524   if (flush) {
525     ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
526     rtpmp4vpay->first_timestamp = timestamp;
527     rtpmp4vpay->duration = 0;
528     avail = 0;
529   }
530
531   /* get packet length of data and see if we exceeded MTU. */
532   packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
533
534   if (gst_rtp_base_payload_is_filled (basepayload,
535           packet_len, rtpmp4vpay->duration + duration)) {
536     ret = gst_rtp_mp4v_pay_flush (rtpmp4vpay);
537     rtpmp4vpay->first_timestamp = timestamp;
538     rtpmp4vpay->duration = 0;
539   }
540
541   /* push new data */
542   gst_adapter_push (rtpmp4vpay->adapter, buffer);
543
544   rtpmp4vpay->duration += duration;
545
546   return ret;
547 }
548
549 static gboolean
550 gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay, GstEvent * event)
551 {
552   GstRtpMP4VPay *rtpmp4vpay;
553
554   rtpmp4vpay = GST_RTP_MP4V_PAY (pay);
555
556   GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
557
558   switch (GST_EVENT_TYPE (event)) {
559     case GST_EVENT_SEGMENT:
560     case GST_EVENT_EOS:
561       /* This flush call makes sure that the last buffer is always pushed
562        * to the base payloader */
563       gst_rtp_mp4v_pay_flush (rtpmp4vpay);
564       break;
565     case GST_EVENT_FLUSH_STOP:
566       gst_rtp_mp4v_pay_empty (rtpmp4vpay);
567       break;
568     default:
569       break;
570   }
571
572   /* let parent handle event too */
573   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pay, event);
574 }
575
576 static void
577 gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
578     const GValue * value, GParamSpec * pspec)
579 {
580   GstRtpMP4VPay *rtpmp4vpay;
581
582   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
583
584   switch (prop_id) {
585     case ARG_CONFIG_INTERVAL:
586       rtpmp4vpay->config_interval = g_value_get_uint (value);
587       break;
588     default:
589       break;
590   }
591 }
592
593 static void
594 gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
595     GValue * value, GParamSpec * pspec)
596 {
597   GstRtpMP4VPay *rtpmp4vpay;
598
599   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
600
601   switch (prop_id) {
602     case ARG_CONFIG_INTERVAL:
603       g_value_set_uint (value, rtpmp4vpay->config_interval);
604       break;
605     default:
606       break;
607   }
608 }
609
610 gboolean
611 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
612 {
613   return gst_element_register (plugin, "rtpmp4vpay",
614       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_PAY);
615 }