rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpg722depay.c
1 /* GStreamer
2  * Copyright (C) <2007> 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 #include <stdlib.h>
26
27 #include <gst/audio/audio.h>
28
29 #include "gstrtpg722depay.h"
30 #include "gstrtpchannels.h"
31
32 GST_DEBUG_CATEGORY_STATIC (rtpg722depay_debug);
33 #define GST_CAT_DEFAULT (rtpg722depay_debug)
34
35 static GstStaticPadTemplate gst_rtp_g722_depay_src_template =
36 GST_STATIC_PAD_TEMPLATE ("src",
37     GST_PAD_SRC,
38     GST_PAD_ALWAYS,
39     GST_STATIC_CAPS ("audio/G722, "
40         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
41     );
42
43 static GstStaticPadTemplate gst_rtp_g722_depay_sink_template =
44     GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("application/x-rtp, "
48         "media = (string) \"audio\", " "clock-rate = (int) 8000, "
49         /* "channels = (int) [1, MAX]"  */
50         /* "channel-order = (string) ANY" */
51         "encoding-name = (string) \"G722\";"
52         "application/x-rtp, "
53         "media = (string) \"audio\", "
54         "payload = (int) " GST_RTP_PAYLOAD_G722_STRING ", "
55         "clock-rate = (int) [ 1, MAX ]"
56         /* "channels = (int) [1, MAX]" */
57         /* "emphasis = (string) ANY" */
58         /* "channel-order = (string) ANY" */
59     )
60     );
61
62 #define gst_rtp_g722_depay_parent_class parent_class
63 G_DEFINE_TYPE (GstRtpG722Depay, gst_rtp_g722_depay,
64     GST_TYPE_RTP_BASE_DEPAYLOAD);
65
66 static gboolean gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload,
67     GstCaps * caps);
68 static GstBuffer *gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload,
69     GstBuffer * buf);
70
71 static void
72 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
73 {
74   GstElementClass *gstelement_class;
75   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
76
77   GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
78       "G722 RTP Depayloader");
79
80   gstelement_class = (GstElementClass *) klass;
81   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
82
83   gst_element_class_add_pad_template (gstelement_class,
84       gst_static_pad_template_get (&gst_rtp_g722_depay_src_template));
85   gst_element_class_add_pad_template (gstelement_class,
86       gst_static_pad_template_get (&gst_rtp_g722_depay_sink_template));
87
88   gst_element_class_set_static_metadata (gstelement_class,
89       "RTP audio depayloader", "Codec/Depayloader/Network/RTP",
90       "Extracts G722 audio from RTP packets",
91       "Wim Taymans <wim.taymans@gmail.com>");
92
93   gstrtpbasedepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
94   gstrtpbasedepayload_class->process = gst_rtp_g722_depay_process;
95 }
96
97 static void
98 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay)
99 {
100 }
101
102 static gint
103 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
104     gint def)
105 {
106   const gchar *str;
107   gint res;
108
109   if ((str = gst_structure_get_string (structure, field)))
110     return atoi (str);
111
112   if (gst_structure_get_int (structure, field, &res))
113     return res;
114
115   return def;
116 }
117
118 static gboolean
119 gst_rtp_g722_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
120 {
121   GstStructure *structure;
122   GstRtpG722Depay *rtpg722depay;
123   gint clock_rate, payload, samplerate;
124   gint channels;
125   GstCaps *srccaps;
126   gboolean res;
127 #if 0
128   const gchar *channel_order;
129   const GstRTPChannelOrder *order;
130 #endif
131
132   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
133
134   structure = gst_caps_get_structure (caps, 0);
135
136   payload = 96;
137   gst_structure_get_int (structure, "payload", &payload);
138   switch (payload) {
139     case GST_RTP_PAYLOAD_G722:
140       channels = 1;
141       clock_rate = 8000;
142       samplerate = 16000;
143       break;
144     default:
145       /* no fixed mapping, we need clock-rate */
146       channels = 0;
147       clock_rate = 0;
148       samplerate = 0;
149       break;
150   }
151
152   /* caps can overwrite defaults */
153   clock_rate =
154       gst_rtp_g722_depay_parse_int (structure, "clock-rate", clock_rate);
155   if (clock_rate == 0)
156     goto no_clockrate;
157
158   if (clock_rate == 8000)
159     samplerate = 16000;
160
161   if (samplerate == 0)
162     samplerate = clock_rate;
163
164   channels =
165       gst_rtp_g722_depay_parse_int (structure, "encoding-params", channels);
166   if (channels == 0) {
167     channels = gst_rtp_g722_depay_parse_int (structure, "channels", channels);
168     if (channels == 0) {
169       /* channels defaults to 1 otherwise */
170       channels = 1;
171     }
172   }
173
174   depayload->clock_rate = clock_rate;
175   rtpg722depay->rate = samplerate;
176   rtpg722depay->channels = channels;
177
178   srccaps = gst_caps_new_simple ("audio/G722",
179       "rate", G_TYPE_INT, samplerate, "channels", G_TYPE_INT, channels, NULL);
180
181   /* FIXME: Do something with the channel order */
182 #if 0
183   /* add channel positions */
184   channel_order = gst_structure_get_string (structure, "channel-order");
185
186   order = gst_rtp_channels_get_by_order (channels, channel_order);
187   if (order) {
188     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
189         order->pos);
190   } else {
191     GstAudioChannelPosition *pos;
192
193     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
194         (NULL), ("Unknown channel order '%s' for %d channels",
195             GST_STR_NULL (channel_order), channels));
196     /* create default NONE layout */
197     pos = gst_rtp_channels_create_default (channels);
198     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
199     g_free (pos);
200   }
201 #endif
202
203   res = gst_pad_set_caps (depayload->srcpad, srccaps);
204   gst_caps_unref (srccaps);
205
206   return res;
207
208   /* ERRORS */
209 no_clockrate:
210   {
211     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
212     return FALSE;
213   }
214 }
215
216 static GstBuffer *
217 gst_rtp_g722_depay_process (GstRTPBaseDepayload * depayload, GstBuffer * buf)
218 {
219   GstRtpG722Depay *rtpg722depay;
220   GstBuffer *outbuf;
221   gint payload_len;
222   gboolean marker;
223   GstRTPBuffer rtp = { NULL };
224
225   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
226
227   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
228
229   payload_len = gst_rtp_buffer_get_payload_len (&rtp);
230
231   if (payload_len <= 0)
232     goto empty_packet;
233
234   GST_DEBUG_OBJECT (rtpg722depay, "got payload of %d bytes", payload_len);
235
236   outbuf = gst_rtp_buffer_get_payload_buffer (&rtp);
237   marker = gst_rtp_buffer_get_marker (&rtp);
238   gst_rtp_buffer_unmap (&rtp);
239
240   if (marker && outbuf) {
241     /* mark talk spurt with RESYNC */
242     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
243   }
244
245   return outbuf;
246
247   /* ERRORS */
248 empty_packet:
249   {
250     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
251         ("Empty Payload."), (NULL));
252     gst_rtp_buffer_unmap (&rtp);
253     return NULL;
254   }
255 }
256
257 gboolean
258 gst_rtp_g722_depay_plugin_init (GstPlugin * plugin)
259 {
260   return gst_element_register (plugin, "rtpg722depay",
261       GST_RANK_SECONDARY, GST_TYPE_RTP_G722_DEPAY);
262 }