2 * Copyright (C) <2007> Thijs Vermeir <thijsvermeir@gmail.com>
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.
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.
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.
26 #include <gst/rtp/gstrtpbuffer.h>
27 #include <gst/video/video.h>
29 #include "gstrtpelements.h"
30 #include "gstrtpmpvpay.h"
31 #include "gstrtputils.h"
33 GST_DEBUG_CATEGORY_STATIC (rtpmpvpay_debug);
34 #define GST_CAT_DEFAULT (rtpmpvpay_debug)
36 static GstStaticPadTemplate gst_rtp_mpv_pay_sink_template =
37 GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_STATIC_CAPS ("video/mpeg, "
41 "mpegversion = (int) 2, systemstream = (boolean) FALSE")
44 static GstStaticPadTemplate gst_rtp_mpv_pay_src_template =
45 GST_STATIC_PAD_TEMPLATE ("src",
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\"; "
53 "media = (string) \"video\", "
54 "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
55 "clock-rate = (int) 90000, " "encoding-name = (string) \"MPV\"")
58 static GstStateChangeReturn gst_rtp_mpv_pay_change_state (GstElement * element,
59 GstStateChange transition);
61 static void gst_rtp_mpv_pay_finalize (GObject * object);
63 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay);
64 static gboolean gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload,
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,
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));
77 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
79 GObjectClass *gobject_class;
80 GstElementClass *gstelement_class;
81 GstRTPBasePayloadClass *gstrtpbasepayload_class;
83 gobject_class = (GObjectClass *) klass;
84 gstelement_class = (GstElementClass *) klass;
85 gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
87 gobject_class->finalize = gst_rtp_mpv_pay_finalize;
89 gstelement_class->change_state = gst_rtp_mpv_pay_change_state;
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);
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>");
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;
105 GST_DEBUG_CATEGORY_INIT (rtpmpvpay_debug, "rtpmpvpay", 0,
106 "MPEG2 ES Video RTP Payloader");
110 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay)
112 GST_RTP_BASE_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
113 GST_RTP_BASE_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
115 rtpmpvpay->adapter = gst_adapter_new ();
119 gst_rtp_mpv_pay_finalize (GObject * object)
121 GstRTPMPVPay *rtpmpvpay;
123 rtpmpvpay = GST_RTP_MPV_PAY (object);
125 g_object_unref (rtpmpvpay->adapter);
126 rtpmpvpay->adapter = NULL;
128 G_OBJECT_CLASS (parent_class)->finalize (object);
132 gst_rtp_mpv_pay_reset (GstRTPMPVPay * pay)
136 gst_adapter_clear (pay->adapter);
137 GST_DEBUG_OBJECT (pay, "reset depayloader");
141 gst_rtp_mpv_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
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);
149 gst_rtp_mpv_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
152 GstRTPMPVPay *rtpmpvpay;
154 rtpmpvpay = GST_RTP_MPV_PAY (payload);
156 switch (GST_EVENT_TYPE (event)) {
158 /* make sure we push the last packets in the adapter on EOS */
159 gst_rtp_mpv_pay_flush (rtpmpvpay);
161 case GST_EVENT_FLUSH_STOP:
162 gst_rtp_mpv_pay_reset (rtpmpvpay);
168 ret = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
173 #define RTP_HEADER_LEN 12
176 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay)
185 avail = gst_adapter_available (rtpmpvpay->adapter);
189 GST_DEBUG_OBJECT (rtpmpvpay, "available %u", avail);
194 gst_buffer_list_new_sized (avail / (GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay) -
195 RTP_HEADER_LEN) + 1);
201 GstRTPBuffer rtp = { NULL };
204 packet_len = gst_rtp_buffer_calc_packet_len (avail + 4, 0, 0);
206 towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmpvpay));
208 payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
211 gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
212 (rtpmpvpay), 4, 0, 0);
216 gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
218 payload = gst_rtp_buffer_get_payload (&rtp);
219 /* enable MPEG Video-specific header
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
229 /* fill in the MPEG Video-specific header
230 * data is set to 0x0 here
232 memset (payload, 0x0, 4);
234 avail -= payload_len;
236 gst_rtp_buffer_set_marker (&rtp, avail == 0);
238 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_MARKER);
239 gst_rtp_buffer_unmap (&rtp);
241 paybuf = gst_adapter_take_buffer_fast (rtpmpvpay->adapter, payload_len);
242 gst_rtp_copy_video_meta (rtpmpvpay, outbuf, paybuf);
243 outbuf = gst_buffer_append (outbuf, paybuf);
245 GST_DEBUG_OBJECT (rtpmpvpay, "Adding buffer");
247 GST_BUFFER_PTS (outbuf) = rtpmpvpay->first_ts;
248 gst_buffer_list_add (list, outbuf);
251 ret = gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmpvpay), list);
257 gst_rtp_mpv_pay_handle_buffer (GstRTPBasePayload * basepayload,
260 GstRTPMPVPay *rtpmpvpay;
261 guint avail, packet_len;
262 GstClockTime timestamp, duration;
263 GstFlowReturn ret = GST_FLOW_OK;
265 rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
267 timestamp = GST_BUFFER_PTS (buffer);
268 duration = GST_BUFFER_DURATION (buffer);
270 if (GST_BUFFER_IS_DISCONT (buffer)) {
271 GST_DEBUG_OBJECT (rtpmpvpay, "DISCONT");
272 gst_rtp_mpv_pay_reset (rtpmpvpay);
275 avail = gst_adapter_available (rtpmpvpay->adapter);
280 if (rtpmpvpay->first_ts == GST_CLOCK_TIME_NONE || avail == 0)
281 rtpmpvpay->first_ts = timestamp;
284 rtpmpvpay->duration = duration;
286 rtpmpvpay->duration += duration;
289 gst_adapter_push (rtpmpvpay->adapter, buffer);
290 avail = gst_adapter_available (rtpmpvpay->adapter);
292 /* get packet length of previous data and this new data,
293 * payload length includes a 4 byte MPEG video-specific header */
294 packet_len = gst_rtp_buffer_calc_packet_len (avail, 4, 0);
295 GST_LOG_OBJECT (rtpmpvpay, "available %d, rtp packet length %d", avail,
298 if (gst_rtp_base_payload_is_filled (basepayload,
299 packet_len, rtpmpvpay->duration)) {
300 ret = gst_rtp_mpv_pay_flush (rtpmpvpay);
302 rtpmpvpay->first_ts = timestamp;
308 static GstStateChangeReturn
309 gst_rtp_mpv_pay_change_state (GstElement * element, GstStateChange transition)
311 GstRTPMPVPay *rtpmpvpay;
312 GstStateChangeReturn ret;
314 rtpmpvpay = GST_RTP_MPV_PAY (element);
316 switch (transition) {
317 case GST_STATE_CHANGE_READY_TO_PAUSED:
318 gst_rtp_mpv_pay_reset (rtpmpvpay);
324 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
326 switch (transition) {
327 case GST_STATE_CHANGE_PAUSED_TO_READY:
328 gst_rtp_mpv_pay_reset (rtpmpvpay);