2006-04-20 04:08:12 +00:00
|
|
|
/*
|
|
|
|
* $Id: $
|
|
|
|
* Simple plug-in api for output, transcode, and scanning plug-ins
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Ron Pedde (ron@pedde.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2006-06-04 08:13:50 +00:00
|
|
|
# include "config.h"
|
2006-04-20 04:08:12 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-28 05:10:06 +00:00
|
|
|
#define _XOPEN_SOURCE 500 /** unix98? pthread_once_t, etc */
|
|
|
|
|
2006-04-20 04:08:12 +00:00
|
|
|
#include <errno.h>
|
2006-09-25 03:20:22 +00:00
|
|
|
#include <fcntl.h>
|
2006-04-20 04:08:12 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2006-11-04 04:17:24 +00:00
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
2006-04-20 04:08:12 +00:00
|
|
|
|
2006-11-22 04:10:29 +00:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2006-07-25 03:49:36 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-04-09 06:50:58 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2007-04-07 22:32:26 +00:00
|
|
|
#include <sys/time.h>
|
2007-04-09 06:50:58 +00:00
|
|
|
#endif
|
2007-04-10 01:39:08 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2007-04-07 22:32:26 +00:00
|
|
|
#include <unistd.h>
|
2007-04-10 01:39:08 +00:00
|
|
|
#endif
|
2007-01-17 01:06:16 +00:00
|
|
|
#include "daapd.h"
|
2006-04-21 06:43:41 +00:00
|
|
|
#include "conf.h"
|
2006-09-25 03:20:22 +00:00
|
|
|
#include "configfile.h"
|
2006-04-21 06:43:41 +00:00
|
|
|
#include "db-generic.h"
|
2006-04-20 04:08:12 +00:00
|
|
|
#include "err.h"
|
|
|
|
#include "os.h"
|
|
|
|
#include "plugin.h"
|
2006-04-25 23:13:04 +00:00
|
|
|
#include "rend.h"
|
2006-09-25 03:20:22 +00:00
|
|
|
#include "restart.h"
|
2006-04-21 06:43:41 +00:00
|
|
|
#include "smart-parser.h"
|
2006-04-20 06:52:21 +00:00
|
|
|
#include "xml-rpc.h"
|
|
|
|
#include "webserver.h"
|
2006-05-27 08:02:39 +00:00
|
|
|
#include "ff-plugins.h"
|
2006-04-20 04:08:12 +00:00
|
|
|
|
|
|
|
typedef struct tag_pluginentry {
|
|
|
|
void *phandle;
|
2006-05-28 04:06:14 +00:00
|
|
|
PLUGIN_INFO *pinfo;
|
2006-04-20 04:08:12 +00:00
|
|
|
struct tag_pluginentry *next;
|
|
|
|
} PLUGIN_ENTRY;
|
|
|
|
|
|
|
|
/* Globals */
|
2006-04-20 06:52:21 +00:00
|
|
|
static PLUGIN_ENTRY _plugin_list;
|
|
|
|
static int _plugin_initialized = 0;
|
2006-05-28 04:06:14 +00:00
|
|
|
static char *_plugin_ssc_codecs = NULL;
|
2006-04-22 18:22:24 +00:00
|
|
|
|
2006-04-20 04:08:12 +00:00
|
|
|
static char* _plugin_error_list[] = {
|
|
|
|
"Success.",
|
|
|
|
"Could not load plugin: %s",
|
|
|
|
"Plugin missing required export: plugin_type/plugin_ver"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Forwards */
|
2006-04-22 18:22:24 +00:00
|
|
|
void _plugin_readlock(void);
|
|
|
|
void _plugin_writelock(void);
|
2006-04-20 04:08:12 +00:00
|
|
|
void _plugin_unlock(void);
|
|
|
|
int _plugin_error(char **pe, int error, ...);
|
2006-05-05 07:38:13 +00:00
|
|
|
void _plugin_free(int *pi);
|
2006-05-28 04:06:14 +00:00
|
|
|
void _plugin_recalc_codecs(void);
|
2006-11-06 03:42:38 +00:00
|
|
|
int _plugin_ssc_transcode(WS_CONNINFO *pwsc, MP3FILE *pmp3, int offset, int headers);
|
2006-04-20 04:08:12 +00:00
|
|
|
|
2006-04-23 04:42:18 +00:00
|
|
|
/* webserver helpers */
|
|
|
|
char *pi_ws_uri(WS_CONNINFO *pwsc);
|
2006-09-25 03:20:22 +00:00
|
|
|
void pi_ws_will_close(WS_CONNINFO *pwsc);
|
2006-04-25 10:02:43 +00:00
|
|
|
int pi_ws_fd(WS_CONNINFO *pwsc);
|
2007-04-13 23:31:57 +00:00
|
|
|
char *pi_ws_gethostname(WS_CONNINFO *pwsc);
|
2006-04-23 04:42:18 +00:00
|
|
|
|
|
|
|
/* misc helpers */
|
|
|
|
char *pi_server_ver(void);
|
|
|
|
int pi_server_name(char *, int *);
|
|
|
|
void pi_log(int, char *, ...);
|
|
|
|
|
|
|
|
/* db helpers */
|
|
|
|
int pi_db_count(void);
|
2006-05-27 08:02:39 +00:00
|
|
|
int pi_db_enum_start(char **pe, DB_QUERY *pinfo);
|
|
|
|
int pi_db_enum_fetch_row(char **pe, char ***row, DB_QUERY *pinfo);
|
2006-04-23 04:42:18 +00:00
|
|
|
int pi_db_enum_end(char **pe);
|
2006-09-25 03:20:22 +00:00
|
|
|
int pi_db_enum_restart(char **pe, DB_QUERY *pinfo);
|
2006-05-27 08:02:39 +00:00
|
|
|
void pi_db_enum_dispose(char **pe, DB_QUERY *pinfo);
|
|
|
|
void pi_stream(WS_CONNINFO *pwsc, char *id);
|
2006-09-25 03:20:22 +00:00
|
|
|
int pi_db_count_items(int what);
|
2006-10-05 03:58:24 +00:00
|
|
|
int pi_db_wait_update(WS_CONNINFO *pwsc);
|
2006-05-19 04:50:45 +00:00
|
|
|
void pi_conf_dispose_string(char *str);
|
2006-04-23 04:42:18 +00:00
|
|
|
|
2006-11-05 22:38:12 +00:00
|
|
|
/* transcode helpers */
|
|
|
|
int pi_ssc_should_transcode(WS_CONNINFO *pwsc, char *codec);
|
|
|
|
|
2006-04-23 04:42:18 +00:00
|
|
|
PLUGIN_INPUT_FN pi = {
|
|
|
|
pi_ws_uri,
|
2006-09-25 03:20:22 +00:00
|
|
|
pi_ws_will_close,
|
2006-04-25 10:02:43 +00:00
|
|
|
ws_returnerror,
|
|
|
|
ws_getvar,
|
|
|
|
ws_writefd,
|
|
|
|
ws_addresponseheader,
|
|
|
|
ws_emitheaders,
|
|
|
|
pi_ws_fd,
|
|
|
|
ws_getrequestheader,
|
2006-04-25 20:46:03 +00:00
|
|
|
ws_writebinary,
|
2007-04-13 23:31:57 +00:00
|
|
|
pi_ws_gethostname,
|
2007-04-15 23:57:00 +00:00
|
|
|
config_matches_role,
|
2006-04-23 04:42:18 +00:00
|
|
|
|
|
|
|
pi_server_ver,
|
|
|
|
pi_server_name,
|
|
|
|
pi_log,
|
2006-11-05 22:38:12 +00:00
|
|
|
pi_ssc_should_transcode,
|
2006-04-23 04:42:18 +00:00
|
|
|
|
|
|
|
pi_db_count,
|
|
|
|
pi_db_enum_start,
|
|
|
|
pi_db_enum_fetch_row,
|
|
|
|
pi_db_enum_end,
|
2006-09-25 03:20:22 +00:00
|
|
|
pi_db_enum_restart,
|
2006-05-27 08:02:39 +00:00
|
|
|
pi_db_enum_dispose,
|
2006-04-23 04:42:18 +00:00
|
|
|
pi_stream,
|
|
|
|
|
2006-09-25 03:20:22 +00:00
|
|
|
db_add_playlist,
|
|
|
|
db_add_playlist_item,
|
|
|
|
db_edit_playlist,
|
|
|
|
db_delete_playlist,
|
|
|
|
db_delete_playlist_item,
|
|
|
|
db_revision,
|
|
|
|
pi_db_count_items,
|
2006-10-05 03:58:24 +00:00
|
|
|
pi_db_wait_update,
|
2006-09-25 03:20:22 +00:00
|
|
|
|
2006-05-19 04:50:45 +00:00
|
|
|
conf_alloc_string,
|
2006-09-25 03:20:22 +00:00
|
|
|
pi_conf_dispose_string,
|
|
|
|
conf_get_int,
|
|
|
|
|
|
|
|
config_set_status
|
2006-04-23 04:42:18 +00:00
|
|
|
};
|
|
|
|
|
2006-05-19 04:50:45 +00:00
|
|
|
|
2006-04-22 18:22:24 +00:00
|
|
|
/**
|
|
|
|
* initialize stuff for plugins
|
|
|
|
*
|
|
|
|
* @returns TRUE on success, FALSE otherwise
|
|
|
|
*/
|
|
|
|
int plugin_init(void) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-05-05 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* free the tls
|
|
|
|
*/
|
|
|
|
void _plugin_free(int *pi) {
|
|
|
|
if(pi)
|
|
|
|
free(pi);
|
|
|
|
}
|
|
|
|
|
2006-04-22 18:22:24 +00:00
|
|
|
/**
|
|
|
|
* deinitialize stuff for plugins
|
|
|
|
*
|
|
|
|
* @returns TRUE on success, FALSE otherwise
|
|
|
|
*/
|
|
|
|
int plugin_deinit(void) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-04-20 04:08:12 +00:00
|
|
|
/**
|
2006-11-22 04:10:29 +00:00
|
|
|
* return the error
|
|
|
|
*
|
2006-04-20 04:08:12 +00:00
|
|
|
* @param pe buffer to store the error string in
|
|
|
|
* @param error error to return
|
|
|
|
* @returns the specified error,to thelp with returns
|
|
|
|
*/
|
|
|
|
int _plugin_error(char **pe, int error, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char errbuf[1024];
|
|
|
|
|
|
|
|
if(!pe)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
va_start(ap, error);
|
|
|
|
vsnprintf(errbuf, sizeof(errbuf), _plugin_error_list[error], ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
DPRINTF(E_SPAM,L_PLUG,"Raising error: %s\n",errbuf);
|
|
|
|
|
|
|
|
*pe = strdup(errbuf);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
/**
|
|
|
|
* walk through the installed plugins and recalculate
|
|
|
|
* the codec string
|
|
|
|
*/
|
|
|
|
void _plugin_recalc_codecs(void) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
2006-05-28 06:11:37 +00:00
|
|
|
size_t size=0;
|
2006-05-28 04:06:14 +00:00
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
|
|
|
if(ppi->pinfo->type & PLUGIN_TRANSCODE) {
|
|
|
|
if(size) size++;
|
|
|
|
size += strlen(ppi->pinfo->codeclist);
|
|
|
|
}
|
|
|
|
ppi=ppi->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_plugin_ssc_codecs) {
|
|
|
|
free(_plugin_ssc_codecs);
|
|
|
|
}
|
|
|
|
|
|
|
|
_plugin_ssc_codecs = (char*)malloc(size+1);
|
|
|
|
if(!_plugin_ssc_codecs) {
|
|
|
|
DPRINTF(E_FATAL,L_PLUG,"_plugin_recalc_codecs: malloc\n");
|
|
|
|
}
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
memset(_plugin_ssc_codecs,0,size+1);
|
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
|
|
|
if(ppi->pinfo->type & PLUGIN_TRANSCODE) {
|
|
|
|
if(strlen(_plugin_ssc_codecs)) {
|
|
|
|
strcat(_plugin_ssc_codecs,",");
|
|
|
|
}
|
|
|
|
strcat(_plugin_ssc_codecs,ppi->pinfo->codeclist);
|
|
|
|
}
|
|
|
|
ppi=ppi->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_DBG,L_PLUG,"New transcode codec list: %s\n",_plugin_ssc_codecs);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-04 20:34:29 +00:00
|
|
|
/**
|
|
|
|
* plugin_get_description
|
|
|
|
*/
|
|
|
|
char *plugin_get_description(void *which) {
|
|
|
|
PLUGIN_ENTRY *ppi = (PLUGIN_ENTRY *)which;
|
|
|
|
|
|
|
|
return ppi->pinfo->server;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* walk through the loaded plugin list
|
|
|
|
*/
|
|
|
|
void *plugin_enum(void *where) {
|
|
|
|
PLUGIN_ENTRY *ppi = (PLUGIN_ENTRY *)where;
|
|
|
|
|
|
|
|
if(!ppi) {
|
|
|
|
// _plugin_readlock();
|
|
|
|
return (void*) _plugin_list.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!ppi->next) {
|
|
|
|
// _plugin_unlock();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void*) ppi->next;
|
|
|
|
}
|
|
|
|
|
2006-04-20 04:08:12 +00:00
|
|
|
/**
|
|
|
|
* load a specified plugin.
|
|
|
|
*
|
|
|
|
* @param pe pointer to error string returned (if error)
|
|
|
|
* @param plugin path to plugin to load
|
|
|
|
*
|
|
|
|
* return PLUGIN_E_SUCCESS, or not, with pe set
|
|
|
|
*/
|
|
|
|
int plugin_load(char **pe, char *path) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
void *phandle;
|
2006-05-28 04:06:14 +00:00
|
|
|
PLUGIN_INFO *(*info_func)(PLUGIN_INPUT_FN *);
|
2006-04-20 06:52:21 +00:00
|
|
|
PLUGIN_INFO *pinfo;
|
2006-04-20 04:08:12 +00:00
|
|
|
|
|
|
|
DPRINTF(E_DBG,L_PLUG,"Attempting to load plugin %s\n",path);
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-04-20 06:52:21 +00:00
|
|
|
phandle = os_loadlib(pe, path);
|
2006-04-20 04:08:12 +00:00
|
|
|
if(!phandle) {
|
2006-04-20 06:52:21 +00:00
|
|
|
DPRINTF(E_INF,L_PLUG,"Couldn't get lib handle for %s\n",path);
|
|
|
|
return PLUGIN_E_NOLOAD;
|
2006-04-20 04:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ppi = (PLUGIN_ENTRY*)malloc(sizeof(PLUGIN_ENTRY));
|
|
|
|
memset(ppi,0x00,sizeof(PLUGIN_ENTRY));
|
|
|
|
|
|
|
|
ppi->phandle = phandle;
|
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
info_func = (PLUGIN_INFO*(*)(PLUGIN_INPUT_FN*)) os_libfunc(pe, phandle,"plugin_info");
|
2006-04-20 06:52:21 +00:00
|
|
|
if(info_func == NULL) {
|
|
|
|
DPRINTF(E_INF,L_PLUG,"Couldn't get info_func for %s\n",path);
|
2006-05-28 04:06:14 +00:00
|
|
|
os_unload(phandle);
|
|
|
|
free(ppi);
|
2006-04-20 06:52:21 +00:00
|
|
|
return PLUGIN_E_BADFUNCS;
|
|
|
|
}
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
pinfo = info_func(&pi);
|
|
|
|
ppi->pinfo = pinfo;
|
2006-04-20 06:52:21 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
if(!pinfo) {
|
|
|
|
if(pe) *pe = strdup("plugin declined to load");
|
|
|
|
os_unload(phandle);
|
|
|
|
free(ppi);
|
|
|
|
return PLUGIN_E_NOLOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(E_INF,L_PLUG,"Loaded plugin %s (%s)\n",path,pinfo->server);
|
|
|
|
|
2006-04-20 06:52:21 +00:00
|
|
|
if(!_plugin_initialized) {
|
|
|
|
_plugin_initialized = 1;
|
|
|
|
memset((void*)&_plugin_list,0,sizeof(_plugin_list));
|
|
|
|
}
|
|
|
|
|
2006-04-20 04:08:12 +00:00
|
|
|
ppi->next = _plugin_list.next;
|
|
|
|
_plugin_list.next = ppi;
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
_plugin_recalc_codecs();
|
2006-04-20 04:08:12 +00:00
|
|
|
return PLUGIN_E_SUCCESS;
|
|
|
|
}
|
2006-04-20 06:52:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check to see if we want to dispatch a particular url
|
|
|
|
*
|
|
|
|
* @param pwsc the connection info (including uri) to check
|
2006-11-22 04:10:29 +00:00
|
|
|
* @returns TRUE if we want to handle it
|
2006-04-20 06:52:21 +00:00
|
|
|
*/
|
|
|
|
int plugin_url_candispatch(WS_CONNINFO *pwsc) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
2006-05-28 04:06:14 +00:00
|
|
|
if(ppi->pinfo->type & PLUGIN_OUTPUT) {
|
2006-09-25 03:20:22 +00:00
|
|
|
if((ppi->pinfo->output_fns)->can_handle(pwsc)) {
|
2006-04-20 06:52:21 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2006-05-05 07:38:13 +00:00
|
|
|
ppi = ppi->next;
|
2006-04-20 06:52:21 +00:00
|
|
|
}
|
2006-09-25 03:20:22 +00:00
|
|
|
|
2006-04-20 06:52:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* actually DISPATCH the hander we said we wanted
|
|
|
|
*
|
|
|
|
* @param pwsc the connection info (including uri) to check
|
2006-11-22 04:10:29 +00:00
|
|
|
* @returns TRUE if we want to handle it
|
2006-04-20 06:52:21 +00:00
|
|
|
*/
|
|
|
|
void plugin_url_handle(WS_CONNINFO *pwsc) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
void (*disp_fn)(WS_CONNINFO *pwsc);
|
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
2006-05-28 04:06:14 +00:00
|
|
|
if(ppi->pinfo->type & PLUGIN_OUTPUT) {
|
2006-09-25 03:20:22 +00:00
|
|
|
if((ppi->pinfo->output_fns)->can_handle(pwsc)) {
|
2006-04-20 06:52:21 +00:00
|
|
|
/* we have a winner */
|
|
|
|
DPRINTF(E_DBG,L_PLUG,"Dispatching %s to %s\n", pwsc->uri,
|
2006-05-28 04:06:14 +00:00
|
|
|
ppi->pinfo->server);
|
2006-04-20 06:52:21 +00:00
|
|
|
|
|
|
|
/* so functions must be a tag_plugin_output_fn */
|
2006-05-28 04:06:14 +00:00
|
|
|
disp_fn=(ppi->pinfo->output_fns)->handler;
|
2006-04-20 06:52:21 +00:00
|
|
|
disp_fn(pwsc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-04-25 23:13:04 +00:00
|
|
|
ppi = ppi->next;
|
2006-04-20 06:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should 500 here or something */
|
|
|
|
ws_returnerror(pwsc, 500, "Can't find plugin handler");
|
|
|
|
return;
|
2006-11-22 04:10:29 +00:00
|
|
|
}
|
2006-04-20 06:52:21 +00:00
|
|
|
|
2006-04-25 23:13:04 +00:00
|
|
|
/**
|
|
|
|
* walk through the plugins and register whatever rendezvous
|
|
|
|
* names the clients want
|
|
|
|
*/
|
2006-05-24 05:14:58 +00:00
|
|
|
int plugin_rend_register(char *name, int port, char *iface, char *txt) {
|
2006-04-25 23:13:04 +00:00
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
PLUGIN_REND_INFO *pri;
|
2006-05-24 05:14:58 +00:00
|
|
|
char *supplied_txt;
|
2006-04-25 23:13:04 +00:00
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
|
|
|
|
while(ppi) {
|
2006-05-28 04:06:14 +00:00
|
|
|
DPRINTF(E_DBG,L_PLUG,"Checking %s\n",ppi->pinfo->server);
|
|
|
|
if(ppi->pinfo->rend_info) {
|
|
|
|
pri = ppi->pinfo->rend_info;
|
2006-04-25 23:13:04 +00:00
|
|
|
while(pri->type) {
|
2006-05-24 05:14:58 +00:00
|
|
|
supplied_txt = pri->txt;
|
2006-04-26 03:29:43 +00:00
|
|
|
if(!pri->txt)
|
2006-05-24 05:14:58 +00:00
|
|
|
supplied_txt = txt;
|
2006-04-26 03:29:43 +00:00
|
|
|
|
2006-04-25 23:13:04 +00:00
|
|
|
DPRINTF(E_DBG,L_PLUG,"Registering %s\n",pri->type);
|
2006-08-03 02:55:57 +00:00
|
|
|
rend_register(name,pri->type,port,iface,supplied_txt);
|
2006-04-25 23:13:04 +00:00
|
|
|
pri++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ppi=ppi->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-04-22 19:45:49 +00:00
|
|
|
/**
|
|
|
|
* Test password for the handled namespace
|
|
|
|
*
|
|
|
|
* @param pwsc the connection info (including uri) to check
|
|
|
|
* @param username user attempting to login
|
|
|
|
* @param pw password attempting
|
2006-11-22 04:10:29 +00:00
|
|
|
* @returns TRUE if we want to handle it
|
2006-04-22 19:45:49 +00:00
|
|
|
*/
|
|
|
|
int plugin_auth_handle(WS_CONNINFO *pwsc, char *username, char *pw) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
int (*auth_fn)(WS_CONNINFO *pwsc, char *username, char *pw);
|
|
|
|
int result;
|
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
2006-05-28 04:06:14 +00:00
|
|
|
if(ppi->pinfo->type & PLUGIN_OUTPUT) {
|
2006-09-25 03:20:22 +00:00
|
|
|
if((ppi->pinfo->output_fns)->can_handle(pwsc)) {
|
2006-04-22 19:45:49 +00:00
|
|
|
/* we have a winner */
|
|
|
|
DPRINTF(E_DBG,L_PLUG,"Dispatching %s to %s\n", pwsc->uri,
|
2006-05-28 04:06:14 +00:00
|
|
|
ppi->pinfo->server);
|
2006-04-22 19:45:49 +00:00
|
|
|
|
|
|
|
/* so functions must be a tag_plugin_output_fn */
|
2006-05-28 04:06:14 +00:00
|
|
|
auth_fn=(ppi->pinfo->output_fns)->auth;
|
2006-06-05 04:18:33 +00:00
|
|
|
if(auth_fn) {
|
|
|
|
result=auth_fn(pwsc,username,pw);
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2006-04-22 19:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-25 23:13:04 +00:00
|
|
|
ppi = ppi->next;
|
2006-04-22 19:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* should 500 here or something */
|
|
|
|
ws_returnerror(pwsc, 500, "Can't find plugin handler");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-05-04 06:20:26 +00:00
|
|
|
/**
|
|
|
|
* send an event to a plugin... this can be a connection, disconnection, etc.
|
|
|
|
*/
|
|
|
|
void plugin_event_dispatch(int event_id, int intval, void *vp, int len) {
|
|
|
|
PLUGIN_ENTRY *ppi;
|
|
|
|
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while(ppi) {
|
2006-05-28 04:06:14 +00:00
|
|
|
if(ppi->pinfo->type & PLUGIN_EVENT) {
|
2006-05-05 07:38:13 +00:00
|
|
|
/* DPRINTF(E_DBG,L_PLUG,"Dispatching event %d to %s\n",
|
|
|
|
event_id,ppi->versionstring); */
|
2006-05-04 06:20:26 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
if((ppi->pinfo->event_fns) && (ppi->pinfo->event_fns->handler)) {
|
|
|
|
ppi->pinfo->event_fns->handler(event_id, intval, vp, len);
|
2006-05-04 06:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ppi=ppi->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
/**
|
|
|
|
* check to see if we can transcode
|
2006-11-22 04:10:29 +00:00
|
|
|
*
|
2006-05-28 04:06:14 +00:00
|
|
|
* @param codec the codec we are trying to serve
|
|
|
|
* @returns TRUE if we can transcode, FALSE otherwise
|
|
|
|
*/
|
2006-11-05 22:38:12 +00:00
|
|
|
int pi_ssc_should_transcode(WS_CONNINFO *pwsc, char *codec) {
|
2006-05-28 04:06:14 +00:00
|
|
|
int result;
|
2006-05-31 06:07:42 +00:00
|
|
|
char *native_codecs=NULL;
|
|
|
|
char *user_agent=NULL;
|
2006-10-31 03:54:10 +00:00
|
|
|
char *never_transcode = NULL;
|
2007-04-09 04:23:51 +00:00
|
|
|
char *always_transcode = NULL;
|
2006-10-31 03:54:10 +00:00
|
|
|
|
2006-07-26 05:44:54 +00:00
|
|
|
if(!codec) {
|
|
|
|
DPRINTF(E_LOG,L_PLUG,"testing transcode on null codec?\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-05-31 06:07:42 +00:00
|
|
|
|
2006-10-31 03:54:10 +00:00
|
|
|
|
|
|
|
never_transcode = conf_alloc_string("general","never_transcode",NULL);
|
|
|
|
if(never_transcode) {
|
|
|
|
if(strstr(never_transcode,codec)) {
|
|
|
|
free(never_transcode);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
free(never_transcode);
|
|
|
|
}
|
|
|
|
|
2006-05-31 06:07:42 +00:00
|
|
|
if(pwsc) {
|
|
|
|
/* see if the headers give us any guidance */
|
|
|
|
native_codecs = ws_getrequestheader(pwsc,"accept-codecs");
|
|
|
|
if(!native_codecs) {
|
|
|
|
user_agent = ws_getrequestheader(pwsc,"user-agent");
|
2006-06-19 05:47:38 +00:00
|
|
|
if(user_agent) {
|
|
|
|
if(strncmp(user_agent,"iTunes",6)==0) {
|
|
|
|
native_codecs = "mpeg,mp4a,wav,mp4v,alac";
|
|
|
|
} else if(strncmp(user_agent,"Roku",4)==0) {
|
|
|
|
native_codecs = "mpeg,mp4a,wav,wma";
|
2007-04-09 04:23:51 +00:00
|
|
|
} else if(strncmp(user_agent,"Hifidelio",9)==0) {
|
|
|
|
return FALSE;
|
2006-06-19 05:47:38 +00:00
|
|
|
}
|
2006-05-31 06:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-04 06:20:26 +00:00
|
|
|
|
2006-05-31 06:07:42 +00:00
|
|
|
if(!native_codecs) {
|
|
|
|
native_codecs = "mpeg,wav";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can't transcode it if we can't transcode it */
|
2006-05-30 19:31:55 +00:00
|
|
|
if(!_plugin_ssc_codecs)
|
|
|
|
return FALSE;
|
|
|
|
|
2007-04-09 04:23:51 +00:00
|
|
|
always_transcode = conf_alloc_string("general","always_transcode",NULL);
|
|
|
|
if(always_transcode) {
|
|
|
|
if(strstr(always_transcode,codec)) {
|
|
|
|
free(always_transcode);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
free(always_transcode);
|
|
|
|
}
|
|
|
|
|
2006-05-31 06:07:42 +00:00
|
|
|
if(strstr(native_codecs,codec))
|
|
|
|
return FALSE;
|
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
result = FALSE;
|
|
|
|
if(strstr(_plugin_ssc_codecs,codec)) {
|
|
|
|
result = TRUE;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2006-05-04 06:20:26 +00:00
|
|
|
|
2006-04-22 19:45:49 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
/**
|
|
|
|
* stupid helper to copy transcode stream to the fd
|
|
|
|
*/
|
2006-11-22 04:10:29 +00:00
|
|
|
int _plugin_ssc_copy(WS_CONNINFO *pwsc, PLUGIN_TRANSCODE_FN *pfn,
|
2006-05-28 04:06:14 +00:00
|
|
|
void *vp,int offset) {
|
|
|
|
int bytes_read;
|
|
|
|
int bytes_to_read;
|
|
|
|
int total_bytes_read = 0;
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
/* first, skip past the offset */
|
|
|
|
while(offset) {
|
|
|
|
bytes_to_read = sizeof(buffer);
|
|
|
|
if(bytes_to_read > offset)
|
|
|
|
bytes_to_read = offset;
|
|
|
|
|
2006-05-28 06:11:37 +00:00
|
|
|
bytes_read = pfn->ssc_read(vp,buffer,bytes_to_read);
|
2006-05-28 04:06:14 +00:00
|
|
|
if(bytes_read <= 0)
|
|
|
|
return bytes_read;
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
offset -= bytes_read;
|
|
|
|
}
|
|
|
|
|
2006-05-28 06:11:37 +00:00
|
|
|
while((bytes_read=pfn->ssc_read(vp,buffer,sizeof(buffer))) > 0) {
|
2006-05-28 04:06:14 +00:00
|
|
|
total_bytes_read += bytes_read;
|
2006-06-05 04:18:33 +00:00
|
|
|
if(ws_writebinary(pwsc,buffer,bytes_read) != bytes_read) {
|
|
|
|
return total_bytes_read;
|
|
|
|
}
|
2006-05-28 04:06:14 +00:00
|
|
|
}
|
|
|
|
|
2006-12-19 01:48:27 +00:00
|
|
|
/*
|
|
|
|
if(bytes_read < 0) {
|
2006-05-28 04:06:14 +00:00
|
|
|
return bytes_read;
|
2006-12-19 01:48:27 +00:00
|
|
|
}
|
|
|
|
*/
|
2006-05-28 04:06:14 +00:00
|
|
|
|
|
|
|
return total_bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* do the transcode, emitting the headers, content type,
|
|
|
|
* and shoving the file down the wire
|
|
|
|
*
|
|
|
|
* @param pwsc connection to transcode to
|
|
|
|
* @param file file to transcode
|
|
|
|
* @param codec source codec
|
|
|
|
* @param duration time in ms
|
|
|
|
* @returns bytes transferred, or -1 on error
|
|
|
|
*/
|
2006-11-06 03:42:38 +00:00
|
|
|
int _plugin_ssc_transcode(WS_CONNINFO *pwsc, MP3FILE *pmp3, int offset, int headers) {
|
2006-05-28 04:06:14 +00:00
|
|
|
PLUGIN_ENTRY *ppi, *ptc=NULL;
|
|
|
|
PLUGIN_TRANSCODE_FN *pfn = NULL;
|
|
|
|
void *vp_ssc;
|
|
|
|
int post_error = 1;
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
/* first, find the plugin that will do the conversion */
|
|
|
|
ppi = _plugin_list.next;
|
|
|
|
while((ppi) && (!pfn)) {
|
|
|
|
if(ppi->pinfo->type & PLUGIN_TRANSCODE) {
|
2006-11-06 03:42:38 +00:00
|
|
|
if(strstr(ppi->pinfo->codeclist,pmp3->codectype)) {
|
2006-05-28 04:06:14 +00:00
|
|
|
ptc = ppi;
|
|
|
|
pfn = ppi->pinfo->transcode_fns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ppi = ppi->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pfn) {
|
2006-11-06 03:42:38 +00:00
|
|
|
DPRINTF(E_DBG,L_PLUG,"Transcoding %s with %s\n",pmp3->path,
|
2006-05-28 04:06:14 +00:00
|
|
|
ptc->pinfo->server);
|
|
|
|
|
2006-05-28 06:11:37 +00:00
|
|
|
vp_ssc = pfn->ssc_init();
|
2006-05-28 04:06:14 +00:00
|
|
|
if(vp_ssc) {
|
2006-11-06 03:42:38 +00:00
|
|
|
if(pfn->ssc_open(vp_ssc,pmp3)) {
|
2006-05-28 04:06:14 +00:00
|
|
|
/* start reading and throwing */
|
2006-06-04 09:31:32 +00:00
|
|
|
if(headers) {
|
|
|
|
ws_addresponseheader(pwsc,"Content-Type","audio/wav");
|
|
|
|
ws_addresponseheader(pwsc,"Connection","Close");
|
|
|
|
if(!offset) {
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
} else {
|
2006-12-19 01:48:27 +00:00
|
|
|
ws_addresponseheader(pwsc,"Content-Range",
|
|
|
|
"bytes %ld-*/*",
|
2006-06-04 09:31:32 +00:00
|
|
|
(long)offset);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 206 Partial Content\r\n");
|
|
|
|
}
|
|
|
|
ws_emitheaders(pwsc);
|
2006-05-28 04:06:14 +00:00
|
|
|
}
|
2006-06-04 09:31:32 +00:00
|
|
|
|
2006-05-28 04:06:14 +00:00
|
|
|
/* start reading/writing */
|
|
|
|
result = _plugin_ssc_copy(pwsc,pfn,vp_ssc,offset);
|
|
|
|
post_error = 0;
|
2006-05-28 06:11:37 +00:00
|
|
|
pfn->ssc_close(vp_ssc);
|
2006-05-28 04:06:14 +00:00
|
|
|
} else {
|
|
|
|
DPRINTF(E_LOG,L_PLUG,"Error opening %s for ssc: %s\n",
|
2006-11-06 03:42:38 +00:00
|
|
|
pmp3->path,pfn->ssc_error(vp_ssc));
|
2006-05-28 04:06:14 +00:00
|
|
|
}
|
2006-05-28 06:11:37 +00:00
|
|
|
pfn->ssc_deinit(vp_ssc);
|
2006-05-28 04:06:14 +00:00
|
|
|
} else {
|
|
|
|
DPRINTF(E_LOG,L_PLUG,"Error initializing transcoder: %s\n",
|
|
|
|
ptc->pinfo->server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(post_error) {
|
|
|
|
pwsc->error = EPERM; /* ?? */
|
|
|
|
ws_returnerror(pwsc,500,"Internal error");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-04-20 06:52:21 +00:00
|
|
|
/* plugin wrappers for utility functions & stuff
|
2006-11-22 04:10:29 +00:00
|
|
|
*
|
2006-04-20 06:52:21 +00:00
|
|
|
* these functions need to be wrapped so we can maintain a stable
|
|
|
|
* interface to older plugins even if we get newer functions or apis
|
|
|
|
* upstream... it's a binary compatibility layer.
|
|
|
|
*/
|
2006-04-21 06:43:41 +00:00
|
|
|
char *pi_ws_uri(WS_CONNINFO *pwsc) {
|
|
|
|
return pwsc->uri;
|
|
|
|
}
|
|
|
|
|
2006-09-25 03:20:22 +00:00
|
|
|
void pi_ws_will_close(WS_CONNINFO *pwsc) {
|
2006-04-21 06:43:41 +00:00
|
|
|
pwsc->close=1;
|
|
|
|
}
|
|
|
|
|
2006-04-25 10:02:43 +00:00
|
|
|
int pi_ws_fd(WS_CONNINFO *pwsc) {
|
|
|
|
return pwsc->fd;
|
2006-04-21 06:43:41 +00:00
|
|
|
}
|
|
|
|
|
2007-04-13 23:31:57 +00:00
|
|
|
char *pi_ws_gethostname(WS_CONNINFO *pwsc) {
|
|
|
|
return pwsc->hostname;
|
|
|
|
}
|
|
|
|
|
2006-04-21 06:43:41 +00:00
|
|
|
void pi_log(int level, char *fmt, ...) {
|
|
|
|
char buf[256];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
|
|
|
vsnprintf(buf,sizeof(buf),fmt,ap);
|
|
|
|
va_end(ap);
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-04-21 06:43:41 +00:00
|
|
|
DPRINTF(level,L_PLUG,"%s",buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *pi_server_ver(void) {
|
|
|
|
return VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pi_server_name(char *name, int *len) {
|
2006-05-24 03:53:22 +00:00
|
|
|
char *servername;
|
|
|
|
|
|
|
|
servername = conf_get_servername();
|
|
|
|
if((servername) && (strlen(servername) < (size_t)len)) {
|
|
|
|
strcpy(name,servername);
|
|
|
|
} else {
|
|
|
|
if((size_t)len > strlen("Firefly Media Server"))
|
|
|
|
strcpy(name,"Firefly Media Server");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(servername);
|
|
|
|
return CONF_E_SUCCESS;
|
2006-04-21 06:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pi_db_count(void) {
|
|
|
|
int count;
|
|
|
|
db_get_song_count(NULL, &count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-05-27 08:02:39 +00:00
|
|
|
int pi_db_enum_start(char **pe, DB_QUERY *pinfo) {
|
|
|
|
DBQUERYINFO *pqi;
|
|
|
|
int result;
|
2006-11-22 04:10:29 +00:00
|
|
|
|
2006-05-27 08:02:39 +00:00
|
|
|
pqi = (DBQUERYINFO*)malloc(sizeof(DBQUERYINFO));
|
|
|
|
if(!pqi) {
|
|
|
|
if(pe) *pe = strdup("Malloc error");
|
|
|
|
return DB_E_MALLOC;
|
|
|
|
}
|
|
|
|
memset(pqi,0,sizeof(DBQUERYINFO));
|
2006-06-05 04:18:33 +00:00
|
|
|
pinfo->priv = (void*)pqi;
|
2006-05-27 08:02:39 +00:00
|
|
|
|
|
|
|
if(pinfo->filter) {
|
|
|
|
pqi->pt = sp_init();
|
|
|
|
if(!sp_parse(pqi->pt,pinfo->filter,pinfo->filter_type)) {
|
|
|
|
DPRINTF(E_LOG,L_PLUG,"Ignoring bad query (%s): %s\n",
|
|
|
|
pinfo->filter,sp_get_error(pqi->pt));
|
|
|
|
sp_dispose(pqi->pt);
|
|
|
|
pqi->pt = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((pinfo->limit) || (pinfo->offset)) {
|
|
|
|
pqi->index_low = pinfo->offset;
|
|
|
|
pqi->index_high = pinfo->offset + pinfo->limit - 1;
|
|
|
|
if(pqi->index_high < pqi->index_low)
|
|
|
|
pqi->index_high = 9999999;
|
|
|
|
|
|
|
|
pqi->index_type = indexTypeSub;
|
|
|
|
} else {
|
|
|
|
pqi->index_type = indexTypeNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqi->want_count = 1;
|
|
|
|
|
|
|
|
switch(pinfo->query_type) {
|
|
|
|
case QUERY_TYPE_PLAYLISTS:
|
|
|
|
pqi->query_type = queryTypePlaylists;
|
|
|
|
break;
|
|
|
|
case QUERY_TYPE_DISTINCT:
|
|
|
|
if((strcmp(pinfo->distinct_field,"artist") == 0)) {
|
|
|
|
pqi->query_type = queryTypeBrowseArtists;
|
|
|
|
} else if((strcmp(pinfo->distinct_field,"genre") == 0)) {
|
|
|
|
pqi->query_type = queryTypeBrowseGenres;
|
|
|
|
} else if((strcmp(pinfo->distinct_field,"album") == 0)) {
|
|
|
|
pqi->query_type = queryTypeBrowseAlbums;
|
|
|
|
} else if((strcmp(pinfo->distinct_field,"composer") == 0)) {
|
|
|
|
pqi->query_type = queryTypeBrowseComposers;
|
|
|
|
} else {
|
|
|
|
if(pe) *pe = strdup("Unsupported browse type");
|
2006-07-12 22:52:50 +00:00
|
|
|
if(pqi->pt)
|
|
|
|
sp_dispose(pqi->pt);
|
2006-05-27 08:02:39 +00:00
|
|
|
pqi->pt = NULL;
|
|
|
|
return -1; /* not really a db error for this */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QUERY_TYPE_ITEMS:
|
|
|
|
default:
|
|
|
|
pqi->query_type = queryTypePlaylistItems;
|
2006-08-07 05:06:23 +00:00
|
|
|
pqi->correct_order = conf_get_int("scan","correct_order",1);
|
2006-05-27 08:02:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pqi->playlist_id = pinfo->playlist_id;
|
|
|
|
result = db_enum_start(pe, pqi);
|
|
|
|
pinfo->totalcount = pqi->specifiedtotalcount;
|
2006-05-28 04:06:14 +00:00
|
|
|
|
|
|
|
return DB_E_SUCCESS;
|
2006-04-21 06:43:41 +00:00
|
|
|
}
|
|
|
|
|
2006-05-27 08:02:39 +00:00
|
|
|
int pi_db_enum_fetch_row(char **pe, char ***row, DB_QUERY *pinfo) {
|
2006-11-22 04:10:29 +00:00
|
|
|
return db_enum_fetch_row(pe, (PACKED_MP3FILE*)row,
|
2006-06-05 04:18:33 +00:00
|
|
|
(DBQUERYINFO*)pinfo->priv);
|
2006-04-21 06:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pi_db_enum_end(char **pe) {
|
|
|
|
return db_enum_end(pe);
|
|
|
|
}
|
|
|
|
|
2006-09-25 03:20:22 +00:00
|
|
|
/* FIXME: error checking */
|
|
|
|
int pi_db_count_items(int what) {
|
|
|
|
int count=0;
|
|
|
|
|
|
|
|
switch(what) {
|
|
|
|
case COUNT_SONGS:
|
|
|
|
db_get_song_count(NULL,&count);
|
|
|
|
break;
|
|
|
|
case COUNT_PLAYLISTS:
|
|
|
|
db_get_playlist_count(NULL,&count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int pi_db_enum_restart(char **pe, DB_QUERY *pinfo) {
|
|
|
|
DBQUERYINFO *pqi;
|
|
|
|
|
|
|
|
pqi = (DBQUERYINFO*)pinfo->priv;
|
|
|
|
return db_enum_reset(pe,pqi);
|
2006-04-20 06:52:21 +00:00
|
|
|
}
|
2006-04-25 10:02:43 +00:00
|
|
|
|
2006-05-27 08:02:39 +00:00
|
|
|
void pi_db_enum_dispose(char **pe, DB_QUERY *pinfo) {
|
|
|
|
DBQUERYINFO *pqi;
|
|
|
|
|
|
|
|
if(!pinfo)
|
|
|
|
return;
|
|
|
|
|
2006-06-05 04:18:33 +00:00
|
|
|
if(pinfo->priv) {
|
|
|
|
pqi = (DBQUERYINFO *)pinfo->priv;
|
2006-05-27 08:02:39 +00:00
|
|
|
if(pqi->pt) {
|
|
|
|
sp_dispose(pqi->pt);
|
|
|
|
pqi->pt = NULL;
|
|
|
|
}
|
|
|
|
}
|
2006-04-25 10:02:43 +00:00
|
|
|
}
|
2006-05-19 04:50:45 +00:00
|
|
|
|
2006-10-05 03:58:24 +00:00
|
|
|
int pi_db_wait_update(WS_CONNINFO *pwsc) {
|
|
|
|
int clientver=1;
|
|
|
|
fd_set rset;
|
|
|
|
struct timeval tv;
|
|
|
|
int result;
|
|
|
|
int lastver=0;
|
|
|
|
|
|
|
|
if(ws_getvar(pwsc,"revision-number")) {
|
|
|
|
clientver=atoi(ws_getvar(pwsc,"revision-number"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for db_version to be stable for 30 seconds */
|
2006-11-22 04:10:29 +00:00
|
|
|
while((clientver == db_revision()) ||
|
2006-10-05 03:58:24 +00:00
|
|
|
(lastver && (db_revision() != lastver))) {
|
2006-11-22 04:10:29 +00:00
|
|
|
lastver = db_revision();
|
2006-10-05 03:58:24 +00:00
|
|
|
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_SET(pwsc->fd,&rset);
|
|
|
|
|
|
|
|
tv.tv_sec=30;
|
|
|
|
tv.tv_usec=0;
|
|
|
|
|
|
|
|
result=select(pwsc->fd+1,&rset,NULL,NULL,&tv);
|
|
|
|
if(FD_ISSET(pwsc->fd,&rset)) {
|
|
|
|
/* can't be ready for read, must be error */
|
|
|
|
DPRINTF(E_DBG,L_DAAP,"Update session stopped\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2006-09-25 03:20:22 +00:00
|
|
|
void pi_stream(WS_CONNINFO *pwsc, char *id) {
|
|
|
|
int session = 0;
|
|
|
|
MP3FILE *pmp3;
|
|
|
|
int file_fd;
|
2006-12-19 01:48:27 +00:00
|
|
|
int bytes_copied=0;
|
|
|
|
off_t real_len=0;
|
2006-09-25 03:20:22 +00:00
|
|
|
off_t file_len;
|
|
|
|
off_t offset=0;
|
|
|
|
int item;
|
|
|
|
|
|
|
|
/* stream out the song */
|
|
|
|
pwsc->close=1;
|
|
|
|
|
|
|
|
item = atoi(id);
|
|
|
|
|
|
|
|
if(ws_getrequestheader(pwsc,"range")) {
|
|
|
|
offset=(off_t)atol(ws_getrequestheader(pwsc,"range") + 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: error handling */
|
|
|
|
pmp3=db_fetch_item(NULL,item);
|
|
|
|
if(!pmp3) {
|
|
|
|
DPRINTF(E_LOG,L_DAAP|L_WS|L_DB,"Could not find requested item %lu\n",item);
|
|
|
|
config_set_status(pwsc,session,NULL);
|
|
|
|
ws_returnerror(pwsc,404,"File Not Found");
|
2006-11-05 22:38:12 +00:00
|
|
|
} else if (pi_ssc_should_transcode(pwsc,pmp3->codectype)) {
|
2006-09-25 03:20:22 +00:00
|
|
|
/************************
|
|
|
|
* Server side conversion
|
|
|
|
************************/
|
|
|
|
config_set_status(pwsc,session,
|
|
|
|
"Transcoding '%s' (id %d)",
|
|
|
|
pmp3->title,pmp3->id);
|
|
|
|
|
|
|
|
DPRINTF(E_WARN,L_WS,
|
|
|
|
"Session %d: Streaming file '%s' to %s (offset %ld)\n",
|
|
|
|
session,pmp3->fname, pwsc->hostname,(long)offset);
|
|
|
|
|
2006-12-19 01:48:27 +00:00
|
|
|
/* estimate the real length of this thing */
|
2006-11-06 03:42:38 +00:00
|
|
|
bytes_copied = _plugin_ssc_transcode(pwsc,pmp3,offset,1);
|
2006-12-19 01:48:27 +00:00
|
|
|
if(bytes_copied != -1)
|
|
|
|
real_len = bytes_copied;
|
2006-09-25 03:20:22 +00:00
|
|
|
|
|
|
|
config_set_status(pwsc,session,NULL);
|
|
|
|
db_dispose_item(pmp3);
|
|
|
|
} else {
|
|
|
|
/**********************
|
|
|
|
* stream file normally
|
|
|
|
**********************/
|
|
|
|
if(pmp3->data_kind != 0) {
|
|
|
|
ws_returnerror(pwsc,500,"Can't stream radio station");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file_fd=r_open2(pmp3->path,O_RDONLY);
|
|
|
|
if(file_fd == -1) {
|
|
|
|
pwsc->error=errno;
|
|
|
|
DPRINTF(E_WARN,L_WS,"Thread %d: Error opening %s: %s\n",
|
|
|
|
pwsc->threadno,pmp3->path,strerror(errno));
|
|
|
|
ws_returnerror(pwsc,404,"Not found");
|
|
|
|
config_set_status(pwsc,session,NULL);
|
|
|
|
db_dispose_item(pmp3);
|
|
|
|
} else {
|
|
|
|
real_len=lseek(file_fd,0,SEEK_END);
|
|
|
|
lseek(file_fd,0,SEEK_SET);
|
|
|
|
file_len = real_len - offset;
|
|
|
|
|
2007-04-09 04:23:51 +00:00
|
|
|
DPRINTF(E_DBG,L_WS,"Thread %d: Length of file (remaining): %ld\n",
|
2006-09-25 03:20:22 +00:00
|
|
|
pwsc->threadno,(long)file_len);
|
|
|
|
|
|
|
|
// DWB: fix content-type to correctly reflect data
|
|
|
|
// content type (dmap tagged) should only be used on
|
|
|
|
// dmap protocol requests, not the actually song data
|
|
|
|
if(pmp3->type)
|
|
|
|
ws_addresponseheader(pwsc,"Content-Type","audio/%s",pmp3->type);
|
|
|
|
|
|
|
|
ws_addresponseheader(pwsc,"Content-Length","%ld",(long)file_len);
|
|
|
|
|
2007-04-09 04:23:51 +00:00
|
|
|
if((ws_getrequestheader(pwsc,"user-agent")) &&
|
|
|
|
(!strncmp(ws_getrequestheader(pwsc,"user-agent"),
|
|
|
|
"Hifidelio",9))) {
|
|
|
|
ws_addresponseheader(pwsc,"Connection","Keep-Alive");
|
|
|
|
pwsc->close=0;
|
|
|
|
} else {
|
|
|
|
ws_addresponseheader(pwsc,"Connection","Close");
|
|
|
|
}
|
2006-09-25 03:20:22 +00:00
|
|
|
|
|
|
|
if(!offset)
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 200 OK\r\n");
|
|
|
|
else {
|
|
|
|
ws_addresponseheader(pwsc,"Content-Range","bytes %ld-%ld/%ld",
|
|
|
|
(long)offset,(long)real_len,
|
|
|
|
(long)real_len+1);
|
|
|
|
ws_writefd(pwsc,"HTTP/1.1 206 Partial Content\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_emitheaders(pwsc);
|
|
|
|
|
|
|
|
config_set_status(pwsc,session,"Streaming '%s' (id %d)",
|
|
|
|
pmp3->title, pmp3->id);
|
|
|
|
DPRINTF(E_WARN,L_WS,"Session %d: Streaming file '%s' to %s (offset %d)\n",
|
|
|
|
session,pmp3->fname, pwsc->hostname,(long)offset);
|
|
|
|
|
2007-04-09 04:23:51 +00:00
|
|
|
if(offset) {
|
2006-09-25 03:20:22 +00:00
|
|
|
DPRINTF(E_INF,L_WS,"Seeking to offset %ld\n",(long)offset);
|
|
|
|
lseek(file_fd,offset,SEEK_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((bytes_copied=copyfile(file_fd,pwsc->fd)) == -1) {
|
|
|
|
DPRINTF(E_INF,L_WS,"Error copying file to remote... %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
DPRINTF(E_INF,L_WS,"Finished streaming file to remote: %d bytes\n",
|
|
|
|
bytes_copied);
|
|
|
|
}
|
|
|
|
|
|
|
|
config_set_status(pwsc,session,NULL);
|
|
|
|
r_close(file_fd);
|
|
|
|
db_dispose_item(pmp3);
|
|
|
|
}
|
2006-12-19 01:48:27 +00:00
|
|
|
/* update play counts */
|
|
|
|
if(bytes_copied >= (real_len * 80 / 100)) {
|
|
|
|
db_playcount_increment(NULL,pmp3->id);
|
2007-04-09 04:23:51 +00:00
|
|
|
if(!offset)
|
|
|
|
config.stats.songs_served++; /* FIXME: remove stat races */
|
2006-12-19 01:48:27 +00:00
|
|
|
}
|
2006-09-25 03:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// free(pqi);
|
|
|
|
}
|
|
|
|
|
2006-05-19 04:50:45 +00:00
|
|
|
void pi_conf_dispose_string(char *str) {
|
|
|
|
free(str);
|
|
|
|
}
|