gst/rtp/gstrtpL16pay.c: Removed some unused code.
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL16pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim@fluendo.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/rtp/gstrtpbuffer.h>
27
28 #include "gstrtpL16pay.h"
29
30 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
31 #define GST_CAT_DEFAULT (rtpL16pay_debug)
32
33 /* elementfactory information */
34 static const GstElementDetails gst_rtp_L16_pay_details =
35 GST_ELEMENT_DETAILS ("RTP packet payloader",
36     "Codec/Payloader/Network",
37     "Payload-encode Raw audio into RTP packets (RFC 3551)",
38     "Wim Taymans <wim@fluendo.com>");
39
40 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
42     GST_PAD_SINK,
43     GST_PAD_ALWAYS,
44     GST_STATIC_CAPS ("audio/x-raw-int, "
45         "endianness = (int) BIG_ENDIAN, "
46         "signed = (boolean) true, "
47         "width = (int) 16, "
48         "depth = (int) 16, "
49         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
50     );
51
52 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
53     GST_STATIC_PAD_TEMPLATE ("src",
54     GST_PAD_SRC,
55     GST_PAD_ALWAYS,
56     GST_STATIC_CAPS ("application/x-rtp, "
57         "media = (string) \"audio\", "
58         "payload = (int) [ 96, 127 ], "
59         "clock-rate = (int) [ 1, MAX ], "
60         "encoding-name = (string) \"L16\", "
61         "channels = (int) [ 1, MAX ], "
62         "rate = (int) [ 1, MAX ];"
63         "application/x-rtp, "
64         "media = (string) \"audio\", "
65         "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
66         GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) 44100")
67     );
68
69 static void gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass);
70 static void gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass);
71 static void gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay);
72 static void gst_rtp_L16_pay_finalize (GObject * object);
73
74 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
75     GstCaps * caps);
76 static GstFlowReturn gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * pad,
77     GstBuffer * buffer);
78
79 static GstBaseRTPPayloadClass *parent_class = NULL;
80
81 static GType
82 gst_rtp_L16_pay_get_type (void)
83 {
84   static GType rtpL16pay_type = 0;
85
86   if (!rtpL16pay_type) {
87     static const GTypeInfo rtpL16pay_info = {
88       sizeof (GstRtpL16PayClass),
89       (GBaseInitFunc) gst_rtp_L16_pay_base_init,
90       NULL,
91       (GClassInitFunc) gst_rtp_L16_pay_class_init,
92       NULL,
93       NULL,
94       sizeof (GstRtpL16Pay),
95       0,
96       (GInstanceInitFunc) gst_rtp_L16_pay_init,
97     };
98
99     rtpL16pay_type =
100         g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpL16Pay",
101         &rtpL16pay_info, 0);
102   }
103   return rtpL16pay_type;
104 }
105
106 static void
107 gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass)
108 {
109   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
110
111   gst_element_class_add_pad_template (element_class,
112       gst_static_pad_template_get (&gst_rtp_L16_pay_src_template));
113   gst_element_class_add_pad_template (element_class,
114       gst_static_pad_template_get (&gst_rtp_L16_pay_sink_template));
115
116   gst_element_class_set_details (element_class, &gst_rtp_L16_pay_details);
117 }
118
119 static void
120 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
121 {
122   GObjectClass *gobject_class;
123   GstElementClass *gstelement_class;
124   GstBaseRTPPayloadClass *gstbasertppayload_class;
125
126   gobject_class = (GObjectClass *) klass;
127   gstelement_class = (GstElementClass *) klass;
128   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
129
130   parent_class = g_type_class_peek_parent (klass);
131
132   gobject_class->finalize = gst_rtp_L16_pay_finalize;
133
134   gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
135   gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
136
137   GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
138       "L16 RTP Payloader");
139 }
140
141 static void
142 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
143 {
144   rtpL16pay->adapter = gst_adapter_new ();
145 }
146
147 static void
148 gst_rtp_L16_pay_finalize (GObject * object)
149 {
150   GstRtpL16Pay *rtpL16pay;
151
152   rtpL16pay = GST_RTP_L16_PAY (object);
153
154   g_object_unref (rtpL16pay->adapter);
155   rtpL16pay->adapter = NULL;
156
157   G_OBJECT_CLASS (parent_class)->finalize (object);
158 }
159
160 static gboolean
161 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
162 {
163   GstRtpL16Pay *rtpL16pay;
164   GstStructure *structure;
165   gint channels, rate;
166
167   rtpL16pay = GST_RTP_L16_PAY (basepayload);
168
169   structure = gst_caps_get_structure (caps, 0);
170
171   /* first parse input caps */
172   if (!gst_structure_get_int (structure, "rate", &rate))
173     goto no_rate;
174
175   if (!gst_structure_get_int (structure, "channels", &channels))
176     goto no_channels;
177
178   gst_basertppayload_set_options (basepayload, "audio", TRUE, "L16", rate);
179   gst_basertppayload_set_outcaps (basepayload,
180       "channels", G_TYPE_INT, channels, "rate", G_TYPE_INT, rate, NULL);
181
182   rtpL16pay->rate = rate;
183   rtpL16pay->channels = channels;
184
185   return TRUE;
186
187   /* ERRORS */
188 no_rate:
189   {
190     GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
191     return FALSE;
192   }
193 no_channels:
194   {
195     GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
196     return FALSE;
197   }
198 }
199
200 static GstFlowReturn
201 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
202 {
203   GstBuffer *outbuf;
204   guint8 *payload;
205   GstFlowReturn ret;
206   guint samples;
207   GstClockTime duration;
208
209   /* now alloc output buffer */
210   outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
211
212   /* get payload, this is now writable */
213   payload = gst_rtp_buffer_get_payload (outbuf);
214
215   /* copy and flush data out of adapter into the RTP payload */
216   gst_adapter_copy (rtpL16pay->adapter, payload, 0, len);
217   gst_adapter_flush (rtpL16pay->adapter, len);
218
219   samples = len / (2 * rtpL16pay->channels);
220   duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
221
222   GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
223   GST_BUFFER_DURATION (outbuf) = duration;
224
225   /* increase count (in ts) of data pushed to basertppayload */
226   if (GST_CLOCK_TIME_IS_VALID (rtpL16pay->first_ts))
227     rtpL16pay->first_ts += duration;
228
229   ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
230
231   return ret;
232 }
233
234 static GstFlowReturn
235 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
236     GstBuffer * buffer)
237 {
238   GstRtpL16Pay *rtpL16pay;
239   GstFlowReturn ret = GST_FLOW_OK;
240   guint payload_len;
241   GstClockTime timestamp;
242   guint mtu, avail;
243
244   rtpL16pay = GST_RTP_L16_PAY (basepayload);
245   mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
246
247   timestamp = GST_BUFFER_TIMESTAMP (buffer);
248
249   if (GST_BUFFER_IS_DISCONT (buffer))
250     gst_adapter_clear (rtpL16pay->adapter);
251
252   avail = gst_adapter_available (rtpL16pay->adapter);
253   if (avail == 0) {
254     rtpL16pay->first_ts = timestamp;
255   }
256
257   /* push buffer in adapter */
258   gst_adapter_push (rtpL16pay->adapter, buffer);
259
260   /* get payload len for MTU */
261   payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
262
263   /* flush complete MTU while we have enough data in the adapter */
264   while (avail >= payload_len) {
265     /* flush payload_len bytes */
266     ret = gst_rtp_L16_pay_flush (rtpL16pay, payload_len);
267     if (ret != GST_FLOW_OK)
268       break;
269
270     avail = gst_adapter_available (rtpL16pay->adapter);
271   }
272   return ret;
273 }
274
275 gboolean
276 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
277 {
278   return gst_element_register (plugin, "rtpL16pay",
279       GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);
280 }