| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
#if defined( WIN32 ) |
|---|
| 34 |
#include <time.h> |
|---|
| 35 |
#endif |
|---|
| 36 |
|
|---|
| 37 |
#ifdef HAVE_CONFIG_H |
|---|
| 38 |
# include "config.h" |
|---|
| 39 |
#endif |
|---|
| 40 |
|
|---|
| 41 |
#include <vlc_common.h> |
|---|
| 42 |
#include <vlc_plugin.h> |
|---|
| 43 |
#include <vlc_interface.h> |
|---|
| 44 |
#include <vlc_meta.h> |
|---|
| 45 |
#include <vlc_md5.h> |
|---|
| 46 |
#include <vlc_block.h> |
|---|
| 47 |
#include <vlc_stream.h> |
|---|
| 48 |
#include <vlc_url.h> |
|---|
| 49 |
#include <vlc_network.h> |
|---|
| 50 |
#include <vlc_interface.h> |
|---|
| 51 |
#include <vlc_playlist.h> |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
#define QUEUE_MAX 50 |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
typedef struct audioscrobbler_song_t |
|---|
| 61 |
{ |
|---|
| 62 |
char *psz_a; |
|---|
| 63 |
char *psz_t; |
|---|
| 64 |
char *psz_b; |
|---|
| 65 |
char *psz_n; |
|---|
| 66 |
int i_l; |
|---|
| 67 |
char *psz_m; |
|---|
| 68 |
time_t date; |
|---|
| 69 |
mtime_t i_start; |
|---|
| 70 |
} audioscrobbler_song_t; |
|---|
| 71 |
|
|---|
| 72 |
struct intf_sys_t |
|---|
| 73 |
{ |
|---|
| 74 |
audioscrobbler_song_t p_queue[QUEUE_MAX]; |
|---|
| 75 |
int i_songs; |
|---|
| 76 |
|
|---|
| 77 |
vlc_mutex_t lock; |
|---|
| 78 |
vlc_cond_t wait; |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
mtime_t next_exchange; |
|---|
| 82 |
unsigned int i_interval; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
char *psz_submit_host; |
|---|
| 86 |
int i_submit_port; |
|---|
| 87 |
char *psz_submit_file; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
#if 0 //NOT USED |
|---|
| 91 |
char *psz_nowp_host; |
|---|
| 92 |
int i_nowp_port; |
|---|
| 93 |
char *psz_nowp_file; |
|---|
| 94 |
#endif |
|---|
| 95 |
bool b_handshaked; |
|---|
| 96 |
char psz_auth_token[33]; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
audioscrobbler_song_t p_current_song; |
|---|
| 100 |
|
|---|
| 101 |
mtime_t time_pause; |
|---|
| 102 |
mtime_t time_total_pauses; |
|---|
| 103 |
|
|---|
| 104 |
bool b_submit; |
|---|
| 105 |
|
|---|
| 106 |
bool b_state_cb; |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
bool b_meta_read; |
|---|
| 110 |
|
|---|
| 111 |
}; |
|---|
| 112 |
|
|---|
| 113 |
static int Open ( vlc_object_t * ); |
|---|
| 114 |
static void Close ( vlc_object_t * ); |
|---|
| 115 |
static void Run ( intf_thread_t * ); |
|---|
| 116 |
|
|---|
| 117 |
static int ItemChange ( vlc_object_t *, const char *, vlc_value_t, |
|---|
| 118 |
vlc_value_t, void * ); |
|---|
| 119 |
static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t, |
|---|
| 120 |
vlc_value_t, void * ); |
|---|
| 121 |
|
|---|
| 122 |
static void AddToQueue ( intf_thread_t * ); |
|---|
| 123 |
static int Handshake ( intf_thread_t * ); |
|---|
| 124 |
static int ReadMetaData ( intf_thread_t * ); |
|---|
| 125 |
static void DeleteSong ( audioscrobbler_song_t* ); |
|---|
| 126 |
static int ParseURL ( char *, char **, char **, int * ); |
|---|
| 127 |
static void HandleInterval ( mtime_t *, unsigned int * ); |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
#define USERNAME_TEXT N_("Username") |
|---|
| 134 |
#define USERNAME_LONGTEXT N_("The username of your last.fm account") |
|---|
| 135 |
#define PASSWORD_TEXT N_("Password") |
|---|
| 136 |
#define PASSWORD_LONGTEXT N_("The password of your last.fm account") |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
#define VLC_AUDIOSCROBBLER_EFATAL -69 |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
#define CLIENT_NAME PACKAGE |
|---|
| 143 |
#define CLIENT_VERSION VERSION |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
#define POST_REQUEST "POST /%s HTTP/1.1\n" \ |
|---|
| 147 |
"Accept-Encoding: identity\n" \ |
|---|
| 148 |
"Content-length: %u\n" \ |
|---|
| 149 |
"Connection: close\n" \ |
|---|
| 150 |
"Content-type: application/x-www-form-urlencoded\n" \ |
|---|
| 151 |
"Host: %s\n" \ |
|---|
| 152 |
"User-agent: VLC Media Player/%s\r\n" \ |
|---|
| 153 |
"\r\n" \ |
|---|
| 154 |
"%s\r\n" \ |
|---|
| 155 |
"\r\n" |
|---|
| 156 |
|
|---|
| 157 |
vlc_module_begin(); |
|---|
| 158 |
set_category( CAT_INTERFACE ); |
|---|
| 159 |
set_subcategory( SUBCAT_INTERFACE_CONTROL ); |
|---|
| 160 |
set_shortname( N_( "Audioscrobbler" ) ); |
|---|
| 161 |
set_description( N_("Submission of played songs to last.fm") ); |
|---|
| 162 |
add_string( "lastfm-username", "", NULL, |
|---|
| 163 |
USERNAME_TEXT, USERNAME_LONGTEXT, false ); |
|---|
| 164 |
add_password( "lastfm-password", "", NULL, |
|---|
| 165 |
PASSWORD_TEXT, PASSWORD_LONGTEXT, false ); |
|---|
| 166 |
set_capability( "interface", 0 ); |
|---|
| 167 |
set_callbacks( Open, Close ); |
|---|
| 168 |
vlc_module_end(); |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
static int Open( vlc_object_t *p_this ) |
|---|
| 174 |
{ |
|---|
| 175 |
playlist_t *p_playlist; |
|---|
| 176 |
intf_thread_t *p_intf = ( intf_thread_t* ) p_this; |
|---|
| 177 |
intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) ); |
|---|
| 178 |
|
|---|
| 179 |
if( !p_sys ) |
|---|
| 180 |
return VLC_ENOMEM; |
|---|
| 181 |
|
|---|
| 182 |
p_intf->p_sys = p_sys; |
|---|
| 183 |
|
|---|
| 184 |
vlc_mutex_init( &p_sys->lock ); |
|---|
| 185 |
vlc_cond_init( &p_sys->wait ); |
|---|
| 186 |
|
|---|
| 187 |
p_playlist = pl_Hold( p_intf ); |
|---|
| 188 |
PL_LOCK; |
|---|
| 189 |
var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf ); |
|---|
| 190 |
PL_UNLOCK; |
|---|
| 191 |
pl_Release( p_intf ); |
|---|
| 192 |
|
|---|
| 193 |
p_intf->pf_run = Run; |
|---|
| 194 |
|
|---|
| 195 |
return VLC_SUCCESS; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
static void Close( vlc_object_t *p_this ) |
|---|
| 202 |
{ |
|---|
| 203 |
playlist_t *p_playlist; |
|---|
| 204 |
input_thread_t *p_input; |
|---|
| 205 |
intf_thread_t *p_intf = ( intf_thread_t* ) p_this; |
|---|
| 206 |
intf_sys_t *p_sys = p_intf->p_sys; |
|---|
| 207 |
|
|---|
| 208 |
p_playlist = pl_Hold( p_intf ); |
|---|
| 209 |
if( p_playlist ) |
|---|
| 210 |
{ |
|---|
| 211 |
|
|---|
| 212 |
var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf ); |
|---|
| 213 |
|
|---|
| 214 |
p_input = playlist_CurrentInput( p_playlist ); |
|---|
| 215 |
if ( p_input ) |
|---|
| 216 |
{ |
|---|
| 217 |
if( p_sys->b_state_cb ) |
|---|
| 218 |
var_DelCallback( p_input, "state", PlayingChange, p_intf ); |
|---|
| 219 |
|
|---|
| 220 |
vlc_object_release( p_input ); |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
pl_Release( p_intf ); |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
int i; |
|---|
| 227 |
for( i = 0; i < p_sys->i_songs; i++ ) |
|---|
| 228 |
DeleteSong( &p_sys->p_queue[i] ); |
|---|
| 229 |
free( p_sys->psz_submit_host ); |
|---|
| 230 |
free( p_sys->psz_submit_file ); |
|---|
| 231 |
#if 0 //NOT USED |
|---|
| 232 |
free( p_sys->psz_nowp_host ); |
|---|
| 233 |
free( p_sys->psz_nowp_file ); |
|---|
| 234 |
#endif |
|---|
| 235 |
vlc_cond_destroy( &p_sys->wait ); |
|---|
| 236 |
vlc_mutex_destroy( &p_sys->lock ); |
|---|
| 237 |
free( p_sys ); |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
static void Run( intf_thread_t *p_intf ) |
|---|
| 245 |
{ |
|---|
| 246 |
char *psz_submit, *psz_submit_song, *psz_submit_tmp; |
|---|
| 247 |
int i_net_ret; |
|---|
| 248 |
int i_song; |
|---|
| 249 |
uint8_t p_buffer[1024]; |
|---|
| 250 |
char *p_buffer_pos; |
|---|
| 251 |
int i_post_socket; |
|---|
| 252 |
int canc = vlc_savecancel(); |
|---|
| 253 |
|
|---|
| 254 |
intf_sys_t *p_sys = p_intf->p_sys; |
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
for( ;; ) |
|---|
| 258 |
{ |
|---|
| 259 |
bool b_wait = false; |
|---|
| 260 |
|
|---|
| 261 |
vlc_restorecancel( canc ); |
|---|
| 262 |
vlc_mutex_lock( &p_sys->lock ); |
|---|
| 263 |
mutex_cleanup_push( &p_sys->lock ); |
|---|
| 264 |
|
|---|
| 265 |
if( mdate() < p_sys->next_exchange ) |
|---|
| 266 |
|
|---|
| 267 |
b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock, |
|---|
| 268 |
p_sys->next_exchange ) == 0; |
|---|
| 269 |
else |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
vlc_cond_wait( &p_sys->wait, &p_sys->lock ); |
|---|
| 273 |
vlc_cleanup_run(); |
|---|
| 274 |
canc = vlc_savecancel(); |
|---|
| 275 |
|
|---|
| 276 |
if( b_wait ) |
|---|
| 277 |
continue; |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
if( p_sys->b_handshaked == false ) |
|---|
| 281 |
{ |
|---|
| 282 |
msg_Dbg( p_intf, "Handshaking with last.fm ..." ); |
|---|
| 283 |
|
|---|
| 284 |
switch( Handshake( p_intf ) ) |
|---|
| 285 |
{ |
|---|
| 286 |
case VLC_ENOMEM: |
|---|
| 287 |
return; |
|---|
| 288 |
|
|---|
| 289 |
case VLC_ENOVAR: |
|---|
| 290 |
|
|---|
| 291 |
intf_UserFatal( p_intf, false, |
|---|
| 292 |
_("Last.fm username not set"), |
|---|
| 293 |
_("Please set a username or disable the " |
|---|
| 294 |
"audioscrobbler plugin, and restart VLC.\n" |
|---|
| 295 |
"Visit http://www.last.fm/join/ to get an account.") |
|---|
| 296 |
); |
|---|
| 297 |
return; |
|---|
| 298 |
|
|---|
| 299 |
case VLC_SUCCESS: |
|---|
| 300 |
msg_Dbg( p_intf, "Handshake successfull :)" ); |
|---|
| 301 |
p_sys->b_handshaked = true; |
|---|
| 302 |
p_sys->i_interval = 0; |
|---|
| 303 |
p_sys->next_exchange = mdate(); |
|---|
| 304 |
break; |
|---|
| 305 |
|
|---|
| 306 |
case VLC_AUDIOSCROBBLER_EFATAL: |
|---|
| 307 |
msg_Warn( p_intf, "Exiting..." ); |
|---|
| 308 |
return; |
|---|
| 309 |
|
|---|
| 310 |
case VLC_EGENERIC: |
|---|
| 311 |
default: |
|---|
| 312 |
|
|---|
| 313 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 314 |
break; |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
if( p_sys->b_handshaked == false ) |
|---|
| 318 |
continue; |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
msg_Dbg( p_intf, "Going to submit some data..." ); |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' ); |
|---|
| 325 |
if( psz_ln ) |
|---|
| 326 |
*psz_ln = '\0'; |
|---|
| 327 |
|
|---|
| 328 |
if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) ) |
|---|
| 329 |
{ |
|---|
| 330 |
return; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
vlc_mutex_lock( &p_sys->lock ); |
|---|
| 335 |
audioscrobbler_song_t *p_song; |
|---|
| 336 |
for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ ) |
|---|
| 337 |
{ |
|---|
| 338 |
p_song = &p_sys->p_queue[i_song]; |
|---|
| 339 |
if( !asprintf( &psz_submit_song, |
|---|
| 340 |
"&a%%5B%d%%5D=%s" |
|---|
| 341 |
"&t%%5B%d%%5D=%s" |
|---|
| 342 |
"&i%%5B%d%%5D=%u" |
|---|
| 343 |
"&o%%5B%d%%5D=P" |
|---|
| 344 |
"&r%%5B%d%%5D=" |
|---|
| 345 |
"&l%%5B%d%%5D=%d" |
|---|
| 346 |
"&b%%5B%d%%5D=%s" |
|---|
| 347 |
"&n%%5B%d%%5D=%s" |
|---|
| 348 |
"&m%%5B%d%%5D=%s", |
|---|
| 349 |
i_song, p_song->psz_a, |
|---|
| 350 |
i_song, p_song->psz_t, |
|---|
| 351 |
i_song, (unsigned)p_song->date, |
|---|
| 352 |
i_song, |
|---|
| 353 |
i_song, |
|---|
| 354 |
i_song, p_song->i_l, |
|---|
| 355 |
i_song, p_song->psz_b, |
|---|
| 356 |
i_song, p_song->psz_n, |
|---|
| 357 |
i_song, p_song->psz_m |
|---|
| 358 |
) ) |
|---|
| 359 |
{ |
|---|
| 360 |
vlc_mutex_unlock( &p_sys->lock ); |
|---|
| 361 |
return; |
|---|
| 362 |
} |
|---|
| 363 |
psz_submit_tmp = psz_submit; |
|---|
| 364 |
if( !asprintf( &psz_submit, "%s%s", |
|---|
| 365 |
psz_submit_tmp, psz_submit_song ) ) |
|---|
| 366 |
{ |
|---|
| 367 |
free( psz_submit_tmp ); |
|---|
| 368 |
free( psz_submit_song ); |
|---|
| 369 |
vlc_mutex_unlock( &p_sys->lock ); |
|---|
| 370 |
return; |
|---|
| 371 |
} |
|---|
| 372 |
free( psz_submit_song ); |
|---|
| 373 |
free( psz_submit_tmp ); |
|---|
| 374 |
} |
|---|
| 375 |
vlc_mutex_unlock( &p_sys->lock ); |
|---|
| 376 |
|
|---|
| 377 |
i_post_socket = net_ConnectTCP( p_intf, |
|---|
| 378 |
p_sys->psz_submit_host, p_sys->i_submit_port ); |
|---|
| 379 |
|
|---|
| 380 |
if ( i_post_socket == -1 ) |
|---|
| 381 |
{ |
|---|
| 382 |
|
|---|
| 383 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 384 |
p_sys->b_handshaked = false; |
|---|
| 385 |
free( psz_submit ); |
|---|
| 386 |
continue; |
|---|
| 387 |
} |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
i_net_ret = net_Printf( |
|---|
| 391 |
VLC_OBJECT( p_intf ), i_post_socket, NULL, |
|---|
| 392 |
POST_REQUEST, p_sys->psz_submit_file, |
|---|
| 393 |
(unsigned)strlen( psz_submit ), p_sys->psz_submit_file, |
|---|
| 394 |
VERSION, psz_submit |
|---|
| 395 |
); |
|---|
| 396 |
|
|---|
| 397 |
free( psz_submit ); |
|---|
| 398 |
if ( i_net_ret == -1 ) |
|---|
| 399 |
{ |
|---|
| 400 |
|
|---|
| 401 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 402 |
p_sys->b_handshaked = false; |
|---|
| 403 |
continue; |
|---|
| 404 |
} |
|---|
| 405 |
|
|---|
| 406 |
i_net_ret = net_Read( p_intf, i_post_socket, NULL, |
|---|
| 407 |
p_buffer, 1023, false ); |
|---|
| 408 |
if ( i_net_ret <= 0 ) |
|---|
| 409 |
{ |
|---|
| 410 |
|
|---|
| 411 |
continue; |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
net_Close( i_post_socket ); |
|---|
| 415 |
p_buffer[i_net_ret] = '\0'; |
|---|
| 416 |
|
|---|
| 417 |
p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" ); |
|---|
| 418 |
if ( p_buffer_pos ) |
|---|
| 419 |
{ |
|---|
| 420 |
msg_Warn( p_intf, "%s", p_buffer_pos ); |
|---|
| 421 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 422 |
continue; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" ); |
|---|
| 426 |
if ( p_buffer_pos ) |
|---|
| 427 |
{ |
|---|
| 428 |
msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" ); |
|---|
| 429 |
p_sys->b_handshaked = false; |
|---|
| 430 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 431 |
continue; |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
p_buffer_pos = strstr( ( char * ) p_buffer, "OK" ); |
|---|
| 435 |
if ( p_buffer_pos ) |
|---|
| 436 |
{ |
|---|
| 437 |
int i; |
|---|
| 438 |
for( i = 0; i < p_sys->i_songs; i++ ) |
|---|
| 439 |
DeleteSong( &p_sys->p_queue[i] ); |
|---|
| 440 |
p_sys->i_songs = 0; |
|---|
| 441 |
p_sys->i_interval = 0; |
|---|
| 442 |
p_sys->next_exchange = mdate(); |
|---|
| 443 |
msg_Dbg( p_intf, "Submission successful!" ); |
|---|
| 444 |
} |
|---|
| 445 |
else |
|---|
| 446 |
{ |
|---|
| 447 |
msg_Err( p_intf, "Authentication failed, handshaking again (%s)", |
|---|
| 448 |
p_buffer ); |
|---|
| 449 |
p_sys->b_handshaked = false; |
|---|
| 450 |
HandleInterval( &p_sys->next_exchange, &p_sys->i_interval ); |
|---|
| 451 |
continue; |
|---|
| 452 |
} |
|---|
| 453 |
} |
|---|
| 454 |
vlc_restorecancel( canc ); |
|---|
| 455 |
} |
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
static int PlayingChange( vlc_object_t *p_this, const char *psz_var, |
|---|
| 461 |
vlc_value_t oldval, vlc_value_t newval, void *p_data ) |
|---|
| 462 |
{ |
|---|
| 463 |
intf_thread_t *p_intf = ( intf_thread_t* ) p_data; |
|---|
| 464 |
intf_sys_t *p_sys = p_intf->p_sys; |
|---|
| 465 |
|
|---|
| 466 |
VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); |
|---|
| 467 |
|
|---|
| 468 |
if( p_sys->b_meta_read == false && newval.i_int >= PLAYING_S ) |
|---|
| 469 |
{ |
|---|
| 470 |
ReadMetaData( p_intf ); |
|---|
| 471 |
return VLC_SUCCESS; |
|---|
| 472 |
} |
|---|
| 473 |
|
|---|
| 474 |
if( newval.i_int >= END_S ) |
|---|
| 475 |
AddToQueue( p_intf ); |
|---|
| 476 |
else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S ) |
|---|
| 477 |
p_sys->time_pause = mdate(); |
|---|
| 478 |
else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S ) |
|---|
| 479 |
p_sys->time_total_pauses += ( mdate() - p_sys->time_pause ); |
|---|
| 480 |
|
|---|
| 481 |
return VLC_SUCCESS; |
|---|
| 482 |
} |
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
static int ItemChange( vlc_object_t *p_this, const char *psz_var, |
|---|
| 488 |
vlc_value_t oldval, vlc_value_t newval, void *p_data ) |
|---|
| 489 |
{ |
|---|
| 490 |
playlist_t *p_playlist; |
|---|
| 491 |
input_thread_t *p_input; |
|---|
| 492 |
intf_thread_t *p_intf = ( intf_thread_t* ) p_data; |
|---|
| 493 |
intf_sys_t *p_sys = p_intf->p_sys; |
|---|
| 494 |
input_item_t *p_item; |
|---|
| 495 |
vlc_value_t video_val; |
|---|
| 496 |
|
|---|
| 497 |
VLC_UNUSED( p_this ); VLC_UNUSED( psz_var ); |
|---|
| 498 |
VLC_UNUSED( oldval ); VLC_UNUSED( newval ); |
|---|
| 499 |
|
|---|
| 500 |
p_sys->b_state_cb = false; |
|---|
| 501 |
p_sys->b_meta_read = false; |
|---|
| 502 |
p_sys->b_submit = false; |
|---|
| 503 |
|
|---|
| 504 |
p_playlist = pl_Hold( p_intf ); |
|---|
| 505 |
p_input = playlist_CurrentInput( p_playlist ); |
|---|
| 506 |
|
|---|
| 507 |
if( !p_input || p_input->b_dead ) |
|---|
| 508 |
{ |
|---|
| 509 |
pl_Release( p_intf ); |
|---|
| 510 |
return VLC_SUCCESS; |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
pl_Release( p_intf ); |
|---|
| 514 |
|
|---|
| 515 |
p_item = input_GetItem( p_input ); |
|---|
| 516 |
if( !p_item ) |
|---|
| 517 |
{ |
|---|
| 518 |
vlc_object_release( p_input ); |
|---|
| 519 |
return VLC_SUCCESS; |
|---|
| 520 |
} |
|---|
| 521 |
|
|---|
| 522 |
var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL ); |
|---|
| 523 |
if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET ) |
|---|
| 524 |
{ |
|---|
| 525 |
msg_Dbg( p_this, "Not an audio local file, not submitting"); |
|---|
| 526 |
vlc_object_release( p_input ); |
|---|
| 527 |
return VLC_SUCCESS; |
|---|
| 528 |
} |
|---|
| 529 |
|
|---|
| 530 |
p_sys->time_total_pauses = 0; |
|---|
| 531 |
time( &p_sys->p_current_song.date ); |
|---|
| 532 |
p_sys->p_current_song.i_start = mdate(); |
|---|
| 533 |
|
|---|
| 534 |
var_AddCallback( p_input, "state", PlayingChange, p_intf ); |
|---|
| 535 |
p_sys->b_state_cb = true; |
|---|
| 536 |
|
|---|
| 537 |
if( input_item_IsPreparsed( p_item ) ) |
|---|
| 538 |
ReadMetaData( p_intf ); |
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
vlc_object_release( p_input ); |
|---|
| 543 |
return VLC_SUCCESS; |
|---|
| 544 |
} |
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
static void AddToQueue ( intf_thread_t *p_this ) |
|---|
| 550 |
{ |
|---|
| 551 |
mtime_t played_time; |
|---|
| 552 |
intf_sys_t *p_sys = p_this->p_sys; |
|---|
| 553 |
|
|---|
| 554 |
vlc_mutex_lock( &p_sys->lock ); |
|---|
| 555 |
if( !p_sys->b_submit ) |
|---|
| 556 |
goto end; |
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
played_time = mdate() - p_sys->p_current_song.i_start - |
|---|
| 560 |
p_sys->time_total_pauses; |
|---|
| 561 |
played_time /= 1000000; |
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
if( p_sys->p_current_song.i_l == 0 ) |
|---|
| 566 |
p_sys->p_current_song.i_l = played_time; |
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
if( p_sys->p_current_song.i_l < 30 ) |
|---|
| 570 |
{ |
|---|
| 571 |
msg_Dbg( p_this, "Song too short (< 30s), not submitting" ); |
|---|
| 572 |
goto end; |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
if( ( played_time < 240 ) && |
|---|
| 577 |
( played_time < ( p_sys->p_current_song.i_l / 2 ) ) ) |
|---|
| 578 |
{ |
|---|
| 579 |
msg_Dbg( p_this, "Song not listened long enough, not submitting" ); |
|---|
| 580 |
goto end; |
|---|
| 581 |
} |
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a || |
|---|
| 585 |
!p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t ) |
|---|
| 586 |
{ |
|---|
| 587 |
msg_Dbg( p_this, "Missing artist or title, not submitting" ); |
|---|
| 588 |
goto end; |
|---|
| 589 |
} |
|---|
| 590 |
|
|---|
| 591 |
if( p_sys->i_songs >= QUEUE_MAX ) |
|---|
| 592 |
{ |
|---|
| 593 |
msg_Warn( p_this, "Submission queue is full, not submitting" ); |
|---|
| 594 |
goto end; |
|---|
| 595 |
} |
|---|
| 596 |
|
|---|
| 597 |
msg_Dbg( p_this, "Song will be submitted." ); |
|---|
| 598 |
|
|---|
| 599 |
#define QUEUE_COPY( a ) \ |
|---|
| 600 |
p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a |
|---|
| 601 |
|
|---|
| 602 |
#define QUEUE_COPY_NULL( a ) \ |
|---|
| 603 |
QUEUE_COPY( a ); \ |
|---|
| 604 |
p_sys->p_current_song.a = NULL |
|---|
| 605 |
|
|---|
| 606 |
QUEUE_COPY( i_l ); |
|---|
| 607 |
QUEUE_COPY_NULL( psz_n ); |
|---|
| 608 |
QUEUE_COPY_NULL( psz_a ); |
|---|
| 609 |
QUEUE_COPY_NULL( psz_t ); |
|---|
| 610 |
QUEUE_COPY_NULL( psz_b ); |
|---|
| 611 |
QUEUE_COPY_NULL( psz_m ); |
|---|
| 612 |
QUEUE_COPY( date ); |
|---|
| 613 |
#undef QUEUE_COPY_NULL |
|---|
| 614 |
#undef QUEUE_COPY |
|---|
| 615 |
|
|---|
| 616 |
p_sys->i_songs++; |
|---|
| 617 |
|
|---|
| 618 |
|
|---|
| 619 |
vlc_cond_signal( &p_sys->wait ); |
|---|
| 620 |
|
|---|
| 621 |
end: |
|---|
| 622 |
DeleteSong( &p_sys->p_current_song ); |
|---|
| 623 |
p_sys->b_submit = false; |
|---|
| 624 |
vlc_mutex_unlock( &p_sys->lock ); |
|---|
| 625 |
} |
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
static int ParseURL( char *psz_url, char **psz_host, char **psz_file, |
|---|
| 642 |
int *i_port ) |
|---|
| 643 |
{ |
|---|
| 644 |
int i_pos; |
|---|
| 645 |
int i_len = strlen( psz_url ); |
|---|
| 646 |
FREENULL( *psz_host ); |
|---|
| 647 |
FREENULL( *psz_file ); |
|---|
| 648 |
|
|---|
| 649 |
i_pos = strcspn( psz_url, ":" ); |
|---|
| 650 |
if( i_pos == i_len ) |
|---|
| 651 |
return VLC_EGENERIC; |
|---|
| 652 |
|
|---|
| 653 |
*psz_host = strndup( psz_url, i_pos ); |
|---|
| 654 |
if( !*psz_host ) |
|---|
| 655 |
return VLC_ENOMEM; |
|---|
| 656 |
|
|---|
| 657 |
i_pos++; |
|---|
| 658 |
*i_port = atoi( psz_url + i_pos ); |
|---|
| 659 |
if( *i_port <= 0 ) |
|---|
| 660 |
{ |
|---|
| 661 |
FREENULL( *psz_host ); |
|---|
| 662 |
return VLC_EGENERIC; |
|---|
| 663 |
} |
|---|
| 664 |
|
|---|
| 665 |
i_pos = strcspn( psz_url, "/" ); |
|---|
| 666 |
|
|---|
| 667 |
if( i_pos == i_len ) |
|---|
| 668 |
return VLC_EGENERIC; |
|---|
| 669 |
|
|---|
| 670 |
i_pos++; |
|---|
| 671 |
*psz_file = strdup( psz_url + i_pos ); |
|---|
| 672 |
if( !*psz_file ) |
|---|
| 673 |
{ |
|---|
| 674 |
FREENULL( *psz_host ); |
|---|
| 675 |
return VLC_ENOMEM; |
|---|
| 676 |
} |
|---|
| 677 |
|
|---|
| 678 |
free( psz_url ); |
|---|
| 679 |
return VLC_SUCCESS; |
|---|
| 680 |
} |
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
static int Handshake( intf_thread_t *p_this ) |
|---|
| 686 |
{ |
|---|
| 687 |
char *psz_username, *psz_password; |
|---|
| 688 |
time_t timestamp; |
|---|
| 689 |
char psz_timestamp[21]; |
|---|
| 690 |
|
|---|
| 691 |
struct md5_s p_struct_md5; |
|---|
| 692 |
|
|---|
| 693 |
stream_t *p_stream; |
|---|
| 694 |
char *psz_handshake_url; |
|---|
| 695 |
uint8_t p_buffer[1024]; |
|---|
| 696 |
char *p_buffer_pos; |
|---|
| 697 |
|
|---|
| 698 |
int i_ret; |
|---|
| 699 |
char *psz_url; |
|---|
| 700 |
|
|---|
| 701 |
intf_thread_t *p_intf = ( intf_thread_t* ) p_this; |
|---|
| 702 |
intf_sys_t *p_sys = p_this->p_sys; |
|---|
| 703 |
|
|---|
| 704 |
psz_username = config_GetPsz( p_this, "lastfm-username" ); |
|---|
| 705 |
if( !psz_username ) |
|---|
| 706 |
return VLC_ENOMEM; |
|---|
| 707 |
|
|---|
| 708 |
psz_password = config_GetPsz( p_this, "lastfm-password" ); |
|---|
| 709 |
if( !psz_password ) |
|---|
| 710 |
{ |
|---|
| 711 |
free( psz_username ); |
|---|
| 712 |
return VLC_ENOMEM; |
|---|
| 713 |
} |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
if ( !*psz_username || !*psz_password ) |
|---|
| 717 |
{ |
|---|
| 718 |
free( psz_username ); |
|---|
| 719 |
free( psz_password ); |
|---|
| 720 |
return VLC_ENOVAR; |
|---|
| 721 |
} |
|---|
| 722 |
|
|---|
| 723 |
time( ×tamp ); |
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
InitMD5( &p_struct_md5 ); |
|---|
| 727 |
AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) ); |
|---|
| 728 |
EndMD5( &p_struct_md5 ); |
|---|
| 729 |
|
|---|
| 730 |
free( psz_password ); |
|---|
| 731 |
|
|---|
| 732 |
char *psz_password_md5 = psz_md5_hash( &p_struct_md5 ); |
|---|
| 733 |
if( !psz_password_md5 ) |
|---|
| 734 |
{ |
|---|
| 735 |
free( psz_username ); |
|---|
| 736 |
return VLC_ENOMEM; |
|---|
| 737 |
} |
|---|
| 738 |
|
|---|
| 739 |
snprintf( psz_timestamp, sizeof( psz_timestamp ), "%"PRIu64, |
|---|
| 740 |
(uint64_t)timestamp ); |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
InitMD5( &p_struct_md5 ); |
|---|
| 747 |
AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 ); |
|---|
| 748 |
AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp )); |
|---|
| 749 |
EndMD5( &p_struct_md5 ); |
|---|
| 750 |
free( psz_password_md5 ); |
|---|
| 751 |
|
|---|
| 752 |
char *psz_auth_token = psz_md5_hash( &p_struct_md5 ); |
|---|
| 753 |
if( !psz_auth_token ) |
|---|
| 754 |
{ |
|---|
| 755 |
free( psz_username ); |
|---|
| 756 |
return VLC_ENOMEM; |
|---|
| 757 |
} |
|---|
| 758 |
strncpy( p_sys->psz_auth_token, psz_auth_token, 33 ); |
|---|
| 759 |
free( psz_auth_token ); |
|---|
| 760 |
|
|---|
| 761 |
if( !asprintf( &psz_handshake_url, |
|---|
| 762 |
"http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s", |
|---|
| 763 |
CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp, |
|---|
| 764 |
p_sys->psz_auth_token ) ) |
|---|
| 765 |
{ |
|---|
| 766 |
free( psz_username ); |
|---|
| 767 |
return VLC_ENOMEM; |
|---|
| 768 |
} |
|---|
| 769 |
free( psz_username ); |
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
p_stream = stream_UrlNew( p_intf, psz_handshake_url ); |
|---|
| 773 |
free( psz_handshake_url ); |
|---|
| 774 |
|
|---|
| 775 |
if( !p_stream ) |
|---|
| 776 |
return VLC_EGENERIC; |
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
i_ret = stream_Read( p_stream, p_buffer, 1023 ); |
|---|
| 780 |
if( i_ret == 0 ) |
|---|
| 781 |
{ |
|---|
| 782 |
stream_Delete( p_stream ); |
|---|
| 783 |
return VLC_EGENERIC; |
|---|
| 784 |
} |
|---|
| 785 |
p_buffer[i_ret] = '\0'; |
|---|
| 786 |
stream_Delete( p_stream ); |
|---|
| 787 |
|
|---|
| 788 |
p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " ); |
|---|
| 789 |
if ( p_buffer_pos ) |
|---|
| 790 |
{ |
|---|
| 791 |
|
|---|
| 792 |
msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 ); |
|---|
| 793 |
return VLC_EGENERIC; |
|---|
| 794 |
} |
|---|
| 795 |
|
|---|
| 796 |
p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" ); |
|---|
| 797 |
if ( p_buffer_pos ) |
|---|
| 798 |
{ |
|---|
| 799 |
|
|---|
| 800 |
intf_UserFatal( p_this, false, |
|---|
| 801 |
_("last.fm: Authentication failed"), |
|---|
| 802 |
_("last.fm username or password is incorrect. " |
|---|
| 803 |
"Please verify your settings and relaunch VLC." ) ); |
|---|
| 804 |
return VLC_AUDIOSCROBBLER_EFATAL; |
|---|
| 805 |
} |
|---|
| 806 |
|
|---|
| 807 |
p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" ); |
|---|
| 808 |
if ( p_buffer_pos ) |
|---|
| 809 |
{ |
|---|
| 810 |
|
|---|
| 811 |
msg_Err( p_intf, "This version of VLC has been banned by last.fm. " |
|---|
| 812 |
"You should upgrade VLC, or disable the last.fm plugin." ); |
|---|
| 813 |
return VLC_AUDIOSCROBBLER_EFATAL; |
|---|
| 814 |
} |
|---|
| 815 |
|
|---|
| 816 |
p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" ); |
|---|
| 817 |
if ( p_buffer_pos ) |
|---|
| 818 |
{ |
|---|
| 819 |
|
|---|
| 820 |
msg_Err( p_intf, "last.fm handshake failed because your clock is too " |
|---|
| 821 |
"much shifted. Please correct it, and relaunch VLC." ); |
|---|
| 822 |
return VLC_AUDIOSCROBBLER_EFATAL; |
|---|
| 823 |
} |
|---|
| 824 |
|
|---|
| 825 |
p_buffer_pos = strstr( ( char* ) p_buffer, "OK" ); |
|---|
| 826 |
if ( !p_buffer_pos ) |
|---|
| 827 |
goto proto; |
|---|
| 828 |
|
|---|
| 829 |
p_buffer_pos = strstr( p_buffer_pos, "\n" ); |
|---|
| 830 |
if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 ) |
|---|
| 831 |
goto proto; |
|---|
| 832 |
p_buffer_pos++; |
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos ); |
|---|
| 836 |
|
|---|
| 837 |
p_buffer_pos = strstr( p_buffer_pos, "http://" ); |
|---|
| 838 |
if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 ) |
|---|
| 839 |
goto proto; |
|---|
| 840 |
|
|---|
| 841 |
|
|---|
| 842 |
p_buffer_pos += 7; |
|---|
| 843 |
#if 0 //NOT USED |
|---|
| 844 |
psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) ); |
|---|
| 845 |
if( !psz_url ) |
|---|
| 846 |
goto oom; |
|---|
| 847 |
|
|---|
| 848 |
switch( ParseURL( psz_url, &p_sys->psz_nowp_host, |
|---|
| 849 |
&p_sys->psz_nowp_file, &p_sys->i_nowp_port ) ) |
|---|
| 850 |
{ |
|---|
| 851 |
case VLC_ENOMEM: |
|---|
| 852 |
goto oom; |
|---|
| 853 |
case VLC_EGENERIC: |
|---|
| 854 |
goto proto; |
|---|
| 855 |
case VLC_SUCCESS: |
|---|
| 856 |
default: |
|---|
| 857 |
break; |
|---|
| 858 |
} |
|---|
| 859 |
#endif |
|---|
| 860 |
p_buffer_pos = strstr( p_buffer_pos, "http://" ); |
|---|
| 861 |
if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 ) |
|---|
| 862 |
goto proto; |
|---|
| 863 |
|
|---|
| 864 |
|
|---|
| 865 |
p_buffer_pos += 7; |
|---|
| 866 |
psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) ); |
|---|
| 867 |
|
|---|