1 /* GStreamer RTP KLV Payloader
2 * Copyright (C) 2014-2015 Tim-Philipp Müller <tim@centricular.com>>
3 * Copyright (C) 2014-2015 Centricular Ltd
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.
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.
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.
22 * SECTION:element-rtpklvpay
23 * @see_also: rtpklvdepay
25 * Payloads KLV metadata into RTP packets according to RFC 6597.
26 * For detailed information see: http://tools.ietf.org/html/rfc6597
29 * <title>Example pipeline</title>
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.
40 #include "gstrtpklvpay.h"
41 #include "gstrtputils.h"
45 GST_DEBUG_CATEGORY_STATIC (klvpay_debug);
46 #define GST_CAT_DEFAULT (klvpay_debug)
48 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
51 GST_STATIC_CAPS ("application/x-rtp, "
52 "media = (string) application, clock-rate = (int) [1, MAX], "
53 "encoding-name = (string) SMPTE336M")
56 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
59 GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
61 #define gst_rtp_klv_pay_parent_class parent_class
62 G_DEFINE_TYPE (GstRtpKlvPay, gst_rtp_klv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
64 static gboolean gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay,
66 static GstFlowReturn gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * pay,
70 gst_rtp_klv_pay_class_init (GstRtpKlvPayClass * klass)
72 GstElementClass *element_class = (GstElementClass *) klass;
73 GstRTPBasePayloadClass *rtpbasepay_class;
75 GST_DEBUG_CATEGORY_INIT (klvpay_debug, "klvpay", 0, "RTP KLV Payloader");
77 gst_element_class_add_static_pad_template (element_class, &src_template);
78 gst_element_class_add_static_pad_template (element_class, &sink_template);
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>");
85 rtpbasepay_class = (GstRTPBasePayloadClass *) klass;
87 rtpbasepay_class->set_caps = gst_rtp_klv_pay_setcaps;
88 rtpbasepay_class->handle_buffer = gst_rtp_klv_pay_handle_buffer;
92 gst_rtp_klv_pay_init (GstRtpKlvPay * klvpay)
94 /* nothing to do here yet */
98 gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay, GstCaps * caps)
100 /* FIXME: allow other clock rates */
101 gst_rtp_base_payload_set_options (pay, "application", TRUE, "SMPTE336M",
104 return gst_rtp_base_payload_set_outcaps (pay, NULL);
108 gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * basepayload, GstBuffer * buf)
110 GstFlowReturn ret = GST_FLOW_OK;
111 GstBufferList *list = NULL;
114 GstBuffer *outbuf = NULL;
116 guint mtu, rtp_header_size, max_payload_size;
118 pay = GST_RTP_KLV_PAY (basepayload);
119 mtu = GST_RTP_BASE_PAYLOAD_MTU (basepayload);
121 rtp_header_size = gst_rtp_buffer_calc_header_len (0);
122 max_payload_size = mtu - rtp_header_size;
124 gst_buffer_map (buf, &map, GST_MAP_READ);
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)
134 if (map.size > max_payload_size)
135 list = gst_buffer_list_new ();
137 GST_LOG_OBJECT (pay, "%" G_GSIZE_FORMAT " bytes of data to payload",
141 while (offset < map.size) {
142 GstBuffer *payloadbuf;
143 GstRTPBuffer rtp = { NULL };
147 bytes_left = map.size - offset;
148 payload_size = MIN (bytes_left, max_payload_size);
150 outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
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);
159 GST_LOG_OBJECT (pay, "packet with payload size %u", payload_size);
161 gst_rtp_copy_meta (GST_ELEMENT_CAST (pay), outbuf, buf, 0);
163 payloadbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_MEMORY,
164 offset, payload_size);
166 /* join rtp header + payload memory parts */
167 outbuf = gst_buffer_append (outbuf, payloadbuf);
169 GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
170 GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (buf);
172 /* and add to list */
174 gst_buffer_list_insert (list, -1, outbuf);
176 offset += payload_size;
181 gst_buffer_unmap (buf, &map);
182 gst_buffer_unref (buf);
185 ret = gst_rtp_base_payload_push_list (basepayload, list);
186 else if (outbuf != NULL)
187 ret = gst_rtp_base_payload_push (basepayload, outbuf);
194 GST_ERROR_OBJECT (pay, "Input doesn't look like a KLV packet, ignoring");
200 gst_rtp_klv_pay_plugin_init (GstPlugin * plugin)
202 return gst_element_register (plugin, "rtpklvpay",
203 GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_PAY);