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