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