sp_session_process_events在调用sp_session_login

时间:2016-04-05 01:37:58

标签: c++ windows spotify libspotify

这是一个非常奇怪的问题,我一直试图弄清楚几天。奇怪的是它只对某些用户帐户执行此操作。这是一个Windows应用程序,我使用Jukebox示例作为起点。

  • 我打电话给sp_session_login()。
  • 我收到了logged_in()的回调。
  • 我看到了notify_main_thread回调。
  • sp_session_process_events()处理许多事件,然后处理访问冲突。

我已经多次追踪并且在导致崩溃的帐户上我得到了userinfo_updated()回调大约8次。没有崩溃的帐户我只看到一次或两次。我在回调中没有做任何事情。我试图看看它是否需要一个我没有提供的回调。

有一种方法可以防止它崩溃。如果我立即加载曲目并在我登录的Login_in()中播放它(就像在Jukebox示例中那样)它不会崩溃。

任何帮助都会在调试这个帮助时非常感激。

-tommy

更新:2016年4月7日 这是日志文件输出。希望有人可以帮助我理解这一点。另外,我认为它与播放列表有关。我确实设置了播放列表回调。

14:57:47.735 sp_session_login登录用户tgsource

14:57:47.788 sp_session_login会话返回:SP_ERROR_OK

14:57:47.859 log_message 21:57:47.859我[user_cache:135] UserCache :: initiateGetUsers()将查询1个用户

14:57:47.906 log_message 21:57:47.906我[ap:1752]连接到AP ap.gslb.spotify.com:4070

14:57:47.937 logged_in logged_in用户是:tgsource

14:57:47.937 log_message 21:57:47.937我[offline-mgr:2084]存储已被清理

14:57:47.984 log_message 21:57:47.984我[ap:1226]连接到AP:194.68.29.165:4070

14:57:48.405 credentials_blob_updated credentials_blob_updated

14:57:48.405 userinfo_updated名为

的Userinfo

14:57:48.452 userinfo_updated名为

的Userinfo

14:57:48.452 userinfo_updated名为

的Userinfo

14:57:48.920 PlaylistContainerLoaded播放列表数量:8

14:57:49.107 log_message 21:57:49.107 E [ap:4172] ChannelError(3,1,播放列表)

14:57:49.139 log_message 21:57:49.139 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.139 log_message 21:57:49.139 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.154 log_message 21:57:49.154 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.154 log_message 21:57:49.154 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.295 log_message 21:57:49.295我[user_cache:135] UserCache :: initiateGetUsers()将查询1个用户

14:57:49.295 log_message 21:57:49.295 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.295 log_message 21:57:49.295 W [core / playlist / playlist.h:45]在更新时添加观察者

14:57:49.327 userinfo_updated名为

的Userinfo

14:57:49.467 userinfo_updated名为

的Userinfo

**崩溃**

2016年4月11日更新: 以下是重现问题的源代码。它是Jukebox示例中playtrack.c文件的一个端口。我删除了轨道加载,它导致了同样的问题。

    /**
 * Copyright (c) 2006-2010 Spotify Ltd
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *
 * This example application is the most minimal way to just play a spotify URI.
 *
 * This file is part of the libspotify examples suite. Jukebox - playtrack.c
 */
#include <errno.h>
#include "stdint.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../libspotify/api.h"
#include "audio.h"
#include <windows.h>

/* --- Data --- */
extern const uint8_t g_appkey[];
extern const size_t g_appkey_size;
static audio_fifo_t g_audiofifo;
static HANDLE g_notify_mutex;
static HANDLE g_notify_cond;
static int g_notify_do;
int    g_playback_done;
sp_session *g_sess;
static sp_track *g_currenttrack;
const  char *g_trackurl; 
static bool is_logged_out = FALSE;
static HANDLE events;



/* ---------------------------  PLAYLIST CALLBACKS  ------------------------- */
/**
 * Playlist container callbacks.
 * If some callbacks should not be of interest, set them to NULL.
 *
 */
static void SP_CALLCONV PlaylistContainerLoaded(sp_playlistcontainer* pc, void* userdata) 
{
    int numPlaylists = sp_playlistcontainer_num_playlists(pc);
    printf("Number of playlists: %d", numPlaylists);                
}

static sp_playlistcontainer_callbacks pc_callbacks = 
{
    NULL,
    NULL,
    NULL,
    &PlaylistContainerLoaded
};

/* ---------------------------  SESSION CALLBACKS  ------------------------- */
/**
 * This callback is called when an attempt to login has succeeded or failed.
 *
 * @sa sp_session_callbacks#logged_in
 */
static void SP_CALLCONV logged_in(sp_session *sess, sp_error error)
{
    sp_link *link;

    if (SP_ERROR_OK != error) {
        fprintf(stderr, "Login failed: %s\n",
            sp_error_message(error));
        exit(2);
    }

    sp_playlistcontainer *pc = sp_session_playlistcontainer(sess);
    sp_playlistcontainer_add_callbacks(pc, &pc_callbacks, NULL);


// Note:  Removing the track start will cause this to crash 
#if 0

    printf("Loading track\n");


    link = sp_link_create_from_string("spotify:track:0W4Kpfp1w2xkY3PrV714B7");

    sp_track_add_ref(g_currenttrack = sp_link_as_track(link));
    sp_link_release(link);

    if (sp_track_error(g_currenttrack) == SP_ERROR_OK) {
        printf("Now playing \"%s\"...\n", sp_track_name(g_currenttrack));


        printf("Duration: %d\n", sp_track_duration(g_currenttrack));
        fflush(stdout);

        sp_session_player_load(g_sess, g_currenttrack);
        sp_session_player_play(g_sess, 1);
        }
#endif

    /* Track not loaded? Then we need to wait for the metadata to
           load before we can start playback (see metadata_updated below) */
}

/**
 * Callback called when libspotify has new metadata available
 *
 * @sa sp_session_callbacks#metadata_updated
 */
static void SP_CALLCONV metadata_updated(sp_session *sess)
{
    puts("Metadata updated, trying to start playback");

    if (sp_track_error(g_currenttrack) != SP_ERROR_OK)
        return;

    sp_session_player_load(g_sess, g_currenttrack);
    sp_session_player_play(g_sess, 1);
}

/**
 * This callback is called from an internal libspotify thread to ask
 * us to reiterate the main loop.
 *
 * We notify the main thread using a condition variable and a protected variable.
 *
 * @sa sp_session_callbacks#notify_main_thread
 */
static void SP_CALLCONV notify_main_thread(sp_session *sess)
{
    g_notify_do = 1;
    SetEvent(events);
}

/**
 * This callback is used from libspotify whenever there is PCM data available.
 *
 * @sa sp_session_callbacks#music_delivery
 */
static int SP_CALLCONV music_delivery(sp_session *sess, const sp_audioformat *format,
                          const void *frames, int num_frames)
{
    audio_fifo_t *af = &g_audiofifo;
    audio_fifo_data_t *afd;
    size_t s;

    if (num_frames == 0)
        return 0; // Audio discontinuity, do nothing

    WaitForSingleObject(af->mutex, INFINITE);

    /* Buffer one second of audio */
    if (af->qlen > format->sample_rate) {
        ReleaseMutex(af->mutex);

        return 0;
    }

    s = num_frames * sizeof(int16_t) * format->channels;

    afd = malloc(sizeof(audio_fifo_data_t) + s);
    memcpy(afd->samples, frames, s);

    afd->nsamples = num_frames;

    afd->rate = format->sample_rate;
    afd->channels = format->channels;

    TAILQ_INSERT_TAIL(&af->q, afd, link);
    af->qlen += num_frames;

    PulseEvent(af->cond);   
    ReleaseMutex(af->mutex);

    return num_frames;
}


/**
 * This callback is used from libspotify when the current track has ended
 *
 * @sa sp_session_callbacks#end_of_track
 */
static void SP_CALLCONV end_of_track(sp_session *sess)
{
    printf("end_of_track\n");
    g_playback_done = 1;
}

/**
 * Notification that some other connection has started playing on this account.
 * Playback has been stopped.
 *
 * @sa sp_session_callbacks#play_token_lost
 */
static void SP_CALLCONV play_token_lost(sp_session *sess)
{
    printf("play_token_lost\n");
    audio_fifo_flush(&g_audiofifo);

    if (g_currenttrack != NULL) {
        sp_session_player_unload(g_sess);
        g_currenttrack = NULL;
    }
}

static void SP_CALLCONV log_message(sp_session *session, const char *msg)
{
    puts(msg);
}

static void SP_CALLCONV userinfo_updated(sp_session *session)
{
    printf("Userinfo called.\n");

}

static void SP_CALLCONV offline_status_updated(sp_session *sess)
{
    sp_offline_sync_status status;
    sp_offline_sync_get_status(sess, &status);
    if(status.syncing) {
        printf("Offline status: queued:%d:%zd done:%d:%zd copied:%d:%zd nocopy:%d err:%d\n",
            status.queued_tracks,
            (size_t)status.queued_bytes,
            status.done_tracks,
            (size_t)status.done_bytes,
            status.copied_tracks,
            (size_t)status.copied_bytes,
            status.willnotcopy_tracks,
            status.error_tracks);
    } else {
        printf("Offline status: Idle\n");
    }
}

/**
 * The session callbacks
 */
static sp_session_callbacks session_callbacks = {
    &logged_in,
    NULL,
    &metadata_updated,
    NULL,
    NULL,
    &notify_main_thread,
    &music_delivery,
    &play_token_lost,
    &log_message,
    &end_of_track,
    NULL,
    &userinfo_updated,
    NULL,
    NULL,
    NULL,
    &offline_status_updated,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
};


/**
 * The session configuration. Note that application_key_size is an
 * external, so we set it in main() instead.
 */
static sp_session_config spconfig = {
    SPOTIFY_API_VERSION,
    "tmp",
    "tmp",
    g_appkey,
    0, // Set in main()
    "spotify-jukebox-playtrack",
    &session_callbacks,
    NULL,
};
/* -------------------------  END SESSION CALLBACKS  ----------------------- */


/**
 * A track has ended. Remove it from the playlist.
 *
 * Called from the main loop when the music_delivery() callback has set g_playback_done.
 */
static void SP_CALLCONV track_ended(void)
{
    printf("track_ended\n");

    if (g_currenttrack) {
        sp_track_release(g_currenttrack);
        g_currenttrack = NULL;
        sp_session_player_unload(g_sess);
        exit(0);
    }
}

/**
 * Show usage information
 *
 * @param  progname  The program name
 */
static void usage(const char *progname)
{
    fprintf(stderr, "usage: %s <username> <password> <trackurl>\n", progname);
}


/*************************************************
 *  app main
 *
 *************************************************/
int main(int argc, char **argv)
{
    sp_session *sp;
    sp_error err;
    int next_timeout = 0;
    DWORD ev;

    const char* username_default = "username";
    const char* password_default = "login";

    const char *username = argc > 1 ? argv[1] : NULL;
    const char *password = argc > 2 ? argv[2] : NULL;
    g_trackurl = argc > 3 ? argv[3] : NULL;


    if (!username || !password) {
        username = username_default;
        password = password_default;
    }

    events = CreateEvent(NULL, FALSE, FALSE, NULL);
    audio_init(&g_audiofifo);

    /* Create session */
    spconfig.application_key_size = g_appkey_size;


    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;

//  Q: Why a mutex and cond here???

//  pthread_mutex_init(&g_notify_mutex, NULL);
//  pthread_cond_init(&g_notify_cond, NULL);

    err = sp_session_login(sp, username, password, 0, NULL);
//  pthread_mutex_lock(&g_notify_mutex);

    if (SP_ERROR_OK != err) {
        fprintf(stderr, "Unable to create session: %s\n",
            sp_error_message(err));
        exit(1);
    }

    while(!is_logged_out) 
    {
        ev = WaitForSingleObject( events, next_timeout > 0 ? next_timeout : INFINITE);

        switch (ev) 
        {
            case WAIT_OBJECT_0:
            case WAIT_TIMEOUT:
                do {
                    sp_session_process_events(g_sess, &next_timeout);
                } while (next_timeout == 0);
                break;
        }

        if( g_playback_done )
        {
            track_ended();
            break;
        }
    }

    audio_free(&g_audiofifo);

    printf("Logged out\n");
    sp_session_release(g_sess);
    printf("Exiting...\n");

    return 0;
}

1 个答案:

答案 0 :(得分:0)

这个问题仍然存在,但我已经成功解决了这个问题。

该问题与播放列表有关,但我还没弄清楚在哪里或如何。我实现了sp_playlistcontainer_callbacks以及sp_playlist_callbacks。

我一直得到 log_message 21:57:49.107 E [ap:4172] ChannelError(3,1,播放列表)但不知道该错误意味着什么。

我考虑的另一件事是,由于混合了libspotify c lib和我的C ++应用程序,编译和链接时可能存在一些字节对齐或打包问题。我尝试了许多不同的设置。我还必须在发布版本中关闭优化以使其运行。

我能找到的最好的工作是将sp_session_config设置为

sp_session_config.dont_save_metadata_for_playlists = TRUE sp_session_config.initially_unload_playlists = TRUE

这解决了现在的问题,但我觉得它只是隐藏了一个更深层次的问题。

由于不再支持 libspotify lib ,因此有办法获取源代码进行调试。我尝试为tracefile输出设置sp_session_config.tracefile参数,但它似乎没有做任何事情。