summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alexey Yakovenko <waker@users.sourceforge.net>2014-07-15 20:32:19 +0200
committerGravatar Alexey Yakovenko <waker@users.sourceforge.net>2014-07-15 20:32:19 +0200
commitda4e79329f33efbde04d7791297c4a60b694640e (patch)
treee108a9436baa735ff435fc51274ffe1ff2479431
parentbc59bdde077ac793acaa967f7d468421f0ac5922 (diff)
fixed handler code, and moved streamer_set_nextsong and streamer_play_current_tracks code into the streamer thread
-rw-r--r--handler.c4
-rw-r--r--streamer.c150
-rw-r--r--streamer.h9
3 files changed, 93 insertions, 70 deletions
diff --git a/handler.c b/handler.c
index d6f19761..acef8f77 100644
--- a/handler.c
+++ b/handler.c
@@ -42,12 +42,12 @@ typedef struct message_s {
typedef struct handler_s {
int queue_size;
- message_t pool[1];
message_t *mfree;
message_t *mqueue;
message_t *mqtail;
uintptr_t mutex;
uintptr_t cond;
+ message_t pool[1];
} handler_t;
static void
@@ -67,8 +67,10 @@ handler_alloc (int queue_size) {
int sz = sizeof (handler_t) + (queue_size-1) * sizeof (message_t);
handler_t *h = malloc (sz);
memset (h, 0, sz);
+ h->queue_size = queue_size;
h->mutex = mutex_create ();
h->cond = cond_create ();
+ handler_reset (h);
return h;
}
diff --git a/streamer.c b/streamer.c
index 17710863..8cd7b5e6 100644
--- a/streamer.c
+++ b/streamer.c
@@ -1301,27 +1301,7 @@ streamer_get_apx_bitrate (void) {
void
streamer_set_nextsong (int song, int pstate) {
- DB_output_t *output = plug_get_output ();
- trace ("streamer_set_nextsong %d %d\n", song, pstate);
- streamer_abort_files ();
- streamer_lock ();
- nextsong = song;
- nextsong_pstate = pstate;
- if (output->state () == OUTPUT_STATE_STOPPED) {
- if (pstate == 1) { // means user initiated this
- pl_lock ();
- if (streamer_playlist) {
- plt_unref (streamer_playlist);
- }
- streamer_playlist = plt_get_curr ();
- pl_unlock ();
- }
- // no sense to wait until end of previous song, reset buffer
- bytes_until_next_song = 0;
- playpos = 0;
- seekpos = -1;
- }
- streamer_unlock ();
+ handler_push (handler, STR_EV_PLAY_TRACK_IDX, 0, song, pstate);
}
void
@@ -1478,6 +1458,85 @@ streamer_thread (void *ctx) {
DB_output_t *output = plug_get_output ();
gettimeofday (&tm1, NULL);
+ uint32_t id;
+ uintptr_t ctx;
+ uint32_t p1, p2;
+ if (!handler_pop (handler, &id, &ctx, &p1, &p2)) {
+ if (id == STR_EV_PLAY_TRACK_IDX) {
+ DB_output_t *output = plug_get_output ();
+ trace ("streamer_set_nextsong %d %d\n", song, pstate);
+ streamer_abort_files ();
+ streamer_lock ();
+ nextsong = p1;
+ nextsong_pstate = p2;
+ if (output->state () == OUTPUT_STATE_STOPPED) {
+ if (p2 == 1) { // means user initiated this
+ pl_lock ();
+ if (streamer_playlist) {
+ plt_unref (streamer_playlist);
+ }
+ streamer_playlist = plt_get_curr ();
+ pl_unlock ();
+ }
+ // no sense to wait until end of previous song, reset buffer
+ bytes_until_next_song = 0;
+ playpos = 0;
+ seekpos = -1;
+ }
+ streamer_unlock ();
+ }
+ else if (id == STR_EV_PLAY_CURR) {
+ playlist_t *plt = plt_get_curr ();
+ DB_output_t *output = plug_get_output ();
+ if (output->state () == OUTPUT_STATE_PAUSED && playing_track) {
+ if (is_remote_stream (playing_track)) {
+ streamer_reset (1);
+ streamer_set_current (NULL);
+ streamer_set_current (playing_track);
+ if (fileinfo && memcmp (&orig_output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t))) {
+ memcpy (&output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t));
+ memcpy (&orig_output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t));
+ streamer_set_output_format ();
+ }
+ }
+ // unpause currently paused track
+ output->unpause ();
+ messagepump_push (DB_EV_PAUSED, 0, 0, 0);
+ }
+ else if (plt->current_row[PL_MAIN] != -1) {
+ // play currently selected track in current playlist
+ output->stop ();
+ // get next song in queue
+ int idx = -1;
+ playItem_t *next = pl_playqueue_getnext ();
+ if (next) {
+ idx = str_get_idx_of (next);
+ pl_playqueue_pop ();
+ pl_item_unref (next);
+ }
+ else {
+ idx = plt->current_row[PL_MAIN];
+ }
+
+ streamer_set_nextsong (idx, 1);
+ pl_lock ();
+ if (streamer_playlist) {
+ plt_unref (streamer_playlist);
+ }
+ streamer_playlist = plt;
+ pl_unlock ();
+ return;
+ }
+ else {
+ output->stop ();
+ streamer_move_to_nextsong (1);
+ }
+ if (plt) {
+ plt_unref (plt);
+ }
+ }
+ }
+
if (nextsong >= 0) { // start streaming next song
trace ("\033[0;34mnextsong=%d\033[37;0m\n", nextsong);
if (playing_track) {
@@ -2598,54 +2657,7 @@ streamer_configchanged (void) {
void
streamer_play_current_track (void) {
- playlist_t *plt = plt_get_curr ();
- DB_output_t *output = plug_get_output ();
- if (output->state () == OUTPUT_STATE_PAUSED && playing_track) {
- if (is_remote_stream (playing_track)) {
- streamer_reset (1);
- streamer_set_current (NULL);
- streamer_set_current (playing_track);
- if (fileinfo && memcmp (&orig_output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t))) {
- memcpy (&output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t));
- memcpy (&orig_output_format, &fileinfo->fmt, sizeof (ddb_waveformat_t));
- streamer_set_output_format ();
- }
- }
- // unpause currently paused track
- output->unpause ();
- messagepump_push (DB_EV_PAUSED, 0, 0, 0);
- }
- else if (plt->current_row[PL_MAIN] != -1) {
- // play currently selected track in current playlist
- output->stop ();
- // get next song in queue
- int idx = -1;
- playItem_t *next = pl_playqueue_getnext ();
- if (next) {
- idx = str_get_idx_of (next);
- pl_playqueue_pop ();
- pl_item_unref (next);
- }
- else {
- idx = plt->current_row[PL_MAIN];
- }
-
- streamer_set_nextsong (idx, 1);
- pl_lock ();
- if (streamer_playlist) {
- plt_unref (streamer_playlist);
- }
- streamer_playlist = plt;
- pl_unlock ();
- return;
- }
- else {
- output->stop ();
- streamer_move_to_nextsong (1);
- }
- if (plt) {
- plt_unref (plt);
- }
+ handler_push (handler, STR_EV_PLAY_CURR, 0, 0, 0);
}
struct DB_fileinfo_s *
diff --git a/streamer.h b/streamer.h
index 5bbfd9b1..0d821117 100644
--- a/streamer.h
+++ b/streamer.h
@@ -30,6 +30,12 @@
#include "playlist.h"
#include "deadbeef.h"
+// events to pass to streamer thread
+enum {
+ STR_EV_PLAY_TRACK_IDX, // p1 = idx, p2 = pstate; see streamer_set_nextsong semantics
+ STR_EV_PLAY_CURR, // will play the current streamer track (playing_track), see more details in streamer_play_current_track
+};
+
int
streamer_init (void);
@@ -82,6 +88,9 @@ streamer_get_playing_track (void);
void
streamer_configchanged (void);
+// if paused -- resume
+// else, if have cursor track -- stop current, play cursor
+// else, play next
void
streamer_play_current_track (void);