Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bug fix

  • Loading branch information...
commit 41c5fe594291320c433b29325fec2a9e205f1526 1 parent 21f478f
@hzqtc authored
Showing with 56 additions and 44 deletions.
  1. +1 −3 README.md
  2. +12 −3 app.c
  3. +40 −31 player.c
  4. +1 −1  player.h
  5. +1 −6 server.c
  6. +1 −0  server.h
View
4 README.md
@@ -42,7 +42,7 @@ Change the config values as described above.
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.
@@ -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}
help
{"status":"error","message":"wrong command: help"}
- end
- Connection closed by foreign host.
## Install
View
15 app.c
@@ -134,6 +134,8 @@ void daemonize(const char *lock_file, const char *log_file, const char *err_file
exit(0);
}
+ chdir("/");
+
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
@@ -182,8 +184,6 @@ int main(int argc, char *argv[])
strcpy(lock_file, pwd->pw_dir);
strcat(lock_file, "/.fmd/fmd.lock");
- daemonize(lock_file, log_file, err_file);
-
int c;
while ((c = getopt(argc, argv, "a:p:")) != -1) {
switch (c) {
@@ -199,7 +199,10 @@ int main(int argc, char *argv[])
}
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);
int player_end_sig = SIGUSR1;
@@ -219,6 +222,12 @@ int main(int argc, char *argv[])
};
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_playlist_cleanup(&app.playlist);
View
71 player.c
@@ -23,12 +23,11 @@ static void* download_thread(void *data)
fm_player_t *pl = (fm_player_t*) data;
curl_easy_perform(pl->curl);
+ pthread_cond_signal(&pl->cond_play);
mpg123_set_filesize(pl->mh, pl->info.file_size);
pl->info.samples = mpg123_length(pl->mh);
- pthread_cond_signal(&pl->cond_play);
-
return pl;
}
@@ -53,32 +52,35 @@ static void* play_thread(void *data)
}
err = mpg123_decode_frame(pl->mh, &off, &audio, &size);
- if (err == MPG123_OK) {
- ao_play(pl->dev, (char*) audio, size);
- }
- else if (err == MPG123_NEED_MORE) {
- if (pthread_kill(pl->tid_dl, 0) == 0) {
- pthread_mutex_lock(&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);
+ switch (err) {
+ case MPG123_OK:
+ ao_play(pl->dev, (char*) audio, size);
+ break;
+ case MPG123_NEED_MORE:
+ if (pthread_kill(pl->tid_dl, 0) == 0) {
+ pthread_mutex_lock(&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);
+ }
+ return pl;
+ }
+ break;
+ case MPG123_NEW_FORMAT:
+ break;
+ default:
+ fprintf(stderr, "mpg123 deocde return: %d\n", err);
break;
- }
- }
- else if (err == MPG123_NEW_FORMAT ) {
- ;
- }
- else {
- fprintf(stderr, "mpg123 deocde return: %d\n", err);
}
}
return pl;
}
-void fm_player_open(fm_player_t *pl)
+int fm_player_open(fm_player_t *pl)
{
pl->format.rate = 44100;
pl->format.channels = 2;
@@ -94,11 +96,16 @@ void fm_player_open(fm_player_t *pl)
format.bits = mpg123_encsize(pl->format.encoding) * 8;
format.byte_format = AO_FMT_NATIVE;
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_append_option(&options, "dev", "default");
- int driver = ao_driver_id("alsa");
pl->dev = ao_open_live(driver, &format, options);
ao_free_options(options);
+ if (pl->dev == NULL)
+ return -1;
pl->curl = curl_easy_init();
curl_easy_setopt(pl->curl, CURLOPT_WRITEFUNCTION, download_callback);
@@ -110,6 +117,8 @@ void fm_player_open(fm_player_t *pl)
pthread_cond_init(&pl->cond_play, NULL);
pl->status = FM_PLAYER_STOP;
+
+ return 0;
}
void fm_player_close(fm_player_t *pl)
@@ -185,16 +194,16 @@ void fm_player_toggle(fm_player_t *pl)
void fm_player_stop(fm_player_t *pl)
{
- pthread_mutex_lock(&pl->mutex_status);
- pl->status = FM_PLAYER_STOP;
- pthread_mutex_unlock(&pl->mutex_status);
- pthread_cond_signal(&pl->cond_play);
+ if (pl->status != FM_PLAYER_STOP) {
+ pthread_mutex_lock(&pl->mutex_status);
+ pl->status = FM_PLAYER_STOP;
+ pthread_mutex_unlock(&pl->mutex_status);
+ pthread_cond_signal(&pl->cond_play);
- pthread_cancel(pl->tid_dl);
- pthread_cancel(pl->tid_play);
- pthread_join(pl->tid_dl, NULL);
- pthread_join(pl->tid_play, NULL);
- mpg123_close(pl->mh);
+ pthread_join(pl->tid_dl, NULL);
+ pthread_join(pl->tid_play, NULL);
+ mpg123_close(pl->mh);
+ }
}
void fm_player_init()
View
2  player.h
@@ -53,7 +53,7 @@ void fm_player_pause(fm_player_t *pl);
void fm_player_toggle(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_init();
void fm_player_exit();
View
7 server.c
@@ -9,7 +9,7 @@
#include <string.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;
@@ -86,11 +86,6 @@ void fm_server_run(fm_server_t *server, server_handle handle, void *handle_data)
int buf_size;
fd_set read_fds;
- if (fm_server_setup(server) < 0) {
- perror("setup");
- return;
- }
-
while (!server->should_quit) {
read_fds = server->fds;
if (select(server->fd_max + 1, &read_fds, NULL, NULL, NULL) < 0) {
View
1  server.h
@@ -16,6 +16,7 @@ typedef struct {
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);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.