/** * Callback from libspotify, telling us the rootlist is fully synchronized * * @param pc The playlist container handle * @param userdata The opaque pointer */ static void container_loaded ( sp_playlistcontainer *pc, void *userdata ) { char *folderName = malloc ( sizeof ( char ) * ( MAX_LENGTH_FOLDER_NAME ) ); int i; if ( folderName == NULL ) { fprintf ( stderr, "jahspotify: Could not allocate folder name variable)\n" ); return; } // fprintf ( stderr, "jahspotify: Rootlist synchronized (%d playlists)\n",sp_playlistcontainer_num_playlists ( pc ) ); signalSynchStarting(sp_playlistcontainer_num_playlists (pc)); for ( i = 0; i < sp_playlistcontainer_num_playlists ( pc ); ++i ) { sp_playlist *pl = sp_playlistcontainer_playlist ( pc, i ); sp_playlist_add_callbacks ( pl, &pl_callbacks, NULL ); sp_link *link = sp_link_create_from_playlist(pl); char *linkStr = malloc(sizeof(char) * 100); if (link) { sp_link_add_ref(link); sp_link_as_string(link,linkStr,100); } else { strcpy(linkStr,"N/A\0"); } switch ( sp_playlistcontainer_playlist_type ( pc,i ) ) { case SP_PLAYLIST_TYPE_PLAYLIST: signalPlaylistSeen(sp_playlist_name ( pl ),linkStr); break; case SP_PLAYLIST_TYPE_START_FOLDER: sp_playlistcontainer_playlist_folder_name ( pc,i,folderName, MAX_LENGTH_FOLDER_NAME); signalStartFolderSeen(folderName, sp_playlistcontainer_playlist_folder_id(pc,i)); break; case SP_PLAYLIST_TYPE_END_FOLDER: sp_playlistcontainer_playlist_folder_name ( pc,i,folderName,MAX_LENGTH_FOLDER_NAME); signalEndFolderSeen(); break; case SP_PLAYLIST_TYPE_PLACEHOLDER: fprintf ( stderr,"jahspotify: placeholder\n"); } if (link) sp_link_release(link); free(linkStr); } signalSynchCompleted(); free ( folderName ); }
jobject createJArtistInstance(JNIEnv *env, sp_artist *artist) { jclass jClass; jobject artistInstance = createInstance(env,"jahspotify/media/Artist"); sp_link *artistLink = sp_link_create_from_artist(artist); jClass = (*env)->FindClass(env, "jahspotify/media/Artist"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJArtistInstance: could not load jahnotify.media.Artist\n"); return NULL; } if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env, artistLink); setObjectObjectField(env,artistInstance,"id","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); setObjectStringField(env,artistInstance,"name",sp_artist_name(artist)); sp_artistbrowse *artistBrowse = sp_artistbrowse_create(g_sess,artist,artistBrowseCompleteCallback,NULL); if (artistBrowse) { sp_artistbrowse_add_ref(artistBrowse); while (!sp_artistbrowse_is_loaded(artistBrowse)) { usleep(100); } int numSimilarArtists = sp_artistbrowse_num_similar_artists(artistBrowse); if (numSimilarArtists > 0) { jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addSimilarArtist","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n"); return NULL; } // Load the artist links int count = 0; for (count = 0; count < numSimilarArtists; count++) { sp_artist *similarArtist = sp_artistbrowse_similar_artist(artistBrowse,0); if (similarArtist) { sp_artist_add_ref(similarArtist); sp_link *similarArtistLink = sp_link_create_from_artist(similarArtist); if (similarArtistLink) { sp_link_add_ref(similarArtistLink); jobject similarArtistJLink = createJLinkInstance(env,similarArtistLink); // set it on the track (*env)->CallVoidMethod(env,artistInstance,jMethod,similarArtistJLink); sp_link_release(similarArtistLink); } sp_artist_release(similarArtist); } } } int numPortraits = sp_artistbrowse_num_portraits(artistBrowse); if (numPortraits > 0) { // Load portrait url const byte *portraitURI = sp_artistbrowse_portrait(artistBrowse,0); if (portraitURI) { char *portraitURIStr = toHexString((byte*)portraitURI); const char spotifyURI[] = "spotify:image:"; int len = strlen(spotifyURI) + strlen(portraitURIStr); char *dest = malloc(len+1); dest[0] = 0; strcat(dest,spotifyURI); strcat(dest,portraitURIStr); sp_link *portraitLink = sp_link_create_from_string(dest); sp_image *portrait = sp_image_create_from_link(g_sess,portraitLink); if (portrait) { sp_image_add_ref(portrait); sp_image_add_load_callback(portrait,imageLoadedCallback,NULL); } free(dest); free(portraitURIStr); if (portraitLink) { sp_link_add_ref(portraitLink); jobject portraitJLlink = createJLinkInstance(env,portraitLink); setObjectObjectField(env,artistInstance,"portrait","Ljahspotify/media/Link;",portraitJLlink); sp_link_release(portraitLink); } } } // sp_artistbrowse_num_albums() // sp_artistbrowse_album() const char *bios = sp_artistbrowse_biography(artistBrowse); if (bios) { setObjectStringField(env,artistInstance,"bios",bios); } // sp_artistbrowse_num_tracks() // sp_artistbrowse_track() sp_artistbrowse_release(artistBrowse); } } return artistInstance; }
jobject createJAlbumInstance(JNIEnv *env, sp_album *album) { jclass albumJClass; jobject albumInstance; albumJClass = (*env)->FindClass(env, "jahspotify/media/Album"); if (albumJClass == NULL) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not load jahnotify.media.Album\n"); return NULL; } albumInstance = (*env)->AllocObject(env,albumJClass); if (!albumInstance) { fprintf(stderr,"jahspotify::createJAlbumInstance: could not create instance of jahspotify.media.Album\n"); return NULL; } sp_albumbrowse *albumBrowse = sp_albumbrowse_create(g_sess,album,albumBrowseCompleteCallback,NULL); if (albumBrowse) { sp_albumbrowse_add_ref(albumBrowse); int count = 0; while (!sp_albumbrowse_is_loaded(albumBrowse) && count < 5) { fprintf(stderr,"jahspotify::createJAlbumInstance: waiting for album browse load to complete\n"); sleep(1); count++; } if (count == 5) { sp_albumbrowse_release(albumBrowse); return NULL; } // By now it looks like the album will also be loaded sp_link *albumLink = sp_link_create_from_album(album); if (albumLink) { sp_link_add_ref(albumLink); jobject albumJLink = createJLinkInstance(env, albumLink); setObjectObjectField(env,albumInstance,"id","Ljahspotify/media/Link;",albumJLink); setObjectStringField(env,albumInstance,"name",sp_album_name(album)); setObjectIntField(env,albumInstance,"year",sp_album_year(album)); sp_albumtype albumType = sp_album_type(album); jclass albumTypeJClass = (*env)->FindClass(env, "jahspotify/media/AlbumType"); jmethodID jMethod = (*env)->GetStaticMethodID(env,albumTypeJClass,"fromOrdinal","(I)Ljahspotify/media/AlbumType;"); jobject albumTypeEnum = (jobjectArray)(*env)->CallStaticObjectMethod(env, albumTypeJClass, jMethod,(int)albumType); setObjectObjectField(env,albumInstance,"type","Ljahspotify/media/AlbumType;",albumTypeEnum); sp_link *albumCoverLink = sp_link_create_from_album_cover(album); if (albumCoverLink) { sp_link_add_ref(albumCoverLink); jobject albumCoverJLink = createJLinkInstance(env, albumCoverLink); setObjectObjectField(env,albumInstance,"cover","Ljahspotify/media/Link;",albumCoverJLink); sp_image *albumCoverImage = sp_image_create_from_link(g_sess,albumCoverLink); if (albumCoverImage) { sp_image_add_ref(albumCoverImage); sp_image_add_load_callback(albumCoverImage,imageLoadedCallback,NULL); } sp_link_release(albumCoverLink); } sp_artist *artist = sp_album_artist(album); if (artist) { sp_artist_add_ref(artist); sp_link *artistLink = sp_link_create_from_artist(artist); if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env,artistLink); setObjectObjectField(env,albumInstance,"artist","Ljahspotify/media/Link;",artistJLink); sp_link_release(artistLink); } sp_artist_release(artist); } sp_link_release(albumLink); } int numTracks = sp_albumbrowse_num_tracks(albumBrowse); if (numTracks > 0) { // Add each track to the album - also pass in the disk as need be jmethodID addTrackJMethodID = (*env)->GetMethodID(env,albumJClass,"addTrack","(ILjahspotify/media/Link;)V"); int i = 0; for (i = 0; i < numTracks; i++) { sp_track *track = sp_albumbrowse_track(albumBrowse,i); if (track) { sp_track_add_ref(track); sp_link *trackLink = sp_link_create_from_track(track,0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); (*env)->CallVoidMethod(env, albumInstance, addTrackJMethodID,sp_track_disc(track),trackJLink); sp_link_release(trackLink); } } } } int numCopyrights = sp_albumbrowse_num_copyrights(albumBrowse); if (numCopyrights > 0) { // Add copyrights to album jmethodID addCopyrightMethodID = (*env)->GetMethodID(env,albumJClass,"addCopyright","(Ljava/lang/String;)V"); int i = 0; for (i = 0; i < numCopyrights; i++) { const char *copyright = sp_albumbrowse_copyright(albumBrowse,i); if (copyright) { jstring str = (*env)->NewStringUTF(env, copyright); (*env)->CallVoidMethod(env, albumInstance, addCopyrightMethodID,str); } } } const char *review = sp_albumbrowse_review(albumBrowse); if (review) { setObjectStringField(env,albumInstance,"review",review); } sp_albumbrowse_release(albumBrowse); } return albumInstance; }
jobject createJTrackInstance(JNIEnv *env, sp_track *track) { jclass jClass; jobject trackInstance; jClass = (*env)->FindClass(env, "jahspotify/media/Track"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load jahnotify.media.Track\n"); return NULL; } trackInstance = (*env)->AllocObject(env,jClass); if (!trackInstance) { fprintf(stderr,"jahspotify::createJTrackInstance: could not create instance of jahspotify.media.Track\n"); return NULL; } sp_link *trackLink = sp_link_create_from_track(track,0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); setObjectObjectField(env,trackInstance,"id","Ljahspotify/media/Link;",trackJLink); setObjectStringField(env,trackInstance,"title",sp_track_name(track)); setObjectIntField(env,trackInstance,"length",sp_track_duration(track)); setObjectIntField(env,trackInstance,"popularity",sp_track_popularity(track)); setObjectIntField(env,trackInstance,"trackNumber",sp_track_index(track)); sp_album *album = sp_track_album(track); if (album) { sp_album_add_ref(album); sp_link *albumLink = sp_link_create_from_album(album); if (albumLink) { sp_link_add_ref(albumLink); jobject albumJLink = createJLinkInstance(env,albumLink); jmethodID jMethod = (*env)->GetMethodID(env,jClass,"setAlbum","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method setAlbum(link) on class Track\n"); return NULL; } // set it on the track (*env)->CallVoidMethod(env,trackInstance,jMethod,albumJLink); sp_link_release(albumLink); } int numArtists = sp_track_num_artists(track); if (numArtists > 0) { jmethodID jMethod = (*env)->GetMethodID(env,jClass,"addArtist","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJTrackInstance: could not load method addArtist(link) on class Track\n"); return NULL; } int i = 0; for (i = 0; i < numArtists; i++) { sp_artist *artist = sp_track_artist(track,i); if (artist) { sp_artist_add_ref(artist); sp_link *artistLink = sp_link_create_from_artist(artist); if (artistLink) { sp_link_add_ref(artistLink); jobject artistJLink = createJLinkInstance(env,artistLink); // set it on the track (*env)->CallVoidMethod(env,trackInstance,jMethod,artistJLink); sp_link_release(artistLink); } sp_artist_release(artist); } } } sp_album_release(album); } sp_link_release(trackLink); } return trackInstance; }
JNIEXPORT int JNICALL Java_jahspotify_impl_JahSpotifyImpl_readImage (JNIEnv *env, jobject obj, jstring uri, jobject outputStream) { uint8_t *nativeURI = ( uint8_t * ) ( *env )->GetStringUTFChars ( env, uri, NULL ); sp_link *imageLink = sp_link_create_from_string(nativeURI); size_t size; jclass jClass; int numBytesWritten = -1; if (imageLink) { sp_link_add_ref(imageLink); sp_image *image = sp_image_create_from_link(g_sess,imageLink); if (image) { sp_image_add_ref(image); sp_image_add_load_callback(image, imageLoadedCallback, NULL); int count = 0; while (!sp_image_is_loaded(image) && count < 5) { sleep(1); count++; } if (count == 5) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: Timeout waiting for image to load ...\n"); sp_image_release(image); sp_link_release(imageLink); return -1; } byte *data = (byte*)sp_image_data(image,&size); jClass = (*env)->FindClass(env,"Ljava/io/OutputStream;"); if (jClass == NULL) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: could not load class java.io.OutputStream\n"); return -1; } // Lookup the method now - saves us looking it up for each iteration of the loop jmethodID jMethod = (*env)->GetMethodID(env,jClass,"write","(I)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::Java_jahspotify_impl_JahSpotifyImpl_readImage: could not load method write(int) on class java.io.OutputStream\n"); return -1; } int i = 0; for (i = 0; i < size; i++) { (*env)->CallVoidMethod(env,outputStream,jMethod,*data); data++; numBytesWritten++; } sp_image_release(image); } sp_link_release(imageLink); } // Plus one because we start at -1 return numBytesWritten+1; }
jobject createJPlaylist(JNIEnv *env, sp_playlist *playlist) { jclass jClass; jobject playlistInstance; jmethodID jMethod; jClass = (*env)->FindClass(env, "jahspotify/media/Playlist"); if (jClass == NULL) { fprintf(stderr,"jahspotify::createJPlaylist: could not load jahnotify.media.Playlist\n"); return NULL; } playlistInstance = (*env)->AllocObject(env,jClass); if (!playlistInstance) { fprintf(stderr,"jahspotify::createJPlaylist: could not create instance of jahspotify.media.Playlistt\n"); return NULL; } sp_link *playlistLink = sp_link_create_from_playlist(playlist); if (playlistLink) { jobject playlistJLink = createJLinkInstance(env,playlistLink); setObjectObjectField(env,playlistInstance,"id","Ljahspotify/media/Link;",playlistJLink); sp_link_release(playlistLink); } setObjectStringField(env,playlistInstance,"name",sp_playlist_name(playlist)); sp_user *owner = sp_playlist_owner(playlist); if (owner) { setObjectStringField(env,playlistInstance,"author",sp_user_display_name(owner)); sp_user_release(owner); } // Lookup the method now - saves us looking it up for each iteration of the loop jMethod = (*env)->GetMethodID(env,jClass,"addTrack","(Ljahspotify/media/Link;)V"); if (jMethod == NULL) { fprintf(stderr,"jahspotify::createJPlaylist: could not load method addTrack(track) on class Playlist\n"); return NULL; } int numTracks = sp_playlist_num_tracks(playlist); int trackCounter = 0; for (trackCounter = 0 ; trackCounter < numTracks; trackCounter++) { sp_track *track = sp_playlist_track(playlist,trackCounter); if (track) { sp_track_add_ref(track); sp_link *trackLink = sp_link_create_from_track(track, 0); if (trackLink) { sp_link_add_ref(trackLink); jobject trackJLink = createJLinkInstance(env,trackLink); // Add it to the playlist (*env)->CallVoidMethod(env,playlistInstance,jMethod,trackJLink); sp_link_release(trackLink); } sp_track_release(track); } } return playlistInstance; }
/* only used to trigger sp_session_process_events when needed, * looks like about once a second */ static void* spotify_thread_func (void *data) { int timeout = -1; GTimeVal t; GstSpotSrc *spot = (GstSpotSrc *) data; if (!spotify_create_session (spot)) { GST_ERROR_OBJECT (spot, "Create_session error"); return FALSE; } while (spot->keep_spotify_thread) { sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); g_get_current_time (&t); g_time_val_add (&t, timeout * 1000); g_cond_timed_wait (spot->process_events_cond, spot->process_events_mutex, &t); spot->spotify_thread_initiated = TRUE; while (spot->spot_works) { struct spot_work *spot_work; sp_error ret = SP_ERROR_INVALID_INDATA; spot_work = (struct spot_work *)spot->spot_works->data; g_mutex_lock (spot_work->spot_mutex); switch (spot_work->cmd) { case SPOT_CMD_START: GST_DEBUG_OBJECT (spot, "Uri = %s", GST_SPOT_SRC_URI_LOCATION (spot)); if (!spotify_login (spot)) { /* error message from within function */ break; } sp_link *link = sp_link_create_from_string (GST_SPOT_SRC_URI_LOCATION (spot)); if (!link) { GST_ERROR_OBJECT (spot, "Incorrect track ID:%s", GST_SPOT_SRC_URI_LOCATION (spot)); break; } GST_SPOT_SRC_CURRENT_TRACK (spot) = sp_link_as_track (link); if (!GST_SPOT_SRC_CURRENT_TRACK (spot)) { GST_ERROR_OBJECT (spot, "Could get track from uri=%s", GST_SPOT_SRC_URI_LOCATION (spot)); break; } #if 0 /* FIXME: why does not this work? */ if (!sp_track_is_available (GST_SPOT_SRC_CURRENT_TRACK (spot))) { /* this probably happens for tracks avaiable in other countries or something */ GST_ERROR_OBJECT (spot, "Track is not available, uri=%s", GST_SPOT_SRC_URI_LOCATION (spot)); break; } #endif sp_track_add_ref (GST_SPOT_SRC_CURRENT_TRACK (spot)); sp_link_add_ref (link); sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); while (sp_track_is_loaded (GST_SPOT_SRC_CURRENT_TRACK (spot)) == 0) { sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); usleep (10000); } GST_DEBUG_OBJECT (spot, "Now playing \"%s\"", sp_track_name (GST_SPOT_SRC_CURRENT_TRACK (spot))); ret = sp_session_player_load (GST_SPOT_SRC_SPOTIFY_SESSION (spot), GST_SPOT_SRC_CURRENT_TRACK (spot)); if (ret != SP_ERROR_OK) { GST_ERROR_OBJECT (spot, "Failed to load track '%s' uri=%s", sp_track_name (GST_SPOT_SRC_CURRENT_TRACK (spot)), (GST_SPOT_SRC_URI_LOCATION (spot))); break; } sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); ret = sp_session_player_play (GST_SPOT_SRC_SPOTIFY_SESSION (spot), TRUE); if (ret != SP_ERROR_OK) { GST_ERROR_OBJECT (spot, "Failed to play track '%s' uri=%s", sp_track_name (GST_SPOT_SRC_CURRENT_TRACK (spot)), (GST_SPOT_SRC_URI_LOCATION (spot))); break; } break; case SPOT_CMD_PROCESS: sp_session_process_events (GST_SPOT_SRC_SPOTIFY_SESSION (spot), &timeout); break; case SPOT_CMD_PLAY: ret = sp_session_player_play (GST_SPOT_SRC_SPOTIFY_SESSION (spot), TRUE); break; case SPOT_CMD_DURATION: if (GST_SPOT_SRC_CURRENT_TRACK (spot)) { ret = sp_track_duration (GST_SPOT_SRC_CURRENT_TRACK (spot)); } break; case SPOT_CMD_STOP: if (GST_SPOT_SRC_CURRENT_TRACK (spot)) { ret = sp_session_player_play (GST_SPOT_SRC_SPOTIFY_SESSION (spot), FALSE); if (ret != SP_ERROR_OK) { break; } ret = SP_ERROR_OK; sp_session_player_unload (GST_SPOT_SRC_SPOTIFY_SESSION (spot)); } break; case SPOT_CMD_SEEK: if (GST_SPOT_SRC_CURRENT_TRACK (spot)) { ret = sp_session_player_seek (GST_SPOT_SRC_SPOTIFY_SESSION (spot), spot_work->opt); } break; default: g_assert_not_reached (); break; } /* print all errors caught and propagate to calling thread */ if (ret != SP_ERROR_OK) { GST_ERROR_OBJECT (spot, "Failed with SPOT_CMD=%d, ret=%d, error=%s", spot_work->cmd, ret, sp_error_message (ret)); } spot_work->ret = ret; spot->spot_works = g_list_remove (spot->spot_works, spot->spot_works->data); g_mutex_unlock (spot_work->spot_mutex); g_cond_broadcast (spot_work->spot_cond); } } return NULL; }