Merge branch 'master' into 0.11
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL16pay.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
26 #include <gst/audio/audio.h>
27 #include <gst/audio/multichannel.h>
28 #include <gst/rtp/gstrtpbuffer.h>
29
30 #include "gstrtpL16pay.h"
31 #include "gstrtpchannels.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
34 #define GST_CAT_DEFAULT (rtpL16pay_debug)
35
36 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38     GST_PAD_SINK,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("audio/x-raw, "
41         "format = (string) S16_BE, "
42         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
43     );
44
45 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
46     GST_STATIC_PAD_TEMPLATE ("src",
47     GST_PAD_SRC,
48     GST_PAD_ALWAYS,
49     GST_STATIC_CAPS ("application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) [ 96, 127 ], "
52         "clock-rate = (int) [ 1, MAX ], "
53         "encoding-name = (string) \"L16\", "
54         "channels = (int) [ 1, MAX ];"
55         "application/x-rtp, "
56         "media = (string) \"audio\", "
57         "encoding-name = (string) \"L16\", "
58         "payload = (int) " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
59         "clock-rate = (int) 44100;"
60         "application/x-rtp, "
61         "media = (string) \"audio\", "
62         "encoding-name = (string) \"L16\", "
63         "payload = (int) " GST_RTP_PAYLOAD_L16_MONO_STRING ", "
64         "clock-rate = (int) 44100")
65     );
66
67 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
68     GstCaps * caps);
69 static GstCaps *gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload,
70     GstPad * pad, GstCaps * filter);
71
72 #define gst_rtp_L16_pay_parent_class parent_class
73 G_DEFINE_TYPE (GstRtpL16Pay, gst_rtp_L16_pay, GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
74
75 static void
76 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
77 {
78   GstElementClass *gstelement_class;
79   GstBaseRTPPayloadClass *gstbasertppayload_class;
80
81   gstelement_class = (GstElementClass *) klass;
82   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
83
84   gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
85   gstbasertppayload_class->get_caps = gst_rtp_L16_pay_getcaps;
86
87   gst_element_class_add_pad_template (gstelement_class,
88       gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
89   gst_element_class_add_pad_template (gstelement_class,
90       gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
91
92   gst_element_class_set_details_simple (gstelement_class, "RTP audio payloader",
93       "Codec/Payloader/Network/RTP",
94       "Payload-encode Raw audio into RTP packets (RFC 3551)",
95       "Wim Taymans <wim.taymans@gmail.com>");
96
97   GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
98       "L16 RTP Payloader");
99 }
100
101 static void
102 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
103 {
104   GstBaseRTPAudioPayload *basertpaudiopayload;
105
106   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpL16pay);
107
108   /* tell basertpaudiopayload that this is a sample based codec */
109   gst_base_rtp_audio_payload_set_sample_based (basertpaudiopayload);
110 }
111
112 static gboolean
113 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
114 {
115   GstRtpL16Pay *rtpL16pay;
116   GstStructure *structure;
117   gint channels, rate;
118   gboolean res;
119   gchar *params;
120   GstAudioChannelPosition *pos;
121   const GstRTPChannelOrder *order;
122   GstBaseRTPAudioPayload *basertpaudiopayload;
123
124   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basepayload);
125   rtpL16pay = GST_RTP_L16_PAY (basepayload);
126
127   structure = gst_caps_get_structure (caps, 0);
128
129   /* first parse input caps */
130   if (!gst_structure_get_int (structure, "rate", &rate))
131     goto no_rate;
132
133   if (!gst_structure_get_int (structure, "channels", &channels))
134     goto no_channels;
135
136   /* get the channel order */
137   pos = gst_audio_get_channel_positions (structure);
138   if (pos)
139     order = gst_rtp_channels_get_by_pos (channels, pos);
140   else
141     order = NULL;
142
143   gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
144   params = g_strdup_printf ("%d", channels);
145
146   if (!order && channels > 2) {
147     GST_ELEMENT_WARNING (rtpL16pay, STREAM, DECODE,
148         (NULL), ("Unknown channel order for %d channels", channels));
149   }
150
151   if (order && order->name) {
152     res = gst_basertppayload_set_outcaps (basepayload,
153         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
154         channels, "channel-order", G_TYPE_STRING, order->name, NULL);
155   } else {
156     res = gst_basertppayload_set_outcaps (basepayload,
157         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
158         channels, NULL);
159   }
160
161   g_free (params);
162   g_free (pos);
163
164   rtpL16pay->rate = rate;
165   rtpL16pay->channels = channels;
166
167   /* octet-per-sample is 2 * channels for L16 */
168   gst_base_rtp_audio_payload_set_sample_options (basertpaudiopayload,
169       2 * rtpL16pay->channels);
170
171   return res;
172
173   /* ERRORS */
174 no_rate:
175   {
176     GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
177     return FALSE;
178   }
179 no_channels:
180   {
181     GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
182     return FALSE;
183   }
184 }
185
186 static GstCaps *
187 gst_rtp_L16_pay_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad,
188     GstCaps * filter)
189 {
190   GstCaps *otherpadcaps;
191   GstCaps *caps;
192
193   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
194   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
195
196   if (otherpadcaps) {
197     if (!gst_caps_is_empty (otherpadcaps)) {
198       GstStructure *structure;
199       gint channels;
200       gint pt;
201       gint rate;
202
203       structure = gst_caps_get_structure (otherpadcaps, 0);
204
205       if (gst_structure_get_int (structure, "channels", &channels)) {
206         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
207       } else if (gst_structure_get_int (structure, "payload", &pt)) {
208         if (pt == 10)
209           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
210         else if (pt == 11)
211           gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
212       }
213
214       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
215         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
216       } else if (gst_structure_get_int (structure, "payload", &pt)) {
217         if (pt == 10 || pt == 11)
218           gst_caps_set_simple (caps, "rate", G_TYPE_INT, 44100, NULL);
219       }
220
221     }
222     gst_caps_unref (otherpadcaps);
223   }
224
225   if (filter) {
226     GstCaps *tcaps = caps;
227
228     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
229     gst_caps_unref (tcaps);
230   }
231
232   return caps;
233 }
234
235 gboolean
236 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
237 {
238   return gst_element_register (plugin, "rtpL16pay",
239       GST_RANK_SECONDARY, GST_TYPE_RTP_L16_PAY);
240 }