rtpjpegdepay: fix logic error when checking if an EOI is present
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / gst / rtp / gstrtpbvpay.c
1 /* GStreamer
2  * Copyright (C) <2009> 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-rtpbvpay
22  * @title: rtpbvpay
23  * @see_also: rtpbvdepay
24  *
25  * Payload BroadcomVoice audio into RTP packets according to RFC 4298.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc4298.txt
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <gst/rtp/gstrtpbuffer.h>
37 #include "gstrtpelements.h"
38 #include "gstrtpbvpay.h"
39
40 GST_DEBUG_CATEGORY_STATIC (rtpbvpay_debug);
41 #define GST_CAT_DEFAULT (rtpbvpay_debug)
42
43 static GstStaticPadTemplate gst_rtp_bv_pay_sink_template =
44 GST_STATIC_PAD_TEMPLATE ("sink",
45     GST_PAD_SINK,
46     GST_PAD_ALWAYS,
47     GST_STATIC_CAPS ("audio/x-bv, " "mode = (int) {16, 32}")
48     );
49
50 static GstStaticPadTemplate gst_rtp_bv_pay_src_template =
51     GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS ("application/x-rtp, "
55         "media = (string) \"audio\", "
56         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
57         "clock-rate = (int) 8000, "
58         "encoding-name = (string) \"BV16\";"
59         "application/x-rtp, "
60         "media = (string) \"audio\", "
61         "payload = (int) " GST_RTP_PAYLOAD_DYNAMIC_STRING ", "
62         "clock-rate = (int) 16000, " "encoding-name = (string) \"BV32\"")
63     );
64
65
66 static GstCaps *gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * payload,
67     GstPad * pad, GstCaps * filter);
68 static gboolean gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * payload,
69     GstCaps * caps);
70
71 #define gst_rtp_bv_pay_parent_class parent_class
72 G_DEFINE_TYPE (GstRTPBVPay, gst_rtp_bv_pay, GST_TYPE_RTP_BASE_AUDIO_PAYLOAD);
73 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (rtpbvpay, "rtpbvpay", GST_RANK_SECONDARY,
74     GST_TYPE_RTP_BV_PAY, rtp_element_init (plugin));
75
76 static void
77 gst_rtp_bv_pay_class_init (GstRTPBVPayClass * klass)
78 {
79   GstElementClass *gstelement_class;
80   GstRTPBasePayloadClass *gstrtpbasepayload_class;
81
82   GST_DEBUG_CATEGORY_INIT (rtpbvpay_debug, "rtpbvpay", 0,
83       "BroadcomVoice audio RTP payloader");
84
85   gstelement_class = (GstElementClass *) klass;
86   gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
87
88   gst_element_class_add_static_pad_template (gstelement_class,
89       &gst_rtp_bv_pay_sink_template);
90   gst_element_class_add_static_pad_template (gstelement_class,
91       &gst_rtp_bv_pay_src_template);
92
93   gst_element_class_set_static_metadata (gstelement_class, "RTP BV Payloader",
94       "Codec/Payloader/Network/RTP",
95       "Packetize BroadcomVoice audio streams into RTP packets (RFC 4298)",
96       "Wim Taymans <wim.taymans@collabora.co.uk>");
97
98   gstrtpbasepayload_class->set_caps = gst_rtp_bv_pay_sink_setcaps;
99   gstrtpbasepayload_class->get_caps = gst_rtp_bv_pay_sink_getcaps;
100 }
101
102 static void
103 gst_rtp_bv_pay_init (GstRTPBVPay * rtpbvpay)
104 {
105   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
106
107   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbvpay);
108
109   rtpbvpay->mode = -1;
110
111   /* tell rtpbaseaudiopayload that this is a frame based codec */
112   gst_rtp_base_audio_payload_set_frame_based (rtpbaseaudiopayload);
113 }
114
115 static gboolean
116 gst_rtp_bv_pay_sink_setcaps (GstRTPBasePayload * rtpbasepayload, GstCaps * caps)
117 {
118   GstRTPBVPay *rtpbvpay;
119   GstRTPBaseAudioPayload *rtpbaseaudiopayload;
120   gint mode;
121   GstStructure *structure;
122   const char *payload_name;
123
124   rtpbvpay = GST_RTP_BV_PAY (rtpbasepayload);
125   rtpbaseaudiopayload = GST_RTP_BASE_AUDIO_PAYLOAD (rtpbasepayload);
126
127   structure = gst_caps_get_structure (caps, 0);
128
129   payload_name = gst_structure_get_name (structure);
130   if (g_ascii_strcasecmp ("audio/x-bv", payload_name))
131     goto wrong_caps;
132
133   if (!gst_structure_get_int (structure, "mode", &mode))
134     goto no_mode;
135
136   if (mode != 16 && mode != 32)
137     goto wrong_mode;
138
139   if (mode == 16) {
140     gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV16",
141         8000);
142     rtpbasepayload->clock_rate = 8000;
143   } else {
144     gst_rtp_base_payload_set_options (rtpbasepayload, "audio", TRUE, "BV32",
145         16000);
146     rtpbasepayload->clock_rate = 16000;
147   }
148
149   /* set options for this frame based audio codec */
150   gst_rtp_base_audio_payload_set_frame_options (rtpbaseaudiopayload,
151       mode, mode == 16 ? 10 : 20);
152
153   if (mode != rtpbvpay->mode && rtpbvpay->mode != -1)
154     goto mode_changed;
155
156   rtpbvpay->mode = mode;
157
158   return TRUE;
159
160   /* ERRORS */
161 wrong_caps:
162   {
163     GST_ERROR_OBJECT (rtpbvpay, "expected audio/x-bv, received %s",
164         payload_name);
165     return FALSE;
166   }
167 no_mode:
168   {
169     GST_ERROR_OBJECT (rtpbvpay, "did not receive a mode");
170     return FALSE;
171   }
172 wrong_mode:
173   {
174     GST_ERROR_OBJECT (rtpbvpay, "mode must be 16 or 32, received %d", mode);
175     return FALSE;
176   }
177 mode_changed:
178   {
179     GST_ERROR_OBJECT (rtpbvpay, "Mode has changed from %d to %d! "
180         "Mode cannot change while streaming", rtpbvpay->mode, mode);
181     return FALSE;
182   }
183 }
184
185 /* we return the padtemplate caps with the mode field fixated to a value if we
186  * can */
187 static GstCaps *
188 gst_rtp_bv_pay_sink_getcaps (GstRTPBasePayload * rtppayload, GstPad * pad,
189     GstCaps * filter)
190 {
191   GstCaps *otherpadcaps;
192   GstCaps *caps;
193
194   caps = gst_pad_get_pad_template_caps (pad);
195
196   otherpadcaps = gst_pad_get_allowed_caps (rtppayload->srcpad);
197   if (otherpadcaps) {
198     if (!gst_caps_is_empty (otherpadcaps)) {
199       GstStructure *structure;
200       const gchar *mode_str;
201       gint mode;
202
203       structure = gst_caps_get_structure (otherpadcaps, 0);
204
205       /* construct mode, if we can */
206       mode_str = gst_structure_get_string (structure, "encoding-name");
207       if (mode_str) {
208         if (!strcmp (mode_str, "BV16"))
209           mode = 16;
210         else if (!strcmp (mode_str, "BV32"))
211           mode = 32;
212         else
213           mode = -1;
214
215         if (mode == 16 || mode == 32) {
216           caps = gst_caps_make_writable (caps);
217           structure = gst_caps_get_structure (caps, 0);
218           gst_structure_set (structure, "mode", G_TYPE_INT, mode, NULL);
219         }
220       }
221     }
222     gst_caps_unref (otherpadcaps);
223   }
224
225   if (filter) {
226     GstCaps *tmp;
227
228     GST_DEBUG_OBJECT (rtppayload, "Intersect %" GST_PTR_FORMAT " and filter %"
229         GST_PTR_FORMAT, caps, filter);
230     tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
231     gst_caps_unref (caps);
232     caps = tmp;
233   }
234
235   return caps;
236 }