rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL8pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  * Copyright (C) <2015> GE Intelligent Platforms Embedded Systems, Inc.
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-rtpL8pay
23  * @see_also: rtpL8depay
24  *
25  * Payload raw audio into RTP packets according to RFC 3551.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3551.txt
27  *
28  * ## Example pipeline
29  *
30  * |[
31  * gst-launch -v audiotestsrc ! audioconvert ! rtpL8pay ! udpsink
32  * ]| This example pipeline will payload raw audio. Refer to
33  * the rtpL8depay example to depayload and play the RTP stream.
34  */
35
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39
40 #include <string.h>
41
42 #include <gst/audio/audio.h>
43 #include <gst/rtp/gstrtpbuffer.h>
44
45 #include "gstrtpL8pay.h"
46 #include "gstrtpchannels.h"
47
48 GST_DEBUG_CATEGORY_STATIC (rtpL8pay_debug);
49 #define GST_CAT_DEFAULT (rtpL8pay_debug)
50
51 static GstStaticPadTemplate gst_rtp_L8_pay_sink_template =
52 GST_STATIC_PAD_TEMPLATE ("sink",
53     GST_PAD_SINK,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("audio/x-raw, "
56         "format = (string) U8, "
57         "layout = (string) interleaved, "
58         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
59     );
60
61 static GstStaticPadTemplate gst_rtp_L8_pay_src_template =
62     GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-rtp, "
66         "media = (string) audio, "
67         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
68         "clock-rate = (int) [ 1, MAX ], "
69         "encoding-name = (string) L8, " "channels = (int) [ 1, MAX ];")
70     );
71
72 static gboolean gst_rtp_L8_pay_setcaps (GstRTPBasePayload * basepayload,
73     GstCaps * caps);
74 static GstCaps *gst_rtp_L8_pay_getcaps (GstRTPBasePayload * rtppayload,
75     GstPad * pad, GstCaps * filter);
76 static GstFlowReturn
77 gst_rtp_L8_pay_handle_buffer (GstRTPBasePayload * basepayload,
78     GstBuffer * buffer);
79
80 #define gst_rtp_L8_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpL8Pay, gst_rtp_L8_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
82
83 static void
84 gst_rtp_L8_pay_class_init (GstRtpL8PayClass * klass)
85 {
86   GstElementClass *gstelement_class;
87   GstRTPBasePayloadClass *gstrtpbasepayload_class;
88
89   gstelement_class = (GstElementClass *) klass;
90   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
91
92   gstrtpbasepayload_class->set_caps = gst_rtp_L8_pay_setcaps;
93   gstrtpbasepayload_class->get_caps = gst_rtp_L8_pay_getcaps;
94   gstrtpbasepayload_class->handle_buffer = gst_rtp_L8_pay_handle_buffer;
95
96   gst_element_class_add_pad_template (gstelement_class,
97       gst_static_pad_template_get (&gst_rtp_L8_pay_src_template));
98   gst_element_class_add_pad_template (gstelement_class,
99       gst_static_pad_template_get (&gst_rtp_L8_pay_sink_template));
100
101   gst_element_class_set_static_metadata (gstelement_class,
102       "RTP audio payloader", "Codec/Payloader/Network/RTP",
103       "Payload-encode Raw audio into RTP packets (RFC 3551)",
104       "Wim Taymans <wim.taymans@gmail.com>, "
105       "GE Intelligent Platforms Embedded Systems, Inc.");
106
107   GST_DEBUG_CATEGORY_INIT (rtpL8pay_debug, "rtpL8pay", 0, "L8 RTP Payloader");
108 }
109
110 static void
111 gst_rtp_L8_pay_init (GstRtpL8Pay * rtpL8pay)
112 {
113   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
114
115   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL8pay);
116
117   /* tell rtpbaseaudiopayload that this is a sample based codec */
118   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
119 }
120
121 static gboolean
122 gst_rtp_L8_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
123 {
124   GstRtpL8Pay *rtpL8pay;
125   gboolean res;
126   gchar *params;
127   GstAudioInfo *info;
128   const GstRTPChannelOrder *order;
129   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
130
131   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
132   rtpL8pay = GST_RTP_L8_PAY (basepayload);
133
134   info = &rtpL8pay->info;
135   gst_audio_info_init (info);
136   if (!gst_audio_info_from_caps (info, caps))
137     goto invalid_caps;
138
139   order = gst_rtp_channels_get_by_pos (info->channels, info->position);
140   rtpL8pay->order = order;
141
142   gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L8",
143       info->rate);
144   params = g_strdup_printf ("%d", info->channels);
145
146   if (!order && info->channels > 2) {
147     GST_ELEMENT_WARNING (rtpL8pay, STREAM, DECODE,
148         (NULL), ("Unknown channel order for %d channels", info->channels));
149   }
150
151   if (order && order->name) {
152     res = gst_rtp_base_payload_set_outcaps (basepayload,
153         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
154         info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
155   } else {
156     res = gst_rtp_base_payload_set_outcaps (basepayload,
157         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
158         info->channels, NULL);
159   }
160
161   g_free (params);
162
163   /* octet-per-sample is # channels for L8 */
164   gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
165       info->channels);
166
167   return res;
168
169   /* ERRORS */
170 invalid_caps:
171   {
172     GST_DEBUG_OBJECT (rtpL8pay, "invalid caps");
173     return FALSE;
174   }
175 }
176
177 static GstCaps *
178 gst_rtp_L8_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
179     GstCaps * filter)
180 {
181   GstCaps *otherpadcaps;
182   GstCaps *caps;
183
184   caps = gst_pad_get_pad_template_caps (pad);
185
186   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
187   if (otherpadcaps) {
188     if (!gst_caps_is_empty (otherpadcaps)) {
189       GstStructure *structure;
190       gint channels;
191       gint rate;
192
193       structure = gst_caps_get_structure (otherpadcaps, 0);
194       caps = gst_caps_make_writable (caps);
195
196       if (gst_structure_get_int (structure, "channels", &channels)) {
197         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
198       }
199
200       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
201         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
202       }
203
204     }
205     gst_caps_unref (otherpadcaps);
206   }
207
208   if (filter) {
209     GstCaps *tcaps = caps;
210
211     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
212     gst_caps_unref (tcaps);
213   }
214
215   return caps;
216 }
217
218 static GstFlowReturn
219 gst_rtp_L8_pay_handle_buffer (GstRTPBasePayload * basepayload,
220     GstBuffer * buffer)
221 {
222   GstRtpL8Pay *rtpL8pay;
223
224   rtpL8pay = GST_RTP_L8_PAY (basepayload);
225   buffer = gst_buffer_make_writable (buffer);
226
227   if (rtpL8pay->order &&
228       !gst_audio_buffer_reorder_channels (buffer, rtpL8pay->info.finfo->format,
229           rtpL8pay->info.channels, rtpL8pay->info.position,
230           rtpL8pay->order->pos)) {
231     return GST_FLOW_ERROR;
232   }
233
234   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
235       buffer);
236 }
237
238 gboolean
239 gst_rtp_L8_pay_plugin_init (GstPlugin * plugin)
240 {
241   return gst_element_register (plugin, "rtpL8pay",
242       GST_RANK_SECONDARY, GST_TYPE_RTP_L8_PAY);
243 }