gstdepay: check for correct fragment offset
[platform/upstream/gstreamer.git] / gst / rtp / gstrtpmp2tpay.c
1 /* GStreamer
2  * Copyright (C) <2007> 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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, 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
28 #include "gstrtpmp2tpay.h"
29
30 static GstStaticPadTemplate gst_rtp_mp2t_pay_sink_template =
31 GST_STATIC_PAD_TEMPLATE ("sink",
32     GST_PAD_SINK,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("video/mpegts,"
35         "packetsize=(int)188," "systemstream=(boolean)true")
36     );
37
38 static GstStaticPadTemplate gst_rtp_mp2t_pay_src_template =
39 GST_STATIC_PAD_TEMPLATE ("src",
40     GST_PAD_SRC,
41     GST_PAD_ALWAYS,
42     GST_STATIC_CAPS ("application/x-rtp, "
43         "media = (string) \"video\", "
44         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
45         "clock-rate = (int) 90000, " "encoding-name = (string) \"MP2T\"")
46     );
47
48 static gboolean gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload,
49     GstCaps * caps);
50 static GstFlowReturn gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload *
51     payload, GstBuffer * buffer);
52 static GstFlowReturn gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay);
53 static void gst_rtp_mp2t_pay_finalize (GObject * object);
54
55 #define gst_rtp_mp2t_pay_parent_class parent_class
56 G_DEFINE_TYPE (GstRTPMP2TPay, gst_rtp_mp2t_pay, GST_TYPE_RTP_BASE_PAYLOAD);
57
58 static void
59 gst_rtp_mp2t_pay_class_init (GstRTPMP2TPayClass * klass)
60 {
61   GObjectClass *gobject_class;
62   GstElementClass *gstelement_class;
63   GstRTPBasePayloadClass *gstrtpbasepayload_class;
64
65   gobject_class = (GObjectClass *) klass;
66   gstelement_class = (GstElementClass *) klass;
67   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
68
69   gobject_class->finalize = gst_rtp_mp2t_pay_finalize;
70
71   gstrtpbasepayload_class->set_caps = gst_rtp_mp2t_pay_setcaps;
72   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp2t_pay_handle_buffer;
73
74   gst_element_class_add_pad_template (gstelement_class,
75       gst_static_pad_template_get (&gst_rtp_mp2t_pay_sink_template));
76   gst_element_class_add_pad_template (gstelement_class,
77       gst_static_pad_template_get (&gst_rtp_mp2t_pay_src_template));
78   gst_element_class_set_static_metadata (gstelement_class,
79       "RTP MPEG2 Transport Stream payloader", "Codec/Payloader/Network/RTP",
80       "Payload-encodes MPEG2 TS into RTP packets (RFC 2250)",
81       "Wim Taymans <wim.taymans@gmail.com>");
82 }
83
84 static void
85 gst_rtp_mp2t_pay_init (GstRTPMP2TPay * rtpmp2tpay)
86 {
87   GST_RTP_BASE_PAYLOAD (rtpmp2tpay)->clock_rate = 90000;
88   GST_RTP_BASE_PAYLOAD_PT (rtpmp2tpay) = GST_RTP_PAYLOAD_MP2T;
89
90   rtpmp2tpay->adapter = gst_adapter_new ();
91 }
92
93 static void
94 gst_rtp_mp2t_pay_finalize (GObject * object)
95 {
96   GstRTPMP2TPay *rtpmp2tpay;
97
98   rtpmp2tpay = GST_RTP_MP2T_PAY (object);
99
100   g_object_unref (rtpmp2tpay->adapter);
101   rtpmp2tpay->adapter = NULL;
102
103   G_OBJECT_CLASS (parent_class)->finalize (object);
104 }
105
106 static gboolean
107 gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
108 {
109   gboolean res;
110
111   gst_rtp_base_payload_set_options (payload, "video", TRUE, "MP2T", 90000);
112   res = gst_rtp_base_payload_set_outcaps (payload, NULL);
113
114   return res;
115 }
116
117 static GstFlowReturn
118 gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay)
119 {
120   guint avail, mtu;
121   GstFlowReturn ret = GST_FLOW_OK;
122   GstBuffer *outbuf;
123
124   avail = gst_adapter_available (rtpmp2tpay->adapter);
125
126   mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp2tpay);
127
128   while (avail > 0 && (ret == GST_FLOW_OK)) {
129     guint towrite;
130     guint8 *payload;
131     guint payload_len;
132     guint packet_len;
133     GstRTPBuffer rtp = { NULL };
134
135     /* this will be the total length of the packet */
136     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
137
138     /* fill one MTU or all available bytes */
139     towrite = MIN (packet_len, mtu);
140
141     /* this is the payload length */
142     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
143     payload_len -= payload_len % 188;
144
145     /* need whole packets */
146     if (!payload_len)
147       break;
148
149     /* create buffer to hold the payload */
150     outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
151
152     /* get payload */
153     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
154     payload = gst_rtp_buffer_get_payload (&rtp);
155
156     /* copy stuff from adapter to payload */
157     gst_adapter_copy (rtpmp2tpay->adapter, payload, 0, payload_len);
158     gst_rtp_buffer_unmap (&rtp);
159     gst_adapter_flush (rtpmp2tpay->adapter, payload_len);
160     avail -= payload_len;
161
162     GST_BUFFER_TIMESTAMP (outbuf) = rtpmp2tpay->first_ts;
163     GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration;
164
165     GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %u",
166         (guint) gst_buffer_get_size (outbuf));
167
168     ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp2tpay), outbuf);
169   }
170
171   return ret;
172 }
173
174 static GstFlowReturn
175 gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
176     GstBuffer * buffer)
177 {
178   GstRTPMP2TPay *rtpmp2tpay;
179   guint size, avail, packet_len;
180   GstClockTime timestamp, duration;
181   GstFlowReturn ret;
182
183   rtpmp2tpay = GST_RTP_MP2T_PAY (basepayload);
184
185   size = gst_buffer_get_size (buffer);
186   timestamp = GST_BUFFER_TIMESTAMP (buffer);
187   duration = GST_BUFFER_DURATION (buffer);
188
189 again:
190   ret = GST_FLOW_OK;
191   avail = gst_adapter_available (rtpmp2tpay->adapter);
192
193   /* Initialize new RTP payload */
194   if (avail == 0) {
195     rtpmp2tpay->first_ts = timestamp;
196     rtpmp2tpay->duration = duration;
197   }
198
199   /* get packet length of previous data and this new data */
200   packet_len = gst_rtp_buffer_calc_packet_len (avail + size, 0, 0);
201
202   /* if this buffer is going to overflow the packet, flush what we have,
203    * or if upstream is handing us several packets, to keep latency low */
204   if (!size || gst_rtp_base_payload_is_filled (basepayload,
205           packet_len, rtpmp2tpay->duration + duration)) {
206     ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay);
207     rtpmp2tpay->first_ts = timestamp;
208     rtpmp2tpay->duration = duration;
209
210     /* keep filling the payload */
211   } else {
212     if (GST_CLOCK_TIME_IS_VALID (duration))
213       rtpmp2tpay->duration += duration;
214   }
215
216   /* copy buffer to adapter */
217   if (buffer) {
218     gst_adapter_push (rtpmp2tpay->adapter, buffer);
219     buffer = NULL;
220   }
221
222   if (size >= (188 * 2)) {
223     size = 0;
224     goto again;
225   }
226
227   return ret;
228
229 }
230
231 gboolean
232 gst_rtp_mp2t_pay_plugin_init (GstPlugin * plugin)
233 {
234   return gst_element_register (plugin, "rtpmp2tpay",
235       GST_RANK_SECONDARY, GST_TYPE_RTP_MP2T_PAY);
236 }