rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpL24pay.c
1 /* GStreamer
2  * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /**
21  * SECTION:element-rtpL24pay
22  * @title: rtpL24pay
23  * @see_also: rtpL24depay
24  *
25  * Payload raw 24-bit audio into RTP packets according to RFC 3190, section 4.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc3190.txt
27  *
28  * ## Example pipeline
29  * |[
30  * gst-launch-1.0 -v audiotestsrc ! audioconvert ! rtpL24pay ! udpsink
31  * ]| This example pipeline will payload raw audio. Refer to
32  * the rtpL24depay example to depayload and play the RTP stream.
33  *
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 "gstrtpL24pay.h"
46 #include "gstrtpchannels.h"
47
48 GST_DEBUG_CATEGORY_STATIC (rtpL24pay_debug);
49 #define GST_CAT_DEFAULT (rtpL24pay_debug)
50
51 static GstStaticPadTemplate gst_rtp_L24_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) S24BE, "
57         "layout = (string) interleaved, "
58         "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
59     );
60
61 static GstStaticPadTemplate gst_rtp_L24_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) [ 96, 127 ], "
68         "clock-rate = (int) [ 1, MAX ], "
69         "encoding-name = (string) \"L24\", " "channels = (int) [ 1, MAX ];")
70     );
71
72 static gboolean gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload,
73     GstCaps * caps);
74 static GstCaps *gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload,
75     GstPad * pad, GstCaps * filter);
76 static GstFlowReturn
77 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
78     GstBuffer * buffer);
79
80 #define gst_rtp_L24_pay_parent_class parent_class
81 G_DEFINE_TYPE (GstRtpL24Pay, gst_rtp_L24_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
82
83 static void
84 gst_rtp_L24_pay_class_init (GstRtpL24PayClass * 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_L24_pay_setcaps;
93   gstrtpbasepayload_class->get_caps = gst_rtp_L24_pay_getcaps;
94   gstrtpbasepayload_class->handle_buffer = gst_rtp_L24_pay_handle_buffer;
95
96   gst_element_class_add_static_pad_template (gstelement_class,
97       &gst_rtp_L24_pay_src_template);
98   gst_element_class_add_static_pad_template (gstelement_class,
99       &gst_rtp_L24_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 24-bit audio into RTP packets (RFC 3190)",
104       "Wim Taymans <wim.taymans@gmail.com>,"
105       "David Holroyd <dave@badgers-in-foil.co.uk>");
106
107   GST_DEBUG_CATEGORY_INIT (rtpL24pay_debug, "rtpL24pay", 0,
108       "L24 RTP Payloader");
109 }
110
111 static void
112 gst_rtp_L24_pay_init (GstRtpL24Pay * rtpL24pay)
113 {
114   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
115
116   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpL24pay);
117
118   /* tell rtpbaseaudiopayload that this is a sample based codec */
119   gst_rtp_base_audio_payload_set_sample_based (rtpbaseaudiopayload);
120 }
121
122 static gboolean
123 gst_rtp_L24_pay_setcaps (GstRTPBasePayload * basepayload, GstCaps * caps)
124 {
125   GstRtpL24Pay *rtpL24pay;
126   gboolean res;
127   gchar *params;
128   GstAudioInfo *info;
129   const GstRTPChannelOrder *order;
130   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
131
132   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (basepayload);
133   rtpL24pay = GST_RTP_L24_PAY (basepayload);
134
135   info = &rtpL24pay->info;
136   gst_audio_info_init (info);
137   if (!gst_audio_info_from_caps (info, caps))
138     goto invalid_caps;
139
140   order = gst_rtp_channels_get_by_pos (info->channels, info->position);
141   rtpL24pay->order = order;
142
143   gst_rtp_base_payload_set_options (basepayload, "audio", TRUE, "L24",
144       info->rate);
145   params = g_strdup_printf ("%d", info->channels);
146
147   if (!order && info->channels > 2) {
148     GST_ELEMENT_WARNING (rtpL24pay, STREAM, DECODE,
149         (NULL), ("Unknown channel order for %d channels", info->channels));
150   }
151
152   if (order && order->name) {
153     res = gst_rtp_base_payload_set_outcaps (basepayload,
154         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
155         info->channels, "channel-order", G_TYPE_STRING, order->name, NULL);
156   } else {
157     res = gst_rtp_base_payload_set_outcaps (basepayload,
158         "encoding-params", G_TYPE_STRING, params, "channels", G_TYPE_INT,
159         info->channels, NULL);
160   }
161
162   g_free (params);
163
164   /* octet-per-sample is 3 * channels for L24 */
165   gst_rtp_base_audio_payload_set_sample_options (rtpbaseaudiopayload,
166       3 * info->channels);
167
168   return res;
169
170   /* ERRORS */
171 invalid_caps:
172   {
173     GST_DEBUG_OBJECT (rtpL24pay, "invalid caps");
174     return FALSE;
175   }
176 }
177
178 static GstCaps *
179 gst_rtp_L24_pay_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
180     GstCaps * filter)
181 {
182   GstCaps *otherpadcaps;
183   GstCaps *caps;
184
185   caps = gst_pad_get_pad_template_caps (pad);
186
187   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
188   if (otherpadcaps) {
189     if (!gst_caps_is_empty (otherpadcaps)) {
190       GstStructure *structure;
191       gint channels;
192       gint rate;
193
194       structure = gst_caps_get_structure (otherpadcaps, 0);
195       caps = gst_caps_make_writable (caps);
196
197       if (gst_structure_get_int (structure, "channels", &channels)) {
198         gst_caps_set_simple (caps, "channels", G_TYPE_INT, channels, NULL);
199       }
200
201       if (gst_structure_get_int (structure, "clock-rate", &rate)) {
202         gst_caps_set_simple (caps, "rate", G_TYPE_INT, rate, NULL);
203       }
204
205     }
206     gst_caps_unref (otherpadcaps);
207   }
208
209   if (filter) {
210     GstCaps *tcaps = caps;
211
212     caps = gst_caps_intersect_full (filter, tcaps, GST_CAPS_INTERSECT_FIRST);
213     gst_caps_unref (tcaps);
214   }
215
216   return caps;
217 }
218
219 static GstFlowReturn
220 gst_rtp_L24_pay_handle_buffer (GstRTPBasePayload * basepayload,
221     GstBuffer * buffer)
222 {
223   GstRtpL24Pay *rtpL24pay;
224
225   rtpL24pay = GST_RTP_L24_PAY (basepayload);
226   buffer = gst_buffer_make_writable (buffer);
227
228   if (rtpL24pay->order &&
229       !gst_audio_buffer_reorder_channels (buffer, rtpL24pay->info.finfo->format,
230           rtpL24pay->info.channels, rtpL24pay->info.position,
231           rtpL24pay->order->pos)) {
232     return GST_FLOW_ERROR;
233   }
234
235   return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->handle_buffer (basepayload,
236       buffer);
237 }
238
239 gboolean
240 gst_rtp_L24_pay_plugin_init (GstPlugin * plugin)
241 {
242   return gst_element_register (plugin, "rtpL24pay",
243       GST_RANK_SECONDARY, GST_TYPE_RTP_L24_PAY);
244 }