Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstasteriskh263.c
1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.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 #include "gstasteriskh263.h"
28
29 /* Cygwin has both netinet/in.h and winsock2.h, but
30  * only one can be included, so prefer the unix one */
31 #ifdef HAVE_NETINET_IN_H
32 #  include <netinet/in.h>
33 #else
34 #ifdef HAVE_WINSOCK2_H
35 #  include <winsock2.h>
36 #endif
37 #endif
38
39 #define GST_ASTERISKH263_HEADER_LEN 6
40
41 typedef struct _GstAsteriskH263Header
42 {
43   guint32 timestamp;            /* Timestamp */
44   guint16 length;               /* Length */
45 } GstAsteriskH263Header;
46
47 #define GST_ASTERISKH263_HEADER_TIMESTAMP(buf) (((GstAsteriskH263Header *)(GST_BUFFER_DATA (buf)))->timestamp)
48 #define GST_ASTERISKH263_HEADER_LENGTH(buf) (((GstAsteriskH263Header *)(GST_BUFFER_DATA (buf)))->length)
49
50 static GstStaticPadTemplate gst_asteriskh263_src_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-asteriskh263")
55     );
56
57 static GstStaticPadTemplate gst_asteriskh263_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp, "
62         "media = (string) \"video\", "
63         "payload = (int) [ 96, 127 ], "
64         "clock-rate = (int) 90000, " "encoding-name = (string) \"H263-1998\"")
65     );
66
67 static void gst_asteriskh263_finalize (GObject * object);
68
69 static GstFlowReturn gst_asteriskh263_chain (GstPad * pad, GstBuffer * buffer);
70
71 static GstStateChangeReturn gst_asteriskh263_change_state (GstElement *
72     element, GstStateChange transition);
73
74 GST_BOILERPLATE (GstAsteriskh263, gst_asteriskh263, GstElement,
75     GST_TYPE_ELEMENT);
76
77 static void
78 gst_asteriskh263_base_init (gpointer klass)
79 {
80   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
81
82   gst_element_class_add_static_pad_template (element_class,
83       &gst_asteriskh263_src_template);
84   gst_element_class_add_static_pad_template (element_class,
85       &gst_asteriskh263_sink_template);
86
87   gst_element_class_set_details_simple (element_class,
88       "RTP Asterisk H263 depayloader", "Codec/Depayloader/Network/RTP",
89       "Extracts H263 video from RTP and encodes in Asterisk H263 format",
90       "Neil Stratford <neils@vipadia.com>");
91 }
92
93 static void
94 gst_asteriskh263_class_init (GstAsteriskh263Class * klass)
95 {
96   GObjectClass *gobject_class;
97   GstElementClass *gstelement_class;
98
99   gobject_class = (GObjectClass *) klass;
100   gstelement_class = (GstElementClass *) klass;
101
102   gobject_class->finalize = gst_asteriskh263_finalize;
103
104   gstelement_class->change_state = gst_asteriskh263_change_state;
105 }
106
107 static void
108 gst_asteriskh263_init (GstAsteriskh263 * asteriskh263,
109     GstAsteriskh263Class * klass)
110 {
111   asteriskh263->srcpad =
112       gst_pad_new_from_static_template (&gst_asteriskh263_src_template, "src");
113   gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->srcpad);
114
115   asteriskh263->sinkpad =
116       gst_pad_new_from_static_template (&gst_asteriskh263_sink_template,
117       "sink");
118   gst_pad_set_chain_function (asteriskh263->sinkpad, gst_asteriskh263_chain);
119   gst_element_add_pad (GST_ELEMENT (asteriskh263), asteriskh263->sinkpad);
120
121   asteriskh263->adapter = gst_adapter_new ();
122 }
123
124 static void
125 gst_asteriskh263_finalize (GObject * object)
126 {
127   GstAsteriskh263 *asteriskh263;
128
129   asteriskh263 = GST_ASTERISK_H263 (object);
130
131   g_object_unref (asteriskh263->adapter);
132   asteriskh263->adapter = NULL;
133
134   G_OBJECT_CLASS (parent_class)->finalize (object);
135 }
136
137 static GstFlowReturn
138 gst_asteriskh263_chain (GstPad * pad, GstBuffer * buf)
139 {
140   GstAsteriskh263 *asteriskh263;
141   GstBuffer *outbuf;
142   GstFlowReturn ret;
143
144   asteriskh263 = GST_ASTERISK_H263 (GST_OBJECT_PARENT (pad));
145
146   if (!gst_rtp_buffer_validate (buf))
147     goto bad_packet;
148
149   {
150     gint payload_len;
151     guint8 *payload;
152     gboolean M;
153     guint32 timestamp;
154     guint32 samples;
155     guint16 asterisk_len;
156
157     payload_len = gst_rtp_buffer_get_payload_len (buf);
158     payload = gst_rtp_buffer_get_payload (buf);
159
160     M = gst_rtp_buffer_get_marker (buf);
161     timestamp = gst_rtp_buffer_get_timestamp (buf);
162
163     outbuf = gst_buffer_new_and_alloc (payload_len +
164         GST_ASTERISKH263_HEADER_LEN);
165
166     /* build the asterisk header */
167     asterisk_len = payload_len;
168     if (M)
169       asterisk_len |= 0x8000;
170     if (!asteriskh263->lastts)
171       asteriskh263->lastts = timestamp;
172     samples = timestamp - asteriskh263->lastts;
173     asteriskh263->lastts = timestamp;
174
175     GST_ASTERISKH263_HEADER_TIMESTAMP (outbuf) = g_htonl (samples);
176     GST_ASTERISKH263_HEADER_LENGTH (outbuf) = g_htons (asterisk_len);
177
178     /* copy the data into place */
179     memcpy (GST_BUFFER_DATA (outbuf) + GST_ASTERISKH263_HEADER_LEN, payload,
180         payload_len);
181
182     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
183     gst_buffer_set_caps (outbuf,
184         (GstCaps *) gst_pad_get_pad_template_caps (asteriskh263->srcpad));
185
186     ret = gst_pad_push (asteriskh263->srcpad, outbuf);
187
188     gst_buffer_unref (buf);
189   }
190
191   return ret;
192
193 bad_packet:
194   {
195     GST_DEBUG ("Packet does not validate");
196     gst_buffer_unref (buf);
197     return GST_FLOW_ERROR;
198   }
199 }
200
201 static GstStateChangeReturn
202 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
203 {
204   GstAsteriskh263 *asteriskh263;
205   GstStateChangeReturn ret;
206
207   asteriskh263 = GST_ASTERISK_H263 (element);
208
209   switch (transition) {
210     case GST_STATE_CHANGE_READY_TO_PAUSED:
211       gst_adapter_clear (asteriskh263->adapter);
212       break;
213     default:
214       break;
215   }
216
217   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
218
219   /*
220      switch (transition) {
221      case GST_STATE_CHANGE_READY_TO_NULL:
222      break;
223      default:
224      break;
225      }
226    */
227   return ret;
228 }
229
230 gboolean
231 gst_asteriskh263_plugin_init (GstPlugin * plugin)
232 {
233   return gst_element_register (plugin, "asteriskh263",
234       GST_RANK_SECONDARY, GST_TYPE_ASTERISK_H263);
235 }