2002-07-23 11:15:11 -04:00
|
|
|
/* format_mp3.c
|
|
|
|
**
|
2002-12-30 02:55:56 -05:00
|
|
|
** format plugin for mp3
|
2002-07-23 11:15:11 -04:00
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "refbuf.h"
|
2002-12-30 02:55:56 -05:00
|
|
|
#include "source.h"
|
|
|
|
#include "client.h"
|
2002-07-23 11:15:11 -04:00
|
|
|
|
|
|
|
#include "stats.h"
|
|
|
|
#include "format.h"
|
2002-12-30 02:55:56 -05:00
|
|
|
#include "httpp/httpp.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
#include "format_mp3.h"
|
|
|
|
|
|
|
|
#define CATMODULE "format-mp3"
|
|
|
|
|
|
|
|
#define ICY_METADATA_INTERVAL 16000
|
2002-07-23 11:15:11 -04:00
|
|
|
|
2002-12-29 03:10:10 -05:00
|
|
|
static void format_mp3_free_plugin(format_plugin_t *self);
|
|
|
|
static int format_mp3_get_buffer(format_plugin_t *self, char *data,
|
|
|
|
unsigned long len, refbuf_t **buffer);
|
|
|
|
static refbuf_queue_t *format_mp3_get_predata(format_plugin_t *self);
|
2002-12-30 02:55:56 -05:00
|
|
|
static void *format_mp3_create_client_data(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client);
|
|
|
|
static int format_mp3_write_buf_to_client(format_plugin_t *self,
|
|
|
|
client_t *client, unsigned char *buf, int len);
|
|
|
|
static void format_mp3_send_headers(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client);
|
|
|
|
|
|
|
|
typedef struct {
|
2002-12-30 06:22:59 -05:00
|
|
|
int use_metadata;
|
2002-12-30 02:55:56 -05:00
|
|
|
int interval;
|
|
|
|
int offset;
|
2002-12-30 06:22:59 -05:00
|
|
|
int metadata_age;
|
|
|
|
int metadata_offset;
|
2002-12-30 02:55:56 -05:00
|
|
|
} mp3_client_data;
|
2002-12-31 15:15:03 -05:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#define alloca _alloca
|
|
|
|
#endif
|
2002-07-23 11:15:11 -04:00
|
|
|
|
|
|
|
format_plugin_t *format_mp3_get_plugin(void)
|
|
|
|
{
|
|
|
|
format_plugin_t *plugin;
|
2002-12-30 06:22:59 -05:00
|
|
|
mp3_state *state = calloc(1, sizeof(mp3_state));
|
2002-07-23 11:15:11 -04:00
|
|
|
|
|
|
|
plugin = (format_plugin_t *)malloc(sizeof(format_plugin_t));
|
|
|
|
|
|
|
|
plugin->type = FORMAT_TYPE_MP3;
|
|
|
|
plugin->has_predata = 0;
|
|
|
|
plugin->get_buffer = format_mp3_get_buffer;
|
|
|
|
plugin->get_predata = format_mp3_get_predata;
|
2002-12-30 02:55:56 -05:00
|
|
|
plugin->write_buf_to_client = format_mp3_write_buf_to_client;
|
2002-12-29 03:10:10 -05:00
|
|
|
plugin->create_client_data = format_mp3_create_client_data;
|
2002-12-30 02:55:56 -05:00
|
|
|
plugin->client_send_headers = format_mp3_send_headers;
|
2002-07-23 11:15:11 -04:00
|
|
|
plugin->free_plugin = format_mp3_free_plugin;
|
2002-08-10 04:01:56 -04:00
|
|
|
plugin->format_description = "MP3 audio";
|
2002-07-23 11:15:11 -04:00
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
plugin->_state = state;
|
|
|
|
|
2002-12-30 06:27:21 -05:00
|
|
|
state->metadata_age = 0;
|
|
|
|
state->metadata = strdup("");
|
2002-12-30 06:22:59 -05:00
|
|
|
thread_mutex_create(&(state->lock));
|
2002-07-23 11:15:11 -04:00
|
|
|
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
static int send_metadata(client_t *client, mp3_client_data *client_state,
|
|
|
|
mp3_state *source_state)
|
|
|
|
{
|
|
|
|
int send_metadata;
|
|
|
|
int len_byte;
|
|
|
|
int len;
|
|
|
|
unsigned char *buf;
|
|
|
|
int ret;
|
|
|
|
int source_age;
|
2002-12-31 14:48:28 -05:00
|
|
|
char *fullmetadata = NULL;
|
|
|
|
int fullmetadata_size = 0;
|
2002-12-30 06:22:59 -05:00
|
|
|
|
|
|
|
thread_mutex_lock(&(source_state->lock));
|
|
|
|
if(source_state->metadata == NULL) {
|
|
|
|
/* Shouldn't be possible */
|
|
|
|
thread_mutex_unlock(&(source_state->lock));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-31 14:48:28 -05:00
|
|
|
fullmetadata_size = strlen(source_state->metadata) + strlen("StreamTitle='';StreamUrl=''") + 1;
|
|
|
|
|
|
|
|
fullmetadata = alloca(fullmetadata_size);
|
|
|
|
|
|
|
|
memset(fullmetadata, 0, fullmetadata_size);
|
|
|
|
|
|
|
|
sprintf(fullmetadata, "StreamTitle='%s';StreamUrl=''", source_state->metadata);
|
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
source_age = source_state->metadata_age;
|
2002-12-30 06:27:21 -05:00
|
|
|
send_metadata = source_age != client_state->metadata_age;
|
|
|
|
|
2002-12-31 14:48:28 -05:00
|
|
|
if(send_metadata && strlen(fullmetadata) > 0)
|
|
|
|
len_byte = strlen(fullmetadata)/16 + 1 -
|
2002-12-30 06:27:21 -05:00
|
|
|
client_state->metadata_offset;
|
|
|
|
else
|
|
|
|
len_byte = 0;
|
2002-12-30 06:22:59 -05:00
|
|
|
len = 1 + len_byte*16;
|
|
|
|
buf = alloca(len);
|
|
|
|
|
|
|
|
memset(buf, 0, len);
|
|
|
|
|
|
|
|
buf[0] = len_byte;
|
|
|
|
|
2002-12-31 14:48:28 -05:00
|
|
|
if (len > 1) {
|
|
|
|
strncpy(buf+1, fullmetadata + client_state->metadata_offset, len-2);
|
|
|
|
DEBUG1("Sending metadata (%s)", buf+1);
|
|
|
|
}
|
2002-12-30 06:22:59 -05:00
|
|
|
|
|
|
|
thread_mutex_unlock(&(source_state->lock));
|
|
|
|
|
|
|
|
ret = sock_write_bytes(client->con->sock, buf, len);
|
|
|
|
|
|
|
|
if(ret > 0 && ret < len) {
|
|
|
|
client_state->metadata_offset += ret;
|
|
|
|
}
|
|
|
|
else if(ret == len) {
|
|
|
|
client_state->metadata_age = source_age;
|
|
|
|
client_state->offset = 0;
|
|
|
|
client_state->metadata_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static int format_mp3_write_buf_to_client(format_plugin_t *self,
|
|
|
|
client_t *client, unsigned char *buf, int len)
|
|
|
|
{
|
|
|
|
int ret;
|
2002-12-30 06:22:59 -05:00
|
|
|
|
|
|
|
if(((mp3_state *)self->_state)->metadata)
|
|
|
|
{
|
|
|
|
mp3_client_data *state = client->format_data;
|
|
|
|
int max = state->interval - state->offset;
|
2002-12-30 02:55:56 -05:00
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
if(len == 0) /* Shouldn't happen */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(max > len)
|
|
|
|
max = len;
|
|
|
|
|
|
|
|
if(max > 0) {
|
|
|
|
ret = sock_write_bytes(client->con->sock, buf, max);
|
|
|
|
if(ret > 0)
|
|
|
|
state->offset += ret;
|
|
|
|
}
|
2002-12-31 14:48:28 -05:00
|
|
|
else {
|
2002-12-30 06:22:59 -05:00
|
|
|
ret = send_metadata(client, state, self->_state);
|
2002-12-31 14:48:28 -05:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = sock_write_bytes(client->con->sock, buf, len);
|
|
|
|
}
|
2002-12-30 02:55:56 -05:00
|
|
|
|
|
|
|
if(ret < 0) {
|
|
|
|
if(sock_recoverable(ret)) {
|
|
|
|
DEBUG1("Client had recoverable error %ld", ret);
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
client->con->sent_bytes += ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void format_mp3_free_plugin(format_plugin_t *self)
|
2002-07-23 11:15:11 -04:00
|
|
|
{
|
|
|
|
/* free the plugin instance */
|
2002-12-30 06:22:59 -05:00
|
|
|
mp3_state *state = self->_state;
|
|
|
|
thread_mutex_destroy(&(state->lock));
|
2002-12-30 06:27:21 -05:00
|
|
|
|
|
|
|
free(state->metadata);
|
2002-12-30 06:22:59 -05:00
|
|
|
free(state);
|
2002-07-23 11:15:11 -04:00
|
|
|
free(self);
|
|
|
|
}
|
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static int format_mp3_get_buffer(format_plugin_t *self, char *data,
|
|
|
|
unsigned long len, refbuf_t **buffer)
|
2002-07-23 11:15:11 -04:00
|
|
|
{
|
|
|
|
refbuf_t *refbuf;
|
|
|
|
if(!data) {
|
|
|
|
*buffer = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
refbuf = refbuf_new(len);
|
|
|
|
|
|
|
|
memcpy(refbuf->data, data, len);
|
|
|
|
|
|
|
|
*buffer = refbuf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static refbuf_queue_t *format_mp3_get_predata(format_plugin_t *self)
|
2002-07-23 11:15:11 -04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static void *format_mp3_create_client_data(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client)
|
|
|
|
{
|
|
|
|
mp3_client_data *data = calloc(1,sizeof(mp3_client_data));
|
|
|
|
char *metadata;
|
|
|
|
|
|
|
|
data->interval = ICY_METADATA_INTERVAL;
|
|
|
|
data->offset = 0;
|
|
|
|
|
2002-12-31 14:48:28 -05:00
|
|
|
metadata = httpp_getvar(client->parser, "icy-metadata");
|
2002-12-30 02:55:56 -05:00
|
|
|
if(metadata)
|
2002-12-30 06:22:59 -05:00
|
|
|
data->use_metadata = atoi(metadata)>0?1:0;
|
2002-12-30 02:55:56 -05:00
|
|
|
|
|
|
|
return data;
|
2002-12-29 03:10:10 -05:00
|
|
|
}
|
2002-07-23 11:15:11 -04:00
|
|
|
|
2002-12-30 02:55:56 -05:00
|
|
|
static void format_mp3_send_headers(format_plugin_t *self,
|
|
|
|
source_t *source, client_t *client)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
client->respcode = 200;
|
2002-12-30 10:19:46 -05:00
|
|
|
/* TODO: This may need to be ICY/1.0 for shoutcast-compatibility? */
|
2002-12-30 02:55:56 -05:00
|
|
|
bytes = sock_write(client->con->sock,
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Content-Type: %s\r\n",
|
|
|
|
format_get_mimetype(source->format->type));
|
|
|
|
|
|
|
|
if(bytes > 0) client->con->sent_bytes += bytes;
|
|
|
|
|
|
|
|
format_send_general_headers(self, source, client);
|
|
|
|
|
2002-12-30 06:22:59 -05:00
|
|
|
if(((mp3_client_data *)(client->format_data))->use_metadata) {
|
2002-12-30 02:55:56 -05:00
|
|
|
int bytes = sock_write(client->con->sock, "icy-metaint: %d\r\n",
|
|
|
|
ICY_METADATA_INTERVAL);
|
|
|
|
if(bytes > 0)
|
|
|
|
client->con->sent_bytes += bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-07-23 11:15:11 -04:00
|
|
|
|