1 /* GStreamer
2  * Copyright (C) <2006> 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/base/gstbitreader.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 
29 #include "gstrtpmp4gpay.h"
30 #include "gstrtputils.h"
31 
32 GST_DEBUG_CATEGORY_STATIC (rtpmp4gpay_debug);
33 #define GST_CAT_DEFAULT (rtpmp4gpay_debug)
34 
35 static GstStaticPadTemplate gst_rtp_mp4g_pay_sink_template =
36     GST_STATIC_PAD_TEMPLATE ("sink",
37     GST_PAD_SINK,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("video/mpeg,"
40         "mpegversion=(int) 4,"
41         "systemstream=(boolean)false;"
42         "audio/mpeg," "mpegversion=(int) 4, " "stream-format=(string) raw")
43     );
44 
45 static GstStaticPadTemplate gst_rtp_mp4g_pay_src_template =
46 GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) { \"video\", \"audio\", \"application\" }, "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) [1, MAX ], "
53         "encoding-name = (string) \"MPEG4-GENERIC\", "
54         /* required string params */
55         "streamtype = (string) { \"4\", \"5\" }, "      /* 4 = video, 5 = audio */
56         /* "profile-level-id = (string) [1,MAX], " */
57         /* "config = (string) [1,MAX]" */
58         "mode = (string) { \"generic\", \"CELP-cbr\", \"CELP-vbr\", \"AAC-lbr\", \"AAC-hbr\" } "
59         /* Optional general parameters */
60         /* "objecttype = (string) [1,MAX], " */
61         /* "constantsize = (string) [1,MAX], " *//* constant size of each AU */
62         /* "constantduration = (string) [1,MAX], " *//* constant duration of each AU */
63         /* "maxdisplacement = (string) [1,MAX], " */
64         /* "de-interleavebuffersize = (string) [1,MAX], " */
65         /* Optional configuration parameters */
66         /* "sizelength = (string) [1, 16], " *//* max 16 bits, should be enough... */
67         /* "indexlength = (string) [1, 8], " */
68         /* "indexdeltalength = (string) [1, 8], " */
69         /* "ctsdeltalength = (string) [1, 64], " */
70         /* "dtsdeltalength = (string) [1, 64], " */
71         /* "randomaccessindication = (string) {0, 1}, " */
72         /* "streamstateindication = (string) [0, 64], " */
73         /* "auxiliarydatasizelength = (string) [0, 64]" */ )
74     );
75 
76 
77 static void gst_rtp_mp4g_pay_finalize (GObject * object);
78 
79 static GstStateChangeReturn gst_rtp_mp4g_pay_change_state (GstElement * element,
80     GstStateChange transition);
81 
82 static gboolean gst_rtp_mp4g_pay_setcaps (GstRTPBasePayload * payload,
83     GstCaps * caps);
84 static GstFlowReturn gst_rtp_mp4g_pay_handle_buffer (GstRTPBasePayload *
85     payload, GstBuffer * buffer);
86 static gboolean gst_rtp_mp4g_pay_sink_event (GstRTPBasePayload * payload,
87     GstEvent * event);
88 
89 #define gst_rtp_mp4g_pay_parent_class parent_class
90 G_DEFINE_TYPE (GstRtpMP4GPay, gst_rtp_mp4g_pay, GST_TYPE_RTP_BASE_PAYLOAD);
91 
92 static void
gst_rtp_mp4g_pay_class_init(GstRtpMP4GPayClass * klass)93 gst_rtp_mp4g_pay_class_init (GstRtpMP4GPayClass * klass)
94 {
95   GObjectClass *gobject_class;
96   GstElementClass *gstelement_class;
97   GstRTPBasePayloadClass *gstrtpbasepayload_class;
98 
99   gobject_class = (GObjectClass *) klass;
100   gstelement_class = (GstElementClass *) klass;
101   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
102 
103   gobject_class->finalize = gst_rtp_mp4g_pay_finalize;
104 
105   gstelement_class->change_state = gst_rtp_mp4g_pay_change_state;
106 
107   gstrtpbasepayload_class->set_caps = gst_rtp_mp4g_pay_setcaps;
108   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4g_pay_handle_buffer;
109   gstrtpbasepayload_class->sink_event = gst_rtp_mp4g_pay_sink_event;
110 
111   gst_element_class_add_static_pad_template (gstelement_class,
112       &gst_rtp_mp4g_pay_src_template);
113   gst_element_class_add_static_pad_template (gstelement_class,
114       &gst_rtp_mp4g_pay_sink_template);
115 
116   gst_element_class_set_static_metadata (gstelement_class,
117       "RTP MPEG4 ES payloader",
118       "Codec/Payloader/Network/RTP",
119       "Payload MPEG4 elementary streams as RTP packets (RFC 3640)",
120       "Wim Taymans <wim.taymans@gmail.com>");
121 
122   GST_DEBUG_CATEGORY_INIT (rtpmp4gpay_debug, "rtpmp4gpay", 0,
123       "MP4-generic RTP Payloader");
124 }
125 
126 static void
gst_rtp_mp4g_pay_init(GstRtpMP4GPay * rtpmp4gpay)127 gst_rtp_mp4g_pay_init (GstRtpMP4GPay * rtpmp4gpay)
128 {
129   rtpmp4gpay->adapter = gst_adapter_new ();
130 }
131 
132 static void
gst_rtp_mp4g_pay_reset(GstRtpMP4GPay * rtpmp4gpay)133 gst_rtp_mp4g_pay_reset (GstRtpMP4GPay * rtpmp4gpay)
134 {
135   GST_DEBUG_OBJECT (rtpmp4gpay, "reset");
136 
137   gst_adapter_clear (rtpmp4gpay->adapter);
138 }
139 
140 static void
gst_rtp_mp4g_pay_cleanup(GstRtpMP4GPay * rtpmp4gpay)141 gst_rtp_mp4g_pay_cleanup (GstRtpMP4GPay * rtpmp4gpay)
142 {
143   gst_rtp_mp4g_pay_reset (rtpmp4gpay);
144 
145   g_free (rtpmp4gpay->params);
146   rtpmp4gpay->params = NULL;
147 
148   if (rtpmp4gpay->config)
149     gst_buffer_unref (rtpmp4gpay->config);
150   rtpmp4gpay->config = NULL;
151 
152   g_free (rtpmp4gpay->profile);
153   rtpmp4gpay->profile = NULL;
154 
155   rtpmp4gpay->streamtype = NULL;
156   rtpmp4gpay->mode = NULL;
157 
158   rtpmp4gpay->frame_len = 0;
159 }
160 
161 static void
gst_rtp_mp4g_pay_finalize(GObject * object)162 gst_rtp_mp4g_pay_finalize (GObject * object)
163 {
164   GstRtpMP4GPay *rtpmp4gpay;
165 
166   rtpmp4gpay = GST_RTP_MP4G_PAY (object);
167 
168   gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
169 
170   g_object_unref (rtpmp4gpay->adapter);
171   rtpmp4gpay->adapter = NULL;
172 
173   G_OBJECT_CLASS (parent_class)->finalize (object);
174 }
175 
176 static const unsigned int sampling_table[16] = {
177   96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
178   16000, 12000, 11025, 8000, 7350, 0, 0, 0
179 };
180 
181 static gboolean
gst_rtp_mp4g_pay_parse_audio_config(GstRtpMP4GPay * rtpmp4gpay,GstBuffer * buffer)182 gst_rtp_mp4g_pay_parse_audio_config (GstRtpMP4GPay * rtpmp4gpay,
183     GstBuffer * buffer)
184 {
185   GstMapInfo map;
186   guint8 objectType = 0;
187   guint8 samplingIdx = 0;
188   guint8 channelCfg = 0;
189   GstBitReader br;
190 
191   gst_buffer_map (buffer, &map, GST_MAP_READ);
192 
193   gst_bit_reader_init (&br, map.data, map.size);
194 
195   /* any object type is fine, we need to copy it to the profile-level-id field. */
196   if (!gst_bit_reader_get_bits_uint8 (&br, &objectType, 5))
197     goto too_short;
198   if (objectType == 0)
199     goto invalid_object;
200 
201   if (!gst_bit_reader_get_bits_uint8 (&br, &samplingIdx, 4))
202     goto too_short;
203   /* only fixed values for now */
204   if (samplingIdx > 12 && samplingIdx != 15)
205     goto wrong_freq;
206 
207   if (!gst_bit_reader_get_bits_uint8 (&br, &channelCfg, 4))
208     goto too_short;
209   if (channelCfg > 7)
210     goto wrong_channels;
211 
212   /* rtp rate depends on sampling rate of the audio */
213   if (samplingIdx == 15) {
214     guint32 rate = 0;
215 
216     /* index of 15 means we get the rate in the next 24 bits */
217     if (!gst_bit_reader_get_bits_uint32 (&br, &rate, 24))
218       goto too_short;
219 
220     rtpmp4gpay->rate = rate;
221   } else {
222     /* else use the rate from the table */
223     rtpmp4gpay->rate = sampling_table[samplingIdx];
224   }
225 
226   rtpmp4gpay->frame_len = 1024;
227 
228   switch (objectType) {
229     case 1:
230     case 2:
231     case 3:
232     case 4:
233     case 6:
234     case 7:
235     {
236       guint8 frameLenFlag = 0;
237 
238       if (gst_bit_reader_get_bits_uint8 (&br, &frameLenFlag, 1))
239         if (frameLenFlag)
240           rtpmp4gpay->frame_len = 960;
241 
242       break;
243     }
244     default:
245       break;
246   }
247 
248   /* extra rtp params contain the number of channels */
249   g_free (rtpmp4gpay->params);
250   rtpmp4gpay->params = g_strdup_printf ("%d", channelCfg);
251   /* audio stream type */
252   rtpmp4gpay->streamtype = "5";
253   /* mode only high bitrate for now */
254   rtpmp4gpay->mode = "AAC-hbr";
255   /* profile */
256   g_free (rtpmp4gpay->profile);
257   rtpmp4gpay->profile = g_strdup_printf ("%d", objectType);
258 
259   GST_DEBUG_OBJECT (rtpmp4gpay,
260       "objectType: %d, samplingIdx: %d (%d), channelCfg: %d, frame_len %d",
261       objectType, samplingIdx, rtpmp4gpay->rate, channelCfg,
262       rtpmp4gpay->frame_len);
263 
264   gst_buffer_unmap (buffer, &map);
265   return TRUE;
266 
267   /* ERROR */
268 too_short:
269   {
270     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
271         (NULL), ("config string too short"));
272     gst_buffer_unmap (buffer, &map);
273     return FALSE;
274   }
275 invalid_object:
276   {
277     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
278         (NULL), ("invalid object type"));
279     gst_buffer_unmap (buffer, &map);
280     return FALSE;
281   }
282 wrong_freq:
283   {
284     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
285         (NULL), ("unsupported frequency index %d", samplingIdx));
286     gst_buffer_unmap (buffer, &map);
287     return FALSE;
288   }
289 wrong_channels:
290   {
291     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, NOT_IMPLEMENTED,
292         (NULL), ("unsupported number of channels %d, must < 8", channelCfg));
293     gst_buffer_unmap (buffer, &map);
294     return FALSE;
295   }
296 }
297 
298 #define VOS_STARTCODE                   0x000001B0
299 
300 static gboolean
gst_rtp_mp4g_pay_parse_video_config(GstRtpMP4GPay * rtpmp4gpay,GstBuffer * buffer)301 gst_rtp_mp4g_pay_parse_video_config (GstRtpMP4GPay * rtpmp4gpay,
302     GstBuffer * buffer)
303 {
304   GstMapInfo map;
305   guint32 code;
306 
307   gst_buffer_map (buffer, &map, GST_MAP_READ);
308 
309   if (map.size < 5)
310     goto too_short;
311 
312   code = GST_READ_UINT32_BE (map.data);
313 
314   g_free (rtpmp4gpay->profile);
315   if (code == VOS_STARTCODE) {
316     /* get profile */
317     rtpmp4gpay->profile = g_strdup_printf ("%d", (gint) map.data[4]);
318   } else {
319     GST_ELEMENT_WARNING (rtpmp4gpay, STREAM, FORMAT,
320         (NULL), ("profile not found in config string, assuming \'1\'"));
321     rtpmp4gpay->profile = g_strdup ("1");
322   }
323 
324   /* fixed rate */
325   rtpmp4gpay->rate = 90000;
326   /* video stream type */
327   rtpmp4gpay->streamtype = "4";
328   /* no params for video */
329   rtpmp4gpay->params = NULL;
330   /* mode */
331   rtpmp4gpay->mode = "generic";
332 
333   GST_LOG_OBJECT (rtpmp4gpay, "profile %s", rtpmp4gpay->profile);
334 
335   gst_buffer_unmap (buffer, &map);
336 
337   return TRUE;
338 
339   /* ERROR */
340 too_short:
341   {
342     GST_ELEMENT_ERROR (rtpmp4gpay, STREAM, FORMAT,
343         (NULL), ("config string too short"));
344     gst_buffer_unmap (buffer, &map);
345     return FALSE;
346   }
347 }
348 
349 static gboolean
gst_rtp_mp4g_pay_new_caps(GstRtpMP4GPay * rtpmp4gpay)350 gst_rtp_mp4g_pay_new_caps (GstRtpMP4GPay * rtpmp4gpay)
351 {
352   gchar *config;
353   GValue v = { 0 };
354   gboolean res;
355 
356 #define MP4GCAPS						\
357   "streamtype", G_TYPE_STRING, rtpmp4gpay->streamtype, 		\
358   "profile-level-id", G_TYPE_STRING, rtpmp4gpay->profile,	\
359   "mode", G_TYPE_STRING, rtpmp4gpay->mode,			\
360   "config", G_TYPE_STRING, config,				\
361   "sizelength", G_TYPE_STRING, "13",				\
362   "indexlength", G_TYPE_STRING, "3",				\
363   "indexdeltalength", G_TYPE_STRING, "3",			\
364   NULL
365 
366   g_value_init (&v, GST_TYPE_BUFFER);
367   gst_value_set_buffer (&v, rtpmp4gpay->config);
368   config = gst_value_serialize (&v);
369 
370   /* hmm, silly */
371   if (rtpmp4gpay->params) {
372     res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
373         "encoding-params", G_TYPE_STRING, rtpmp4gpay->params, MP4GCAPS);
374   } else {
375     res = gst_rtp_base_payload_set_outcaps (GST_RTP_BASE_PAYLOAD (rtpmp4gpay),
376         MP4GCAPS);
377   }
378 
379   g_value_unset (&v);
380   g_free (config);
381 
382 #undef MP4GCAPS
383   return res;
384 }
385 
386 static gboolean
gst_rtp_mp4g_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)387 gst_rtp_mp4g_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
388 {
389   GstRtpMP4GPay *rtpmp4gpay;
390   GstStructure *structure;
391   const GValue *codec_data;
392   const gchar *media_type = NULL;
393   gboolean res;
394 
395   rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
396 
397   structure = gst_caps_get_structure (caps, 0);
398 
399   codec_data = gst_structure_get_value (structure, "codec_data");
400   if (codec_data) {
401     GST_LOG_OBJECT (rtpmp4gpay, "got codec_data");
402     if (G_VALUE_TYPE (codec_data) == GST_TYPE_BUFFER) {
403       GstBuffer *buffer;
404       const gchar *name;
405 
406       buffer = gst_value_get_buffer (codec_data);
407       GST_LOG_OBJECT (rtpmp4gpay, "configuring codec_data");
408 
409       name = gst_structure_get_name (structure);
410 
411       /* parse buffer */
412       if (!strcmp (name, "audio/mpeg")) {
413         res = gst_rtp_mp4g_pay_parse_audio_config (rtpmp4gpay, buffer);
414         media_type = "audio";
415       } else if (!strcmp (name, "video/mpeg")) {
416         res = gst_rtp_mp4g_pay_parse_video_config (rtpmp4gpay, buffer);
417         media_type = "video";
418       } else {
419         res = FALSE;
420       }
421       if (!res)
422         goto config_failed;
423 
424       /* now we can configure the buffer */
425       if (rtpmp4gpay->config)
426         gst_buffer_unref (rtpmp4gpay->config);
427 
428       rtpmp4gpay->config = gst_buffer_copy (buffer);
429     }
430   }
431   if (media_type == NULL)
432     goto config_failed;
433 
434   gst_rtp_base_payload_set_options (payload, media_type, TRUE, "MPEG4-GENERIC",
435       rtpmp4gpay->rate);
436 
437   res = gst_rtp_mp4g_pay_new_caps (rtpmp4gpay);
438 
439   return res;
440 
441   /* ERRORS */
442 config_failed:
443   {
444     GST_DEBUG_OBJECT (rtpmp4gpay, "failed to parse config");
445     return FALSE;
446   }
447 }
448 
449 static GstFlowReturn
gst_rtp_mp4g_pay_flush(GstRtpMP4GPay * rtpmp4gpay)450 gst_rtp_mp4g_pay_flush (GstRtpMP4GPay * rtpmp4gpay)
451 {
452   guint avail, total;
453   GstBuffer *outbuf;
454   GstFlowReturn ret;
455   guint mtu;
456 
457   /* the data available in the adapter is either smaller
458    * than the MTU or bigger. In the case it is smaller, the complete
459    * adapter contents can be put in one packet. In the case the
460    * adapter has more than one MTU, we need to fragment the MPEG data
461    * over multiple packets. */
462   total = avail = gst_adapter_available (rtpmp4gpay->adapter);
463 
464   ret = GST_FLOW_OK;
465   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4gpay);
466 
467   while (avail > 0) {
468     guint towrite;
469     guint8 *payload;
470     guint payload_len;
471     guint packet_len;
472     GstRTPBuffer rtp = { NULL };
473     GstBuffer *paybuf;
474 
475     /* this will be the total lenght of the packet */
476     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
477 
478     /* fill one MTU or all available bytes, we need 4 spare bytes for
479      * the AU header. */
480     towrite = MIN (packet_len, mtu - 4);
481 
482     /* this is the payload length */
483     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
484 
485     GST_DEBUG_OBJECT (rtpmp4gpay,
486         "avail %d, towrite %d, packet_len %d, payload_len %d", avail, towrite,
487         packet_len, payload_len);
488 
489     /* create buffer to hold the payload, also make room for the 4 header bytes. */
490     outbuf = gst_rtp_buffer_new_allocate (4, 0, 0);
491 
492     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
493 
494     /* copy payload */
495     payload = gst_rtp_buffer_get_payload (&rtp);
496 
497     /* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
498      * |AU-headers-length|AU-header|AU-header|      |AU-header|padding|
499      * |                 |   (1)   |   (2)   |      |   (n)   | bits  |
500      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- .. -+-+-+-+-+-+-+-+-+-+
501      */
502     /* AU-headers-length, we only have 1 AU-header */
503     payload[0] = 0x00;
504     payload[1] = 0x10;          /* we use 16 bits for the header */
505 
506     /* +---------------------------------------+
507      * |     AU-size                           |
508      * +---------------------------------------+
509      * |     AU-Index / AU-Index-delta         |
510      * +---------------------------------------+
511      * |     CTS-flag                          |
512      * +---------------------------------------+
513      * |     CTS-delta                         |
514      * +---------------------------------------+
515      * |     DTS-flag                          |
516      * +---------------------------------------+
517      * |     DTS-delta                         |
518      * +---------------------------------------+
519      * |     RAP-flag                          |
520      * +---------------------------------------+
521      * |     Stream-state                      |
522      * +---------------------------------------+
523      */
524     /* The AU-header, no CTS, DTS, RAP, Stream-state
525      *
526      * AU-size is always the total size of the AU, not the fragmented size
527      */
528     payload[2] = (total & 0x1fe0) >> 5;
529     payload[3] = (total & 0x1f) << 3;   /* we use 13 bits for the size, 3 bits index */
530 
531     /* marker only if the packet is complete */
532     gst_rtp_buffer_set_marker (&rtp, avail <= payload_len);
533 
534     gst_rtp_buffer_unmap (&rtp);
535 
536     paybuf = gst_adapter_take_buffer_fast (rtpmp4gpay->adapter, payload_len);
537     gst_rtp_copy_meta (GST_ELEMENT_CAST (rtpmp4gpay), outbuf, paybuf, 0);
538     outbuf = gst_buffer_append (outbuf, paybuf);
539 
540     GST_BUFFER_PTS (outbuf) = rtpmp4gpay->first_timestamp;
541     GST_BUFFER_DURATION (outbuf) = rtpmp4gpay->first_duration;
542 
543     GST_BUFFER_OFFSET (outbuf) = GST_BUFFER_OFFSET_NONE;
544 
545     if (rtpmp4gpay->discont) {
546       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
547       /* Only the first outputted buffer has the DISCONT flag */
548       rtpmp4gpay->discont = FALSE;
549     }
550 
551     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4gpay), outbuf);
552 
553     avail -= payload_len;
554   }
555 
556   return ret;
557 }
558 
559 /* we expect buffers as exactly one complete AU
560  */
561 static GstFlowReturn
gst_rtp_mp4g_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)562 gst_rtp_mp4g_pay_handle_buffer (GstRTPBasePayload * basepayload,
563     GstBuffer * buffer)
564 {
565   GstRtpMP4GPay *rtpmp4gpay;
566 
567   rtpmp4gpay = GST_RTP_MP4G_PAY (basepayload);
568 
569   rtpmp4gpay->first_timestamp = GST_BUFFER_PTS (buffer);
570   rtpmp4gpay->first_duration = GST_BUFFER_DURATION (buffer);
571   rtpmp4gpay->discont = GST_BUFFER_IS_DISCONT (buffer);
572 
573   /* we always encode and flush a full AU */
574   gst_adapter_push (rtpmp4gpay->adapter, buffer);
575 
576   return gst_rtp_mp4g_pay_flush (rtpmp4gpay);
577 }
578 
579 static gboolean
gst_rtp_mp4g_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)580 gst_rtp_mp4g_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
581 {
582   GstRtpMP4GPay *rtpmp4gpay;
583 
584   rtpmp4gpay = GST_RTP_MP4G_PAY (payload);
585 
586   GST_DEBUG ("Got event: %s", GST_EVENT_TYPE_NAME (event));
587 
588   switch (GST_EVENT_TYPE (event)) {
589     case GST_EVENT_SEGMENT:
590     case GST_EVENT_EOS:
591       /* This flush call makes sure that the last buffer is always pushed
592        * to the base payloader */
593       gst_rtp_mp4g_pay_flush (rtpmp4gpay);
594       break;
595     case GST_EVENT_FLUSH_STOP:
596       gst_rtp_mp4g_pay_reset (rtpmp4gpay);
597       break;
598     default:
599       break;
600   }
601 
602   /* let parent handle event too */
603   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
604 }
605 
606 static GstStateChangeReturn
gst_rtp_mp4g_pay_change_state(GstElement * element,GstStateChange transition)607 gst_rtp_mp4g_pay_change_state (GstElement * element, GstStateChange transition)
608 {
609   GstStateChangeReturn ret;
610   GstRtpMP4GPay *rtpmp4gpay;
611 
612   rtpmp4gpay = GST_RTP_MP4G_PAY (element);
613 
614   switch (transition) {
615     case GST_STATE_CHANGE_READY_TO_PAUSED:
616       gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
617       break;
618     default:
619       break;
620   }
621 
622   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
623 
624   switch (transition) {
625     case GST_STATE_CHANGE_PAUSED_TO_READY:
626       gst_rtp_mp4g_pay_cleanup (rtpmp4gpay);
627       break;
628     default:
629       break;
630   }
631 
632   return ret;
633 }
634 
635 gboolean
gst_rtp_mp4g_pay_plugin_init(GstPlugin * plugin)636 gst_rtp_mp4g_pay_plugin_init (GstPlugin * plugin)
637 {
638   return gst_element_register (plugin, "rtpmp4gpay",
639       GST_RANK_SECONDARY, GST_TYPE_RTP_MP4G_PAY);
640 }
641