1 /* GStreamer
2  * Copyright (C) <2005> 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23 
24 #include <gst/rtp/gstrtpbuffer.h>
25 #include <gst/audio/audio.h>
26 
27 #include <string.h>
28 #include "gstrtpmpadepay.h"
29 #include "gstrtputils.h"
30 
31 GST_DEBUG_CATEGORY_STATIC (rtpmpadepay_debug);
32 #define GST_CAT_DEFAULT (rtpmpadepay_debug)
33 
34 static GstStaticPadTemplate gst_rtp_mpa_depay_src_template =
35 GST_STATIC_PAD_TEMPLATE ("src",
36     GST_PAD_SRC,
37     GST_PAD_ALWAYS,
38     GST_STATIC_CAPS ("audio/mpeg, " "mpegversion = (int) 1")
39     );
40 
41 static GstStaticPadTemplate gst_rtp_mpa_depay_sink_template =
42     GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("application/x-rtp, "
46         "media = (string) \"audio\", "
47         "payload = (int) " GST_RTP_PAYLOAD_MPA_STRING ", "
48         "clock-rate = (int) 90000 ;"
49         "application/x-rtp, "
50         "media = (string) \"audio\", "
51         "encoding-name = (string) \"MPA\", clock-rate = (int) [1, MAX]")
52     );
53 
54 #define gst_rtp_mpa_depay_parent_class parent_class
55 G_DEFINE_TYPE (GstRtpMPADepay, gst_rtp_mpa_depay, GST_TYPE_RTP_BASE_DEPAYLOAD);
56 
57 static gboolean gst_rtp_mpa_depay_setcaps (GstRTPBaseDepayload * depayload,
58     GstCaps * caps);
59 static GstBuffer *gst_rtp_mpa_depay_process (GstRTPBaseDepayload * depayload,
60     GstRTPBuffer * rtp);
61 
62 static void
gst_rtp_mpa_depay_class_init(GstRtpMPADepayClass * klass)63 gst_rtp_mpa_depay_class_init (GstRtpMPADepayClass * klass)
64 {
65   GstElementClass *gstelement_class;
66   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
67 
68   GST_DEBUG_CATEGORY_INIT (rtpmpadepay_debug, "rtpmpadepay", 0,
69       "MPEG Audio RTP Depayloader");
70 
71   gstelement_class = (GstElementClass *) klass;
72   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
73 
74   gst_element_class_add_static_pad_template (gstelement_class,
75       &gst_rtp_mpa_depay_src_template);
76   gst_element_class_add_static_pad_template (gstelement_class,
77       &gst_rtp_mpa_depay_sink_template);
78 
79   gst_element_class_set_static_metadata (gstelement_class,
80       "RTP MPEG audio depayloader", "Codec/Depayloader/Network/RTP",
81       "Extracts MPEG audio from RTP packets (RFC 2038)",
82       "Wim Taymans <wim.taymans@gmail.com>");
83 
84   gstrtpbasedepayload_class->set_caps = gst_rtp_mpa_depay_setcaps;
85   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_mpa_depay_process;
86 }
87 
88 static void
gst_rtp_mpa_depay_init(GstRtpMPADepay * rtpmpadepay)89 gst_rtp_mpa_depay_init (GstRtpMPADepay * rtpmpadepay)
90 {
91 }
92 
93 static gboolean
gst_rtp_mpa_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)94 gst_rtp_mpa_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
95 {
96   GstStructure *structure;
97   GstCaps *outcaps;
98   gint clock_rate;
99   gboolean res;
100 
101   structure = gst_caps_get_structure (caps, 0);
102 
103   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
104     clock_rate = 90000;
105   depayload->clock_rate = clock_rate;
106 
107   outcaps =
108       gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, 1, NULL);
109   res = gst_pad_set_caps (depayload->srcpad, outcaps);
110   gst_caps_unref (outcaps);
111 
112   return res;
113 }
114 
115 static GstBuffer *
gst_rtp_mpa_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)116 gst_rtp_mpa_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
117 {
118   GstRtpMPADepay *rtpmpadepay;
119   GstBuffer *outbuf;
120   gint payload_len;
121 #if 0
122   guint8 *payload;
123   guint16 frag_offset;
124 #endif
125   gboolean marker;
126 
127   rtpmpadepay = GST_RTP_MPA_DEPAY (depayload);
128 
129   payload_len = gst_rtp_buffer_get_payload_len (rtp);
130 
131   if (payload_len <= 4)
132     goto empty_packet;
133 
134 #if 0
135   payload = gst_rtp_buffer_get_payload (&rtp);
136   /* strip off header
137    *
138    *  0                   1                   2                   3
139    *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
140    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141    * |             MBZ               |          Frag_offset          |
142    * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143    */
144   frag_offset = (payload[2] << 8) | payload[3];
145 #endif
146 
147   /* subbuffer skipping the 4 header bytes */
148   outbuf = gst_rtp_buffer_get_payload_subbuffer (rtp, 4, -1);
149   marker = gst_rtp_buffer_get_marker (rtp);
150 
151   if (marker) {
152     /* mark start of talkspurt with RESYNC */
153     GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
154   }
155   GST_DEBUG_OBJECT (rtpmpadepay,
156       "gst_rtp_mpa_depay_chain: pushing buffer of size %" G_GSIZE_FORMAT "",
157       gst_buffer_get_size (outbuf));
158 
159   if (outbuf) {
160     gst_rtp_drop_non_audio_meta (rtpmpadepay, outbuf);
161   }
162 
163   /* FIXME, we can push half mpeg frames when they are split over multiple
164    * RTP packets */
165   return outbuf;
166 
167   /* ERRORS */
168 empty_packet:
169   {
170     GST_ELEMENT_WARNING (rtpmpadepay, STREAM, DECODE,
171         ("Empty Payload."), (NULL));
172     return NULL;
173   }
174 }
175 
176 gboolean
gst_rtp_mpa_depay_plugin_init(GstPlugin * plugin)177 gst_rtp_mpa_depay_plugin_init (GstPlugin * plugin)
178 {
179   return gst_element_register (plugin, "rtpmpadepay",
180       GST_RANK_SECONDARY, GST_TYPE_RTP_MPA_DEPAY);
181 }
182