2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
24 #include "gstrtpL16enc.h"
26 /* elementfactory information */
27 static GstElementDetails gst_rtpL16enc_details = {
28 "RTP RAW Audio Encoder",
29 "Codec/Encoder/Network",
30 "Encodes Raw Audio into an RTP packet",
31 "Zeeshan Ali <zak147@yahoo.com>"
34 /* RtpL16Enc signals and args */
47 static GstStaticPadTemplate gst_rtpL16enc_sink_template =
48 GST_STATIC_PAD_TEMPLATE (
52 GST_STATIC_CAPS ( "audio/x-raw-int, "
53 "endianness = (int) BYTE_ORDER, "
54 "signed = (boolean) true, "
57 "rate = (int) [ 1000, 48000 ], "
58 "channels = (int) [ 1, 2 ]"
62 static GstStaticPadTemplate gst_rtpL16enc_src_template =
63 GST_STATIC_PAD_TEMPLATE (
67 GST_STATIC_CAPS ("application/x-rtp")
70 static void gst_rtpL16enc_class_init (GstRtpL16EncClass * klass);
71 static void gst_rtpL16enc_base_init (GstRtpL16EncClass * klass);
72 static void gst_rtpL16enc_init (GstRtpL16Enc * rtpL16enc);
73 static void gst_rtpL16enc_chain (GstPad * pad, GstData *_data);
74 static void gst_rtpL16enc_set_property (GObject * object, guint prop_id,
75 const GValue * value, GParamSpec * pspec);
76 static void gst_rtpL16enc_get_property (GObject * object, guint prop_id,
77 GValue * value, GParamSpec * pspec);
78 static GstPadLinkReturn gst_rtpL16enc_sinkconnect (GstPad * pad, const GstCaps * caps);
79 static GstElementStateReturn gst_rtpL16enc_change_state (GstElement * element);
81 static GstElementClass *parent_class = NULL;
83 static GType gst_rtpL16enc_get_type (void)
85 static GType rtpL16enc_type = 0;
87 if (!rtpL16enc_type) {
88 static const GTypeInfo rtpL16enc_info = {
89 sizeof (GstRtpL16EncClass),
90 (GBaseInitFunc) gst_rtpL16enc_base_init,
92 (GClassInitFunc) gst_rtpL16enc_class_init,
95 sizeof (GstRtpL16Enc),
97 (GInstanceInitFunc) gst_rtpL16enc_init,
100 rtpL16enc_type = g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Enc", &rtpL16enc_info, 0);
102 return rtpL16enc_type;
106 gst_rtpL16enc_base_init (GstRtpL16EncClass * klass)
108 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&gst_rtpL16enc_sink_template));
112 gst_element_class_add_pad_template (element_class,
113 gst_static_pad_template_get (&gst_rtpL16enc_src_template));
114 gst_element_class_set_details (element_class, &gst_rtpL16enc_details);
118 gst_rtpL16enc_class_init (GstRtpL16EncClass * klass)
120 GObjectClass *gobject_class;
121 GstElementClass *gstelement_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
126 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
128 gobject_class->set_property = gst_rtpL16enc_set_property;
129 gobject_class->get_property = gst_rtpL16enc_get_property;
131 gstelement_class->change_state = gst_rtpL16enc_change_state;
135 gst_rtpL16enc_init (GstRtpL16Enc * rtpL16enc)
137 rtpL16enc->sinkpad = gst_pad_new_from_template (
138 gst_static_pad_template_get (&gst_rtpL16enc_sink_template), "sink");
139 rtpL16enc->srcpad = gst_pad_new_from_template (
140 gst_static_pad_template_get (&gst_rtpL16enc_src_template), "src");
141 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->sinkpad);
142 gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->srcpad);
143 gst_pad_set_chain_function (rtpL16enc->sinkpad, gst_rtpL16enc_chain);
144 gst_pad_set_link_function (rtpL16enc->sinkpad, gst_rtpL16enc_sinkconnect);
146 rtpL16enc->frequency = 44100;
147 rtpL16enc->channels = 2;
149 rtpL16enc->next_time = 0;
150 rtpL16enc->time_interval = 0;
153 rtpL16enc->ssrc = random ();
156 static GstPadLinkReturn
157 gst_rtpL16enc_sinkconnect (GstPad * pad, const GstCaps * caps)
159 GstRtpL16Enc *rtpL16enc;
160 GstStructure *structure;
163 rtpL16enc = GST_RTP_L16_ENC (gst_pad_get_parent (pad));
165 structure = gst_caps_get_structure (caps, 0);
167 ret = gst_structure_get_int (structure, "rate", &rtpL16enc->frequency);
168 ret &= gst_structure_get_int (structure, "channels", &rtpL16enc->channels);
170 if (!ret) return GST_PAD_LINK_REFUSED;
172 /* Pre-calculate what we can */
173 rtpL16enc->time_interval = GST_SECOND / (2 * rtpL16enc->channels * rtpL16enc->frequency);
175 return GST_PAD_LINK_OK;
180 gst_rtpL16enc_htons (GstBuffer *buf)
184 /* FIXME: is this code correct or even sane at all? */
185 i = (gint16 *) GST_BUFFER_DATA(buf);
186 len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
194 gst_rtpL16enc_chain (GstPad * pad, GstData *_data)
196 GstBuffer *buf = GST_BUFFER (_data);
197 GstRtpL16Enc *rtpL16enc;
201 g_return_if_fail (pad != NULL);
202 g_return_if_fail (GST_IS_PAD (pad));
203 g_return_if_fail (buf != NULL);
205 rtpL16enc = GST_RTP_L16_ENC (GST_OBJECT_PARENT (pad));
207 g_return_if_fail (rtpL16enc != NULL);
208 g_return_if_fail (GST_IS_RTP_L16_ENC (rtpL16enc));
210 if (GST_IS_EVENT (buf)) {
211 GstEvent *event = GST_EVENT (buf);
213 switch (GST_EVENT_TYPE (event)) {
214 case GST_EVENT_DISCONTINUOUS:
215 GST_DEBUG ("discont");
216 rtpL16enc->next_time = 0;
217 gst_pad_event_default (pad, event);
220 gst_pad_event_default (pad, event);
225 /* We only need the header */
226 packet = rtp_packet_new_allocate (0, 0, 0);
228 rtp_packet_set_csrc_count (packet, 0);
229 rtp_packet_set_extension (packet, 0);
230 rtp_packet_set_padding (packet, 0);
231 rtp_packet_set_version (packet, RTP_VERSION);
232 rtp_packet_set_marker (packet, 0);
233 rtp_packet_set_ssrc (packet, g_htonl (rtpL16enc->ssrc));
234 rtp_packet_set_seq (packet, g_htons (rtpL16enc->seq));
235 rtp_packet_set_timestamp (packet, g_htonl ((guint32) rtpL16enc->next_time / GST_SECOND));
237 if (rtpL16enc->channels == 1) {
238 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_MONO);
242 rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_STEREO);
245 /* FIXME: According to RFC 1890, this is required, right? */
246 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
247 gst_rtpL16enc_htons (buf);
250 outbuf = gst_buffer_new ();
251 GST_BUFFER_SIZE (outbuf) = rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
252 GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
253 GST_BUFFER_TIMESTAMP (outbuf) = rtpL16enc->next_time;
255 memcpy (GST_BUFFER_DATA (outbuf), packet->data, rtp_packet_get_packet_len (packet));
256 memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len(packet), GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
258 GST_DEBUG ("gst_rtpL16enc_chain: pushing buffer of size %d", GST_BUFFER_SIZE(outbuf));
259 gst_pad_push (rtpL16enc->srcpad, GST_DATA (outbuf));
262 rtpL16enc->next_time += rtpL16enc->time_interval * GST_BUFFER_SIZE (buf);
264 rtp_packet_free (packet);
265 gst_buffer_unref (buf);
269 gst_rtpL16enc_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
271 GstRtpL16Enc *rtpL16enc;
273 /* it's not null if we got it, but it might not be ours */
274 g_return_if_fail (GST_IS_RTP_L16_ENC (object));
275 rtpL16enc = GST_RTP_L16_ENC (object);
284 gst_rtpL16enc_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
286 GstRtpL16Enc *rtpL16enc;
288 /* it's not null if we got it, but it might not be ours */
289 g_return_if_fail (GST_IS_RTP_L16_ENC (object));
290 rtpL16enc = GST_RTP_L16_ENC (object);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299 static GstElementStateReturn
300 gst_rtpL16enc_change_state (GstElement * element)
302 GstRtpL16Enc *rtpL16enc;
304 g_return_val_if_fail (GST_IS_RTP_L16_ENC (element), GST_STATE_FAILURE);
306 rtpL16enc = GST_RTP_L16_ENC (element);
308 GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
310 /* if going down into NULL state, close the file if it's open */
311 switch (GST_STATE_TRANSITION (element)) {
312 case GST_STATE_NULL_TO_READY:
315 case GST_STATE_READY_TO_NULL:
322 /* if we haven't failed already, give the parent class a chance to ;-) */
323 if (GST_ELEMENT_CLASS (parent_class)->change_state)
324 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
326 return GST_STATE_SUCCESS;
330 gst_rtpL16enc_plugin_init (GstPlugin * plugin)
332 return gst_element_register (plugin, "rtpL16enc",
333 GST_RANK_NONE, GST_TYPE_RTP_L16_ENC);