rtpvrawpay: Add missing break
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpchannels.c
1 /* GStreamer
2  * Copyright (C) <2008> 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 #include <string.h>
21 #include <stdlib.h>
22
23 #include "gstrtpchannels.h"
24
25 /* 
26  * RTP channel positions as discussed in RFC 3551 and also RFC 3555
27  *
28  * We can't really represent the described channel positions in GStreamer but we
29  * implement a (very rough) approximation here.
30  */
31
32 static gboolean
33 check_channels (const GstRTPChannelOrder * order,
34     const GstAudioChannelPosition * pos)
35 {
36   gint i, j;
37   gboolean res = TRUE;
38
39   for (i = 0; i < order->channels; i++) {
40     for (j = 0; j < order->channels; j++) {
41       if (order->pos[j] == pos[i])
42         break;
43     }
44     if (j == order->channels)
45       return FALSE;
46   }
47   return res;
48 }
49
50 /**
51  * gst_rtp_channels_get_by_pos:
52  * @channels: the amount of channels
53  * @pos: a channel layout
54  *
55  * Return a description of the channel layout.
56  *
57  * Returns: a #GstRTPChannelOrder with the channel information or NULL when @pos
58  * is not a valid layout.
59  */
60 const GstRTPChannelOrder *
61 gst_rtp_channels_get_by_pos (gint channels, const GstAudioChannelPosition * pos)
62 {
63   gint i;
64   const GstRTPChannelOrder *res = NULL;
65
66   g_return_val_if_fail (pos != NULL, NULL);
67
68   for (i = 0; channel_orders[i].pos; i++) {
69     if (channel_orders[i].channels != channels)
70       continue;
71
72     if (check_channels (&channel_orders[i], pos)) {
73       res = &channel_orders[i];
74       break;
75     }
76   }
77   return res;
78 }
79
80 /**
81  * gst_rtp_channels_create_default:
82  * @channels: the amount of channels
83  * @order: a channel order
84  *
85  * Get the channel order info the @order and @channels.
86  *
87  * Returns: a #GstRTPChannelOrder with the channel information or NULL when
88  * @order is not a know layout for @channels.
89  */
90 const GstRTPChannelOrder *
91 gst_rtp_channels_get_by_order (gint channels, const gchar * order)
92 {
93   gint i;
94   const GstRTPChannelOrder *res = NULL;
95
96   for (i = 0; channel_orders[i].pos; i++) {
97     if (channel_orders[i].channels != channels)
98       continue;
99
100     /* no name but channels match, continue */
101     if (!channel_orders[i].name || !order) {
102       res = &channel_orders[i];
103       break;
104     }
105
106     /* compare names */
107     if (g_ascii_strcasecmp (channel_orders[i].name, order)) {
108       res = &channel_orders[i];
109       break;
110     }
111   }
112   return res;
113 }
114
115 /**
116  * gst_rtp_channels_get_by_index:
117  * @channels: the amount of channels
118  * @idx: the channel index to retrieve
119  *
120  * Get the allowed channel order descriptions for @channels. @idx can be used to
121  * retrieve the desired index.
122  *
123  * Returns: a #GstRTPChannelOrder at @idx, NULL when there are no valid channel
124  * orders.
125  */
126 const GstRTPChannelOrder *
127 gst_rtp_channels_get_by_index (gint channels, guint idx)
128 {
129   gint i;
130   const GstRTPChannelOrder *res = NULL;
131
132   for (i = 0; channel_orders[i].pos; i++) {
133     if (channel_orders[i].channels != channels)
134       continue;
135
136     if (idx == 0) {
137       res = &channel_orders[i];
138       break;
139     }
140     idx--;
141   }
142   return res;
143 }
144
145
146 /**
147  * gst_rtp_channels_create_default:
148  * @channels: the amount of channels
149  *
150  * Create a default none channel mapping for @channels.
151  *
152  * Returns: a #GstAudioChannelPosition with all the channel position info set to
153  * #GST_AUDIO_CHANNEL_POSITION_NONE.
154  */
155 void
156 gst_rtp_channels_create_default (gint channels, GstAudioChannelPosition * posn)
157 {
158   gint i;
159
160   g_return_if_fail (channels > 0);
161
162   for (i = 0; i < channels; i++)
163     posn[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
164 }