2 This file is part of PulseAudio.
4 Copyright 2005-2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/core-util.h>
29 #include "mime-type.h"
31 pa_bool_t pa_sample_spec_is_mime(const pa_sample_spec *ss, const pa_channel_map *cm) {
33 pa_assert(pa_channel_map_compatible(cm, ss));
40 if (ss->rate != 8000 &&
50 if (ss->channels != 1 &&
54 if ((cm->channels == 1 && cm->map[0] != PA_CHANNEL_POSITION_MONO) ||
55 (cm->channels == 2 && (cm->map[0] != PA_CHANNEL_POSITION_LEFT || cm->map[1] != PA_CHANNEL_POSITION_RIGHT)))
65 if (ss->channels != 1)
68 if (cm->map[0] != PA_CHANNEL_POSITION_MONO)
78 void pa_sample_spec_mimefy(pa_sample_spec *ss, pa_channel_map *cm) {
80 pa_assert(pa_channel_map_compatible(cm, ss));
82 /* Turns the sample type passed in into the next 'better' one that
83 * can be encoded for HTTP. If there is no 'better' one we pick
84 * the 'best' one that is 'worse'. */
91 else if (ss->rate > 32000)
93 else if (ss->rate > 24000)
95 else if (ss->rate > 22050)
97 else if (ss->rate > 16000)
99 else if (ss->rate > 11025)
101 else if (ss->rate > 8000)
106 switch (ss->format) {
107 case PA_SAMPLE_S24BE:
108 case PA_SAMPLE_S24LE:
109 case PA_SAMPLE_S24_32LE:
110 case PA_SAMPLE_S24_32BE:
111 case PA_SAMPLE_S32LE:
112 case PA_SAMPLE_S32BE:
113 case PA_SAMPLE_FLOAT32LE:
114 case PA_SAMPLE_FLOAT32BE:
115 ss->format = PA_SAMPLE_S24BE;
118 case PA_SAMPLE_S16BE:
119 case PA_SAMPLE_S16LE:
120 ss->format = PA_SAMPLE_S16BE;
126 if (ss->rate == 8000 && ss->channels == 1)
127 ss->format = PA_SAMPLE_ULAW;
129 ss->format = PA_SAMPLE_S16BE;
133 ss->format = PA_SAMPLE_U8;
137 case PA_SAMPLE_INVALID:
138 pa_assert_not_reached();
141 pa_channel_map_init_auto(cm, ss->channels, PA_CHANNEL_MAP_DEFAULT);
143 pa_assert(pa_sample_spec_is_mime(ss, cm));
146 char *pa_sample_spec_to_mime_type(const pa_sample_spec *ss, const pa_channel_map *cm) {
147 pa_assert(pa_channel_map_compatible(cm, ss));
149 if (!pa_sample_spec_is_mime(ss, cm))
152 switch (ss->format) {
154 case PA_SAMPLE_S16BE:
155 case PA_SAMPLE_S24BE:
157 /* Stupid UPnP implementations (PS3...) choke on spaces in
158 * the mime type, that's why we write only ';' here,
159 * instead of '; '. */
160 return pa_sprintf_malloc("audio/%s;rate=%u;channels=%u",
161 ss->format == PA_SAMPLE_S16BE ? "L16" :
162 (ss->format == PA_SAMPLE_S24BE ? "L24" : "L8"),
163 ss->rate, ss->channels);
166 return pa_xstrdup("audio/basic");
169 pa_assert_not_reached();
172 pa_assert(pa_sample_spec_valid(ss));
175 char *pa_sample_spec_to_mime_type_mimefy(const pa_sample_spec *_ss, const pa_channel_map *_cm) {
176 pa_sample_spec ss = *_ss;
177 pa_channel_map cm = *_cm;
179 pa_sample_spec_mimefy(&ss, &cm);
181 return pa_sample_spec_to_mime_type(&ss, &cm);