1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright (C) 2006-2007 the VideoLAN team
7 * Author: Rafaël Carré <funman at videolanorg>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /* audioscrobbler protocol version: 1.2
25 * http://www.audioscrobbler.net/development/protocol/
27 * TODO: "Now Playing" feature (not mandatory)
29 /*****************************************************************************
31 *****************************************************************************/
38 #include <vlc_interface.h>
41 #include <vlc_block.h>
42 #include <vlc_stream.h>
44 #include <vlc_network.h>
45 #include <vlc_interface.h>
46 #include <vlc_playlist.h>
48 /*****************************************************************************
50 *****************************************************************************/
52 /* Keeps track of metadata to be submitted */
53 typedef struct audioscrobbler_song_t
55 char *psz_a; /**< track artist */
56 char *psz_t; /**< track title */
57 char *psz_b; /**< track album */
58 char *psz_n; /**< track number */
59 int i_l; /**< track length */
60 char *psz_m; /**< musicbrainz id */
61 time_t date; /**< date since epoch */
62 } audioscrobbler_song_t;
66 audioscrobbler_song_t p_queue[50]; /**< songs not submitted yet*/
67 int i_songs; /**< number of songs */
69 vlc_mutex_t lock; /**< p_sys mutex */
71 /* data about audioscrobbler session */
72 time_t next_exchange; /**< when can we send data */
73 unsigned int i_interval; /**< waiting interval (secs)*/
75 /* submission of played songs */
76 char *psz_submit_host; /**< where to submit data */
77 int i_submit_port; /**< port to which submit */
78 char *psz_submit_file; /**< file to which submit */
80 /* submission of playing song */
82 char *psz_nowp_host; /**< where to submit data */
83 int i_nowp_port; /**< port to which submit */
84 char *psz_nowp_file; /**< file to which submit */
86 vlc_bool_t b_handshaked; /**< are we authenticated ? */
87 char psz_auth_token[33]; /**< Authentication token */
89 /* data about song currently playing */
90 audioscrobbler_song_t p_current_song; /**< song being played */
92 time_t time_pause; /**< time when vlc paused */
93 time_t time_total_pauses; /**< total time in pause */
95 vlc_bool_t b_submit; /**< do we have to submit ? */
97 vlc_bool_t b_state_cb; /**< if we registered the
100 vlc_bool_t b_meta_read; /**< if we read the song's
101 * metadata already */
104 static int Open ( vlc_object_t * );
105 static void Close ( vlc_object_t * );
106 static void Unload ( intf_thread_t * );
107 static void Run ( intf_thread_t * );
109 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
110 vlc_value_t, void * );
111 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
112 vlc_value_t, void * );
114 static int AddToQueue ( intf_thread_t * );
115 static int Handshake ( intf_thread_t * );
116 static int ReadMetaData ( intf_thread_t * );
117 static void DeleteSong ( audioscrobbler_song_t* );
118 static int ParseURL ( char *, char **, char **, int * );
119 static void HandleInterval ( time_t *, unsigned int * );
121 /*****************************************************************************
123 ****************************************************************************/
125 #define USERNAME_TEXT N_("Username")
126 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
127 #define PASSWORD_TEXT N_("Password")
128 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
130 /* This error value is used when last.fm plugin has to be unloaded. */
131 #define VLC_AUDIOSCROBBLER_EFATAL -69
133 /* last.fm client identifier */
134 #define CLIENT_NAME PACKAGE
135 #define CLIENT_VERSION VERSION
137 /* HTTP POST request : to submit data */
138 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
139 "Accept-Encoding: identity\n" \
140 "Content-length: %u\n" \
141 "Connection: close\n" \
142 "Content-type: application/x-www-form-urlencoded\n" \
144 "User-agent: VLC Media Player/%s\r\n" \
150 set_category( CAT_INTERFACE );
151 set_subcategory( SUBCAT_INTERFACE_CONTROL );
152 set_shortname( N_( "Audioscrobbler" ) );
153 set_description( N_("Submission of played songs to last.fm") );
154 add_string( "lastfm-username", "", NULL,
155 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
156 add_password( "lastfm-password", "", NULL,
157 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
158 set_capability( "interface", 0 );
159 set_callbacks( Open, Close );
162 /*****************************************************************************
163 * Open: initialize and create stuff
164 *****************************************************************************/
165 static int Open( vlc_object_t *p_this )
167 playlist_t *p_playlist;
168 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
169 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
174 p_intf->p_sys = p_sys;
176 vlc_mutex_init( p_this, &p_sys->lock );
178 p_playlist = pl_Yield( p_intf );
180 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
182 pl_Release( p_playlist );
184 p_intf->pf_run = Run;
189 /*****************************************************************************
190 * Close: destroy interface stuff
191 *****************************************************************************/
192 static void Close( vlc_object_t *p_this )
194 playlist_t *p_playlist;
195 input_thread_t *p_input;
196 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
197 intf_sys_t *p_sys = p_intf->p_sys;
199 p_playlist = pl_Yield( p_intf );
202 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
204 p_input = p_playlist->p_input;
207 vlc_object_yield( p_input );
209 if( p_sys->b_state_cb )
210 var_DelCallback( p_input, "state", PlayingChange, p_intf );
212 vlc_object_release( p_input );
216 pl_Release( p_playlist );
218 p_intf->b_dead = VLC_TRUE;
219 /* we lock the mutex in case p_sys is being accessed from a callback */
220 vlc_mutex_lock ( &p_sys->lock );
222 for( i = 0; i < p_sys->i_songs; i++ )
223 DeleteSong( &p_sys->p_queue[i] );
224 free( p_sys->psz_submit_host );
225 free( p_sys->psz_submit_file );
227 free( p_sys->psz_nowp_host );
228 free( p_sys->psz_nowp_file );
230 vlc_mutex_unlock ( &p_sys->lock );
231 vlc_mutex_destroy( &p_sys->lock );
236 /*****************************************************************************
237 * Unload: Unloads the audioscrobbler when encountering fatal errors
238 *****************************************************************************/
239 static void Unload( intf_thread_t *p_this )
241 vlc_object_kill( p_this );
242 vlc_object_detach( p_this );
243 if( p_this->p_module )
244 module_Unneed( p_this, p_this->p_module );
245 vlc_mutex_destroy( &p_this->change_lock );
246 vlc_object_destroy( p_this );
249 /*****************************************************************************
250 * Run : call Handshake() then submit songs
251 *****************************************************************************/
252 static void Run( intf_thread_t *p_intf )
254 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
257 uint8_t p_buffer[1024];
262 intf_sys_t *p_sys = p_intf->p_sys;
265 while( !p_intf->b_die && !p_intf->p_libvlc->b_die )
267 /* waiting for data to submit, if waiting interval is elapsed */
268 vlc_object_lock( p_intf );
269 if( time( NULL ) < p_sys->next_exchange )
270 b_die = vlc_object_timedwait( p_intf, p_sys->next_exchange );
272 b_die = vlc_object_wait( p_intf );
273 vlc_object_unlock( p_intf );
277 msg_Dbg( p_intf, "audioscrobbler is dying");
281 /* handshake if needed */
282 if( p_sys->b_handshaked == VLC_FALSE )
284 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
286 switch( Handshake( p_intf ) )
293 /* username not set */
294 intf_UserFatal( p_intf, VLC_FALSE,
295 _("Last.fm username not set"),
296 _("Please set a username or disable the "
297 "audioscrobbler plugin, and restart VLC.\n"
298 "Visit http://www.last.fm/join/ to get an account.")
304 msg_Dbg( p_intf, "Handshake successfull :)" );
305 p_sys->b_handshaked = VLC_TRUE;
306 p_sys->i_interval = 0;
307 time( &p_sys->next_exchange );
310 case VLC_AUDIOSCROBBLER_EFATAL:
311 msg_Warn( p_intf, "Unloading..." );
317 /* protocol error : we'll try later */
318 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
321 /* if handshake failed let's restart the loop */
322 if( p_sys->b_handshaked == VLC_FALSE )
326 msg_Dbg( p_intf, "Going to submit some data..." );
328 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
329 { /* Out of memory */
334 /* forge the HTTP POST request */
335 vlc_mutex_lock( &p_sys->lock );
336 audioscrobbler_song_t *p_song;
337 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
339 p_song = &p_sys->p_queue[i_song];
340 if( !asprintf( &psz_submit_song,
341 "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s"
342 "&i%%5B%d%%5D=%llu&o%%5B%d%%5D=P&r%%5B%d%%5D="
343 "&l%%5B%d%%5D=%d&b%%5B%d%%5D=%s"
344 "&n%%5B%d%%5D=%s&m%%5B%d%%5D=%s",
345 i_song, p_song->psz_a, i_song, p_song->psz_t,
346 i_song, (uintmax_t)p_song->date, i_song, i_song,
347 i_song, p_song->i_l, i_song, p_song->psz_b,
348 i_song, p_song->psz_n, i_song, p_song->psz_m
350 { /* Out of memory */
351 vlc_mutex_unlock( &p_sys->lock );
355 psz_submit_tmp = psz_submit;
356 if( !asprintf( &psz_submit, "%s%s",
357 psz_submit_tmp, psz_submit_song ) )
358 { /* Out of memory */
359 free( psz_submit_tmp );
360 free( psz_submit_song );
361 vlc_mutex_unlock( &p_sys->lock );
365 free( psz_submit_song );
366 free( psz_submit_tmp );
368 vlc_mutex_unlock( &p_sys->lock );
370 i_post_socket = net_ConnectTCP( p_intf,
371 p_sys->psz_submit_host, p_sys->i_submit_port );
373 if ( i_post_socket == -1 )
375 /* If connection fails, we assume we must handshake again */
376 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
377 p_sys->b_handshaked = VLC_FALSE;
382 /* we transmit the data */
383 i_net_ret = net_Printf(
384 VLC_OBJECT( p_intf ), i_post_socket, NULL,
385 POST_REQUEST, p_sys->psz_submit_file,
386 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
391 if ( i_net_ret == -1 )
393 /* If connection fails, we assume we must handshake again */
394 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
395 p_sys->b_handshaked = VLC_FALSE;
399 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
400 p_buffer, 1023, VLC_FALSE );
401 if ( i_net_ret <= 0 )
403 /* if we get no answer, something went wrong : try again */
407 net_Close( i_post_socket );
408 p_buffer[i_net_ret] = '\0';
410 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
413 msg_Warn( p_intf, "%s", p_buffer_pos );
414 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
418 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
421 msg_Dbg( p_intf, "Authentication failed, handshaking again" );
422 p_sys->b_handshaked = VLC_FALSE;
423 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
427 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
431 for( i = 0; i < p_sys->i_songs; i++ )
432 DeleteSong( &p_sys->p_queue[i] );
434 p_sys->i_interval = 0;
435 time( &p_sys->next_exchange );
436 msg_Dbg( p_intf, "Submission successful!" );
440 msg_Dbg( p_intf, "Authentication failed, handshaking again" );
441 p_sys->b_handshaked = VLC_FALSE;
442 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
448 /*****************************************************************************
449 * PlayingChange: Playing status change callback
450 *****************************************************************************/
451 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
452 vlc_value_t oldval, vlc_value_t newval, void *p_data )
454 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
455 intf_sys_t *p_sys = p_intf->p_sys;
457 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
462 if( p_sys->b_meta_read == VLC_FALSE && newval.i_int == PLAYING_S )
463 ReadMetaData( p_intf );
465 if( newval.i_int == END_S || newval.i_int == ERROR_S )
467 /* We'll try to add the previously playing song in the queue */
468 if( AddToQueue( p_intf ) == VLC_ENOMEM )
471 else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S )
472 time( &p_sys->time_pause );
473 else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S )
474 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
479 /*****************************************************************************
480 * ItemChange: Playlist item change callback
481 *****************************************************************************/
482 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
483 vlc_value_t oldval, vlc_value_t newval, void *p_data )
485 playlist_t *p_playlist;
486 input_thread_t *p_input;
487 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
488 intf_sys_t *p_sys = p_intf->p_sys;
489 input_item_t *p_item;
490 vlc_value_t video_val;
492 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
493 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
498 p_sys->b_state_cb = VLC_FALSE;
499 p_sys->b_meta_read = VLC_FALSE;
500 p_sys->b_submit = VLC_FALSE;
502 p_playlist = pl_Yield( p_intf );
504 p_input = p_playlist->p_input;
506 if( !p_input || p_input->b_dead )
509 pl_Release( p_playlist );
513 vlc_object_yield( p_input );
515 pl_Release( p_playlist );
517 p_item = input_GetItem( p_input );
520 vlc_object_release( p_input );
524 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
525 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
527 msg_Dbg( p_this, "Not an audio local file, not submitting");
528 vlc_object_release( p_input );
532 p_sys->time_total_pauses = 0;
533 time( &p_sys->p_current_song.date );
535 var_AddCallback( p_input, "state", PlayingChange, p_intf );
536 p_sys->b_state_cb = VLC_TRUE;
538 if( input_item_IsPreparsed( p_item ) )
539 ReadMetaData( p_intf );
540 /* if the input item was not preparsed, we'll do it in PlayingChange()
541 * callback, when "state" == PLAYING_S */
543 vlc_object_release( p_input );
547 /*****************************************************************************
548 * AddToQueue: Add the played song to the queue to be submitted
549 *****************************************************************************/
550 static int AddToQueue ( intf_thread_t *p_this )
553 intf_sys_t *p_sys = p_this->p_sys;
555 vlc_mutex_lock( &p_sys->lock );
556 if( !p_sys->b_submit )
558 DeleteSong( &p_sys->p_current_song );
559 vlc_mutex_unlock( &p_sys->lock );
563 /* wait for the user to listen enough before submitting */
564 time ( &played_time );
565 played_time -= p_sys->p_current_song.date;
566 played_time -= p_sys->time_total_pauses;
567 if( ( played_time < 240 ) &&
568 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
570 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
571 DeleteSong( &p_sys->p_current_song );
572 vlc_mutex_unlock( &p_sys->lock );
576 if( p_sys->p_current_song.i_l < 30 )
578 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
579 DeleteSong( &p_sys->p_current_song );
580 vlc_mutex_unlock( &p_sys->lock );
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 )
587 msg_Dbg( p_this, "Missing artist or title, not submitting" );
588 DeleteSong( &p_sys->p_current_song );
589 vlc_mutex_unlock( &p_sys->lock );
593 if( p_sys->i_songs == 50 )
595 msg_Warn( p_this, "Submission queue is full, not submitting" );
596 DeleteSong( &p_sys->p_current_song );
597 vlc_mutex_unlock( &p_sys->lock );
601 msg_Dbg( p_this, "Song will be submitted." );
603 #define QUEUE_COPY( a ) \
604 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
606 #define QUEUE_COPY_NULL( a ) \
608 p_sys->p_current_song.a = NULL
611 QUEUE_COPY_NULL( psz_n );
612 QUEUE_COPY_NULL( psz_a );
613 QUEUE_COPY_NULL( psz_t );
614 QUEUE_COPY_NULL( psz_b );
615 QUEUE_COPY_NULL( psz_m );
617 #undef QUEUE_COPY_NULL
622 /* signal the main loop we have something to submit */
623 vlc_object_lock( p_this );
624 vlc_cond_signal( &p_this->object_wait );
625 vlc_object_unlock( p_this );
627 vlc_mutex_unlock( &p_sys->lock );
631 /*****************************************************************************
632 * ParseURL : Split an http:// URL into host, file, and port
634 * Example: "62.216.251.205:80/protocol_1.2"
635 * will be split into "62.216.251.205", 80, "protocol_1.2"
637 * psz_url will be freed before returning
638 * *psz_file & *psz_host will be freed before use
641 * VLC_ENOMEM Out Of Memory
642 * VLC_EGENERIC Invalid url provided
643 * VLC_SUCCESS Success
644 *****************************************************************************/
645 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
649 int i_len = strlen( psz_url );
650 FREENULL( *psz_host );
651 FREENULL( *psz_file );
653 i_pos = strcspn( psz_url, ":" );
657 *psz_host = strndup( psz_url, i_pos );
661 i_pos++; /* skip the ':' */
662 *i_port = atoi( psz_url + i_pos );
665 FREENULL( *psz_host );
669 i_pos = strcspn( psz_url, "/" );
674 i_pos++; /* skip the '/' */
675 *psz_file = strdup( psz_url + i_pos );
678 FREENULL( *psz_host );
686 /*****************************************************************************
687 * Handshake : Init audioscrobbler connection
688 *****************************************************************************/
689 static int Handshake( intf_thread_t *p_this )
691 char *psz_username, *psz_password;
693 char psz_timestamp[33];
695 struct md5_s p_struct_md5;
696 char psz_password_md5[33];
699 char *psz_handshake_url;
700 uint8_t p_buffer[1024];
706 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
707 intf_sys_t *p_sys = p_this->p_sys;
709 psz_username = config_GetPsz( p_this, "lastfm-username" );
713 psz_password = config_GetPsz( p_this, "lastfm-password" );
716 free( psz_username );
720 /* username or password have not been setup */
721 if ( !*psz_username || !*psz_password )
723 free( psz_username );
724 free( psz_password );
730 /* generates a md5 hash of the password */
731 InitMD5( &p_struct_md5 );
732 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
733 EndMD5( &p_struct_md5 );
735 free( psz_password );
738 for ( i = 0; i < 4; i++ )
740 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
741 p_struct_md5.p_digest[i] & 0xff,
742 ( p_struct_md5.p_digest[i] >> 8 ) & 0xff,
743 ( p_struct_md5.p_digest[i] >> 16 ) & 0xff,
744 p_struct_md5.p_digest[i] >> 24
748 snprintf( psz_timestamp, 33, "%llu", (uintmax_t)timestamp );
750 /* generates a md5 hash of :
751 * - md5 hash of the password, plus
752 * - timestamp in clear text
754 InitMD5( &p_struct_md5 );
755 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
756 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
757 EndMD5( &p_struct_md5 );
759 for ( i = 0; i < 4; i++ )
761 sprintf( &p_sys->psz_auth_token[8*i], "%02x%02x%02x%02x",
762 p_struct_md5.p_digest[i] & 0xff,
763 ( p_struct_md5.p_digest[i] >> 8 ) & 0xff,
764 ( p_struct_md5.p_digest[i] >> 16 ) & 0xff,
765 p_struct_md5.p_digest[i] >> 24
769 p_sys->psz_auth_token[32] = '\0';
771 if( !asprintf( &psz_handshake_url,
772 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
773 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
774 p_sys->psz_auth_token ) )
776 free( psz_username );
779 free( psz_username );
781 /* send the http handshake request */
782 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
783 free( psz_handshake_url );
789 i_ret = stream_Read( p_stream, p_buffer, 1023 );
792 stream_Delete( p_stream );
795 p_buffer[i_ret] = '\0';
796 stream_Delete( p_stream );
798 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
801 /* handshake request failed, sorry */
802 msg_Warn( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
806 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
809 /* authentication failed, bad username/password combination */
810 intf_UserFatal( p_this, VLC_FALSE,
811 _("last.fm: Authentication failed"),
812 _("last.fm username or password is incorrect. "
813 "Please verify your settings and relaunch VLC." ) );
814 return VLC_AUDIOSCROBBLER_EFATAL;
817 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
820 /* oops, our version of vlc has been banned by last.fm servers */
821 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
822 "You should upgrade VLC, or disable the last.fm plugin." );
823 return VLC_AUDIOSCROBBLER_EFATAL;
826 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
829 /* The system clock isn't good */
830 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
831 "much shifted. Please correct it, and relaunch VLC." );
832 return VLC_AUDIOSCROBBLER_EFATAL;
835 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
839 p_buffer_pos = strstr( p_buffer_pos, "\n" );
840 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
842 p_buffer_pos++; /* we skip the '\n' */
844 /* save the session ID */
845 snprintf( &p_sys->psz_auth_token[0], 33, "%s", p_buffer_pos );
847 p_buffer_pos = strstr( p_buffer_pos, "http://" );
848 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
851 /* We need to read the nowplaying url */
852 p_buffer_pos += 7; /* we skip "http://" */
854 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
858 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
859 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
870 p_buffer_pos = strstr( p_buffer_pos, "http://" );
871 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
874 /* We need to read the submission url */
875 p_buffer_pos += 7; /* we skip "http://" */
876 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
880 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
881 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
898 msg_Warn( p_intf, "Handshake: can't recognize server protocol" );
902 /*****************************************************************************
903 * DeleteSong : Delete the char pointers in a song
904 *****************************************************************************/
905 static void DeleteSong( audioscrobbler_song_t* p_song )
907 FREENULL( p_song->psz_a );
908 FREENULL( p_song->psz_b );
909 FREENULL( p_song->psz_t );
910 FREENULL( p_song->psz_m );
911 FREENULL( p_song->psz_n );
914 /*****************************************************************************
915 * ReadMetaData : Read meta data when parsed by vlc
916 *****************************************************************************/
917 static int ReadMetaData( intf_thread_t *p_this )
919 playlist_t *p_playlist;
920 input_thread_t *p_input;
921 input_item_t *p_item;
923 intf_sys_t *p_sys = p_this->p_sys;
925 p_playlist = pl_Yield( p_this );
927 p_input = p_playlist->p_input;
931 pl_Release( p_playlist );
932 return( VLC_SUCCESS );
935 vlc_object_yield( p_input );
937 pl_Release( p_playlist );
939 p_item = input_GetItem( p_input );
944 #define ALLOC_ITEM_META( a, b ) \
945 psz_meta = input_item_Get##b( p_item ); \
946 if( psz_meta && *psz_meta ) \
948 a = encode_URI_component( psz_meta ); \
957 vlc_mutex_lock( &p_sys->lock );
959 p_sys->b_meta_read = VLC_TRUE;
961 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
964 vlc_mutex_unlock( &p_sys->lock );
965 msg_Dbg( p_this, "No artist.." );
966 vlc_object_release( p_input );
971 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
974 vlc_mutex_unlock( &p_sys->lock );
975 msg_Dbg( p_this, "No track name.." );
976 vlc_object_release( p_input );
977 free( p_sys->p_current_song.psz_a );
982 /* Now we have read the mandatory meta data, so we can submit that info */
983 p_sys->b_submit = VLC_TRUE;
985 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
987 p_sys->p_current_song.psz_b = calloc( 1, 1 );
989 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
991 p_sys->p_current_song.psz_m = calloc( 1, 1 );
993 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
995 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
997 p_sys->p_current_song.psz_n = calloc( 1, 1 );
998 #undef ALLOC_ITEM_META
1000 msg_Dbg( p_this, "Meta data registered" );
1002 vlc_mutex_unlock( &p_sys->lock );
1003 vlc_object_release( p_input );
1008 static void HandleInterval( time_t *next, unsigned int *i_interval )
1010 if( *i_interval == 0 )
1012 /* first interval is 1 minute */
1017 /* else we double the previous interval, up to 120 minutes */
1018 *i_interval = *i_interval * 2;
1019 if( *i_interval > 60*120 )
1020 *i_interval = 60*120;
1022 *next = time( NULL ) + *i_interval;