Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpL16depay.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 "gstrtpL16depay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpL16depay_debug);
34 #define GST_CAT_DEFAULT (rtpL16depay_debug)
35
36 static GstStaticPadTemplate gst_rtp_L16_depay_src_template =
37 GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-raw-int, "
41         "endianness = (int) BIG_ENDIAN, "
42         "signed = (boolean) true, "
43         "width = (int) 16, "
44         "depth = (int) 16, "
45         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
46     );
47
48 static GstStaticPadTemplate gst_rtp_L16_depay_sink_template =
49     GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("application/x-rtp, "
53         "media = (string) \"audio\", "
54         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55         "clock-rate = (int) [ 1, MAX ], "
56         /* "channels = (int) [1, MAX]"  */
57         /* "emphasis = (string) ANY" */
58         /* "channel-order = (string) ANY" */
59         "encoding-name = (string) \"L16\";"
60         "application/x-rtp, "
61         "media = (string) \"audio\", "
62         "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
63         GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) [ 1, MAX ]"
64         /* "channels = (int) [1, MAX]" */
65         /* "emphasis = (string) ANY" */
66         /* "channel-order = (string) ANY" */
67     )
68     );
69
70 GST_BOILERPLATE (GstRtpL16Depay, gst_rtp_L16_depay, GstBaseRTPDepayload,
71     GST_TYPE_BASE_RTP_DEPAYLOAD);
72
73 static gboolean gst_rtp_L16_depay_setcaps (GstBaseRTPDepayload * depayload,
74     GstCaps * caps);
75 static GstBuffer *gst_rtp_L16_depay_process (GstBaseRTPDepayload * depayload,
76     GstBuffer * buf);
77
78 static void
79 gst_rtp_L16_depay_base_init (gpointer klass)
80 {
81   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
82
83   gst_element_class_add_static_pad_template (element_class,
84       &gst_rtp_L16_depay_src_template);
85   gst_element_class_add_static_pad_template (element_class,
86       &gst_rtp_L16_depay_sink_template);
87
88   gst_element_class_set_details_simple (element_class, "RTP audio depayloader",
89       "Codec/Depayloader/Network/RTP",
90       "Extracts raw audio from RTP packets",
91       "Zeeshan Ali <zak147@yahoo.com>," "Wim Taymans <wim.taymans@gmail.com>");
92 }
93
94 static void
95 gst_rtp_L16_depay_class_init (GstRtpL16DepayClass * klass)
96 {
97   GstBaseRTPDepayloadClass *gstbasertpdepayload_class;
98
99   gstbasertpdepayload_class = (GstBaseRTPDepayloadClass *) klass;
100
101   gstbasertpdepayload_class->set_caps = gst_rtp_L16_depay_setcaps;
102   gstbasertpdepayload_class->process = gst_rtp_L16_depay_process;
103
104   GST_DEBUG_CATEGORY_INIT (rtpL16depay_debug, "rtpL16depay", 0,
105       "Raw Audio RTP Depayloader");
106 }
107
108 static void
109 gst_rtp_L16_depay_init (GstRtpL16Depay * rtpL16depay,
110     GstRtpL16DepayClass * klass)
111 {
112   /* needed because of GST_BOILERPLATE */
113 }
114
115 static gint
116 gst_rtp_L16_depay_parse_int (GstStructure * structure, const gchar * field,
117     gint def)
118 {
119   const gchar *str;
120   gint res;
121
122   if ((str = gst_structure_get_string (structure, field)))
123     return atoi (str);
124
125   if (gst_structure_get_int (structure, field, &res))
126     return res;
127
128   return def;
129 }
130
131 static gboolean
132 gst_rtp_L16_depay_setcaps (GstBaseRTPDepayload * depayload, GstCaps * caps)
133 {
134   GstStructure *structure;
135   GstRtpL16Depay *rtpL16depay;
136   gint clock_rate, payload;
137   gint channels;
138   GstCaps *srccaps;
139   gboolean res;
140   const gchar *channel_order;
141   const GstRTPChannelOrder *order;
142
143   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
144
145   structure = gst_caps_get_structure (caps, 0);
146
147   payload = 96;
148   gst_structure_get_int (structure, "payload", &payload);
149   switch (payload) {
150     case GST_RTP_PAYLOAD_L16_STEREO:
151       channels = 2;
152       clock_rate = 44100;
153       break;
154     case GST_RTP_PAYLOAD_L16_MONO:
155       channels = 1;
156       clock_rate = 44100;
157       break;
158     default:
159       /* no fixed mapping, we need clock-rate */
160       channels = 0;
161       clock_rate = 0;
162       break;
163   }
164
165   /* caps can overwrite defaults */
166   clock_rate =
167       gst_rtp_L16_depay_parse_int (structure, "clock-rate", clock_rate);
168   if (clock_rate == 0)
169     goto no_clockrate;
170
171   channels =
172       gst_rtp_L16_depay_parse_int (structure, "encoding-params", channels);
173   if (channels == 0) {
174     channels = gst_rtp_L16_depay_parse_int (structure, "channels", channels);
175     if (channels == 0) {
176       /* channels defaults to 1 otherwise */
177       channels = 1;
178     }
179   }
180
181   depayload->clock_rate = clock_rate;
182   rtpL16depay->rate = clock_rate;
183   rtpL16depay->channels = channels;
184
185   srccaps = gst_caps_new_simple ("audio/x-raw-int",
186       "endianness", G_TYPE_INT, G_BIG_ENDIAN,
187       "signed", G_TYPE_BOOLEAN, TRUE,
188       "width", G_TYPE_INT, 16,
189       "depth", G_TYPE_INT, 16,
190       "rate", G_TYPE_INT, clock_rate, "channels", G_TYPE_INT, channels, NULL);
191
192   /* add channel positions */
193   channel_order = gst_structure_get_string (structure, "channel-order");
194
195   order = gst_rtp_channels_get_by_order (channels, channel_order);
196   if (order) {
197     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0),
198         order->pos);
199   } else {
200     GstAudioChannelPosition *pos;
201
202     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
203         (NULL), ("Unknown channel order '%s' for %d channels",
204             GST_STR_NULL (channel_order), channels));
205     /* create default NONE layout */
206     pos = gst_rtp_channels_create_default (channels);
207     gst_audio_set_channel_positions (gst_caps_get_structure (srccaps, 0), pos);
208     g_free (pos);
209   }
210
211   res = gst_pad_set_caps (depayload->srcpad, srccaps);
212   gst_caps_unref (srccaps);
213
214   return res;
215
216   /* ERRORS */
217 no_clockrate:
218   {
219     GST_ERROR_OBJECT (depayload, "no clock-rate specified");
220     return FALSE;
221   }
222 }
223
224 static GstBuffer *
225 gst_rtp_L16_depay_process (GstBaseRTPDepayload * depayload, GstBuffer * buf)
226 {
227   GstRtpL16Depay *rtpL16depay;
228   GstBuffer *outbuf;
229   gint payload_len;
230   gboolean marker;
231
232   rtpL16depay = GST_RTP_L16_DEPAY (depayload);
233
234   payload_len = gst_rtp_buffer_get_payload_len (buf);
235
236   if (payload_len <= 0)
237     goto empty_packet;
238
239   GST_DEBUG_OBJECT (rtpL16depay, "got payload of %d bytes", payload_len);
240
241   outbuf = gst_rtp_buffer_get_payload_buffer (buf);
242   marker = gst_rtp_buffer_get_marker (buf);
243
244   if (marker) {
245     /* mark talk spurt with DISCONT */
246     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT);
247   }
248
249   return outbuf;
250
251   /* ERRORS */
252 empty_packet:
253   {
254     GST_ELEMENT_WARNING (rtpL16depay, STREAM, DECODE,
255         ("Empty Payload."), (NULL));
256     return NULL;
257   }
258 }
259
260 gboolean
261 gst_rtp_L16_depay_plugin_init (GstPlugin * plugin)
262 {
263   return gst_element_register (plugin, "rtpL16depay",
264       GST_RANK_SECONDARY, GST_TYPE_RTP_L16_DEPAY);
265 }