Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqtc committed Jun 5, 2012
1 parent 21f478f commit 41c5fe5
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -42,7 +42,7 @@ Change the config values as described above.


The communication between FMD and clients go throught TCP connection. The communication between FMD and clients go throught TCP connection.


Commands client can send are "play", "stop", "pause", "toggle", "skip", "ban", "rate", "unrate", "info" and "end", these commands are all self-explained. Only notice that "end" will tell FMD to quit and close all client connections. Commands client can send are "play", "stop", "pause", "toggle", "skip", "ban", "rate", "unrate", and "info", these commands are all self-explained.


No response to command "end". Responses to all other commands are json formmated strings containing current playing infomation. No response to command "end". Responses to all other commands are json formmated strings containing current playing infomation.


Expand Down Expand Up @@ -78,8 +78,6 @@ The simplest FMD client is telnet:
{"status":"play","channel":0,"user":"小强","title":"What's My Name (Intro #1)","artist":"Rihanna / Drake", "album":"Promo Only Rhythm...","year":2010,"cover":"http://img1.douban.com/mpic/s4615061.jpg","url":"/subject/5951920/","sid":1561924,"like":0,"pos":111,"len":254} {"status":"play","channel":0,"user":"小强","title":"What's My Name (Intro #1)","artist":"Rihanna / Drake", "album":"Promo Only Rhythm...","year":2010,"cover":"http://img1.douban.com/mpic/s4615061.jpg","url":"/subject/5951920/","sid":1561924,"like":0,"pos":111,"len":254}
help help
{"status":"error","message":"wrong command: help"} {"status":"error","message":"wrong command: help"}
end
Connection closed by foreign host.




## Install ## Install
Expand Down
15 changes: 12 additions & 3 deletions app.c
Expand Up @@ -134,6 +134,8 @@ void daemonize(const char *lock_file, const char *log_file, const char *err_file
exit(0); exit(0);
} }


chdir("/");

close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
Expand Down Expand Up @@ -182,8 +184,6 @@ int main(int argc, char *argv[])
strcpy(lock_file, pwd->pw_dir); strcpy(lock_file, pwd->pw_dir);
strcat(lock_file, "/.fmd/fmd.lock"); strcat(lock_file, "/.fmd/fmd.lock");


daemonize(lock_file, log_file, err_file);

int c; int c;
while ((c = getopt(argc, argv, "a:p:")) != -1) { while ((c = getopt(argc, argv, "a:p:")) != -1) {
switch (c) { switch (c) {
Expand All @@ -199,7 +199,10 @@ int main(int argc, char *argv[])
} }


fm_player_init(); fm_player_init();
fm_player_open(&app.player); if (fm_player_open(&app.player) < 0) {
perror("open audio output");
return 1;
}
fm_playlist_init(&app.playlist); fm_playlist_init(&app.playlist);


int player_end_sig = SIGUSR1; int player_end_sig = SIGUSR1;
Expand All @@ -219,6 +222,12 @@ int main(int argc, char *argv[])
}; };
fm_config_parse(config_file, configs, sizeof(configs) / sizeof(fm_config_t)); fm_config_parse(config_file, configs, sizeof(configs) / sizeof(fm_config_t));


if (fm_server_setup(&app.server) < 0) {
perror("setup server");
return 1;
}
printf("Daemonize...\n");
daemonize(lock_file, log_file, err_file);
fm_server_run(&app.server, app_client_handler, &app); fm_server_run(&app.server, app_client_handler, &app);


fm_playlist_cleanup(&app.playlist); fm_playlist_cleanup(&app.playlist);
Expand Down
71 changes: 40 additions & 31 deletions player.c
Expand Up @@ -23,12 +23,11 @@ static void* download_thread(void *data)
fm_player_t *pl = (fm_player_t*) data; fm_player_t *pl = (fm_player_t*) data;


curl_easy_perform(pl->curl); curl_easy_perform(pl->curl);
pthread_cond_signal(&pl->cond_play);


mpg123_set_filesize(pl->mh, pl->info.file_size); mpg123_set_filesize(pl->mh, pl->info.file_size);
pl->info.samples = mpg123_length(pl->mh); pl->info.samples = mpg123_length(pl->mh);


pthread_cond_signal(&pl->cond_play);

return pl; return pl;
} }


Expand All @@ -53,32 +52,35 @@ static void* play_thread(void *data)
} }


err = mpg123_decode_frame(pl->mh, &off, &audio, &size); err = mpg123_decode_frame(pl->mh, &off, &audio, &size);
if (err == MPG123_OK) { switch (err) {
ao_play(pl->dev, (char*) audio, size); case MPG123_OK:
} ao_play(pl->dev, (char*) audio, size);
else if (err == MPG123_NEED_MORE) { break;
if (pthread_kill(pl->tid_dl, 0) == 0) { case MPG123_NEED_MORE:
pthread_mutex_lock(&pl->mutex_status); if (pthread_kill(pl->tid_dl, 0) == 0) {
pthread_cond_wait(&pl->cond_play, &pl->mutex_status); pthread_mutex_lock(&pl->mutex_status);
pthread_mutex_unlock(&pl->mutex_status); pthread_cond_wait(&pl->cond_play, &pl->mutex_status);
} pthread_mutex_unlock(&pl->mutex_status);
else if (pl->tid_ack >= 0) { }
pthread_kill(pl->tid_ack, pl->sig_ack); else {
if (pl->tid_ack >= 0) {
pthread_kill(pl->tid_ack, pl->sig_ack);
}
return pl;
}
break;
case MPG123_NEW_FORMAT:
break;
default:
fprintf(stderr, "mpg123 deocde return: %d\n", err);
break; break;
}
}
else if (err == MPG123_NEW_FORMAT ) {
;
}
else {
fprintf(stderr, "mpg123 deocde return: %d\n", err);
} }
} }


return pl; return pl;
} }


void fm_player_open(fm_player_t *pl) int fm_player_open(fm_player_t *pl)
{ {
pl->format.rate = 44100; pl->format.rate = 44100;
pl->format.channels = 2; pl->format.channels = 2;
Expand All @@ -94,11 +96,16 @@ void fm_player_open(fm_player_t *pl)
format.bits = mpg123_encsize(pl->format.encoding) * 8; format.bits = mpg123_encsize(pl->format.encoding) * 8;
format.byte_format = AO_FMT_NATIVE; format.byte_format = AO_FMT_NATIVE;
format.matrix = 0; format.matrix = 0;

int driver = ao_driver_id("alsa");
ao_info *driver_info = ao_driver_info(driver);
printf("Audio Driver: %s\n", driver_info->name);
ao_option *options = NULL; ao_option *options = NULL;
ao_append_option(&options, "dev", "default"); ao_append_option(&options, "dev", "default");
int driver = ao_driver_id("alsa");
pl->dev = ao_open_live(driver, &format, options); pl->dev = ao_open_live(driver, &format, options);
ao_free_options(options); ao_free_options(options);
if (pl->dev == NULL)
return -1;


pl->curl = curl_easy_init(); pl->curl = curl_easy_init();
curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, download_callback); curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, download_callback);
Expand All @@ -110,6 +117,8 @@ void fm_player_open(fm_player_t *pl)
pthread_cond_init(&pl->cond_play, NULL); pthread_cond_init(&pl->cond_play, NULL);


pl->status = FM_PLAYER_STOP; pl->status = FM_PLAYER_STOP;

return 0;
} }


void fm_player_close(fm_player_t *pl) void fm_player_close(fm_player_t *pl)
Expand Down Expand Up @@ -185,16 +194,16 @@ void fm_player_toggle(fm_player_t *pl)


void fm_player_stop(fm_player_t *pl) void fm_player_stop(fm_player_t *pl)
{ {
pthread_mutex_lock(&pl->mutex_status); if (pl->status != FM_PLAYER_STOP) {
pl->status = FM_PLAYER_STOP; pthread_mutex_lock(&pl->mutex_status);
pthread_mutex_unlock(&pl->mutex_status); pl->status = FM_PLAYER_STOP;
pthread_cond_signal(&pl->cond_play); pthread_mutex_unlock(&pl->mutex_status);
pthread_cond_signal(&pl->cond_play);


pthread_cancel(pl->tid_dl); pthread_join(pl->tid_dl, NULL);
pthread_cancel(pl->tid_play); pthread_join(pl->tid_play, NULL);
pthread_join(pl->tid_dl, NULL); mpg123_close(pl->mh);
pthread_join(pl->tid_play, NULL); }
mpg123_close(pl->mh);
} }


void fm_player_init() void fm_player_init()
Expand Down
2 changes: 1 addition & 1 deletion player.h
Expand Up @@ -53,7 +53,7 @@ void fm_player_pause(fm_player_t *pl);
void fm_player_toggle(fm_player_t *pl); void fm_player_toggle(fm_player_t *pl);
void fm_player_stop(fm_player_t *pl); void fm_player_stop(fm_player_t *pl);


void fm_player_open(fm_player_t *pl); int fm_player_open(fm_player_t *pl);
void fm_player_close(fm_player_t *pl); void fm_player_close(fm_player_t *pl);
void fm_player_init(); void fm_player_init();
void fm_player_exit(); void fm_player_exit();
Expand Down
7 changes: 1 addition & 6 deletions server.c
Expand Up @@ -9,7 +9,7 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>


static int fm_server_setup(fm_server_t *server) int fm_server_setup(fm_server_t *server)
{ {
struct addrinfo hints, *results, *p; struct addrinfo hints, *results, *p;


Expand Down Expand Up @@ -86,11 +86,6 @@ void fm_server_run(fm_server_t *server, server_handle handle, void *handle_data)
int buf_size; int buf_size;
fd_set read_fds; fd_set read_fds;


if (fm_server_setup(server) < 0) {
perror("setup");
return;
}

while (!server->should_quit) { while (!server->should_quit) {
read_fds = server->fds; read_fds = server->fds;
if (select(server->fd_max + 1, &read_fds, NULL, NULL, NULL) < 0) { if (select(server->fd_max + 1, &read_fds, NULL, NULL, NULL) < 0) {
Expand Down
1 change: 1 addition & 0 deletions server.h
Expand Up @@ -16,6 +16,7 @@ typedef struct {


typedef void (*server_handle)(void *ptr, const char *input, char *output); typedef void (*server_handle)(void *ptr, const char *input, char *output);


int fm_server_setup(fm_server_t *server);
void fm_server_run(fm_server_t *server, server_handle handle, void *handle_data); void fm_server_run(fm_server_t *server, server_handle handle, void *handle_data);


#endif #endif

0 comments on commit 41c5fe5

Please sign in to comment.