1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2005> Edgard Lima <edgard.lima@gmail.com>
4 * Copyright (C) <2005> Zeeshan Ali <zeenix@gmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <string.h>
27 #include <gst/rtp/gstrtpbuffer.h>
28 #include <gst/audio/audio.h>
29 #include "gstrtpelements.h"
30 #include "gstrtppcmudepay.h"
31 #include "gstrtputils.h"
32
33 /* RtpPcmuDepay signals and args */
34 enum
35 {
36 /* FILL ME */
37 LAST_SIGNAL
38 };
39
40 enum
41 {
42 PROP_0
43 };
44
45 static GstStaticPadTemplate gst_rtp_pcmu_depay_sink_template =
46 GST_STATIC_PAD_TEMPLATE ("sink",
47 GST_PAD_SINK,
48 GST_PAD_ALWAYS,
49 GST_STATIC_CAPS ("application/x-rtp, "
50 "media = (string) \"audio\", "
51 "payload = (int) " GST_RTP_PAYLOAD_PCMU_STRING ", "
52 "clock-rate = (int) 8000; "
53 "application/x-rtp, "
54 "media = (string) \"audio\", "
55 "encoding-name = (string) \"PCMU\", clock-rate = (int) [1, MAX ]")
56 );
57
58 static GstStaticPadTemplate gst_rtp_pcmu_depay_src_template =
59 GST_STATIC_PAD_TEMPLATE ("src",
60 GST_PAD_SRC,
61 GST_PAD_ALWAYS,
62 GST_STATIC_CAPS ("audio/x-mulaw, "
63 "channels = (int) 1, rate = (int) [1, MAX ]")
64 );
65
66 static GstBuffer *gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload,
67 GstRTPBuffer * rtp);
68 static gboolean gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload,
69 GstCaps * caps);
70
71 #define gst_rtp_pcmu_depay_parent_class parent_class
72 G_DEFINE_TYPE (GstRtpPcmuDepay, gst_rtp_pcmu_depay,
73 GST_TYPE_RTP_BASE_DEPAYLOAD);
74 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtppcmudepay, "rtppcmudepay",
75 GST_RANK_SECONDARY, GST_TYPE_RTP_PCMU_DEPAY, rtp_element_init (plugin));
76
77 static void
gst_rtp_pcmu_depay_class_init(GstRtpPcmuDepayClass * klass)78 gst_rtp_pcmu_depay_class_init (GstRtpPcmuDepayClass * 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_pcmu_depay_src_template);
88 gst_element_class_add_static_pad_template (gstelement_class,
89 &gst_rtp_pcmu_depay_sink_template);
90
91 gst_element_class_set_static_metadata (gstelement_class,
92 "RTP PCMU depayloader", "Codec/Depayloader/Network/RTP",
93 "Extracts PCMU audio from RTP packets",
94 "Edgard Lima <edgard.lima@gmail.com>, Zeeshan Ali <zeenix@gmail.com>");
95
96 gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_pcmu_depay_process;
97 gstrtpbasedepayload_class->set_caps = gst_rtp_pcmu_depay_setcaps;
98 }
99
100 static void
gst_rtp_pcmu_depay_init(GstRtpPcmuDepay * rtppcmudepay)101 gst_rtp_pcmu_depay_init (GstRtpPcmuDepay * rtppcmudepay)
102 {
103 GstRTPBaseDepayload *depayload;
104
105 depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmudepay);
106
107 gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
108 }
109
110 static gboolean
gst_rtp_pcmu_depay_setcaps(GstRTPBaseDepayload * depayload,GstCaps * caps)111 gst_rtp_pcmu_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
112 {
113 GstCaps *srccaps;
114 GstStructure *structure;
115 gboolean ret;
116 gint clock_rate;
117
118 structure = gst_caps_get_structure (caps, 0);
119
120 if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
121 clock_rate = 8000; /* default */
122 depayload->clock_rate = clock_rate;
123
124 srccaps = gst_caps_new_simple ("audio/x-mulaw",
125 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
126 ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
127 gst_caps_unref (srccaps);
128
129 return ret;
130 }
131
132 static GstBuffer *
gst_rtp_pcmu_depay_process(GstRTPBaseDepayload * depayload,GstRTPBuffer * rtp)133 gst_rtp_pcmu_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
134 {
135 GstBuffer *outbuf = NULL;
136 guint len;
137 gboolean marker;
138
139 marker = gst_rtp_buffer_get_marker (rtp);
140
141 GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
142 gst_buffer_get_size (rtp->buffer), marker,
143 gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
144
145 len = gst_rtp_buffer_get_payload_len (rtp);
146 outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
147
148 if (outbuf) {
149 GST_BUFFER_DURATION (outbuf) =
150 gst_util_uint64_scale_int (len, GST_SECOND, depayload->clock_rate);
151
152 if (marker) {
153 /* mark start of talkspurt with RESYNC */
154 GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
155 }
156
157 gst_rtp_drop_non_audio_meta (depayload, outbuf);
158 }
159
160 return outbuf;
161 }
162