rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmpapay.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 "gstrtpmpapay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpmpapay_debug);
31 #define GST_CAT_DEFAULT (rtpmpapay_debug)
32
33 static GstStaticPadTemplate gst_rtp_mpa_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
38     );
39
40 static GstStaticPadTemplate gst_rtp_mpa_pay_src_template =
41     GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("application/x-rtp, "
45         "media = (string) \"audio\", "
46         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
47         "clock-rate = (int) 90000; "
48         "application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
51         "clock-rate = (int) 90000, " "encoding-name = (string) \"MPA\"")
52     );
53
54 static void gst_rtp_mpa_pay_finalize (GObject * object);
55
56 static GstStateChangeReturn gst_rtp_mpa_pay_change_state (GstElement * element,
57     GstStateChange transition);
58
59 static gboolean gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload,
60     GstCaps * caps);
61 static gboolean gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload,
62     GstEvent * event);
63 static GstFlowReturn gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay);
64 static GstFlowReturn gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * payload,
65     GstBuffer * buffer);
66
67 #define gst_rtp_mpa_pay_parent_class parent_class
68 G_DEFINE_TYPE (GstRtpMPAPay, gst_rtp_mpa_pay, GST_TYPE_RTP_BASE_PAYLOAD);
69
70 static void
71 gst_rtp_mpa_pay_class_init (GstRtpMPAPayClass * klass)
72 {
73   GObjectClass *gobject_class;
74   GstElementClass *gstelement_class;
75   GstRTPBasePayloadClass *gstrtpbasepayload_class;
76
77   GST_DEBUG_CATEGORY_INIT (rtpmpapay_debug, "rtpmpapay", 0,
78       "MPEG Audio RTP Depayloader");
79
80   gobject_class = (GObjectClass *) klass;
81   gstelement_class = (GstElementClass *) klass;
82   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
83
84   gobject_class->finalize = gst_rtp_mpa_pay_finalize;
85
86   gstelement_class->change_state = gst_rtp_mpa_pay_change_state;
87
88   gst_element_class_add_pad_template (gstelement_class,
89       gst_static_pad_template_get (&gst_rtp_mpa_pay_src_template));
90   gst_element_class_add_pad_template (gstelement_class,
91       gst_static_pad_template_get (&gst_rtp_mpa_pay_sink_template));
92
93   gst_element_class_set_static_metadata (gstelement_class,
94       "RTP MPEG audio payloader", "Codec/Payloader/Network/RTP",
95       "Payload MPEG audio as RTP packets (RFC 2038)",
96       "Wim Taymans <wim.taymans@gmail.com>");
97
98   gstrtpbasepayload_class->set_caps = gst_rtp_mpa_pay_setcaps;
99   gstrtpbasepayload_class->sink_event = gst_rtp_mpa_pay_sink_event;
100   gstrtpbasepayload_class->handle_buffer = gst_rtp_mpa_pay_handle_buffer;
101 }
102
103 static void
104 gst_rtp_mpa_pay_init (GstRtpMPAPay * rtpmpapay)
105 {
106   rtpmpapay->adapter = gst_adapter_new ();
107 }
108
109 static void
110 gst_rtp_mpa_pay_finalize (GObject * object)
111 {
112   GstRtpMPAPay *rtpmpapay;
113
114   rtpmpapay = GST_RTP_MPA_PAY (object);
115
116   g_object_unref (rtpmpapay->adapter);
117   rtpmpapay->adapter = NULL;
118
119   G_OBJECT_CLASS (parent_class)->finalize (object);
120 }
121
122 static void
123 gst_rtp_mpa_pay_reset (GstRtpMPAPay * pay)
124 {
125   pay->first_ts = -1;
126   pay->duration = 0;
127   gst_adapter_clear (pay->adapter);
128   GST_DEBUG_OBJECT (pay, "reset depayloader");
129 }
130
131 static gboolean
132 gst_rtp_mpa_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
133 {
134   gboolean res;
135
136   gst_rtp_base_payload_set_options (payload, "audio", TRUE, "MPA", 90000);
137   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
138
139   return res;
140 }
141
142 static gboolean
143 gst_rtp_mpa_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
144 {
145   gboolean ret;
146   GstRtpMPAPay *rtpmpapay;
147
148   rtpmpapay = GST_RTP_MPA_PAY (payload);
149
150   switch (GST_EVENT_TYPE (event)) {
151     case GST_EVENT_EOS:
152       /* make sure we push the last packets in the adapter on EOS */
153       gst_rtp_mpa_pay_flush (rtpmpapay);
154       break;
155     case GST_EVENT_FLUSH_STOP:
156       gst_rtp_mpa_pay_reset (rtpmpapay);
157       break;
158     default:
159       break;
160   }
161
162   ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
163
164   return ret;
165 }
166
167 static GstFlowReturn
168 gst_rtp_mpa_pay_flush (GstRtpMPAPay * rtpmpapay)
169 {
170   guint avail;
171   GstBuffer *outbuf;
172   GstFlowReturn ret;
173   guint16 frag_offset;
174
175   /* the data available in the adapter is either smaller
176    * than the MTU or bigger. In the case it is smaller, the complete
177    * adapter contents can be put in one packet. In the case the
178    * adapter has more than one MTU, we need to split the MPA data
179    * over multiple packets. The frag_offset in each packet header
180    * needs to be updated with the position in the MPA frame. */
181   avail = gst_adapter_available (rtpmpapay->adapter);
182
183   ret = GST_FLOW_OK;
184
185   frag_offset = 0;
186   while (avail > 0) {
187     guint towrite;
188     guint8 *payload;
189     guint payload_len;
190     guint packet_len;
191     GstRTPBuffer rtp = { NULL };
192     GstBuffer *paybuf;
193
194     /* this will be the total length of the packet */
195     packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
196
197     /* fill one MTU or all available bytes */
198     towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpapay));
199
200     /* this is the payload length */
201     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
202
203     /* create buffer to hold the payload */
204     outbuf = gst_rtp_buffer_new_allocate (4, 0, 0);
205
206     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
207
208     payload_len -= 4;
209
210     gst_rtp_buffer_set_payload_type (&rtp, GST_RTP_PAYLOAD_MPA);
211
212     /*
213      *  0                   1                   2                   3
214      *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
215      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216      * |             MBZ               |          Frag_offset          |
217      * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218      */
219     payload = gst_rtp_buffer_get_payload (&rtp);
220     payload[0] = 0;
221     payload[1] = 0;
222     payload[2] = frag_offset >> 8;
223     payload[3] = frag_offset & 0xff;
224
225     avail -= payload_len;
226     frag_offset += payload_len;
227
228     if (avail == 0)
229       gst_rtp_buffer_set_marker (&rtp, TRUE);
230
231     gst_rtp_buffer_unmap (&rtp);
232
233     paybuf = gst_adapter_take_buffer_fast (rtpmpapay->adapter, payload_len);
234     outbuf = gst_buffer_append (outbuf, paybuf);
235
236     GST_BUFFER_TIMESTAMP (outbuf) = rtpmpapay->first_ts;
237     GST_BUFFER_DURATION (outbuf) = rtpmpapay->duration;
238
239     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmpapay), outbuf);
240   }
241
242   return ret;
243 }
244
245 static GstFlowReturn
246 gst_rtp_mpa_pay_handle_buffer (GstRTPBasePayload * basepayload,
247     GstBuffer * buffer)
248 {
249   GstRtpMPAPay *rtpmpapay;
250   GstFlowReturn ret;
251   guint size, avail;
252   guint packet_len;
253   GstClockTime duration, timestamp;
254
255   rtpmpapay = GST_RTP_MPA_PAY (basepayload);
256
257   size = gst_buffer_get_size (buffer);
258   duration = GST_BUFFER_DURATION (buffer);
259   timestamp = GST_BUFFER_TIMESTAMP (buffer);
260
261   if (GST_BUFFER_IS_DISCONT (buffer)) {
262     GST_DEBUG_OBJECT (rtpmpapay, "DISCONT");
263     gst_rtp_mpa_pay_reset (rtpmpapay);
264   }
265
266   avail = gst_adapter_available (rtpmpapay->adapter);
267
268   /* get packet length of previous data and this new data,
269    * payload length includes a 4 byte header */
270   packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
271
272   /* if this buffer is going to overflow the packet, flush what we
273    * have. */
274   if (gst_rtp_base_payload_is_filled (basepayload,
275           packet_len, rtpmpapay->duration + duration)) {
276     ret = gst_rtp_mpa_pay_flush (rtpmpapay);
277     avail = 0;
278   } else {
279     ret = GST_FLOW_OK;
280   }
281
282   if (avail == 0) {
283     GST_DEBUG_OBJECT (rtpmpapay,
284         "first packet, save timestamp %" GST_TIME_FORMAT,
285         GST_TIME_ARGS (timestamp));
286     rtpmpapay->first_ts = timestamp;
287     rtpmpapay->duration = 0;
288   }
289
290   gst_adapter_push (rtpmpapay->adapter, buffer);
291   rtpmpapay->duration = duration;
292
293   return ret;
294 }
295
296 static GstStateChangeReturn
297 gst_rtp_mpa_pay_change_state (GstElement * element, GstStateChange transition)
298 {
299   GstRtpMPAPay *rtpmpapay;
300   GstStateChangeReturn ret;
301
302   rtpmpapay = GST_RTP_MPA_PAY (element);
303
304   switch (transition) {
305     case GST_STATE_CHANGE_READY_TO_PAUSED:
306       gst_rtp_mpa_pay_reset (rtpmpapay);
307       break;
308     default:
309       break;
310   }
311
312   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
313
314   switch (transition) {
315     case GST_STATE_CHANGE_PAUSED_TO_READY:
316       gst_rtp_mpa_pay_reset (rtpmpapay);
317       break;
318     default:
319       break;
320   }
321   return ret;
322 }
323
324 gboolean
325 gst_rtp_mpa_pay_plugin_init (GstPlugin * plugin)
326 {
327   return gst_element_register (plugin, "rtpmpapay",
328       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_PAY);
329 }