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