format.c 1.63 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6 7 8
/* format.c
**
** format plugin implementation
**
*/

#include <stdlib.h>
#include <string.h>
9
#include <time.h>
Jack Moffitt's avatar
Jack Moffitt committed
10 11 12 13 14 15 16

#include "connection.h"
#include "refbuf.h"

#include "format.h"

#include "format_vorbis.h"
Michael Smith's avatar
Michael Smith committed
17
#include "format_mp3.h"
Jack Moffitt's avatar
Jack Moffitt committed
18

19
#include "log.h"
20 21 22
#include "logging.h"
#define CATMODULE "format"

Michael Smith's avatar
Michael Smith committed
23 24 25 26
format_type_t format_get_type(char *contenttype)
{
    if(strcmp(contenttype, "application/x-ogg") == 0)
        return FORMAT_TYPE_VORBIS;
Michael Smith's avatar
Michael Smith committed
27 28
    else if(strcmp(contenttype, "audio/mpeg") == 0)
        return FORMAT_TYPE_MP3; 
Michael Smith's avatar
Michael Smith committed
29 30 31 32
    else
        return -1;
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46
char *format_get_mimetype(format_type_t type)
{
    switch(type) {
        case FORMAT_TYPE_VORBIS:
            return "application/x-ogg";
            break;
        case FORMAT_TYPE_MP3:
            return "audio/mpeg";
            break;
        default:
            return NULL;
    }
}

Jack Moffitt's avatar
Jack Moffitt committed
47
format_plugin_t *format_get_plugin(format_type_t type, char *mount)
Jack Moffitt's avatar
Jack Moffitt committed
48 49 50 51 52 53
{
	format_plugin_t *plugin;

	switch (type) {
	case FORMAT_TYPE_VORBIS:
		plugin = format_vorbis_get_plugin();
Jack Moffitt's avatar
Jack Moffitt committed
54
		if (plugin) plugin->mount = mount;
Jack Moffitt's avatar
Jack Moffitt committed
55
		break;
Michael Smith's avatar
Michael Smith committed
56 57 58 59
    case FORMAT_TYPE_MP3:
        plugin = format_mp3_get_plugin();
        if (plugin) plugin->mount = mount;
        break;
Jack Moffitt's avatar
Jack Moffitt committed
60 61 62 63 64 65 66
	default:
		plugin = NULL;
		break;
	}

	return plugin;
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

int format_generic_write_buf_to_client(format_plugin_t *format, 
        client_t *client, unsigned char *buf, int len)
{
    int ret;
    
    ret = sock_write_bytes(client->con->sock, buf, len);

    if(ret < 0) {
        if(sock_recoverable(ret)) {
            DEBUG1("Client had recoverable error %ld", ret);
            ret = 0;
        }
    }
    else
        client->con->sent_bytes += ret;

    return ret;
}