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