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