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