Remove unused variables in _class_init
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpilbcpay.c
1 /* GStreamer
2  * Copyright (C) <2006> Philippe Khalaf <burger@speedy.org>
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 <gst/rtp/gstrtpbuffer.h>
26 #include "gstrtpilbcpay.h"
27
28 /* elementfactory information */
29 static GstElementDetails gst_rtpilbcpay_details = {
30   "RTP iLBC Payloader",
31   "Codec/Payloader/Network",
32   "Packetize iLBC audio streams into RTP packets",
33   "Philippe Kalaf <philippe.kalaf@collabora.co.uk>"
34 };
35
36 GST_DEBUG_CATEGORY_STATIC (rtpilbcpay_debug);
37 #define GST_CAT_DEFAULT (rtpilbcpay_debug)
38
39 static GstStaticPadTemplate gst_rtpilbcpay_sink_template =
40 GST_STATIC_PAD_TEMPLATE ("sink",
41     GST_PAD_SINK,
42     GST_PAD_ALWAYS,
43     GST_STATIC_CAPS ("audio/x-iLBC, " "mode = (int) {20, 30}")
44     );
45
46 static GstStaticPadTemplate gst_rtpilbcpay_src_template =
47 GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("application/x-rtp, "
51         "media = (string) \"audio\", "
52         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
53         "clock-rate = (int) 8000, "
54         "encoding-name = (string) \"ILBC\", "
55         "mode = (string) { \"20\", \"30\" }")
56     );
57
58
59 static GstCaps *gst_rtpilbcpay_sink_getcaps (GstBaseRTPPayload * payload,
60     GstPad * pad);
61 static gboolean gst_rtpilbcpay_sink_setcaps (GstBaseRTPPayload * payload,
62     GstCaps * caps);
63
64 GST_BOILERPLATE (GstRTPILBCPay, gst_rtpilbcpay, GstBaseRTPAudioPayload,
65     GST_TYPE_BASE_RTP_AUDIO_PAYLOAD);
66
67 static void
68 gst_rtpilbcpay_base_init (gpointer klass)
69 {
70   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
71
72   gst_element_class_add_pad_template (element_class,
73       gst_static_pad_template_get (&gst_rtpilbcpay_sink_template));
74   gst_element_class_add_pad_template (element_class,
75       gst_static_pad_template_get (&gst_rtpilbcpay_src_template));
76   gst_element_class_set_details (element_class, &gst_rtpilbcpay_details);
77 }
78
79 static void
80 gst_rtpilbcpay_class_init (GstRTPILBCPayClass * klass)
81 {
82   GstBaseRTPPayloadClass *gstbasertppayload_class;
83
84   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
85
86   parent_class = g_type_class_ref (GST_TYPE_BASE_RTP_PAYLOAD);
87
88   gstbasertppayload_class->set_caps = gst_rtpilbcpay_sink_setcaps;
89   gstbasertppayload_class->get_caps = gst_rtpilbcpay_sink_getcaps;
90
91   GST_DEBUG_CATEGORY_INIT (rtpilbcpay_debug, "rtpilbcpay", 0,
92       "iLBC audio RTP payloader");
93 }
94
95 static void
96 gst_rtpilbcpay_init (GstRTPILBCPay * rtpilbcpay, GstRTPILBCPayClass * klass)
97 {
98   GstBaseRTPPayload *basertppayload;
99   GstBaseRTPAudioPayload *basertpaudiopayload;
100
101   basertppayload = GST_BASE_RTP_PAYLOAD (rtpilbcpay);
102   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (rtpilbcpay);
103
104   /* we don't set the payload type, it should be set by the application using
105    * the pt property or the default 96 will be used */
106   basertppayload->clock_rate = 8000;
107
108   rtpilbcpay->mode = -1;
109
110   /* tell basertpaudiopayload that this is a frame based codec */
111   gst_base_rtp_audio_payload_set_frame_based (basertpaudiopayload);
112 }
113
114 static gboolean
115 gst_rtpilbcpay_sink_setcaps (GstBaseRTPPayload * basertppayload, GstCaps * caps)
116 {
117   GstRTPILBCPay *rtpilbcpay;
118   GstBaseRTPAudioPayload *basertpaudiopayload;
119   gboolean ret;
120   gint mode;
121   gchar *mode_str;
122   GstStructure *structure;
123   const char *payload_name;
124
125   rtpilbcpay = GST_RTP_ILBC_PAY (basertppayload);
126   basertpaudiopayload = GST_BASE_RTP_AUDIO_PAYLOAD (basertppayload);
127
128   structure = gst_caps_get_structure (caps, 0);
129
130   payload_name = gst_structure_get_name (structure);
131   if (g_ascii_strcasecmp ("audio/x-iLBC", payload_name))
132     goto wrong_caps;
133
134   if (!gst_structure_get_int (structure, "mode", &mode))
135     goto no_mode;
136
137   if (mode != 20 && mode != 30)
138     goto wrong_mode;
139
140   gst_basertppayload_set_options (basertppayload, "audio", TRUE, "ILBC", 8000);
141   /* set options for this frame based audio codec */
142   gst_base_rtp_audio_payload_set_frame_options (basertpaudiopayload,
143       mode, mode == 30 ? 50 : 38);
144
145   mode_str = g_strdup_printf ("%d", mode);
146   ret =
147       gst_basertppayload_set_outcaps (basertppayload, "mode", G_TYPE_STRING,
148       mode_str, NULL);
149   g_free (mode_str);
150
151   if (mode != rtpilbcpay->mode && rtpilbcpay->mode != -1)
152     goto mode_changed;
153
154   rtpilbcpay->mode = mode;
155
156   return ret;
157
158   /* ERRORS */
159 wrong_caps:
160   {
161     GST_ERROR_OBJECT (rtpilbcpay, "expected audio/x-iLBC, received %s",
162         payload_name);
163     return FALSE;
164   }
165 no_mode:
166   {
167     GST_ERROR_OBJECT (rtpilbcpay, "did not receive a mode");
168     return FALSE;
169   }
170 wrong_mode:
171   {
172     GST_ERROR_OBJECT (rtpilbcpay, "mode must be 20 or 30, received %d", mode);
173     return FALSE;
174   }
175 mode_changed:
176   {
177     GST_ERROR_OBJECT (rtpilbcpay, "Mode has changed from %d to %d! "
178         "Mode cannot change while streaming", rtpilbcpay->mode, mode);
179     return FALSE;
180   }
181 }
182
183 /* we return the padtemplate caps with the mode field fixated to a value if we
184  * can */
185 static GstCaps *
186 gst_rtpilbcpay_sink_getcaps (GstBaseRTPPayload * rtppayload, GstPad * pad)
187 {
188   GstCaps *otherpadcaps;
189   GstCaps *caps;
190
191   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
192   caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
193
194   if (otherpadcaps) {
195     if (!gst_caps_is_empty (otherpadcaps)) {
196       GstStructure *structure;
197       const gchar *mode_str;
198       gint mode;
199
200       structure = gst_caps_get_structure (otherpadcaps, 0);
201
202       /* parse mode, if we can */
203       mode_str = gst_structure_get_string (structure, "mode");
204       if (mode_str) {
205         mode = strtol (mode_str, NULL, 10);
206         if (mode == 20 || mode == 30) {
207           structure = gst_caps_get_structure (caps, 0);
208           gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
209         }
210       }
211     }
212     gst_caps_unref (otherpadcaps);
213   }
214   return caps;
215 }
216
217 gboolean
218 gst_rtp_ilbc_pay_plugin_init (GstPlugin * plugin)
219 {
220   return gst_element_register (plugin, "rtpilbcpay",
221       GST_RANK_NONE, GST_TYPE_RTP_ILBC_PAY);
222 }