tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpbvpay.c
1 /* GStreamer
2  * Copyright (C) <2009> 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 <stdlib.h>
25 #include <string.h>
26
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpbvpay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpbvpay_debug);
31 #define GST_CAT_DEFAULT (rtpbvpay_debug)
32
33 static GstStaticPadTemplate gst_rtp_bv_pay_sink_template =
34 GST_STATIC_PAD_TEMPLATE ("sink",
35     GST_PAD_SINK,
36     GST_PAD_ALWAYS,
37     GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) {16, 32}")
38     );
39
40 static GstStaticPadTemplate gst_rtp_bv_pay_src_template =
41     GST_STATIC_PAD_TEMPLATE ("src",
42     GST_PAD_SRC,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("application/x-rtp, "
45         "media = (string) \"audio\", "
46         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
47         "clock-rate = (int) 8000, "
48         "encoding-name = (string) \"BV16\";"
49         "application/x-rtp, "
50         "media = (string) \"audio\", "
51         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
52         "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
53     );
54
55
56 static GstCaps *gst_rtp_bv_pay_sink_getcaps (GstBaseRTPPayload * payload,
57     GstPad * pad);
58 static gboolean gst_rtp_bv_pay_sink_setcaps (GstBaseRTPPayload * payload,
59     GstCaps * caps);
60
61 GST_BOILERPLATE (GstRTPBVPay, gst_rtp_bv_pay, GstBaseRTPAudioPayload,
62     GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
63
64 static void
65 gst_rtp_bv_pay_base_init (gpointer klass)
66 {
67   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
68
69   gst_element_class_add_static_pad_template (element_class,
70       &gst_rtp_bv_pay_sink_template);
71   gst_element_class_add_static_pad_template (element_class,
72       &gst_rtp_bv_pay_src_template);
73   gst_element_class_set_details_simple (element_class, "RTP BV Payloader",
74       "Codec/Payloader/Network/RTP",
75       "Packetize BroadcomVoice audio streams into RTP packets (RFC 4298)",
76       "Wim Taymans <wim.taymans@collabora.co.uk>");
77 }
78
79 static void
80 gst_rtp_bv_pay_class_init (GstRTPBVPayClass * klass)
81 {
82   GstBaseRTPPayloadClass *gstbasertppayload_class;
83
84   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
85
86   gstbasertppayload_class->set_caps = gst_rtp_bv_pay_sink_setcaps;
87   gstbasertppayload_class->get_caps = gst_rtp_bv_pay_sink_getcaps;
88
89   GST_DEBUG_CATEGORY_INIT (rtpbvpay_debug, "rtpbvpay", 0,
90       "BroadcomVoice audio RTP payloader");
91 }
92
93 static void
94 gst_rtp_bv_pay_init (GstRTPBVPay * rtpbvpay, GstRTPBVPayClass * klass)
95 {
96   GstBaseRTPAudioPayload *basertpaudiopayload;
97
98   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpbvpay);
99
100   rtpbvpay->mode = -1;
101
102   /* tell basertpaudiopayload that this is a frame based codec */
103   gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
104 }
105
106 static gboolean
107 gst_rtp_bv_pay_sink_setcaps (GstBaseRTPPayload * basertppayload, GstCaps * caps)
108 {
109   GstRTPBVPay *rtpbvpay;
110   GstBaseRTPAudioPayload *basertpaudiopayload;
111   gint mode;
112   GstStructure *structure;
113   const char *payload_name;
114
115   rtpbvpay = GST_RTP_BV_PAY (basertppayload);
116   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
117
118   structure = gst_caps_get_structure (caps, 0);
119
120   payload_name = gst_structure_get_name (structure);
121   if (g_ascii_strcasecmp ("audio/x-bv", payload_name))
122     goto wrong_caps;
123
124   if (!gst_structure_get_int (structure, "mode", &mode))
125     goto no_mode;
126
127   if (mode != 16 && mode != 32)
128     goto wrong_mode;
129
130   if (mode == 16) {
131     gst_basertppayload_set_options (basertppayload, "audio", TRUE, "BV16",
132         8000);
133     basertppayload->clock_rate = 8000;
134   } else {
135     gst_basertppayload_set_options (basertppayload, "audio", TRUE, "BV32",
136         16000);
137     basertppayload->clock_rate = 16000;
138   }
139
140   /* set options for this frame based audio codec */
141   gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload,
142       mode, mode == 16 ? 10 : 20);
143
144   if (mode != rtpbvpay->mode && rtpbvpay->mode != -1)
145     goto mode_changed;
146
147   rtpbvpay->mode = mode;
148
149   return TRUE;
150
151   /* ERRORS */
152 wrong_caps:
153   {
154     GST_ERROR_OBJECT (rtpbvpay, "expected audio/x-bv, received %s",
155         payload_name);
156     return FALSE;
157   }
158 no_mode:
159   {
160     GST_ERROR_OBJECT (rtpbvpay, "did not receive a mode");
161     return FALSE;
162   }
163 wrong_mode:
164   {
165     GST_ERROR_OBJECT (rtpbvpay, "mode must be 16 or 32, received %d", mode);
166     return FALSE;
167   }
168 mode_changed:
169   {
170     GST_ERROR_OBJECT (rtpbvpay, "Mode has changed from %d to %d! "
171         "Mode cannot change while streaming", rtpbvpay->mode, mode);
172     return FALSE;
173   }
174 }
175
176 /* we return the padtemplate caps with the mode field fixated to a value if we
177  * can */
178 static GstCaps *
179 gst_rtp_bv_pay_sink_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
180 {
181   GstCaps *otherpadcaps;
182   GstCaps *caps;
183
184   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
185   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
186
187   if (otherpadcaps) {
188     if (!gst_caps_is_empty (otherpadcaps)) {
189       GstStructure *structure;
190       const gchar *mode_str;
191       gint mode;
192
193       structure = gst_caps_get_structure (otherpadcaps, 0);
194
195       /* construct mode, if we can */
196       mode_str = gst_structure_get_string (structure, "encoding-name");
197       if (mode_str) {
198         if (!strcmp (mode_str, "BV16"))
199           mode = 16;
200         else if (!strcmp (mode_str, "BV32"))
201           mode = 32;
202         else
203           mode = -1;
204
205         if (mode == 16 || mode == 32) {
206           structure = gst_caps_get_structure (caps, 0);
207           gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
208         }
209       }
210     }
211     gst_caps_unref (otherpadcaps);
212   }
213   return caps;
214 }
215
216 gboolean
217 gst_rtp_bv_pay_plugin_init (GstPlugin * plugin)
218 {
219   return gst_element_register (plugin, "rtpbvpay",
220       GST_RANK_SECONDARY, GST_TYPE_RTP_BV_PAY);
221 }