2 * Copyright (C) <2007> Wim Taymans <wim@fluendo.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpL16pay.h"
30 GST_DEBUG_CATEGORY_STATIC (rtpL16pay_debug);
31 #define GST_CAT_DEFAULT (rtpL16pay_debug)
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>");
40 static GstStaticPadTemplate gst_rtp_L16_pay_sink_template =
41 GST_STATIC_PAD_TEMPLATE ("sink",
44 GST_STATIC_CAPS ("audio/x-raw-int, "
45 "endianness = (int) BIG_ENDIAN, "
46 "signed = (boolean) true, "
49 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
52 static GstStaticPadTemplate gst_rtp_L16_pay_src_template =
53 GST_STATIC_PAD_TEMPLATE ("src",
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 ];"
64 "media = (string) \"audio\", "
65 "payload = (int) { " GST_RTP_PAYLOAD_L16_STEREO_STRING ", "
66 GST_RTP_PAYLOAD_L16_MONO_STRING " }," "clock-rate = (int) 44100")
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);
74 static gboolean gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload,
76 static GstFlowReturn gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * pad,
79 static GstBaseRTPPayloadClass *parent_class = NULL;
82 gst_rtp_L16_pay_get_type (void)
84 static GType rtpL16pay_type = 0;
86 if (!rtpL16pay_type) {
87 static const GTypeInfo rtpL16pay_info = {
88 sizeof (GstRtpL16PayClass),
89 (GBaseInitFunc) gst_rtp_L16_pay_base_init,
91 (GClassInitFunc) gst_rtp_L16_pay_class_init,
94 sizeof (GstRtpL16Pay),
96 (GInstanceInitFunc) gst_rtp_L16_pay_init,
100 g_type_register_static (GST_TYPE_BASE_RTP_PAYLOAD, "GstRtpL16Pay",
103 return rtpL16pay_type;
107 gst_rtp_L16_pay_base_init (GstRtpL16PayClass * klass)
109 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
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));
116 gst_element_class_set_details (element_class, &gst_rtp_L16_pay_details);
120 gst_rtp_L16_pay_class_init (GstRtpL16PayClass * klass)
122 GObjectClass *gobject_class;
123 GstElementClass *gstelement_class;
124 GstBaseRTPPayloadClass *gstbasertppayload_class;
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
128 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
130 parent_class = g_type_class_peek_parent (klass);
132 gobject_class->finalize = gst_rtp_L16_pay_finalize;
134 gstbasertppayload_class->set_caps = gst_rtp_L16_pay_setcaps;
135 gstbasertppayload_class->handle_buffer = gst_rtp_L16_pay_handle_buffer;
137 GST_DEBUG_CATEGORY_INIT (rtpL16pay_debug, "rtpL16pay", 0,
138 "L16 RTP Payloader");
142 gst_rtp_L16_pay_init (GstRtpL16Pay * rtpL16pay)
144 rtpL16pay->adapter = gst_adapter_new ();
148 gst_rtp_L16_pay_finalize (GObject * object)
150 GstRtpL16Pay *rtpL16pay;
152 rtpL16pay = GST_RTP_L16_PAY (object);
154 g_object_unref (rtpL16pay->adapter);
155 rtpL16pay->adapter = NULL;
157 G_OBJECT_CLASS (parent_class)->finalize (object);
161 gst_rtp_L16_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
163 GstRtpL16Pay *rtpL16pay;
164 GstStructure *structure;
167 rtpL16pay = GST_RTP_L16_PAY (basepayload);
169 structure = gst_caps_get_structure (caps, 0);
171 /* first parse input caps */
172 if (!gst_structure_get_int (structure, "rate", &rate))
175 if (!gst_structure_get_int (structure, "channels", &channels))
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);
182 rtpL16pay->rate = rate;
183 rtpL16pay->channels = channels;
190 GST_DEBUG_OBJECT (rtpL16pay, "no rate given");
195 GST_DEBUG_OBJECT (rtpL16pay, "no channels given");
201 gst_rtp_L16_pay_flush (GstRtpL16Pay * rtpL16pay, guint len)
207 GstClockTime duration;
209 /* now alloc output buffer */
210 outbuf = gst_rtp_buffer_new_allocate (len, 0, 0);
212 /* get payload, this is now writable */
213 payload = gst_rtp_buffer_get_payload (outbuf);
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);
219 samples = len / (2 * rtpL16pay->channels);
220 duration = gst_util_uint64_scale_int (samples, GST_SECOND, rtpL16pay->rate);
222 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16pay->first_ts;
223 GST_BUFFER_DURATION (outbuf) = duration;
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;
229 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpL16pay), outbuf);
235 gst_rtp_L16_pay_handle_buffer (GstBaseRTPPayload * basepayload,
238 GstRtpL16Pay *rtpL16pay;
239 GstFlowReturn ret = GST_FLOW_OK;
241 GstClockTime timestamp;
244 rtpL16pay = GST_RTP_L16_PAY (basepayload);
245 mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpL16pay);
247 timestamp = GST_BUFFER_TIMESTAMP (buffer);
249 if (GST_BUFFER_IS_DISCONT (buffer))
250 gst_adapter_clear (rtpL16pay->adapter);
252 avail = gst_adapter_available (rtpL16pay->adapter);
254 rtpL16pay->first_ts = timestamp;
257 /* push buffer in adapter */
258 gst_adapter_push (rtpL16pay->adapter, buffer);
260 /* get payload len for MTU */
261 payload_len = gst_rtp_buffer_calc_payload_len (mtu, 0, 0);
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)
270 avail = gst_adapter_available (rtpL16pay->adapter);
276 gst_rtp_L16_pay_plugin_init (GstPlugin * plugin)
278 return gst_element_register (plugin, "rtpL16pay",
279 GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);