static gboolean spotify_login (GstSpotSrc *spot) { sp_error error; if (GST_SPOT_SRC_LOGGED_IN (spot)) { GST_DEBUG_OBJECT (spot, "Already logged in"); return TRUE; } GST_DEBUG_OBJECT (spot, "Trying to login"); /* login using the credentials given on the command line */ error = sp_session_login (GST_SPOT_SRC_SPOTIFY_SESSION (spot), GST_SPOT_SRC_USER (spot), GST_SPOT_SRC_PASS (spot)); if (SP_ERROR_OK != error) { GST_ERROR_OBJECT (spot, "Failed to login: %s", sp_error_message (error)); return FALSE; } int timeout = -1; sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); while (!GST_SPOT_SRC_LOGGED_IN (spot)) { usleep (10000); sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); } GST_DEBUG_OBJECT (spot, "Login ok!"); return TRUE; }
static void send_reply(struct mg_connection *conn) { if(!strcmp(conn->uri, "/search")) { printf("Received \"search\" request.\n"); fflush(stdout); //mg_printf_data(conn, "Search %s", conn->query_string); //Call search function int isLoaded = 0; sp_search* search = sp_search_create(g_sess, conn->query_string, 0, 100, 0, 100, 0, 100, 0, 100, SP_SEARCH_STANDARD, &search_complete, &isLoaded); int timeout = 0; sp_session_process_events(g_sess, &timeout); while(!isLoaded) { usleep(100000); printf("Waiting...\n"); sp_session_process_events(g_sess, &timeout); } // pthread_mutex_lock(&g_search_mutex); // while (!isLoaded) { // while(!sp_search_is_loaded(search)) { // printf("Waiting...\n"); // fflush(stdout); // pthread_cond_wait(&g_search_cond, &g_search_mutex); // printConnectionState(); // print_search_error(search); // usleep(1000000); // } // printf("Received search_complete signal."); // fflush(stdout); char* rv = search_to_json(search); // pthread_mutex_unlock(&g_search_mutex); sp_search_release(search); mg_printf_data(conn, rv); } else if(!strcmp(conn->uri, "/upvote")) { mg_printf_data(conn, "Upvote %s", conn->query_string); //call upvote function mg_printf_data(conn,"Upvoted"); upvoteHelper(conn->query_string); try_playback_start(); } else if(!strcmp(conn->uri, "/queue")) { //call queue function // TODO: print this to user not console printf(print_queue()); mg_printf_data(conn,print_queue()); } else if(!strcmp(conn->uri, "/key")) { //mg_printf_data(conn, "Key: %d", g_appkey[0]); // this is not part of our project. it will not be done. } else if(!strcmp(conn->uri, "/ping")) { mg_printf_data(conn,"Hello. Partyfy is running."); } else { } }
int main() { struct mg_server *server = mg_create_server(NULL, event_handler); mg_set_option(server, "Partyfy", "."); mg_set_option(server, "listening_port", "8080"); const char *username = "******"; //size_t size; char *password = getpass("Enter the password for the account\n"); sp_session *sp; spconfig.application_key_size = g_appkey_size; sp_error err = sp_session_create(&spconfig, &sp); if(SP_ERROR_OK != err) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(err)); exit(1); } g_sess = sp; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); sp_session_login(g_sess, username, password, 0, NULL); // See if the user logged in successfully printConnectionState(); int timeout = 0; sp_connectionstate state = sp_session_connectionstate(g_sess); while (state != SP_CONNECTION_STATE_LOGGED_IN) { sp_session_process_events(g_sess, &timeout); printf("Logging in...\n"); usleep(100000); state = sp_session_connectionstate(g_sess); } //audio_init(&g_audiofifo); printf("Logged in\n"); for(;;) { mg_poll_server(server, 1000); sp_session_process_events(g_sess, &timeout); //while(!g_notify_do) // pthread_cond_wait(&g_notify_cond, &g_notify_mutex); //g_notify_do = 0; //pthread_mutex_unlock(&g_notify_mutex); //if(g_playback_done) { // g_playback_done = 0; //} //pthread_mutex_lock(&g_notify_mutex); } mg_destroy_server(&server); }
int main(void) { sp_error error; sp_session *session; // create the spotify session spconfig.application_key_size = g_appkey_size; error = sp_session_create(&spconfig, &session); if (error != SP_ERROR_OK) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(error)); return 1; } // initialize audio audio_init(&g_audiofifo); // log in g_logged_in = 0; sp_session_login(session, username, password, 0, NULL); // main loop g_playing = 0; g_running = 1; int next_timeout = 0; while (g_running) { sp_session_process_events(session, &next_timeout); if (g_logged_in && !g_playing) { run_search(session); g_playing = 1; } } return 0; }
PHP_METHOD(SpotifyAlbumIterator, __construct) { zval *thisptr = getThis(), *parent; spotifyartist_object *artistobj; int timeout = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &parent, spotifyartist_ce) == FAILURE) { return; } zval *spotifyobject = GET_PROPERTY(spotifyartist_ce, parent, "spotify"); spotify_object *p = (spotify_object*)zend_object_store_get_object(spotifyobject TSRMLS_CC); artistobj = (spotifyartist_object*)zend_object_store_get_object(parent TSRMLS_CC); spotifyalbumiterator_object *obj = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC); obj->session = p->session; obj->artist = artistobj->artist; obj->artistbrowse = sp_artistbrowse_create(p->session, artistobj->artist, SP_ARTISTBROWSE_FULL, spotify_artistbrowse_complete, obj); while (!sp_artistbrowse_is_loaded(obj->artistbrowse)) { sp_session_process_events(p->session, &timeout); } obj->position = 0; obj->length = sp_artistbrowse_num_albums(obj->artistbrowse); zend_update_property(spotifyalbumiterator_ce, getThis(), "spotify", strlen("spotify"), spotifyobject TSRMLS_CC); sp_artistbrowse_add_ref(obj->artistbrowse); }
PHP_METHOD(Spotify, getAlbumByURI) { zval *uri, temp, *object = getThis(); int timeout = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &uri) == FAILURE) { return; } spotify_object *p = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC); sp_link *link = sp_link_create_from_string(Z_STRVAL_P(uri)); if (NULL == link) { RETURN_FALSE; } if (SP_LINKTYPE_ALBUM != sp_link_type(link)) { RETURN_FALSE; } sp_album *album = sp_link_as_album(link); while (!sp_album_is_loaded(album)) { sp_session_process_events(p->session, &timeout); } object_init_ex(return_value, spotifyalbum_ce); SPOTIFY_METHOD2(SpotifyAlbum, __construct, &temp, return_value, object, album); sp_link_release(link); }
int mainloop(sp_session *session, int listen_fd) { int event, timeout; int loops; event = 0; do { syslog(LOG_DEBUG, "EVENTLOOP [id %d]: Processing Spotify events", event); loops = 0; do { sp_session_process_events(session, &timeout); loops++; } while(timeout == 0); syslog(LOG_DEBUG, "EVENTLOOP [id %d]: Done processing %d Spotify events, next timeout %dms", event, loops, timeout); if(app_process_events() < 0) { syslog(LOG_INFO, "EVENTLOOP [id %d]: app_process_events() failed", event); break; } if(net_poll(listen_fd, timeout) < 0) { syslog(LOG_INFO, "EVENTLOOP [id %d]: net_poll() failed", event); break; } event++; } while(1); return 0; }
static PyObject *Session_process_events(Session *self) { int timeout; Py_BEGIN_ALLOW_THREADS sp_session_process_events(self->_session, &timeout); Py_END_ALLOW_THREADS return Py_BuildValue("i", timeout); }
/* * Called from login_gui when the user has pushed login */ void session_login(const char *username, const char *password) { if (g_session != NULL) { int next_timeout = 0; g_logged_in = 0; sp_session_login(g_session, username, password, 0, NULL); while (!g_logged_in) { sp_session_process_events(g_session, &next_timeout); usleep(1000); } /*sp_session_login(g_session, username, password, 0, NULL); pthread_mutex_lock(&g_notify_mutex); pthread_cond_wait(&g_notify_cond, &g_notify_mutex); pthread_mutex_unlock(&g_notify_mutex); int next_timeout; do { sp_session_process_events(g_session, &next_timeout); } while (next_timeout == 0);*/ //pthread_mutex_lock(&g_notify_mutex); //pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { printf("session_login - Session is NULL!\n"); } }
void* notification_routine(void *s) { fprintf(stderr, "Start notification"); sp_session *sp = (sp_session*)s; int next_timeout = 0; pthread_mutex_lock(&g_notify_mutex); for (;;) { if (next_timeout == 0) { while(!g_notify_do) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; clock_gettime(0, &ts); ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); do { sp_session_process_events(sp, &next_timeout); } while (next_timeout == 0); pthread_mutex_lock(&g_notify_mutex); } }
static void event_loop(sp_session *session) { int timeout = -1; #ifndef _WIN32 sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, SIGIO); #endif while (g_exit_code < 0) { #ifndef _WIN32 pthread_sigmask(SIG_BLOCK, &sigset, NULL); #endif DSFYDEBUG("Calling sp_session_process_events()\n"); sp_session_process_events(session, &timeout); if(test_run() < 0) { DSFYDEBUG("Done running test, existing event loop\n"); break; } #ifdef _WIN32 WaitForSingleObject(g_notify_event, timeout); #else pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); usleep(timeout * 1000); #endif } DSFYDEBUG("Exiting from loop()\n"); }
/** notifyMainThread this will be called when spotify needs to process events **/ void SpotifySession::notifyMainThread() { int timeout = 0; do { sp_session_process_events( m_session, &timeout ); } while( !timeout ); QTimer::singleShot( timeout, this, SLOT( notifyMainThread() ) ); }
void *start_spotify(void *arg) { printf("Spotify: Started\n"); int next_timeout = 0; sp_error err; /* Create session */ session_config.application_key_size = g_appkey_size; err = sp_session_create(&session_config, &session); if(err != SP_ERROR_OK) { fprintf(stderr, "Unable to create session: %s\n", sp_error_message(err)); return NULL; } sp_session_login(session, username, password, 1); for (;;) { if (next_timeout == 0) { while(!g_notify_do && !g_playback_done) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); if (g_playback_done) { // track_ended(); g_playback_done = 0; } do { sp_session_process_events(session, &next_timeout); } while (next_timeout == 0); pthread_mutex_lock(&g_notify_mutex); } }
void Session::Update() { if (m_pSession) { m_isProcessEventsRequired = false; int nextTimeout = 0; sp_session_process_events( m_pSession, &nextTimeout ); } }
DWORD WINAPI spotifyThread(void *data) { SpotifyThreadData *dat = (SpotifyThreadData*)data; int nextTimeout = INFINITE; while (true) { WaitForSingleObject(dat->processEventsEvent, nextTimeout); LockedCS lock(dat->cs); sp_session_process_events(dat->sess, &nextTimeout); } }
static void try_playback_start(void) { sp_track *t; if(amtSongs() == 0) { fprintf(stderr, "Playlist: No tracks in playlist. Waiting\n"); return; } t = sp_link_as_track(firstSong->song); if(g_currenttrack && t != g_currenttrack) { audio_fifo_flush(&g_audiofifo); sp_session_player_unload(g_sess); g_currenttrack = NULL; } if(!t) { printf("Null Song\n"); fflush(stdout); return; } int next_timeout = 0; sp_session_process_events(g_sess, &next_timeout); while(sp_track_error(t) != SP_ERROR_OK) { sp_session_process_events(g_sess, &next_timeout); printf("Loading Track...\n"); usleep(100000); } if(sp_track_error(t) != SP_ERROR_OK) { printf("SP_ERRoR\n"); printf("%i\n", sp_track_error(t)); fflush(stdout); return; } if(g_currenttrack == t) { printf("Coninuting same track\n"); fflush(stdout); return; } g_currenttrack = t; printf("Partyfy: Now playing \"%s\"...\n", sp_track_name(t)); fflush(stdout); sp_session_player_load(g_sess, t); sp_session_player_play(g_sess, 1); }
static PyObject * Session_process_events(PyObject *self) { int timeout; Py_BEGIN_ALLOW_THREADS; sp_session_process_events(Session_SP_SESSION(self), &timeout); Py_END_ALLOW_THREADS; return Py_BuildValue("i", timeout); }
void process_events(evutil_socket_t socket, short what, void *userdata) { struct state *state = userdata; event_del(state->timer); int timeout = 0; do { sp_session_process_events(state->session, &timeout); } while (timeout == 0); state->next_timeout.tv_sec = timeout / 1000; state->next_timeout.tv_usec = (timeout % 1000) * 1000; evtimer_add(state->timer, &state->next_timeout); }
PHP_METHOD(Spotify, __destruct) { spotify_object *obj = (spotify_object*)zend_object_store_get_object(getThis() TSRMLS_CC); int timeout = 0; if (obj->playlistcontainer != NULL) { //sp_playlistcontainer_release(obj->playlistcontainer); } do { sp_session_process_events(obj->session, &timeout); } while (timeout == 0); sp_session_logout(obj->session); timeout = 0; do { sp_session_process_events(obj->session, &timeout); } while (!obj->is_logged_out || timeout == 0); efree(obj->key_data); }
static LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static char *username, *password; switch(uMsg) { case WM_TIMER: DSFYDEBUG("uMsg=%u (%s), wParam=%u, lParam=%u\n", uMsg, WM2STR(uMsg), wParam, lParam); if(session) { int timeout; KillTimer(hwnd, TIMERID); sp_session_process_events(session, &timeout); SetTimer(hwnd, TIMERID, timeout, NULL); } break; case WM_USER: switch(LOWORD(wParam)) { case 1: // Set tree root ID m_TreeRootId = (UINT_PTR)lParam; break; case 2: // Set username username = (char *)lParam; DSFYDEBUG("Got username '%s'\n", username); break; case 3: // Set password password = (char *)lParam; DSFYDEBUG("Got password '%s'\n", password); break; case 4: // Login if not yet logged in if(sp_session_connectionstate(session) != SP_CONNECTION_STATE_LOGGED_IN) sp_session_login(session, username, password); break; case 5: // Get connection status if(!session) return -1; else if(sp_session_connectionstate(session) != SP_CONNECTION_STATE_LOGGED_IN) return -1; return 0; } break; } return DefWindowProc(hwnd, uMsg, wParam, lParam); }
/* * Log in and wait for rootlist to be loaded */ void launch(void) { printf("Welcome to spotify_terminal\n"); printf("Using libspotify %s\n", sp_build_id()); init(); get_user_info(); printf("Logging in user: '******'... \n", username); usleep(500000); log_in(); while(is_logged_in != TRUE) { sp_session_process_events(g_session, &next_timeout); } printf("Loading playlists... \n"); usleep(500000); sp_session_process_events(g_session, &next_timeout); while(playlist_loaded != TRUE) { sp_session_process_events(g_session, &next_timeout); } printf("ready!\n"); printf("> "); fflush(stdout); }
/************************* *** Events management *** *************************/ gboolean session_libspotify_event(gpointer data) { static guint evid = 0; int timeout; if (evid > 0) g_source_remove(evid); do { sp_session_process_events(g_session, &timeout); } while (timeout <= 1); /* Add next timeout */ evid = g_timeout_add(timeout, session_libspotify_event, NULL); return FALSE; }
PHP_METHOD(SpotifyArtist, browse) { int timeout = 0; spotifyartist_object *p = (spotifyartist_object*)zend_object_store_get_object(getThis() TSRMLS_CC); if (p->artistbrowse != NULL) { RETURN_TRUE; } sp_artistbrowse *tmpbrowse = sp_artistbrowse_create(p->session, p->artist, SP_ARTISTBROWSE_FULL, artistbrowse_complete, p); while (!sp_artistbrowse_is_loaded(tmpbrowse)) { sp_session_process_events(p->session, &timeout); } RETURN_TRUE; }
PHP_METHOD(Spotify, getStarredPlaylist) { zval *object = getThis(); zval temp; spotify_object *obj = (spotify_object*)zend_object_store_get_object(object TSRMLS_CC); do { sp_session_process_events(obj->session, &obj->timeout); } while (obj->timeout == 0); sp_playlist *playlist = sp_session_starred_create(obj->session); object_init_ex(return_value, spotifyplaylist_ce); SPOTIFY_METHOD2(SpotifyPlaylist, __construct, &temp, return_value, object, playlist); }
// Process Spotify events in main event loop. void sess_event_cb(evutil_socket_t sock, short event, void *arg) { (void)sock; (void)event; session_t *sess = arg; int timeout = 0; while (!timeout) sp_session_process_events(sess->spotify, &timeout); // Schedule next event. struct timeval tv; tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; evtimer_add(sess->spot_ev, &tv); }
static void *spotify_loop(void *arg) { gazify_t *gazify = (gazify_t *)arg; sp_session *session = gazify->session; int next_timeout = 0; pthread_mutex_init(&g_notify_mutex, NULL); pthread_cond_init(&g_notify_cond, NULL); while (!g_gazify.want_exit) { if (next_timeout == 0) { while(!g_notify_do) pthread_cond_wait(&g_notify_cond, &g_notify_mutex); } else { struct timespec ts; #if _POSIX_TIMERS > 0 clock_gettime(CLOCK_REALTIME, &ts); #else struct timeval tv; gettimeofday(&tv, NULL); TIMEVAL_TO_TIMESPEC(&tv, &ts); #endif ts.tv_sec += next_timeout / 1000; ts.tv_nsec += (next_timeout % 1000) * 1000000; pthread_cond_timedwait(&g_notify_cond, &g_notify_mutex, &ts); } g_notify_do = 0; pthread_mutex_unlock(&g_notify_mutex); do { sp_session_process_events(session, &next_timeout); } while (next_timeout == 0); //FIXME: If we are ever to call any blocking functions here, make sure we use a more granual lock! if(g_gazify.inbox.size() > 0) { pthread_mutex_lock(&g_gazify.inbox_mutex); for(std::list<boost::function<void()> >::iterator it = g_gazify.inbox.begin(); it != g_gazify.inbox.end();++it) { (*it)(); } g_gazify.inbox.clear(); pthread_mutex_unlock(&g_gazify.inbox_mutex); } pthread_mutex_lock(&g_notify_mutex); } }
PHP_METHOD(Spotify, initPlaylistContainer) { int timeout = 0; spotify_object *p = (spotify_object*)zend_object_store_get_object(getThis() TSRMLS_CC); if (p->playlistcontainer != NULL) { RETURN_TRUE; } sp_playlistcontainer *tempcontainer = sp_session_playlistcontainer(p->session); sp_playlistcontainer_add_callbacks(tempcontainer, &playlistcontainer_callbacks, p); while (p->playlistcontainer == NULL) { sp_session_process_events(p->session, &timeout); } RETURN_TRUE; }
/** * The main method, contains a select loop. * Checks for keyboard input, and if nothing is recevied, * processes the libspotfy events */ int main(void) { int select_ret; fd_set read_set; struct timeval tv; launch(); while(1) { FD_ZERO( &read_set ); FD_SET(STDIN_FILENO, &read_set); /* keyboard input */ tv.tv_sec = 0; tv.tv_usec = 1000000; select_ret = select ( 1, &read_set, 0, 0, &tv ); switch(select_ret) { case -1 : printf("error in select"); break; case 0 : /* nothing has happend on the keyboard, timeout expired? */ /* Process libspotify events */ notify_events = 0; pthread_mutex_unlock(¬ify_mutex); do { debug("do, while(next_timeout == 0)"); sp_session_process_events(g_session, &next_timeout); if( playlist_loading )check_playlist_status(g_playlist); } while (next_timeout == 0); pthread_mutex_lock(¬ify_mutex); if( playlist_loading )check_playlist_status(g_playlist); default : debug("default"); if( FD_ISSET (STDIN_FILENO, &read_set)) handle_keyboard(g_session, queue_entry); } } }
void QSpotifySession::processSpotifyEvents() { qDebug() << "QSpotifySession::processSpotifyEvents"; if (m_timerID) killTimer(m_timerID); int nextTimeout = 0; if (!m_aboutToQuit) QSpotifyCacheManager::instance().clean(); do { assert(isValid()); qDebug() << "Processing events..."; sp_session_process_events(m_sp_session, &nextTimeout); } while (nextTimeout == 0); m_timerID = startTimer(nextTimeout); }
PHP_METHOD(SpotifyAlbumIterator, current) { spotifyalbumiterator_object *p; sp_album *album; zval temp, *spotifyobject; int timeout; p = (spotifyalbumiterator_object*)zend_object_store_get_object(getThis() TSRMLS_CC); album = sp_artistbrowse_album(p->artistbrowse, p->position); while (!sp_album_is_loaded(album)) { sp_session_process_events(p->session, &timeout); } spotifyobject = GET_THIS_PROPERTY(spotifyalbumiterator_ce, "spotify"); object_init_ex(return_value, spotifyalbum_ce); SPOTIFY_METHOD2(SpotifyAlbum, __construct, &temp, return_value, spotifyobject, album); }