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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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
gst_asteriskh263_class_init(GstAsteriskh263Class * klass)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_static_pad_template (gstelement_class,
82       &gst_asteriskh263_src_template);
83   gst_element_class_add_static_pad_template (gstelement_class,
84       &gst_asteriskh263_sink_template);
85 
86   gst_element_class_set_static_metadata (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
gst_asteriskh263_init(GstAsteriskh263 * asteriskh263)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
gst_asteriskh263_finalize(GObject * object)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
gst_asteriskh263_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)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   {
131     gint payload_len;
132     guint8 *payload;
133     gboolean M;
134     guint32 timestamp;
135     guint32 samples;
136     guint16 asterisk_len;
137     GstRTPBuffer rtp = { NULL };
138     GstMapInfo map;
139 
140     if (!gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp))
141       goto bad_packet;
142 
143     payload_len = gst_rtp_buffer_get_payload_len (&rtp);
144     payload = gst_rtp_buffer_get_payload (&rtp);
145 
146     M = gst_rtp_buffer_get_marker (&rtp);
147     timestamp = gst_rtp_buffer_get_timestamp (&rtp);
148 
149     gst_rtp_buffer_unmap (&rtp);
150 
151     outbuf = gst_buffer_new_and_alloc (payload_len +
152         GST_ASTERISKH263_HEADER_LEN);
153 
154     /* build the asterisk header */
155     asterisk_len = payload_len;
156     if (M)
157       asterisk_len |= 0x8000;
158     if (!asteriskh263->lastts)
159       asteriskh263->lastts = timestamp;
160     samples = timestamp - asteriskh263->lastts;
161     asteriskh263->lastts = timestamp;
162 
163     gst_buffer_map (outbuf, &map, GST_MAP_WRITE);
164     GST_ASTERISKH263_HEADER_TIMESTAMP (map.data) = g_htonl (samples);
165     GST_ASTERISKH263_HEADER_LENGTH (map.data) = g_htons (asterisk_len);
166 
167     /* copy the data into place */
168     memcpy (map.data + GST_ASTERISKH263_HEADER_LEN, payload, payload_len);
169 
170     gst_buffer_unmap (outbuf, &map);
171 
172     GST_BUFFER_PTS (outbuf) = timestamp;
173     if (!gst_pad_has_current_caps (asteriskh263->srcpad)) {
174       GstCaps *caps;
175 
176       caps = gst_pad_get_pad_template_caps (asteriskh263->srcpad);
177       gst_pad_set_caps (asteriskh263->srcpad, caps);
178       gst_caps_unref (caps);
179     }
180 
181     ret = gst_pad_push (asteriskh263->srcpad, outbuf);
182 
183     gst_buffer_unref (buf);
184   }
185 
186   return ret;
187 
188 bad_packet:
189   {
190     GST_DEBUG ("Packet does not validate");
191     gst_buffer_unref (buf);
192     return GST_FLOW_ERROR;
193   }
194 }
195 
196 static GstStateChangeReturn
gst_asteriskh263_change_state(GstElement * element,GstStateChange transition)197 gst_asteriskh263_change_state (GstElement * element, GstStateChange transition)
198 {
199   GstAsteriskh263 *asteriskh263;
200   GstStateChangeReturn ret;
201 
202   asteriskh263 = GST_ASTERISK_H263 (element);
203 
204   switch (transition) {
205     case GST_STATE_CHANGE_READY_TO_PAUSED:
206       gst_adapter_clear (asteriskh263->adapter);
207       break;
208     default:
209       break;
210   }
211 
212   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
213 
214   /*
215      switch (transition) {
216      case GST_STATE_CHANGE_READY_TO_NULL:
217      break;
218      default:
219      break;
220      }
221    */
222   return ret;
223 }
224 
225 gboolean
gst_asteriskh263_plugin_init(GstPlugin * plugin)226 gst_asteriskh263_plugin_init (GstPlugin * plugin)
227 {
228   return gst_element_register (plugin, "asteriskh263",
229       GST_RANK_NONE, GST_TYPE_ASTERISK_H263);
230 }
231