Skip to content

Instantly share code, notes, and snippets.

@ivankelly
Last active December 17, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivankelly/5564318 to your computer and use it in GitHub Desktop.
Save ivankelly/5564318 to your computer and use it in GitHub Desktop.
Get a segfault with spotify
#include <stdio.h>
#include <libspotify/api.h>
#include <glib.h>
#include "tmpappkey.c"
sp_track* current_track = NULL;
int i = 0;
int test_spotify_music_delivery(sp_session *session, const sp_audioformat *format,
const void *frames, int num_frames) {
//printf("music delivery %d\n", i++);
return num_frames;
}
int eot = 0;
int logged_out = 0;
void test_spotify_end_of_track(sp_session *session) {
printf("EOT\n");
eot = 1;
}
void test_spotify_logged_in(sp_session *session, sp_error error) {
if (error == SP_ERROR_OK) {
printf("logged in\n");
sp_link *link = sp_link_create_from_string("spotify:track:2QVHl9l3MnSHTr7l5p8gNK");
sp_track_add_ref(current_track = sp_link_as_track(link));
sp_link_release(link);
} else {
printf("error logging in %d\n", error);
}
}
void test_spotify_metadata_updated(sp_session *session) {
printf("metadata updated\n");
sp_error err = sp_session_player_load(session, current_track);
err = sp_session_player_play(session, TRUE);
}
void test_spotify_connection_error(sp_session *session, sp_error error) {
printf("connection error\n");
}
void test_spotify_message_to_user(sp_session *session, const char *message) {
printf("UserMessage: %s\n", message);
}
void test_spotify_connectionstate_updated(sp_session *session) {
printf("Connection state update\n");
}
GCond main_cond;
GMutex main_mutex;
int do_something = 0;
void test_spotify_logged_out(sp_session *session) {
printf("logged out\n");
logged_out = 1;
g_mutex_lock(&main_mutex);
do_something = 1;
g_cond_signal(&main_cond);
g_mutex_unlock(&main_mutex);
}
void test_spotify_notify_main_thread(sp_session *session) {
g_mutex_lock(&main_mutex);
do_something = 1;
g_cond_signal(&main_cond);
g_mutex_unlock(&main_mutex);
}
sp_session_callbacks test_spotify_session_callbacks = {
.logged_in = &test_spotify_logged_in,
.logged_out = &test_spotify_logged_out,
.connection_error = &test_spotify_connection_error,
.message_to_user = &test_spotify_message_to_user,
.connectionstate_updated = &test_spotify_connectionstate_updated,
.notify_main_thread = &test_spotify_notify_main_thread,
.music_delivery = &test_spotify_music_delivery,
.metadata_updated = &test_spotify_metadata_updated,
.play_token_lost = NULL,
.log_message = NULL,
.end_of_track = &test_spotify_end_of_track,
};
int main(int argc, char** argv) {
sp_session_config sp_config;
sp_session *session;
char* username = argv[1];
char* password = argv[2];
memset(&sp_config, 0, sizeof(sp_config));
sp_config.api_version = SPOTIFY_API_VERSION;
sp_config.cache_location = "tmp";
sp_config.settings_location = "tmp";
sp_config.user_agent = "test_spotify";
sp_config.application_key = &g_appkey;
sp_config.application_key_size = g_appkey_size;
sp_config.callbacks = &test_spotify_session_callbacks;
sp_config.userdata = NULL;
g_mutex_init(&main_mutex);
g_cond_init(&main_cond);
sp_error error;
int next_timeout = 0;
int done_log_out = 0;
sp_error err = sp_session_create(&sp_config, &session);
if (err != SP_ERROR_OK) {
printf("Error initializing session %s", sp_error_message(err));
exit(-1);
}
err = sp_session_login(session, username, password, 0, NULL);
do_something = 1;
eot = 0;
logged_out = 0;
int logged_out_called = 0;
while (logged_out != 1) {
if (do_something == 0) {
if (next_timeout == 0) {
while (do_something == 0) {
g_cond_wait(&main_cond, &main_mutex);
}
} else {
GTimeVal time;
g_get_current_time(&time);
g_time_val_add(&time, 1000*next_timeout);
g_cond_timed_wait(&main_cond, &main_mutex, &time);
}
}
g_mutex_unlock(&main_mutex);
do_something = 0;
next_timeout = 0;
printf("process\n");
error = sp_session_process_events(session, &next_timeout);
if (eot == 1 && logged_out_called == 0) {
logged_out_called = 1;
printf("logging out\n");
sp_session_player_unload(session);
sp_session_logout(session);
}
g_mutex_lock(&main_mutex);
}
printf("releasing\n");
sp_session_release(session);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment