tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.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 GST_BOILERPLATE (GstRtpG722Depay, gst_rtp_g722_depay, GstBaseRTPDepayload,
66     GST_TYPE_BASE_RTP_DEPAYLOAD);
67
68 static gboolean gst_rtp_g722_depay_setcaps (GstBaseRTPDepayload * depayload,
69     GstCaps * caps);
70 static GstBuffer *gst_rtp_g722_depay_process (GstBaseRTPDepayload * depayload,
71     GstBuffer * buf);
72
73 static void
74 gst_rtp_g722_depay_base_init (gpointer klass)
75 {
76   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
77
78   gst_element_class_add_static_pad_template (element_class,
79       &gst_rtp_g722_depay_src_template);
80   gst_element_class_add_static_pad_template (element_class,
81       &gst_rtp_g722_depay_sink_template);
82
83   gst_element_class_set_details_simple (element_class, "RTP audio depayloader",
84       "Codec/Depayloader/Network/RTP",
85       "Extracts G722 audio from RTP packets",
86       "Wim Taymans <wim.taymans@gmail.com>");
87 }
88
89 static void
90 gst_rtp_g722_depay_class_init (GstRtpG722DepayClass * klass)
91 {
92   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
93
94   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
95
96   gstbasertpdepayload_class->set_caps = gst_rtp_g722_depay_setcaps;
97   gstbasertpdepayload_class->process = gst_rtp_g722_depay_process;
98
99   GST_DEBUG_CATEGORY_INIT (rtpg722depay_debug, "rtpg722depay", 0,
100       "G722 RTP Depayloader");
101 }
102
103 static void
104 gst_rtp_g722_depay_init (GstRtpG722Depay * rtpg722depay,
105     GstRtpG722DepayClass * klass)
106 {
107   /* needed because of GST_BOILERPLATE */
108 }
109
110 static gint
111 gst_rtp_g722_depay_parse_int (GstStructure * structure, const gchar * field,
112     gint def)
113 {
114   const gchar *str;
115   gint res;
116
117   if ((str = gst_structure_get_string (structure, field)))
118     return atoi (str);
119
120   if (gst_structure_get_int (structure, field, &res))
121     return res;
122
123   return def;
124 }
125
126 static gboolean
127 gst_rtp_g722_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
128 {
129   GstStructure *structure;
130   GstRtpG722Depay *rtpg722depay;
131   gint clock_rate, payload, samplerate;
132   gint channels;
133   GstCaps *srccaps;
134   gboolean res;
135   const gchar *channel_order;
136   const GstRTPChannelOrder *order;
137
138   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
139
140   structure = gst_caps_get_structure (caps, 0);
141
142   payload = 96;
143   gst_structure_get_int (structure, "payload", &payload);
144   switch (payload) {
145     case GST_RTP_PAYLOAD_G722:
146       channels = 1;
147       clock_rate = 8000;
148       samplerate = 16000;
149       break;
150     default:
151       /* no fixed mapping, we need clock-rate */
152       channels = 0;
153       clock_rate = 0;
154       samplerate = 0;
155       break;
156   }
157
158   /* caps can overwrite defaults */
159   clock_rate =
160       gst_rtp_g722_depay_parse_int (structure, "clock-rate", clock_rate);
161   if (clock_rate == 0)
162     goto no_clockrate;
163
164   if (clock_rate == 8000)
165     samplerate = 16000;
166
167   if (samplerate == 0)
168     samplerate = clock_rate;
169
170   channels =
171       gst_rtp_g722_depay_parse_int (structure, "encoding-params", channels);
172   if (channels == 0) {
173     channels = gst_rtp_g722_depay_parse_int (structure, "channels", channels);
174     if (channels == 0) {
175       /* channels defaults to 1 otherwise */
176       channels = 1;
177     }
178   }
179
180   depayload->clock_rate = clock_rate;
181   rtpg722depay->rate = samplerate;
182   rtpg722depay->channels = channels;
183
184   srccaps = gst_caps_new_simple ("audio/G722",
185       "rate", G_TYPE_INT, samplerate, "channels", G_TYPE_INT, channels, NULL);
186
187   /* add channel positions */
188   channel_order = gst_structure_get_string (structure, "channel-order");
189
190   order = gst_rtp_channels_get_by_order (channels, channel_order);
191   if (order) {
192     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
193         order->pos);
194   } else {
195     GstAudioChannelPosition *pos;
196
197     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
198         (NULL), ("Unknown channel order '%s' for %d channels",
199             GST_STR_NULL (channel_order), channels));
200     /* create default NONE layout */
201     pos = gst_rtp_channels_create_default (channels);
202     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
203     g_free (pos);
204   }
205
206   res = gst_pad_set_caps (depayload->srcpad, srccaps);
207   gst_caps_unref (srccaps);
208
209   return res;
210
211   /* ERRORS */
212 no_clockrate:
213   {
214     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
215     return FALSE;
216   }
217 }
218
219 static GstBuffer *
220 gst_rtp_g722_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
221 {
222   GstRtpG722Depay *rtpg722depay;
223   GstBuffer *outbuf;
224   gint payload_len;
225   gboolean marker;
226
227   rtpg722depay = GST_RTP_G722_DEPAY (depayload);
228
229   payload_len = gst_rtp_buffer_get_payload_len (buf);
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 (buf);
237   marker = gst_rtp_buffer_get_marker (buf);
238
239   if (marker && outbuf) {
240     /* mark talk spurt with DISCONT */
241     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
242   }
243
244   return outbuf;
245
246   /* ERRORS */
247 empty_packet:
248   {
249     GST_ELEMENT_WARNING (rtpg722depay, STREAM, DECODE,
250         ("Empty Payload."), (NULL));
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 }