1 /* -*- c-basic-offset: 8; -*- */
2 /* speex.c: Ogg Speex data handlers for libshout
3 *
4 * Copyright (C) 2005 the Icecast team <team@icecast.org>
5 * Copyright (C) 2015-2019 Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <speex/speex.h>
27 #include <speex/speex_header.h>
28
29 #include "shout_private.h"
30 #include "format_ogg.h"
31
32 /* -- local data structures -- */
33 typedef struct {
34 SpeexHeader *sh;
35 } speex_data_t;
36
37 /* -- local prototypes -- */
38 static int read_speex_page(ogg_codec_t *codec, ogg_page *page);
39 static void free_speex_data(void *codec_data);
40
41 /* -- speex functions -- */
_shout_open_speex(ogg_codec_t * codec,ogg_page * page)42 int _shout_open_speex(ogg_codec_t *codec, ogg_page *page)
43 {
44 speex_data_t *speex_data = calloc(1, sizeof(speex_data_t));
45 ogg_packet packet;
46
47 (void) page;
48
49 if (!speex_data)
50 return SHOUTERR_MALLOC;
51
52 ogg_stream_packetout(&codec->os, &packet);
53
54 if ( !(speex_data->sh = speex_packet_to_header((char*)packet.packet, packet.bytes)) ) {
55 free_speex_data(speex_data);
56 return SHOUTERR_UNSUPPORTED;
57 }
58
59 codec->codec_data = speex_data;
60 codec->read_page = read_speex_page;
61 codec->free_data = free_speex_data;
62
63 return SHOUTERR_SUCCESS;
64 }
65
read_speex_page(ogg_codec_t * codec,ogg_page * page)66 static int read_speex_page(ogg_codec_t *codec, ogg_page *page)
67 {
68 ogg_packet packet;
69 speex_data_t *speex_data = codec->codec_data;
70 uint64_t samples = 0;
71
72 (void) page;
73
74 while (ogg_stream_packetout(&codec->os, &packet) > 0) {
75 samples += speex_data->sh->frames_per_packet * speex_data->sh->frame_size;
76 }
77
78 codec->senttime += ((samples * 1000000) / speex_data->sh->rate);
79
80 return SHOUTERR_SUCCESS;
81 }
82
free_speex_data(void * codec_data)83 static void free_speex_data(void *codec_data)
84 {
85 speex_data_t *speex_data = (speex_data_t*)codec_data;
86
87 if (speex_data->sh)
88 free(speex_data->sh);
89
90 free(speex_data);
91 }
92