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