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