[v4l2videodecoder] Post message for number of buffers
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtppcmadepay.c
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 "gstrtppcmadepay.h"
30 #include "gstrtputils.h"
31
32 /* RtpPcmaDepay signals and args */
33 enum
34 {
35   /* FILL ME */
36   LAST_SIGNAL
37 };
38
39 enum
40 {
41   PROP_0
42 };
43
44 static GstStaticPadTemplate gst_rtp_pcma_depay_sink_template =
45     GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("application/x-rtp, "
49         "media = (string) \"audio\", "
50         "payload = (int) " GST_RTP_PAYLOAD_PCMA_STRING ", "
51         "clock-rate = (int) 8000;"
52         "application/x-rtp, "
53         "media = (string) \"audio\", "
54         "clock-rate = (int) [1, MAX ], encoding-name = (string) \"PCMA\"")
55     );
56
57 static GstStaticPadTemplate gst_rtp_pcma_depay_src_template =
58 GST_STATIC_PAD_TEMPLATE ("src",
59     GST_PAD_SRC,
60     GST_PAD_ALWAYS,
61     GST_STATIC_CAPS ("audio/x-alaw, channels = (int) 1, rate = (int) [1, MAX ]")
62     );
63
64 static GstBuffer *gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload,
65     GstRTPBuffer * rtp);
66 static gboolean gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload,
67     GstCaps * caps);
68
69 #define gst_rtp_pcma_depay_parent_class parent_class
70 G_DEFINE_TYPE (GstRtpPcmaDepay, gst_rtp_pcma_depay,
71     GST_TYPE_RTP_BASE_DEPAYLOAD);
72
73 static void
74 gst_rtp_pcma_depay_class_init (GstRtpPcmaDepayClass * klass)
75 {
76   GstElementClass *gstelement_class;
77   GstRTPBaseDepayloadClass *gstrtpbasedepayload_class;
78
79   gstelement_class = (GstElementClass *) klass;
80   gstrtpbasedepayload_class = (GstRTPBaseDepayloadClass *) klass;
81
82   gst_element_class_add_static_pad_template (gstelement_class,
83       &gst_rtp_pcma_depay_src_template);
84   gst_element_class_add_static_pad_template (gstelement_class,
85       &gst_rtp_pcma_depay_sink_template);
86
87   gst_element_class_set_static_metadata (gstelement_class,
88       "RTP PCMA depayloader", "Codec/Depayloader/Network/RTP",
89       "Extracts PCMA audio from RTP packets",
90       "Edgard Lima <edgard.lima@gmail.com>, Zeeshan Ali <zeenix@gmail.com>");
91
92   gstrtpbasedepayload_class->process_rtp_packet = gst_rtp_pcma_depay_process;
93   gstrtpbasedepayload_class->set_caps = gst_rtp_pcma_depay_setcaps;
94 }
95
96 static void
97 gst_rtp_pcma_depay_init (GstRtpPcmaDepay * rtppcmadepay)
98 {
99   GstRTPBaseDepayload *depayload;
100
101   depayload = GST_RTP_BASE_DEPAYLOAD (rtppcmadepay);
102
103   gst_pad_use_fixed_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload));
104 }
105
106 static gboolean
107 gst_rtp_pcma_depay_setcaps (GstRTPBaseDepayload * depayload, GstCaps * caps)
108 {
109   GstCaps *srccaps;
110   GstStructure *structure;
111   gboolean ret;
112   gint clock_rate;
113
114   structure = gst_caps_get_structure (caps, 0);
115
116   if (!gst_structure_get_int (structure, "clock-rate", &clock_rate))
117     clock_rate = 8000;          /* default */
118   depayload->clock_rate = clock_rate;
119
120   srccaps = gst_caps_new_simple ("audio/x-alaw",
121       "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, clock_rate, NULL);
122   ret = gst_pad_set_caps (GST_RTP_BASE_DEPAYLOAD_SRCPAD (depayload), srccaps);
123   gst_caps_unref (srccaps);
124
125   return ret;
126 }
127
128 static GstBuffer *
129 gst_rtp_pcma_depay_process (GstRTPBaseDepayload * depayload, GstRTPBuffer * rtp)
130 {
131   GstBuffer *outbuf = NULL;
132   gboolean marker;
133   guint len;
134
135   marker = gst_rtp_buffer_get_marker (rtp);
136
137   GST_DEBUG ("process : got %" G_GSIZE_FORMAT " bytes, mark %d ts %u seqn %d",
138       gst_buffer_get_size (rtp->buffer), marker,
139       gst_rtp_buffer_get_timestamp (rtp), gst_rtp_buffer_get_seq (rtp));
140
141   len = gst_rtp_buffer_get_payload_len (rtp);
142   outbuf = gst_rtp_buffer_get_payload_buffer (rtp);
143
144   if (outbuf) {
145     GST_BUFFER_DURATION (outbuf) =
146         gst_util_uint64_scale_int (len, GST_SECOND, depayload->clock_rate);
147
148     if (marker) {
149       /* mark start of talkspurt with RESYNC */
150       GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_RESYNC);
151     }
152
153     gst_rtp_drop_non_audio_meta (depayload, outbuf);
154   }
155
156   return outbuf;
157 }
158
159 gboolean
160 gst_rtp_pcma_depay_plugin_init (GstPlugin * plugin)
161 {
162   return gst_element_register (plugin, "rtppcmadepay",
163       GST_RANK_SECONDARY, GST_TYPE_RTP_PCMA_DEPAY);
164 }