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