Advertisement
Guest User

SpotifyService

a guest
Jan 27th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <libspotify/api.h>
  3. #include <pthread.h>
  4. #include <assert.h>
  5.  
  6. #include <mach/clock.h>
  7. #include <mach/mach.h>
  8.  
  9. #define COMMAND_SP_LOGOUT 1
  10. #define COMMAND_SP_PRINTUSER 2
  11.  
  12. extern uint8_t g_appkey[];
  13. extern int g_appkey_size;
  14. void notifyMainThread(sp_session* session);
  15. void* spotifyLoop(void*);
  16. void loggedIn(sp_session* session, sp_error error);
  17. void loggedOut(sp_session* session);
  18. void logMessage(sp_session* session, const char* data);
  19.  
  20. static sp_session* spotifySession;
  21. static int notify_do = 0;
  22. static int command = 0;
  23. static int logged_out = 0;
  24.  
  25. static pthread_mutex_t notify_mutex;
  26. static pthread_cond_t notify_cond;
  27. static pthread_t spotifyThread;
  28.  
  29. static sp_session_callbacks sessionCallbacks = {
  30.     .notify_main_thread = &notifyMainThread,
  31.     .logged_in = &loggedIn,
  32.     .logged_out = &loggedOut,
  33.     .log_message = &logMessage,
  34. };
  35.  
  36. static sp_session_config sessionConfig = {
  37.     .api_version = SPOTIFY_API_VERSION,
  38.     .cache_location = "tmp",
  39.     .settings_location = "tmp",
  40.     .application_key = g_appkey,
  41.     .application_key_size = 0,
  42.     .user_agent = "nodejs-spotify-adapter",
  43.     .callbacks = &sessionCallbacks,
  44.     NULL,
  45. };
  46.  
  47.  
  48. void* spotifyLoop(void* nervNicht) {
  49.     sp_error error;
  50.     sp_session* session;
  51.  
  52.     int next_timeout = 0;
  53.     const char* username = "SET";
  54.     const char* password = "SET";
  55.  
  56.     sessionConfig.application_key_size = g_appkey_size;
  57.  
  58.     error = sp_session_create(&sessionConfig, &session);
  59.  
  60.     if(SP_ERROR_OK != error) {
  61.         fprintf(stderr, "Could not create Spotify session: %s\n", sp_error_message(error));
  62.     }
  63.    
  64.     spotifySession = session;
  65.  
  66.     pthread_mutex_init(&notify_mutex, NULL);
  67.     pthread_cond_init(&notify_cond, NULL);
  68.  
  69.     sp_session_login(session, username, password, 0, NULL);
  70.     pthread_mutex_lock(&notify_mutex);
  71.     int log_out_called = 0;
  72.     while(!logged_out) {
  73.         if(log_out_called) fprintf(stdout, "Loop while logout in process, command: %d, notify: %d, timeout: %d\n", command, notify_do, next_timeout);
  74.         if(next_timeout == 0) {
  75.             while(notify_do == 0) {
  76.                 pthread_cond_wait(&notify_cond, &notify_mutex);
  77.             }
  78.         } else {
  79.             struct timespec ts;
  80.                         clock_serv_t cclock;
  81.                         mach_timespec_t mts;
  82.                     host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
  83.                     clock_get_time(cclock, &mts);
  84.                     mach_port_deallocate(mach_task_self(), cclock);
  85.                     ts.tv_sec = mts.tv_sec + next_timeout / 1000;
  86.                     ts.tv_nsec = mts.tv_nsec + (next_timeout % 1000) * 1000;
  87.            
  88.             while(!notify_do && !command) {
  89.                 if(pthread_cond_timedwait(&notify_cond, &notify_mutex, &ts))
  90.                     break;
  91.             }
  92.         }
  93.  
  94.         if(log_out_called) fprintf(stdout, "2 Loop while logout in process, command: %d, notify: %d, timeout: %d\n", command, notify_do, next_timeout);
  95.         if(command) {
  96.             if(command == COMMAND_SP_LOGOUT) {
  97.                 sp_session_logout(spotifySession);
  98.                 log_out_called = 1;
  99.             } else if(command == COMMAND_SP_PRINTUSER) {
  100.                 sp_user* user = sp_session_user(spotifySession);
  101.                 const char* name = sp_user_display_name(user);
  102.                 fprintf(stdout, "User: %s\n", name);
  103.             }
  104.         }
  105.         command = 0;
  106.         notify_do = 0;
  107.         pthread_mutex_unlock(&notify_mutex);
  108.  
  109.         do {
  110.             if(log_out_called) fprintf(stdout, "3 Loop while logout in process, command: %d, notify: %d, timeout: %d\n", command, notify_do, next_timeout);
  111.             sp_session_process_events(session, &next_timeout);
  112.             if(log_out_called) fprintf(stdout, "4 Loop while logout in process, command: %d, notify: %d, timeout: %d\n", command, notify_do, next_timeout);
  113.         } while(next_timeout == 0);
  114.  
  115.         pthread_mutex_lock(&notify_mutex);
  116.     }
  117.     sp_session_release(session);
  118.     fprintf(stdout, "Exiting spotify main loop\n");
  119.     return 0;
  120. }
  121.  
  122. void notifyMainThread(sp_session* session) {
  123.     pthread_mutex_lock(&notify_mutex);
  124.     notify_do = 1;
  125.     pthread_cond_signal(&notify_cond);
  126.     pthread_mutex_unlock(&notify_mutex);
  127. }
  128.  
  129. void loggedIn(sp_session* session, sp_error error) {
  130.     if(SP_ERROR_OK != error) {
  131.         fprintf(stderr, "Error logging in: %s\n", sp_error_message(error));
  132.     } else {
  133.         fprintf(stdout, "Service is logged in!\n");
  134.     }
  135. }
  136.  
  137. void loggedOut(sp_session* session) {
  138.     fprintf(stdout, "Service is logged out\n");
  139.     logged_out = 1;
  140. }
  141.  
  142. void logMessage(sp_session* session, const char* data) {
  143.     fprintf(stdout, "%s\n", data);
  144. }
  145.  
  146. void printUser() {
  147.     pthread_mutex_lock(&notify_mutex);
  148.     command = COMMAND_SP_PRINTUSER;
  149.     pthread_cond_signal(&notify_cond);
  150.     pthread_mutex_unlock(&notify_mutex);
  151. }
  152.  
  153. void logout() {
  154.     pthread_t caller =  pthread_self();
  155.     fprintf(stdout, "Logout called by %u\n", (unsigned int)caller);
  156.     pthread_mutex_lock(&notify_mutex);
  157.     command = COMMAND_SP_LOGOUT;
  158.     pthread_cond_signal(&notify_cond);
  159.     pthread_mutex_unlock(&notify_mutex);
  160. }
  161.  
  162. void login() {
  163.     pthread_t caller =  pthread_self();
  164.     fprintf(stdout, "Login called by %u\n", (unsigned int)caller);
  165.     pthread_create(&spotifyThread, NULL, spotifyLoop, NULL);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement