1 /* GStreamer
2 * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@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 <stdlib.h>
25 #include <string.h>
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
28
29 #include "gstrtpelements.h"
30 #include "gstrtpmpvpay.h"
31 #include "gstrtputils.h"
32
33 GST_DEBUG_CATEGORY_STATIC (rtpmpvpay_debug);
34 #define GST_CAT_DEFAULT (rtpmpvpay_debug)
35
36 static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
38 GST_PAD_SINK,
39 GST_PAD_ALWAYS,
40 GST_STATIC_CAPS ("video/mpeg, "
41 "mpegversion = (int) 2, systemstream = (boolean) FALSE")
42 );
43
44 static GstStaticPadTemplate gst_rtp_mpv_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
46 GST_PAD_SRC,
47 GST_PAD_ALWAYS,
48 GST_STATIC_CAPS ("application/x-rtp, "
49 "media = (string) \"video\", "
50 "payload = (int) " GST_RTP_PAYLOAD_MPV_STRING ", "
51 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"; "
52 "application/x-rtp, "
53 "media = (string) \"video\", "
54 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
56 );
57
58 static GstStateChangeReturn gst_rtp_mpv_pay_change_state (GstElement * element,
59 GstStateChange transition);
60
61 static void gst_rtp_mpv_pay_finalize (GObject * object);
62
63 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay);
64 static gboolean gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload,
65 GstCaps * caps);
66 static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload *
67 payload, GstBuffer * buffer);
68 static gboolean gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload,
69 GstEvent * event);
70
71 #define gst_rtp_mpv_pay_parent_class parent_class
72 G_DEFINE_TYPE (GstRTPMPVPay, gst_rtp_mpv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
73 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpmpvpay, "rtpmpvpay",
74 GST_RANK_SECONDARY, GST_TYPE_RTP_MPV_PAY, rtp_element_init (plugin));
75
76 static void
gst_rtp_mpv_pay_class_init(GstRTPMPVPayClass * klass)77 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
78 {
79 GObjectClass *gobject_class;
80 GstElementClass *gstelement_class;
81 GstRTPBasePayloadClass *gstrtpbasepayload_class;
82
83 gobject_class = (GObjectClass *) klass;
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
86
87 gobject_class->finalize = gst_rtp_mpv_pay_finalize;
88
89 gstelement_class->change_state = gst_rtp_mpv_pay_change_state;
90
91 gst_element_class_add_static_pad_template (gstelement_class,
92 &gst_rtp_mpv_pay_sink_template);
93 gst_element_class_add_static_pad_template (gstelement_class,
94 &gst_rtp_mpv_pay_src_template);
95
96 gst_element_class_set_static_metadata (gstelement_class,
97 "RTP MPEG2 ES video payloader", "Codec/Payloader/Network/RTP",
98 "Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
99 "Thijs Vermeir <thijsvermeir@gmail.com>");
100
101 gstrtpbasepayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
102 gstrtpbasepayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
103 gstrtpbasepayload_class->sink_event = gst_rtp_mpv_pay_sink_event;
104
105 GST_DEBUG_CATEGORY_INIT (rtpmpvpay_debug, "rtpmpvpay", 0,
106 "MPEG2 ES Video RTP Payloader");
107 }
108
109 static void
gst_rtp_mpv_pay_init(GstRTPMPVPay * rtpmpvpay)110 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay)
111 {
112 GST_RTP_BASE_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
113 GST_RTP_BASE_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
114
115 rtpmpvpay->adapter = gst_adapter_new ();
116 }
117
118 static void
gst_rtp_mpv_pay_finalize(GObject * object)119 gst_rtp_mpv_pay_finalize (GObject * object)
120 {
121 GstRTPMPVPay *rtpmpvpay;
122
123 rtpmpvpay = GST_RTP_MPV_PAY (object);
124
125 g_object_unref (rtpmpvpay->adapter);
126 rtpmpvpay->adapter = NULL;
127
128 G_OBJECT_CLASS (parent_class)->finalize (object);
129 }
130
131 static void
gst_rtp_mpv_pay_reset(GstRTPMPVPay * pay)132 gst_rtp_mpv_pay_reset (GstRTPMPVPay * pay)
133 {
134 pay->first_ts = -1;
135 pay->duration = 0;
136 gst_adapter_clear (pay->adapter);
137 GST_DEBUG_OBJECT (pay, "reset depayloader");
138 }
139
140 static gboolean
gst_rtp_mpv_pay_setcaps(GstRTPBasePayload * payload,GstCaps * caps)141 gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
142 {
143 gst_rtp_base_payload_set_options (payload, "video",
144 payload->pt != GST_RTP_PAYLOAD_MPV, "MPV", 90000);
145 return gst_rtp_base_payload_set_outcaps (payload, NULL);
146 }
147
148 static gboolean
gst_rtp_mpv_pay_sink_event(GstRTPBasePayload * payload,GstEvent * event)149 gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
150 {
151 gboolean ret;
152 GstRTPMPVPay *rtpmpvpay;
153
154 rtpmpvpay = GST_RTP_MPV_PAY (payload);
155
156 switch (GST_EVENT_TYPE (event)) {
157 case GST_EVENT_EOS:
158 /* make sure we push the last packets in the adapter on EOS */
159 gst_rtp_mpv_pay_flush (rtpmpvpay);
160 break;
161 case GST_EVENT_FLUSH_STOP:
162 gst_rtp_mpv_pay_reset (rtpmpvpay);
163 break;
164 default:
165 break;
166 }
167
168 ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
169
170 return ret;
171 }
172
173 #define RTP_HEADER_LEN 12
174
175 static GstFlowReturn
gst_rtp_mpv_pay_flush(GstRTPMPVPay * rtpmpvpay)176 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay)
177 {
178 GstFlowReturn ret;
179 guint avail;
180 GstBufferList *list;
181 GstBuffer *outbuf;
182
183 guint8 *payload;
184
185 avail = gst_adapter_available (rtpmpvpay->adapter);
186
187 ret = GST_FLOW_OK;
188
189 GST_DEBUG_OBJECT (rtpmpvpay, "available %u", avail);
190 if (avail == 0)
191 return GST_FLOW_OK;
192
193 list =
194 gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay) -
195 RTP_HEADER_LEN) + 1);
196
197 while (avail > 0) {
198 guint towrite;
199 guint packet_len;
200 guint payload_len;
201 GstRTPBuffer rtp = { NULL };
202 GstBuffer *paybuf;
203
204 packet_len = gst_rtp_buffer_calc_packet_len (avail + 4, 0, 0);
205
206 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay));
207
208 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
209
210 outbuf =
211 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
212 (rtpmpvpay), 4, 0, 0);
213
214 payload_len -= 4;
215
216 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
217
218 payload = gst_rtp_buffer_get_payload (&rtp);
219 /* enable MPEG Video-specific header
220 *
221 * 0 1 2 3
222 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
223 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224 * | MBZ |T| TR | |N|S|B|E| P | | BFC | | FFC |
225 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226 * AN FBV FFV
227 */
228
229 /* fill in the MPEG Video-specific header
230 * data is set to 0x0 here
231 */
232 memset (payload, 0x0, 4);
233
234 avail -= payload_len;
235
236 gst_rtp_buffer_set_marker (&rtp, avail == 0);
237 gst_rtp_buffer_unmap (&rtp);
238
239 paybuf = gst_adapter_take_buffer_fast (rtpmpvpay->adapter, payload_len);
240 gst_rtp_copy_video_meta (rtpmpvpay, outbuf, paybuf);
241 outbuf = gst_buffer_append (outbuf, paybuf);
242
243 GST_DEBUG_OBJECT (rtpmpvpay, "Adding buffer");
244
245 GST_BUFFER_PTS (outbuf) = rtpmpvpay->first_ts;
246 gst_buffer_list_add (list, outbuf);
247 }
248
249 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpvpay), list);
250
251 return ret;
252 }
253
254 static GstFlowReturn
gst_rtp_mpv_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buffer)255 gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload * basepayload,
256 GstBuffer * buffer)
257 {
258 GstRTPMPVPay *rtpmpvpay;
259 guint avail, packet_len;
260 GstClockTime timestamp, duration;
261 GstFlowReturn ret = GST_FLOW_OK;
262
263 rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
264
265 timestamp = GST_BUFFER_PTS (buffer);
266 duration = GST_BUFFER_DURATION (buffer);
267
268 if (GST_BUFFER_IS_DISCONT (buffer)) {
269 GST_DEBUG_OBJECT (rtpmpvpay, "DISCONT");
270 gst_rtp_mpv_pay_reset (rtpmpvpay);
271 }
272
273 avail = gst_adapter_available (rtpmpvpay->adapter);
274
275 if (duration == -1)
276 duration = 0;
277
278 if (rtpmpvpay->first_ts == GST_CLOCK_TIME_NONE || avail == 0)
279 rtpmpvpay->first_ts = timestamp;
280
281 if (avail == 0) {
282 rtpmpvpay->duration = duration;
283 } else {
284 rtpmpvpay->duration += duration;
285 }
286
287 gst_adapter_push (rtpmpvpay->adapter, buffer);
288 avail = gst_adapter_available (rtpmpvpay->adapter);
289
290 /* get packet length of previous data and this new data,
291 * payload length includes a 4 byte MPEG video-specific header */
292 packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
293 GST_LOG_OBJECT (rtpmpvpay, "available %d, rtp packet length %d", avail,
294 packet_len);
295
296 if (gst_rtp_base_payload_is_filled (basepayload,
297 packet_len, rtpmpvpay->duration)) {
298 ret = gst_rtp_mpv_pay_flush (rtpmpvpay);
299 } else {
300 rtpmpvpay->first_ts = timestamp;
301 }
302
303 return ret;
304 }
305
306 static GstStateChangeReturn
gst_rtp_mpv_pay_change_state(GstElement * element,GstStateChange transition)307 gst_rtp_mpv_pay_change_state (GstElement * element, GstStateChange transition)
308 {
309 GstRTPMPVPay *rtpmpvpay;
310 GstStateChangeReturn ret;
311
312 rtpmpvpay = GST_RTP_MPV_PAY (element);
313
314 switch (transition) {
315 case GST_STATE_CHANGE_READY_TO_PAUSED:
316 gst_rtp_mpv_pay_reset (rtpmpvpay);
317 break;
318 default:
319 break;
320 }
321
322 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
323
324 switch (transition) {
325 case GST_STATE_CHANGE_PAUSED_TO_READY:
326 gst_rtp_mpv_pay_reset (rtpmpvpay);
327 break;
328 default:
329 break;
330 }
331 return ret;
332 }
333