Merge remote-tracking branch 'remotes/origin/upstream/1.6' into tizen
[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 #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
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_pad_template (element_class,
78       gst_static_pad_template_get (&src_template));
79   gst_element_class_add_pad_template (element_class,
80       gst_static_pad_template_get (&sink_template));
81
82   gst_element_class_set_static_metadata (element_class,
83       "RTP KLV Payloader", "Codec/Payloader/Network/RTP",
84       "Payloads KLV (SMPTE ST 336) metadata as RTP packets",
85       "Tim-Philipp Müller <tim@centricular.com>");
86
87   rtpbasepay_class = (GstRTPBasePayloadClass *) klass;
88
89   rtpbasepay_class->set_caps = gst_rtp_klv_pay_setcaps;
90   rtpbasepay_class->handle_buffer = gst_rtp_klv_pay_handle_buffer;
91 }
92
93 static void
94 gst_rtp_klv_pay_init (GstRtpKlvPay * klvpay)
95 {
96   /* nothing to do here yet */
97 }
98
99 static gboolean
100 gst_rtp_klv_pay_setcaps (GstRTPBasePayload * pay, GstCaps * caps)
101 {
102   /* FIXME: allow other clock rates */
103   gst_rtp_base_payload_set_options (pay, "application", TRUE, "SMPTE336M",
104       90000);
105
106   return gst_rtp_base_payload_set_outcaps (pay, NULL);
107 }
108
109 static GstFlowReturn
110 gst_rtp_klv_pay_handle_buffer (GstRTPBasePayload * basepayload, GstBuffer * buf)
111 {
112   GstFlowReturn ret = GST_FLOW_OK;
113   GstBufferList *list = NULL;
114   GstRtpKlvPay *pay;
115   GstMapInfo map;
116   GstBuffer *outbuf = NULL;
117   gsize offset;
118   guint mtu, rtp_header_size, max_payload_size;
119
120   pay = GST_RTP_KLV_PAY (basepayload);
121   mtu = GST_RTP_BASE_PAYLOAD_MTU (basepayload);
122
123   rtp_header_size = gst_rtp_buffer_calc_header_len (0);
124   max_payload_size = mtu - rtp_header_size;
125
126   gst_buffer_map (buf, &map, GST_MAP_READ);
127
128   if (map.size == 0)
129     goto done;
130
131   /* KLV coding shall use and only use a fixed 16-byte SMPTE-administered
132    * Universal Label, according to SMPTE 298M as Key (Rec. ITU R-BT.1653-1) */
133   if (map.size < 16 || GST_READ_UINT32_BE (map.data) != 0x060E2B34)
134     goto bad_input;
135
136   if (map.size > max_payload_size)
137     list = gst_buffer_list_new ();
138
139   GST_LOG_OBJECT (pay, "%" G_GSIZE_FORMAT " bytes of data to payload",
140       map.size);
141
142   offset = 0;
143   while (offset < map.size) {
144     GstBuffer *payloadbuf;
145     GstRTPBuffer rtp = { NULL };
146     guint payload_size;
147     guint bytes_left;
148
149     bytes_left = map.size - offset;
150     payload_size = MIN (bytes_left, max_payload_size);
151
152     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
153
154     if (payload_size == bytes_left) {
155       GST_LOG_OBJECT (pay, "last packet of KLV unit");
156       gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
157       gst_rtp_buffer_set_marker (&rtp, 1);
158       gst_rtp_buffer_unmap (&rtp);
159     }
160
161     GST_LOG_OBJECT (pay, "packet with payload size %u", payload_size);
162
163     gst_rtp_copy_meta (GST_ELEMENT_CAST (pay), outbuf, buf, 0);
164
165     payloadbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_MEMORY,
166         offset, payload_size);
167
168     /* join rtp header + payload memory parts */
169     outbuf = gst_buffer_append (outbuf, payloadbuf);
170
171     GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (buf);
172     GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (buf);
173
174     /* and add to list */
175     if (list != NULL)
176       gst_buffer_list_insert (list, -1, outbuf);
177
178     offset += payload_size;
179   }
180
181 done:
182
183   gst_buffer_unmap (buf, &map);
184   gst_buffer_unref (buf);
185
186   if (list != NULL)
187     ret = gst_rtp_base_payload_push_list (basepayload, list);
188   else if (outbuf != NULL)
189     ret = gst_rtp_base_payload_push (basepayload, outbuf);
190
191   return ret;
192
193 /* ERRORS */
194 bad_input:
195   {
196     GST_ERROR_OBJECT (pay, "Input doesn't look like a KLV packet, ignoring");
197     goto done;
198   }
199 }
200
201 gboolean
202 gst_rtp_klv_pay_plugin_init (GstPlugin * plugin)
203 {
204   return gst_element_register (plugin, "rtpklvpay",
205       GST_RANK_SECONDARY, GST_TYPE_RTP_KLV_PAY);
206 }