rtpklv(de)pay: add "RTP" in the klass string
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpklvpay.c
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
42 #include <string.h>
43
44 GST_DEBUG_CATEGORY_STATIC (klvpay_debug);
45 #define GST_CAT_DEFAULT (klvpay_debug)
46
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS ("application/x-rtp, "
51         "media = (string) application, clock-rate = (int) [1, MAX], "
52         "encoding-name = (string) SMPTE336M")
53     );
54
55 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
56     GST_PAD_SINK,
57     GST_PAD_ALWAYS,
58     GST_STATIC_CAPS ("meta/x-klv, parsed = (bool) true"));
59
60 #define gst_rtp_klv_pay_parent_class parent_class
61 G_DEFINE_TYPE (GstRtpKlvPay, gst_rtp_klv_pay, GST_TYPE_RTP_BASE_PAYLOAD);
62
63 static gboolean gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay,
64     GstCaps * caps);
65 static GstFlowReturn gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * pay,
66     GstBuffer * buf);
67
68 static void
69 gst_rtp_klv_pay_class_init (GstRtpKlvPayClass * klass)
70 {
71   GstElementClass *element_class = (GstElementClass *) klass;
72   GstRTPBasePayloadClass *rtpbasepay_class;
73
74   GST_DEBUG_CATEGORY_INIT (klvpay_debug, "klvpay", 0, "RTP KLV Payloader");
75
76   gst_element_class_add_pad_template (element_class,
77       gst_static_pad_template_get (&src_template));
78   gst_element_class_add_pad_template (element_class,
79       gst_static_pad_template_get (&sink_template));
80
81   gst_element_class_set_static_metadata (element_class,
82       "RTP KLV Payloader", "Codec/Payloader/Network/RTP",
83       "Payloads KLV (SMPTE ST 336) metadata as RTP packets",
84       "Tim-Philipp Müller <tim@centricular.com>");
85
86   rtpbasepay_class = (GstRTPBasePayloadClass *) klass;
87
88   rtpbasepay_class->set_caps = gst_rtp_klv_pay_setcaps;
89   rtpbasepay_class->handle_buffer = gst_rtp_klv_pay_handle_buffer;
90 }
91
92 static void
93 gst_rtp_klv_pay_init (GstRtpKlvPay * klvpay)
94 {
95   /* nothing to do here yet */
96 }
97
98 static gboolean
99 gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay, GstCaps * caps)
100 {
101   /* FIXME: allow other clock rates */
102   gst_rtp_base_payload_set_options (pay, "application", TRUE, "SMPTE336M",
103       90000);
104
105   return gst_rtp_base_payload_set_outcaps (pay, NULL);
106 }
107
108 static GstFlowReturn
109 gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * basepayload, GstBuffer * buf)
110 {
111   GstFlowReturn ret = GST_FLOW_OK;
112   GstBufferList *list = NULL;
113   GstRtpKlvPay *pay;
114   GstMapInfo map;
115   GstBuffer *outbuf = NULL;
116   gsize offset;
117   guint mtu, rtp_header_size, max_payload_size;
118
119   pay = GST_RTP_KLV_PAY (basepayload);
120   mtu = GST_RTP_BASE_PAYLOAD_MTU (basepayload);
121
122   rtp_header_size = gst_rtp_buffer_calc_header_len (0);
123   max_payload_size = mtu - rtp_header_size;
124
125   gst_buffer_map (buf, &map, GST_MAP_READ);
126
127   if (map.size == 0)
128     goto done;
129
130   /* KLV coding shall use and only use a fixed 16-byte SMPTE-administered
131    * Universal Label, according to SMPTE 298M as Key (Rec. ITU R-BT.1653-1) */
132   if (map.size < 16 || GST_READ_UINT32_BE (map.data) != 0x060E2B34)
133     goto bad_input;
134
135   if (map.size > max_payload_size)
136     list = gst_buffer_list_new ();
137
138   GST_LOG_OBJECT (pay, "%" G_GSIZE_FORMAT " bytes of data to payload",
139       map.size);
140
141   offset = 0;
142   while (offset < map.size) {
143     GstBuffer *payloadbuf;
144     GstRTPBuffer rtp = { NULL };
145     guint payload_size;
146     guint bytes_left;
147
148     bytes_left = map.size - offset;
149     payload_size = MIN (bytes_left, max_payload_size);
150
151     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
152
153     if (payload_size == bytes_left) {
154       GST_LOG_OBJECT (pay, "last packet of KLV unit");
155       gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
156       gst_rtp_buffer_set_marker (&rtp, 1);
157       gst_rtp_buffer_unmap (&rtp);
158     }
159
160     GST_LOG_OBJECT (pay, "packet with payload size %u", payload_size);
161
162     payloadbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_MEMORY,
163         offset, payload_size);
164
165     /* join rtp header + payload memory parts */
166     outbuf = gst_buffer_append (outbuf, payloadbuf);
167
168     GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
169     GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (buf);
170
171     /* and add to list */
172     if (list != NULL)
173       gst_buffer_list_insert (list, -1, outbuf);
174
175     offset += payload_size;
176   }
177
178 done:
179
180   gst_buffer_unmap (buf, &map);
181   gst_buffer_unref (buf);
182
183   if (list != NULL)
184     ret = gst_rtp_base_payload_push_list (basepayload, list);
185   else if (outbuf != NULL)
186     ret = gst_rtp_base_payload_push (basepayload, outbuf);
187
188   return ret;
189
190 /* ERRORS */
191 bad_input:
192   {
193     GST_ERROR_OBJECT (pay, "Input doesn't look like a KLV packet, ignoring");
194     goto done;
195   }
196 }
197
198 gboolean
199 gst_rtp_klv_pay_plugin_init (GstPlugin * plugin)
200 {
201   return gst_element_register (plugin, "rtpklvpay",
202       GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_PAY);
203 }