Skip to content

Commit

Permalink
spotify_source: play a random playlist when a one cannot be found in …
Browse files Browse the repository at this point in the history
…the user library (closes #274)
  • Loading branch information
Juan A. Rubio committed Nov 13, 2016
1 parent 81a0096 commit e3b3e2c
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions plugins/spotify_source/src/spfysrcprc.c
Expand Up @@ -34,6 +34,7 @@
#include <limits.h>
#include <assert.h>
#include <string.h>
#include <time.h>

#include <OMX_TizoniaExt.h>

Expand All @@ -54,6 +55,23 @@
#define SPFYSRC_MIN_QUEUE_UNUSED_SPACES 5
#define SPFYSRC_MAX_STRING_SIZE 2 * OMX_MAX_STRINGNAME_SIZE

static unsigned int rand_interval(unsigned int min, unsigned int max)
{
int r;
const unsigned int range = 1 + max - min;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;

/* Create equal size buckets all in a row, then fire randomly towards
* the buckets until you land in one of them. All buckets are equally
* likely. If you land off the end of the line of buckets, try again. */
do
{
r = rand();
} while (r >= limit);

return min + (r / buckets);
}
/* This macro assumes the existence of an "ap_prc" local variable */
#define goto_end_on_sp_error(expr) \
do \
Expand Down Expand Up @@ -1506,16 +1524,20 @@ playlist_state_changed (sp_playlist * pl, void * userdata)
p_prc->p_sp_playlist_ = p_prc->p_sp_playlist_
? p_prc->p_sp_playlist_
: p_prc->p_sp_tentative_playlist_;
if (!p_prc->p_sp_playlist_ && tiz_map_size (p_prc->p_ready_playlists_))
{
/* Choose a random playlist */
int r = rand_interval(0, tiz_map_size (p_prc->p_ready_playlists_) - 1);
p_prc->p_sp_playlist_
= tiz_map_value_at (p_prc->p_ready_playlists_, r);
TIZ_PRINTF_BLU ("[Spotify] : 'Playlist not found. Feeling lucky?\n");
}
if (p_prc->p_sp_playlist_)
{
init_track_index (p_prc,
sp_playlist_num_tracks (p_prc->p_sp_playlist_));
start_playback (p_prc);
}
else
{
tiz_srv_issue_err_event ((OMX_PTR) p_prc, OMX_ErrorContentURIError);
}
}
}

Expand Down

0 comments on commit e3b3e2c

Please sign in to comment.