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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <gst/rtp/gstrtpbuffer.h>
28 #include "gstrtpmpvpay.h"
30 static const GstElementDetails gst_rtp_mpv_pay_details =
31 GST_ELEMENT_DETAILS ("RTP MPEG2 ES video payloader",
32 "Codec/Payloader/Network",
33 "Payload-encodes MPEG2 ES into RTP packets (RFC 2250)",
34 "Thijs Vermeir <thijsvermeir@gmail.com>");
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\"")
54 static gboolean gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload,
56 static GstFlowReturn gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload *
57 payload, GstBuffer * buffer);
58 static GstFlowReturn gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay,
59 GstClockTime timestamp, GstClockTime duration);
60 static void gst_rtp_mpv_pay_finalize (GObject * object);
62 GST_BOILERPLATE (GstRTPMPVPay, gst_rtp_mpv_pay, GstBaseRTPPayload,
63 GST_TYPE_BASE_RTP_PAYLOAD);
66 gst_rtp_mpv_pay_base_init (gpointer klass)
68 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
70 gst_element_class_add_pad_template (element_class,
71 gst_static_pad_template_get (&gst_rtp_mpv_pay_sink_template));
72 gst_element_class_add_pad_template (element_class,
73 gst_static_pad_template_get (&gst_rtp_mpv_pay_src_template));
74 gst_element_class_set_details (element_class, &gst_rtp_mpv_pay_details);
78 gst_rtp_mpv_pay_class_init (GstRTPMPVPayClass * klass)
80 GObjectClass *gobject_class;
81 GstBaseRTPPayloadClass *gstbasertppayload_class;
83 gobject_class = (GObjectClass *) klass;
84 gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
86 gobject_class->finalize = gst_rtp_mpv_pay_finalize;
88 gstbasertppayload_class->set_caps = gst_rtp_mpv_pay_setcaps;
89 gstbasertppayload_class->handle_buffer = gst_rtp_mpv_pay_handle_buffer;
93 gst_rtp_mpv_pay_init (GstRTPMPVPay * rtpmpvpay, GstRTPMPVPayClass * klass)
95 GST_BASE_RTP_PAYLOAD (rtpmpvpay)->clock_rate = 90000;
96 GST_BASE_RTP_PAYLOAD_PT (rtpmpvpay) = GST_RTP_PAYLOAD_MPV;
98 rtpmpvpay->adapter = gst_adapter_new ();
102 gst_rtp_mpv_pay_finalize (GObject * object)
104 GstRTPMPVPay *rtpmpvpay;
106 rtpmpvpay = GST_RTP_MPV_PAY (object);
108 g_object_unref (rtpmpvpay->adapter);
109 rtpmpvpay->adapter = NULL;
111 G_OBJECT_CLASS (parent_class)->finalize (object);
115 gst_rtp_mpv_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
118 GstStructure *structure;
121 structure = gst_caps_get_structure (caps, 0);
123 stname = gst_structure_get_name (structure);
125 gst_basertppayload_set_options (payload, "video", FALSE, "MPV", 90000);
126 res = gst_basertppayload_set_outcaps (payload, NULL);
132 gst_rtp_mpv_pay_flush (GstRTPMPVPay * rtpmpvpay, GstClockTime timestamp,
133 GstClockTime duration)
142 avail = gst_adapter_available (rtpmpvpay->adapter);
143 packet_size = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
145 /* check for the maximum size of the rtp buffer */
146 if (packet_size > GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay)) {
148 GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay) -
149 gst_rtp_buffer_calc_packet_len (4, 0, 0);
151 payload_size = avail;
153 outbuf = gst_rtp_buffer_new_allocate (4 + payload_size, 0, 0);
154 /* enable MPEG Video-specific header
157 * 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
158 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
159 * | MBZ |T| TR | |N|S|B|E| P | | BFC | | FFC |
160 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
163 payload = gst_rtp_buffer_get_payload (outbuf);
164 /* fill in the MPEG Video-specific header */
165 memset (payload, 0x0, 4);
166 /* copy stuff from adapter to payload */
167 gst_adapter_copy (rtpmpvpay->adapter, payload + 4, 0, payload_size);
168 GST_BUFFER_TIMESTAMP (outbuf) = rtpmpvpay->first_ts;
169 GST_BUFFER_DURATION (outbuf) = rtpmpvpay->duration;
171 GST_DEBUG_OBJECT (rtpmpvpay, "pushing buffer of size %d",
172 GST_BUFFER_SIZE (outbuf));
173 ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpmpvpay), outbuf);
174 gst_adapter_flush (rtpmpvpay->adapter, payload_size);
176 /* update the timestamp and duration */
177 rtpmpvpay->first_ts = timestamp;
178 rtpmpvpay->duration = duration;
180 /* check if there is enough data for another rtp buffer */
181 avail = gst_adapter_available (rtpmpvpay->adapter);
182 packet_size = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
184 if (packet_size >= GST_BASE_RTP_PAYLOAD_MTU (rtpmpvpay) && ret == GST_FLOW_OK) {
185 GST_DEBUG_OBJECT (rtpmpvpay, "Have enough data for another rtp packet");
186 ret = gst_rtp_mpv_pay_flush (rtpmpvpay, timestamp, duration);
192 gst_rtp_mpv_pay_handle_buffer (GstBaseRTPPayload * basepayload,
195 GstRTPMPVPay *rtpmpvpay;
196 guint size, avail, packet_len;
198 GstClockTime timestamp, duration;
201 rtpmpvpay = GST_RTP_MPV_PAY (basepayload);
203 size = GST_BUFFER_SIZE (buffer);
204 data = GST_BUFFER_DATA (buffer);
205 timestamp = GST_BUFFER_TIMESTAMP (buffer);
206 duration = GST_BUFFER_DURATION (buffer);
208 gst_adapter_push (rtpmpvpay->adapter, buffer);
209 avail = gst_adapter_available (rtpmpvpay->adapter);
211 /* Initialize new RTP payload */
213 rtpmpvpay->first_ts = timestamp;
214 rtpmpvpay->duration = duration;
217 /* get packet length of previous data and this new data,
218 * payload length includes a 4 byte MPEG video-specific header */
219 packet_len = gst_rtp_buffer_calc_packet_len (4 + avail, 0, 0);
221 if (gst_basertppayload_is_filled (basepayload,
222 packet_len, rtpmpvpay->duration + duration)) {
223 ret = gst_rtp_mpv_pay_flush (rtpmpvpay, timestamp, duration);
225 if (GST_CLOCK_TIME_IS_VALID (duration))
226 rtpmpvpay->duration += duration;
233 gst_rtp_mpv_pay_plugin_init (GstPlugin * plugin)
235 return gst_element_register (plugin, "rtpmpvpay",
236 GST_RANK_NONE, GST_TYPE_RTP_MPV_PAY);