1 /* GStreamer RTP KLV Payloader
2  * Copyright (C) 2014-2015 Tim-Philipp Müller <tim@centricular.com>>
3  * Copyright (C) 2014-2015 Centricular Ltd
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 /**
22  * SECTION:element-rtpklvpay
23  * @see_also: rtpklvdepay
24  *
25  * Payloads KLV metadata into RTP packets according to RFC 6597.
26  * For detailed information see: http://tools.ietf.org/html/rfc6597
27  *
28  * <refsect2>
29  * <title>Example pipeline</title>
30  * |[
31  * gst-launch-1.0 filesrc location=video-with-klv.ts ! tsdemux ! rtpklvpay ! udpsink
32  * ]| This example pipeline will payload an RTP KLV stream extracted from an
33  * MPEG-TS stream and send it via UDP to an RTP receiver.
34  * </refsect2>
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include "gstrtpklvpay.h"
41 #include "gstrtputils.h"
42 
43 #include <string.h>
44 
45 GST_DEBUG_CATEGORY_STATIC (klvpay_debug);
46 #define GST_CAT_DEFAULT (klvpay_debug)
47 
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("application/x-rtp, "
52         "media = (string) application, clock-rate = (int) [1, MAX], "
53         "encoding-name = (string) SMPTE336M")
54     );
55 
56 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
60 
61 #define gst_rtp_klv_pay_parent_class parent_class
62 G_DEFINE_TYPE (GstRtpKlvPay, gst_rtp_klv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
63 
64 static gboolean gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay,
65     GstCaps * caps);
66 static GstFlowReturn gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * pay,
67     GstBuffer * buf);
68 
69 static void
gst_rtp_klv_pay_class_init(GstRtpKlvPayClass * klass)70 gst_rtp_klv_pay_class_init (GstRtpKlvPayClass * klass)
71 {
72   GstElementClass *element_class = (GstElementClass *) klass;
73   GstRTPBasePayloadClass *rtpbasepay_class;
74 
75   GST_DEBUG_CATEGORY_INIT (klvpay_debug, "klvpay", 0, "RTP KLV Payloader");
76 
77   gst_element_class_add_static_pad_template (element_class, &src_template);
78   gst_element_class_add_static_pad_template (element_class, &sink_template);
79 
80   gst_element_class_set_static_metadata (element_class,
81       "RTP KLV Payloader", "Codec/Payloader/Network/RTP",
82       "Payloads KLV (SMPTE ST 336) metadata as RTP packets",
83       "Tim-Philipp Müller <tim@centricular.com>");
84 
85   rtpbasepay_class = (GstRTPBasePayloadClass *) klass;
86 
87   rtpbasepay_class->set_caps = gst_rtp_klv_pay_setcaps;
88   rtpbasepay_class->handle_buffer = gst_rtp_klv_pay_handle_buffer;
89 }
90 
91 static void
gst_rtp_klv_pay_init(GstRtpKlvPay * klvpay)92 gst_rtp_klv_pay_init (GstRtpKlvPay * klvpay)
93 {
94   /* nothing to do here yet */
95 }
96 
97 static gboolean
gst_rtp_klv_pay_setcaps(GstRTPBasePayload * pay,GstCaps * caps)98 gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay, GstCaps * caps)
99 {
100   /* FIXME: allow other clock rates */
101   gst_rtp_base_payload_set_options (pay, "application", TRUE, "SMPTE336M",
102       90000);
103 
104   return gst_rtp_base_payload_set_outcaps (pay, NULL);
105 }
106 
107 static GstFlowReturn
gst_rtp_klv_pay_handle_buffer(GstRTPBasePayload * basepayload,GstBuffer * buf)108 gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * basepayload, GstBuffer * buf)
109 {
110   GstFlowReturn ret = GST_FLOW_OK;
111   GstBufferList *list = NULL;
112   GstRtpKlvPay *pay;
113   GstMapInfo map;
114   GstBuffer *outbuf = NULL;
115   gsize offset;
116   guint mtu, rtp_header_size, max_payload_size;
117 
118   pay = GST_RTP_KLV_PAY (basepayload);
119   mtu = GST_RTP_BASE_PAYLOAD_MTU (basepayload);
120 
121   rtp_header_size = gst_rtp_buffer_calc_header_len (0);
122   max_payload_size = mtu - rtp_header_size;
123 
124   gst_buffer_map (buf, &map, GST_MAP_READ);
125 
126   if (map.size == 0)
127     goto done;
128 
129   /* KLV coding shall use and only use a fixed 16-byte SMPTE-administered
130    * Universal Label, according to SMPTE 298M as Key (Rec. ITU R-BT.1653-1) */
131   if (map.size < 16 || GST_READ_UINT32_BE (map.data) != 0x060E2B34)
132     goto bad_input;
133 
134   if (map.size > max_payload_size)
135     list = gst_buffer_list_new ();
136 
137   GST_LOG_OBJECT (pay, "%" G_GSIZE_FORMAT " bytes of data to payload",
138       map.size);
139 
140   offset = 0;
141   while (offset < map.size) {
142     GstBuffer *payloadbuf;
143     GstRTPBuffer rtp = { NULL };
144     guint payload_size;
145     guint bytes_left;
146 
147     bytes_left = map.size - offset;
148     payload_size = MIN (bytes_left, max_payload_size);
149 
150     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
151 
152     if (payload_size == bytes_left) {
153       GST_LOG_OBJECT (pay, "last packet of KLV unit");
154       gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
155       gst_rtp_buffer_set_marker (&rtp, 1);
156       gst_rtp_buffer_unmap (&rtp);
157     }
158 
159     GST_LOG_OBJECT (pay, "packet with payload size %u", payload_size);
160 
161     gst_rtp_copy_meta (GST_ELEMENT_CAST (pay), outbuf, buf, 0);
162 
163     payloadbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_MEMORY,
164         offset, payload_size);
165 
166     /* join rtp header + payload memory parts */
167     outbuf = gst_buffer_append (outbuf, payloadbuf);
168 
169     GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
170     GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (buf);
171 
172     /* and add to list */
173     if (list != NULL)
174       gst_buffer_list_insert (list, -1, outbuf);
175 
176     offset += payload_size;
177   }
178 
179 done:
180 
181   gst_buffer_unmap (buf, &map);
182   gst_buffer_unref (buf);
183 
184   if (list != NULL)
185     ret = gst_rtp_base_payload_push_list (basepayload, list);
186   else if (outbuf != NULL)
187     ret = gst_rtp_base_payload_push (basepayload, outbuf);
188 
189   return ret;
190 
191 /* ERRORS */
192 bad_input:
193   {
194     GST_ERROR_OBJECT (pay, "Input doesn't look like a KLV packet, ignoring");
195     goto done;
196   }
197 }
198 
199 gboolean
gst_rtp_klv_pay_plugin_init(GstPlugin * plugin)200 gst_rtp_klv_pay_plugin_init (GstPlugin * plugin)
201 {
202   return gst_element_register (plugin, "rtpklvpay",
203       GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_PAY);
204 }
205