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, see <http://www.gnu.org/licenses/>.
24 #include <pulse/xmalloc.h>
25 #include <pulsecore/core-util.h>
27 #include "mime-type.h"
29 bool pa_sample_spec_is_mime(const pa_sample_spec *ss, const pa_channel_map *cm) {
31 pa_assert(pa_channel_map_compatible(cm, ss));
38 if (ss->rate != 8000 &&
48 if (ss->channels != 1 &&
52 if ((cm->channels == 1 && cm->map[0] != PA_CHANNEL_POSITION_MONO) ||
53 (cm->channels == 2 && (cm->map[0] != PA_CHANNEL_POSITION_LEFT || cm->map[1] != PA_CHANNEL_POSITION_RIGHT)))
63 if (ss->channels != 1)
66 if (cm->map[0] != PA_CHANNEL_POSITION_MONO)
76 void pa_sample_spec_mimefy(pa_sample_spec *ss, pa_channel_map *cm) {
78 pa_assert(pa_channel_map_compatible(cm, ss));
80 /* Turns the sample type passed in into the next 'better' one that
81 * can be encoded for HTTP. If there is no 'better' one we pick
82 * the 'best' one that is 'worse'. */
89 else if (ss->rate > 32000)
91 else if (ss->rate > 24000)
93 else if (ss->rate > 22050)
95 else if (ss->rate > 16000)
97 else if (ss->rate > 11025)
99 else if (ss->rate > 8000)
104 switch (ss->format) {
105 case PA_SAMPLE_S24BE:
106 case PA_SAMPLE_S24LE:
107 case PA_SAMPLE_S24_32LE:
108 case PA_SAMPLE_S24_32BE:
109 case PA_SAMPLE_S32LE:
110 case PA_SAMPLE_S32BE:
111 case PA_SAMPLE_FLOAT32LE:
112 case PA_SAMPLE_FLOAT32BE:
113 ss->format = PA_SAMPLE_S24BE;
116 case PA_SAMPLE_S16BE:
117 case PA_SAMPLE_S16LE:
118 ss->format = PA_SAMPLE_S16BE;
124 if (ss->rate == 8000 && ss->channels == 1)
125 ss->format = PA_SAMPLE_ULAW;
127 ss->format = PA_SAMPLE_S16BE;
131 ss->format = PA_SAMPLE_U8;
135 case PA_SAMPLE_INVALID:
136 pa_assert_not_reached();
139 pa_channel_map_init_auto(cm, ss->channels, PA_CHANNEL_MAP_DEFAULT);
141 pa_assert(pa_sample_spec_is_mime(ss, cm));
144 char *pa_sample_spec_to_mime_type(const pa_sample_spec *ss, const pa_channel_map *cm) {
145 pa_assert(pa_channel_map_compatible(cm, ss));
146 pa_assert(pa_sample_spec_valid(ss));
148 if (!pa_sample_spec_is_mime(ss, cm))
151 switch (ss->format) {
153 case PA_SAMPLE_S16BE:
154 case PA_SAMPLE_S24BE:
156 /* Stupid UPnP implementations (PS3...) choke on spaces in
157 * the mime type, that's why we write only ';' here,
158 * instead of '; '. */
159 return pa_sprintf_malloc("audio/%s;rate=%u;channels=%u",
160 ss->format == PA_SAMPLE_S16BE ? "L16" :
161 (ss->format == PA_SAMPLE_S24BE ? "L24" : "L8"),
162 ss->rate, ss->channels);
165 return pa_xstrdup("audio/basic");
168 pa_assert_not_reached();
172 char *pa_sample_spec_to_mime_type_mimefy(const pa_sample_spec *_ss, const pa_channel_map *_cm) {
173 pa_sample_spec ss = *_ss;
174 pa_channel_map cm = *_cm;
176 pa_sample_spec_mimefy(&ss, &cm);
178 return pa_sample_spec_to_mime_type(&ss, &cm);