close #333784 unref the result of gst_pad_get_parent() by: Christophe Fergeau.
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpL16pay.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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 #include <string.h>
24 #include <stdlib.h>
25 #include "gstrtpL16pay.h"
26
27 /* elementfactory information */
28 static GstElementDetails gst_rtpL16pay_details = {
29   "RTP RAW Audio Payloader",
30   "Codec/Payloader/Network",
31   "Payload-encodes Raw Audio into a RTP packet",
32   "Zeeshan Ali <zak147@yahoo.com>"
33 };
34
35 /* RtpL16Pay signals and args */
36 enum
37 {
38   /* FILL ME */
39   LAST_SIGNAL
40 };
41
42 enum
43 {
44   /* FILL ME */
45   ARG_0
46 };
47
48 static GstStaticPadTemplate gst_rtpL16pay_sink_template =
49 GST_STATIC_PAD_TEMPLATE ("sink",
50     GST_PAD_SINK,
51     GST_PAD_ALWAYS,
52     GST_STATIC_CAPS ("audio/x-raw-int, "
53         "endianness = (int) BYTE_ORDER, "
54         "signed = (boolean) true, "
55         "width = (int) 16, "
56         "depth = (int) 16, "
57         "rate = (int) [ 1000, 48000 ], " "channels = (int) [ 1, 2 ]")
58     );
59
60 static GstStaticPadTemplate gst_rtpL16pay_src_template =
61 GST_STATIC_PAD_TEMPLATE ("src",
62     GST_PAD_SRC,
63     GST_PAD_ALWAYS,
64     GST_STATIC_CAPS ("application/x-rtp")
65     );
66
67 static void gst_rtpL16pay_class_init (GstRtpL16PayClass * klass);
68 static void gst_rtpL16pay_base_init (GstRtpL16PayClass * klass);
69 static void gst_rtpL16pay_init (GstRtpL16Pay * rtpL16enc);
70 static void gst_rtpL16pay_chain (GstPad * pad, GstData * _data);
71 static void gst_rtpL16pay_set_property (GObject * object, guint prop_id,
72     const GValue * value, GParamSpec * pspec);
73 static void gst_rtpL16pay_get_property (GObject * object, guint prop_id,
74     GValue * value, GParamSpec * pspec);
75 static GstPadLinkReturn gst_rtpL16pay_sinkconnect (GstPad * pad,
76     const GstCaps * caps);
77 static GstStateChangeReturn gst_rtpL16pay_change_state (GstElement * element,
78     GstStateChange transition);
79
80 static GstElementClass *parent_class = NULL;
81
82 static GType
83 gst_rtpL16pay_get_type (void)
84 {
85   static GType rtpL16pay_type = 0;
86
87   if (!rtpL16pay_type) {
88     static const GTypeInfo rtpL16pay_info = {
89       sizeof (GstRtpL16PayClass),
90       (GBaseInitFunc) gst_rtpL16pay_base_init,
91       NULL,
92       (GClassInitFunc) gst_rtpL16pay_class_init,
93       NULL,
94       NULL,
95       sizeof (GstRtpL16Pay),
96       0,
97       (GInstanceInitFunc) gst_rtpL16pay_init,
98     };
99
100     rtpL16pay_type =
101         g_type_register_static (GST_TYPE_ELEMENT, "GstRtpL16Pay",
102         &rtpL16pay_info, 0);
103   }
104   return rtpL16pay_type;
105 }
106
107 static void
108 gst_rtpL16pay_base_init (GstRtpL16PayClass * klass)
109 {
110   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
111
112   gst_element_class_add_pad_template (element_class,
113       gst_static_pad_template_get (&gst_rtpL16pay_sink_template));
114   gst_element_class_add_pad_template (element_class,
115       gst_static_pad_template_get (&gst_rtpL16pay_src_template));
116   gst_element_class_set_details (element_class, &gst_rtpL16pay_details);
117 }
118
119 static void
120 gst_rtpL16pay_class_init (GstRtpL16PayClass * klass)
121 {
122   GObjectClass *gobject_class;
123   GstElementClass *gstelement_class;
124
125   gobject_class = (GObjectClass *) klass;
126   gstelement_class = (GstElementClass *) klass;
127
128   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
129
130   gobject_class->set_property = gst_rtpL16pay_set_property;
131   gobject_class->get_property = gst_rtpL16pay_get_property;
132
133   gstelement_class->change_state = gst_rtpL16pay_change_state;
134 }
135
136 static void
137 gst_rtpL16pay_init (GstRtpL16Pay * rtpL16enc)
138 {
139   rtpL16enc->sinkpad =
140       gst_pad_new_from_template (gst_static_pad_template_get
141       (&gst_rtpL16pay_sink_template), "sink");
142   rtpL16enc->srcpad =
143       gst_pad_new_from_template (gst_static_pad_template_get
144       (&gst_rtpL16pay_src_template), "src");
145   gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->sinkpad);
146   gst_element_add_pad (GST_ELEMENT (rtpL16enc), rtpL16enc->srcpad);
147   gst_pad_set_chain_function (rtpL16enc->sinkpad, gst_rtpL16pay_chain);
148   gst_pad_set_link_function (rtpL16enc->sinkpad, gst_rtpL16pay_sinkconnect);
149
150   rtpL16enc->frequency = 44100;
151   rtpL16enc->channels = 2;
152
153   rtpL16enc->next_time = 0;
154   rtpL16enc->time_interval = 0;
155
156   rtpL16enc->seq = 0;
157   rtpL16enc->ssrc = random ();
158 }
159
160 static GstPadLinkReturn
161 gst_rtpL16pay_sinkconnect (GstPad * pad, const GstCaps * caps)
162 {
163   GstRtpL16Pay *rtpL16enc;
164   GstStructure *structure;
165   gboolean ret;
166
167   rtpL16enc = GST_RTP_L16_PAY (gst_pad_get_parent (pad));
168
169   structure = gst_caps_get_structure (caps, 0);
170
171   ret = gst_structure_get_int (structure, "rate", &rtpL16enc->frequency);
172   ret &= gst_structure_get_int (structure, "channels", &rtpL16enc->channels);
173
174   if (!ret) {
175     gst_object_unref (rtpL16enc);
176     return GST_PAD_LINK_REFUSED;
177   }
178
179   /* Pre-calculate what we can */
180   rtpL16enc->time_interval =
181       GST_SECOND / (2 * rtpL16enc->channels * rtpL16enc->frequency);
182
183   gst_object_unref (rtpL16enc);
184
185   return GST_PAD_LINK_OK;
186 }
187
188
189 void
190 gst_rtpL16pay_htons (GstBuffer * buf)
191 {
192   gint16 *i, *len;
193
194   /* FIXME: is this code correct or even sane at all? */
195   i = (gint16 *) GST_BUFFER_DATA (buf);
196   len = i + GST_BUFFER_SIZE (buf) / sizeof (gint16 *);
197
198   for (; i < len; i++) {
199     *i = g_htons (*i);
200   }
201 }
202
203 static void
204 gst_rtpL16pay_chain (GstPad * pad, GstData * _data)
205 {
206   GstBuffer *buf = GST_BUFFER (_data);
207   GstRtpL16Pay *rtpL16enc;
208   GstBuffer *outbuf;
209   Rtp_Packet packet;
210
211   g_return_if_fail (pad != NULL);
212   g_return_if_fail (GST_IS_PAD (pad));
213   g_return_if_fail (buf != NULL);
214
215   rtpL16enc = GST_RTP_L16_PAY (GST_OBJECT_PARENT (pad));
216
217   g_return_if_fail (rtpL16enc != NULL);
218   g_return_if_fail (GST_IS_RTP_L16_PAY (rtpL16enc));
219
220   if (GST_IS_EVENT (buf)) {
221     GstEvent *event = GST_EVENT (buf);
222
223     switch (GST_EVENT_TYPE (event)) {
224       case GST_EVENT_DISCONTINUOUS:
225         GST_DEBUG ("discont");
226         rtpL16enc->next_time = 0;
227         gst_pad_event_default (pad, event);
228         return;
229       default:
230         gst_pad_event_default (pad, event);
231         return;
232     }
233   }
234
235   /* We only need the header */
236   packet = rtp_packet_new_allocate (0, 0, 0);
237
238   rtp_packet_set_csrc_count (packet, 0);
239   rtp_packet_set_extension (packet, 0);
240   rtp_packet_set_padding (packet, 0);
241   rtp_packet_set_version (packet, RTP_VERSION);
242   rtp_packet_set_marker (packet, 0);
243   rtp_packet_set_ssrc (packet, g_htonl (rtpL16enc->ssrc));
244   rtp_packet_set_seq (packet, g_htons (rtpL16enc->seq));
245   rtp_packet_set_timestamp (packet,
246       g_htonl ((guint32) rtpL16enc->next_time / GST_SECOND));
247
248   if (rtpL16enc->channels == 1) {
249     rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_MONO);
250   }
251
252   else {
253     rtp_packet_set_payload_type (packet, (guint8) PAYLOAD_L16_STEREO);
254   }
255
256   /* FIXME: According to RFC 1890, this is required, right? */
257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
258   gst_rtpL16pay_htons (buf);
259 #endif
260
261   outbuf = gst_buffer_new ();
262   GST_BUFFER_SIZE (outbuf) =
263       rtp_packet_get_packet_len (packet) + GST_BUFFER_SIZE (buf);
264   GST_BUFFER_DATA (outbuf) = g_malloc (GST_BUFFER_SIZE (outbuf));
265   GST_BUFFER_TIMESTAMP (outbuf) = rtpL16enc->next_time;
266
267   memcpy (GST_BUFFER_DATA (outbuf), packet->data,
268       rtp_packet_get_packet_len (packet));
269   memcpy (GST_BUFFER_DATA (outbuf) + rtp_packet_get_packet_len (packet),
270       GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
271
272   GST_DEBUG ("gst_rtpL16pay_chain: pushing buffer of size %d",
273       GST_BUFFER_SIZE (outbuf));
274   gst_pad_push (rtpL16enc->srcpad, GST_DATA (outbuf));
275
276   ++rtpL16enc->seq;
277   rtpL16enc->next_time += rtpL16enc->time_interval * GST_BUFFER_SIZE (buf);
278
279   rtp_packet_free (packet);
280   gst_buffer_unref (buf);
281 }
282
283 static void
284 gst_rtpL16pay_set_property (GObject * object, guint prop_id,
285     const GValue * value, GParamSpec * pspec)
286 {
287   GstRtpL16Pay *rtpL16enc;
288
289   g_return_if_fail (GST_IS_RTP_L16_PAY (object));
290   rtpL16enc = GST_RTP_L16_PAY (object);
291
292   switch (prop_id) {
293     default:
294       break;
295   }
296 }
297
298 static void
299 gst_rtpL16pay_get_property (GObject * object, guint prop_id, GValue * value,
300     GParamSpec * pspec)
301 {
302   GstRtpL16Pay *rtpL16enc;
303
304   g_return_if_fail (GST_IS_RTP_L16_PAY (object));
305   rtpL16enc = GST_RTP_L16_PAY (object);
306
307   switch (prop_id) {
308     default:
309       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310       break;
311   }
312 }
313
314 static GstStateChangeReturn
315 gst_rtpL16pay_change_state (GstElement * element, GstStateChange transition)
316 {
317   GstRtpL16Pay *rtpL16enc;
318
319   g_return_val_if_fail (GST_IS_RTP_L16_PAY (element), GST_STATE_CHANGE_FAILURE);
320
321   rtpL16enc = GST_RTP_L16_PAY (element);
322
323   GST_DEBUG ("state pending %d\n", GST_STATE_PENDING (element));
324
325   /* if going down into NULL state, close the file if it's open */
326   switch (transition) {
327     case GST_STATE_CHANGE_NULL_TO_READY:
328       break;
329
330     case GST_STATE_CHANGE_READY_TO_NULL:
331       break;
332
333     default:
334       break;
335   }
336
337   /* if we haven't failed already, give the parent class a chance to ;-) */
338   if (GST_ELEMENT_CLASS (parent_class)->change_state)
339     return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
340
341   return GST_STATE_CHANGE_SUCCESS;
342 }
343
344 gboolean
345 gst_rtpL16pay_plugin_init (GstPlugin * plugin)
346 {
347   return gst_element_register (plugin, "rtpL16enc",
348       GST_RANK_NONE, GST_TYPE_RTP_L16_PAY);
349 }