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