From 390fe02aa9215692c385e90028df1acdf6a73f77 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 29 Apr 2009 23:22:08 +0200 Subject: http: split out mime type handling calls --- src/Makefile.am | 2 +- src/pulsecore/mime-type.c | 179 ++++++++++++++++++++++++++++++++++++++++++ src/pulsecore/mime-type.h | 37 +++++++++ src/pulsecore/protocol-http.c | 159 ++----------------------------------- 4 files changed, 222 insertions(+), 155 deletions(-) create mode 100644 src/pulsecore/mime-type.c create mode 100644 src/pulsecore/mime-type.h diff --git a/src/Makefile.am b/src/Makefile.am index 719f9dd3..c3c69459 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -883,7 +883,7 @@ libprotocol_cli_la_SOURCES = pulsecore/protocol-cli.c pulsecore/protocol-cli.h libprotocol_cli_la_LDFLAGS = -avoid-version libprotocol_cli_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la libcli.la -libprotocol_http_la_SOURCES = pulsecore/protocol-http.c pulsecore/protocol-http.h +libprotocol_http_la_SOURCES = pulsecore/protocol-http.c pulsecore/protocol-http.h pulsecore/mime-type.c pulsecore/mime-type.h libprotocol_http_la_LDFLAGS = -avoid-version libprotocol_http_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la diff --git a/src/pulsecore/mime-type.c b/src/pulsecore/mime-type.c new file mode 100644 index 00000000..55b8ffd5 --- /dev/null +++ b/src/pulsecore/mime-type.c @@ -0,0 +1,179 @@ +/*** + This file is part of PulseAudio. + + Copyright 2005-2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include "mime-type.h" + +pa_bool_t pa_sample_spec_is_mime(const pa_sample_spec *ss, const pa_channel_map *cm) { + + pa_assert(pa_channel_map_compatible(cm, ss)); + + switch (ss->format) { + case PA_SAMPLE_S16BE: + case PA_SAMPLE_S24BE: + case PA_SAMPLE_U8: + + if (ss->rate != 8000 && + ss->rate != 11025 && + ss->rate != 16000 && + ss->rate != 22050 && + ss->rate != 24000 && + ss->rate != 32000 && + ss->rate != 44100 && + ss->rate != 48000) + return FALSE; + + if (ss->channels != 1 && + ss->channels != 2) + return FALSE; + + if ((cm->channels == 1 && cm->map[0] != PA_CHANNEL_POSITION_MONO) || + (cm->channels == 2 && (cm->map[0] != PA_CHANNEL_POSITION_LEFT || cm->map[1] != PA_CHANNEL_POSITION_RIGHT))) + return FALSE; + + return TRUE; + + case PA_SAMPLE_ULAW: + + if (ss->rate != 8000) + return FALSE; + + if (ss->channels != 1) + return FALSE; + + if (cm->map[0] != PA_CHANNEL_POSITION_MONO) + return FALSE; + + return TRUE; + + default: + return FALSE; + } +} + +void pa_sample_spec_mimefy(pa_sample_spec *ss, pa_channel_map *cm) { + + pa_assert(pa_channel_map_compatible(cm, ss)); + + /* Turns the sample type passed in into the next 'better' one that + * can be encoded for HTTP. If there is no 'better' one we pick + * the 'best' one that is 'worse'. */ + + if (ss->channels > 2) + ss->channels = 2; + + if (ss->rate > 44100) + ss->rate = 48000; + else if (ss->rate > 32000) + ss->rate = 44100; + else if (ss->rate > 24000) + ss->rate = 32000; + else if (ss->rate > 22050) + ss->rate = 24000; + else if (ss->rate > 16000) + ss->rate = 22050; + else if (ss->rate > 11025) + ss->rate = 16000; + else if (ss->rate > 8000) + ss->rate = 11025; + else + ss->rate = 8000; + + switch (ss->format) { + case PA_SAMPLE_S24BE: + case PA_SAMPLE_S24LE: + case PA_SAMPLE_S24_32LE: + case PA_SAMPLE_S24_32BE: + case PA_SAMPLE_S32LE: + case PA_SAMPLE_S32BE: + case PA_SAMPLE_FLOAT32LE: + case PA_SAMPLE_FLOAT32BE: + ss->format = PA_SAMPLE_S24BE; + break; + + case PA_SAMPLE_S16BE: + case PA_SAMPLE_S16LE: + ss->format = PA_SAMPLE_S16BE; + break; + + case PA_SAMPLE_ULAW: + case PA_SAMPLE_ALAW: + + if (ss->rate == 8000 && ss->channels == 1) + ss->format = PA_SAMPLE_ULAW; + else + ss->format = PA_SAMPLE_S16BE; + break; + + case PA_SAMPLE_U8: + ss->format = PA_SAMPLE_U8; + break; + + case PA_SAMPLE_MAX: + case PA_SAMPLE_INVALID: + pa_assert_not_reached(); + } + + pa_channel_map_init_auto(cm, ss->channels, PA_CHANNEL_MAP_DEFAULT); + + pa_assert(pa_sample_spec_is_mime(ss, cm)); +} + +char *pa_sample_spec_to_mime_type(const pa_sample_spec *ss, const pa_channel_map *cm) { + pa_assert(pa_channel_map_compatible(cm, ss)); + + if (!pa_sample_spec_is_mime(ss, cm)) + return NULL; + + switch (ss->format) { + + case PA_SAMPLE_S16BE: + case PA_SAMPLE_S24BE: + case PA_SAMPLE_U8: + return pa_sprintf_malloc("audio/%s; rate=%u; channels=%u", + ss->format == PA_SAMPLE_S16BE ? "L16" : + (ss->format == PA_SAMPLE_S24BE ? "L24" : "L8"), + ss->rate, ss->channels); + + case PA_SAMPLE_ULAW: + return pa_xstrdup("audio/basic"); + + default: + pa_assert_not_reached(); + } + + pa_assert(pa_sample_spec_valid(ss)); +} + +char *pa_sample_spec_to_mime_type_mimefy(const pa_sample_spec *_ss, const pa_channel_map *_cm) { + pa_sample_spec ss = *_ss; + pa_channel_map cm = *_cm; + + pa_sample_spec_mimefy(&ss, &cm); + + return pa_sample_spec_to_mime_type(&ss, &cm); +} diff --git a/src/pulsecore/mime-type.h b/src/pulsecore/mime-type.h new file mode 100644 index 00000000..db77379b --- /dev/null +++ b/src/pulsecore/mime-type.h @@ -0,0 +1,37 @@ +#ifndef foopulsecoremimetypehfoo +#define foopulsecoremimetypehfoo +/*** + This file is part of PulseAudio. + + Copyright 2009 Lennart Poettering + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +pa_bool_t pa_sample_spec_is_mime(const pa_sample_spec *ss, const pa_channel_map *cm); +void pa_sample_spec_mimefy(pa_sample_spec *ss, pa_channel_map *cm); +char *pa_sample_spec_to_mime_type(const pa_sample_spec *ss, const pa_channel_map *cm); +char *pa_sample_spec_to_mime_type_mimefy(const pa_sample_spec *_ss, const pa_channel_map *_cm); + +#endif diff --git a/src/pulsecore/protocol-http.c b/src/pulsecore/protocol-http.c index 64670244..46850b28 100644 --- a/src/pulsecore/protocol-http.c +++ b/src/pulsecore/protocol-http.c @@ -40,6 +40,7 @@ #include #include #include +#include #include "protocol-http.h" @@ -256,156 +257,6 @@ static void io_callback(pa_iochannel*io, void *userdata) { do_work(c); } -static pa_bool_t is_mime_sample_spec(const pa_sample_spec *ss, const pa_channel_map *cm) { - - pa_assert(pa_channel_map_compatible(cm, ss)); - - switch (ss->format) { - case PA_SAMPLE_S16BE: - case PA_SAMPLE_S24BE: - case PA_SAMPLE_U8: - - if (ss->rate != 8000 && - ss->rate != 11025 && - ss->rate != 16000 && - ss->rate != 22050 && - ss->rate != 24000 && - ss->rate != 32000 && - ss->rate != 44100 && - ss->rate != 48000) - return FALSE; - - if (ss->channels != 1 && - ss->channels != 2) - return FALSE; - - if ((cm->channels == 1 && cm->map[0] != PA_CHANNEL_POSITION_MONO) || - (cm->channels == 2 && (cm->map[0] != PA_CHANNEL_POSITION_LEFT || cm->map[1] != PA_CHANNEL_POSITION_RIGHT))) - return FALSE; - - return TRUE; - - case PA_SAMPLE_ULAW: - - if (ss->rate != 8000) - return FALSE; - - if (ss->channels != 1) - return FALSE; - - if (cm->map[0] != PA_CHANNEL_POSITION_MONO) - return FALSE; - - return TRUE; - - default: - return FALSE; - } -} - -static void mimefy_sample_spec(pa_sample_spec *ss, pa_channel_map *cm) { - - pa_assert(pa_channel_map_compatible(cm, ss)); - - /* Turns the sample type passed in into the next 'better' one that - * can be encoded for HTTP. If there is no 'better' one we pick - * the 'best' one that is 'worse'. */ - - if (ss->channels > 2) - ss->channels = 2; - - if (ss->rate > 44100) - ss->rate = 48000; - else if (ss->rate > 32000) - ss->rate = 44100; - else if (ss->rate > 24000) - ss->rate = 32000; - else if (ss->rate > 22050) - ss->rate = 24000; - else if (ss->rate > 16000) - ss->rate = 22050; - else if (ss->rate > 11025) - ss->rate = 16000; - else if (ss->rate > 8000) - ss->rate = 11025; - else - ss->rate = 8000; - - switch (ss->format) { - case PA_SAMPLE_S24BE: - case PA_SAMPLE_S24LE: - case PA_SAMPLE_S24_32LE: - case PA_SAMPLE_S24_32BE: - case PA_SAMPLE_S32LE: - case PA_SAMPLE_S32BE: - case PA_SAMPLE_FLOAT32LE: - case PA_SAMPLE_FLOAT32BE: - ss->format = PA_SAMPLE_S24BE; - break; - - case PA_SAMPLE_S16BE: - case PA_SAMPLE_S16LE: - ss->format = PA_SAMPLE_S16BE; - break; - - case PA_SAMPLE_ULAW: - case PA_SAMPLE_ALAW: - - if (ss->rate == 8000 && ss->channels == 1) - ss->format = PA_SAMPLE_ULAW; - else - ss->format = PA_SAMPLE_S16BE; - break; - - case PA_SAMPLE_U8: - ss->format = PA_SAMPLE_U8; - break; - - case PA_SAMPLE_MAX: - case PA_SAMPLE_INVALID: - pa_assert_not_reached(); - } - - pa_channel_map_init_auto(cm, ss->channels, PA_CHANNEL_MAP_DEFAULT); - - pa_assert(is_mime_sample_spec(ss, cm)); -} - -static char *sample_spec_to_mime_type(const pa_sample_spec *ss, const pa_channel_map *cm) { - pa_assert(pa_channel_map_compatible(cm, ss)); - - if (!is_mime_sample_spec(ss, cm)) - return NULL; - - switch (ss->format) { - - case PA_SAMPLE_S16BE: - case PA_SAMPLE_S24BE: - case PA_SAMPLE_U8: - return pa_sprintf_malloc("audio/%s; rate=%u; channels=%u", - ss->format == PA_SAMPLE_S16BE ? "L16" : - (ss->format == PA_SAMPLE_S24BE ? "L24" : "L8"), - ss->rate, ss->channels); - - case PA_SAMPLE_ULAW: - return pa_xstrdup("audio/basic"); - - default: - pa_assert_not_reached(); - } - - pa_assert(pa_sample_spec_valid(ss)); -} - -static char *mimefy_and_stringify_sample_spec(const pa_sample_spec *_ss, const pa_channel_map *_cm) { - pa_sample_spec ss = *_ss; - pa_channel_map cm = *_cm; - - mimefy_sample_spec(&ss, &cm); - - return sample_spec_to_mime_type(&ss, &cm); -} - static char *escape_html(const char *t) { pa_strbuf *sb; const char *p, *e; @@ -587,7 +438,7 @@ static void handle_listen(struct connection *c) { char *t, *m; t = escape_html(pa_strna(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION))); - m = mimefy_and_stringify_sample_spec(&sink->sample_spec, &sink->channel_map); + m = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map); pa_ioline_printf(c->line, "%s
\n", @@ -609,7 +460,7 @@ static void handle_listen(struct connection *c) { continue; t = escape_html(pa_strna(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION))); - m = mimefy_and_stringify_sample_spec(&source->sample_spec, &source->channel_map); + m = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map); pa_ioline_printf(c->line, "%s
\n", @@ -666,7 +517,7 @@ static void handle_listen_prefix(struct connection *c, const char *source_name) ss = source->sample_spec; cm = source->channel_map; - mimefy_sample_spec(&ss, &cm); + pa_sample_spec_mimefy(&ss, &cm); pa_source_output_new_data_init(&data); data.driver = __FILE__; @@ -706,7 +557,7 @@ static void handle_listen_prefix(struct connection *c, const char *source_name) pa_source_output_put(c->source_output); - t = sample_spec_to_mime_type(&ss, &cm); + t = pa_sample_spec_to_mime_type(&ss, &cm); http_response(c, 200, "OK", t); pa_xfree(t); -- cgit