我正试图为nodeJS编写一个模块,它封装了libspotify。其目标是编写一个webapp,允许远程控制从spotify播放音乐的设备。
我决定继续执行spshell示例,以确保线程安全,并在普通C中编写一个"Spotify服务“,它启动一个单独的线程,调用所有API函数。
然后,nodeJS模块只调用几个提供的函数来与spotify交互。服务的代码可以在这里找到:http://pastebin.com/KB6uwSC8,新线程在底部启动。
现在,如果我在这样一个简单的程序中调用它( fget只是有一个简单的登录方式来完成)。我使用c++来接近于node编译代码。
#include <stdio.h>
extern "C" {
#include "objects/SpotifyService.h"
}
int main(int argc, char** argv) {
login();
char string[100];
fgets(string, 100, stdin);
fprintf(stdout, "Got: %s", string);
logout();
fgets(string, 100, stdin);
fprintf(stdout, "Got: %s", string);
return 0;
}效果很好。我不能让这个崩溃。
如果我在nodeJS中使用相同的“服务”(意思是我只调用login()和logout()而不做其他操作),它有时会在注销时使崩溃,比如7-8/10次。我试过很多东西,包括:
都没有用。它只是崩溃了。当从gdb内部调用时,它的崩溃似乎较少,但这可能是随机的。
gdb的堆栈跟踪显示如下:
Thread 3 (Thread 0x7ffff65fd700 (LWP 21838)):
#0 0x00007ffff678f746 in ?? () from /usr/local/lib/libspotify.so.12
#1 0x00007ffff6702289 in ?? () from /usr/local/lib/libspotify.so.12
#2 0x00007ffff6702535 in ?? () from /usr/local/lib/libspotify.so.12
#3 0x00007ffff6703b5a in ?? () from /usr/local/lib/libspotify.so.12
#4 0x00007ffff6703c86 in ?? () from /usr/local/lib/libspotify.so.12
#5 0x00007ffff66c5c8b in ?? () from /usr/local/lib/libspotify.so.12
#6 0x00007ffff679a5b3 in sp_session_process_events () from /usr/local/lib/libspotify.so.12
#7 0x00007ffff6aa7839 in spotifyLoop (nervNicht=<value optimized out>) at ../src/SpotifyService.c:103
#8 0x00007ffff70118ca in start_thread () from /lib/libpthread.so.0
#9 0x00007ffff6d78b6d in clone () from /lib/libc.so.6
#10 0x0000000000000000 in ?? ()(在OSX中,libspotify中调用的函数称为“process_title”。)
由于到目前为止没有任何帮助,我只是不知道我是否能让它工作,或者它是否是libspotify中的什么东西,只是与nodeJS不兼容。我不明白node是如何链接.o文件的,可能出了什么问题?
我在github上找到了另外两个尝试这样做的项目,但其中一个项目将spotify主循环实际放在Javascript中,另一个项目使用节点0.1.100和libspotify 0.0.4,已经两年没有更新了。我不能从他们俩身上学到任何东西。
发布于 2013-02-03 14:29:24
好吧,我还玩了几次。我只是忽略了注销错误,并继续实现其他功能。
我在sp_playlist_container回调中添加了一个新的logged_in创建,这显然有所帮助。之后,节点模块不再崩溃(或者还没有崩溃)。
static sp_playlistcontainer_callbacks pc_callbacks = {
.container_loaded = &rootPlaylistContainerLoaded,
};
static void rootPlaylistContainerLoaded(sp_playlistcontainer* pc, void* userdata) {
int numPlaylists = sp_playlistcontainer_num_playlists(pc);
fprintf(stdout, "Root playlist synchronized, number of Playlists: %d\n", numPlaylists);
}
static void loggedIn(sp_session* session, sp_error error) {
if(SP_ERROR_OK != error) {
fprintf(stderr, "Error logging in: %s\n", sp_error_message(error));
} else {
fprintf(stdout, "Service is logged in!\n");
}
//This is absolutely necessary here, otherwise following callbacks can crash.
sp_playlistcontainer *pc = sp_session_playlistcontainer(spotifySession);
sp_playlistcontainer_add_callbacks(pc, &pc_callbacks, NULL);
} 但是sp_playlist_container创建必须在logged_in回调中,如果我在另一个函数(例如,"getPlaylistNames")中调用它,程序也会崩溃。
我会看看它是否继续有效,并希望这个答案能帮助其他人。
https://stackoverflow.com/questions/14633511
复制相似问题