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-rtpac3depay
22  * @see_also: rtpac3pay
23  *
24  * Extract AC3 audio from RTP packets according to RFC 4184.
25  * For detailed information see: http://www.rfc-editor.org/rfc/rfc4184.txt
26  *
27  * <refsect2>
28  * <title>Example pipeline</title>
29  * |[
30  * gst-launch-1.0 udpsrc caps='application/x-rtp, media=(string)audio, clock-rate=(int)44100, encoding-name=(string)AC3, payload=(int)96' ! rtpac3depay ! a52dec ! pulsesink
31  * ]| This example pipeline will depayload and decode an RTP AC3 stream. Refer to
32  * the rtpac3pay example to create the RTP stream.
33  * </refsect2>
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #  include "config.h"
38 #endif
39 
40 #include <gst/rtp/gstrtpbuffer.h>
41 #include <gst/audio/audio.h>
42 
43 #include <string.h>
44 #include "gstrtpac3depay.h"
45 #include "gstrtputils.h"
46 
47 GST_DEBUG_CATEGORY_STATIC (rtpac3depay_debug);
48 #define GST_CAT_DEFAULT (rtpac3depay_debug)
49 
50 static GstStaticPadTemplate gst_rtp_ac3_depay_src_template =
51 GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("audio/ac3")
55     );
56 
57 static GstStaticPadTemplate gst_rtp_ac3_depay_sink_template =
58 GST_STATIC_PAD_TEMPLATE ("sink",
59     GST_PAD_SINK,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("application/x-rtp, "
62         "media = (string) \"audio\", "
63         "clock-rate = (int) { 32000, 44100, 48000 }, "
64         "encoding-name = (string) \"AC3\"")
65     );
66 
67 G_DEFINE_TYPE (GstRtpAC3Depay, gst_rtp_ac3_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
68 
69 static gboolean gst_rtp_ac3_depay_setcaps (GstRTPBaseDepayload * depayload,
70     GstCaps * caps);
71 static GstBuffer *gst_rtp_ac3_depay_process (GstRTPBaseDepayload * depayload,
72     GstRTPBuffer * rtp);
73 
74 static void
gst_rtp_ac3_depay_class_init(GstRtpAC3DepayClass * klass)75 gst_rtp_ac3_depay_class_init (GstRtpAC3DepayClass * klass)
76 {
77   GstElementClass *gstelement_class;
78   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
79 
80   gstelement_class = (GstElementClass *) klass;
81   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
82 
83   gst_element_class_add_static_pad_template (gstelement_class,
84       &gst_rtp_ac3_depay_src_template);
85   gst_element_class_add_static_pad_template (gstelement_class,
86       &gst_rtp_ac3_depay_sink_template);
87 
88   gst_element_class_set_static_metadata (gstelement_class,
89       "RTP AC3 depayloader", "Codec/Depayloader/Network/RTP",
90       "Extracts AC3 audio from RTP packets (RFC 4184)",
91       "Wim Taymans <wim.taymans@gmail.com>");
92 
93   gstrtpbasedepayload_class->set_caps = gst_rtp_ac3_depay_setcaps;
94   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_ac3_depay_process;
95 
96   GST_DEBUG_CATEGORY_INIT (rtpac3depay_debug, "rtpac3depay", 0,
97       "AC3 Audio RTP Depayloader");
98 }
99 
100 static void
gst_rtp_ac3_depay_init(GstRtpAC3Depay * rtpac3depay)101 gst_rtp_ac3_depay_init (GstRtpAC3Depay * rtpac3depay)
102 {
103   /* needed because of G_DEFINE_TYPE */
104 }
105 
106 static gboolean
gst_rtp_ac3_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)107 gst_rtp_ac3_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
108 {
109   GstStructure *structure;
110   gint clock_rate;
111   GstCaps *srccaps;
112   gboolean res;
113 
114   structure = gst_caps_get_structure (caps, 0);
115 
116   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
117     clock_rate = 90000;         /* default */
118   depayload->clock_rate = clock_rate;
119 
120   srccaps = gst_caps_new_empty_simple ("audio/ac3");
121   res = gst_pad_set_caps (depayload->srcpad, srccaps);
122   gst_caps_unref (srccaps);
123 
124   return res;
125 }
126 
127 static GstBuffer *
gst_rtp_ac3_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)128 gst_rtp_ac3_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
129 {
130   GstRtpAC3Depay *rtpac3depay;
131   GstBuffer *outbuf;
132   guint8 *payload;
133   guint16 FT, NF;
134 
135   rtpac3depay = GST_RTP_AC3_DEPAY (depayload);
136 
137   if (gst_rtp_buffer_get_payload_len (rtp) < 2)
138     goto empty_packet;
139 
140   payload = gst_rtp_buffer_get_payload (rtp);
141 
142   /* strip off header
143    *
144    *  0                   1
145    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
146    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147    * |    MBZ    | FT|       NF      |
148    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
149    */
150   FT = payload[0] & 0x3;
151   NF = payload[1];
152 
153   GST_DEBUG_OBJECT (rtpac3depay, "FT: %d, NF: %d", FT, NF);
154 
155   /* We don't bother with fragmented packets yet */
156   outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 2, -1);
157 
158   if (outbuf) {
159     gst_rtp_drop_non_audio_meta (rtpac3depay, outbuf);
160     GST_DEBUG_OBJECT (rtpac3depay, "pushing buffer of size %" G_GSIZE_FORMAT,
161         gst_buffer_get_size (outbuf));
162   }
163 
164   return outbuf;
165 
166   /* ERRORS */
167 empty_packet:
168   {
169     GST_ELEMENT_WARNING (rtpac3depay, STREAM, DECODE,
170         ("Empty Payload."), (NULL));
171     return NULL;
172   }
173 }
174 
175 gboolean
gst_rtp_ac3_depay_plugin_init(GstPlugin * plugin)176 gst_rtp_ac3_depay_plugin_init (GstPlugin * plugin)
177 {
178   return gst_element_register (plugin, "rtpac3depay",
179       GST_RANK_SECONDARY, GST_TYPE_RTP_AC3_DEPAY);
180 }
181