1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Rafaël Carré <funman at videolan org>
8 * Kenneth Ostby <kenneo -at- idi -dot- ntnu -dot- no>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /* audioscrobbler protocol version: 1.1
26 * http://audioscrobbler.net/wiki/Protocol1.1
29 /*****************************************************************************
31 *****************************************************************************/
41 * check meta_engine's state, and remove delaying of metadata reading
47 #include <vlc_block.h>
48 #include <vlc_stream.h>
51 #include <vlc_interaction.h>
53 /*****************************************************************************
55 *****************************************************************************/
57 /* Keeps track of metadata to be submitted */
58 typedef struct audioscrobbler_song_t
60 char *psz_a; /* track artist */
61 char *psz_t; /* track title */
62 char *psz_b; /* track album */
63 int i_l; /* track length */
64 char *psz_m; /* musicbrainz id */
65 char *psz_i; /* date */
66 time_t time_playing; /* date (epoch) */
67 } audioscrobbler_song_t;
70 /* Queue to be submitted to server, 10 songs max */
71 typedef struct audioscrobbler_queue_t
73 audioscrobbler_song_t **p_queue; /* contains up to 10 songs */
74 int i_songs_nb; /* number of songs */
75 void *p_next_queue; /* if queue full, pointer to next */
76 } audioscrobbler_queue_t;
80 audioscrobbler_queue_t *p_first_queue; /* 1st queue */
81 vlc_mutex_t lock; /* p_sys mutex */
83 /* data about audioscrobbler session */
84 int i_interval; /* last interval recorded */
85 time_t time_last_interval; /* when was it recorded ? */
86 char *psz_submit_host; /* where to submit data ? */
87 int i_submit_port; /* at which port ? */
88 char *psz_submit_file; /* in which file ? */
89 char *psz_username; /* last.fm username */
90 vlc_bool_t b_handshaked; /* did we handshake ? */
91 char *psz_response_md5; /* md5 response to use */
93 /* data about song currently playing */
94 audioscrobbler_song_t *p_current_song; /* song being played */
95 time_t time_pause; /* time when vlc paused */
96 time_t time_total_pauses; /* sum of time in pause */
97 vlc_bool_t b_queued; /* has it been queud ? */
98 vlc_bool_t b_metadata_read; /* did we read metadata ? */
99 vlc_bool_t b_paused; /* is vlc paused ? */
100 vlc_bool_t b_waiting_meta; /* we need fetched data? */
103 intf_sys_t *p_sys_global; /* to retrieve p_sys in Run() thread */
105 static int Open ( vlc_object_t * );
106 static void Close ( vlc_object_t * );
107 static void Run ( intf_thread_t * );
108 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
109 vlc_value_t, void * );
110 static int PlayingChange( vlc_object_t *, const char *, vlc_value_t,
111 vlc_value_t, void * );
112 static int AddToQueue ( intf_thread_t *p_this );
113 static int Handshake ( intf_thread_t *p_sd );
114 static int ReadMetaData ( intf_thread_t *p_this );
115 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input );
116 void DeleteQueue ( audioscrobbler_queue_t *p_queue );
118 /*****************************************************************************
120 ****************************************************************************/
122 #define USERNAME_TEXT N_("Username")
123 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
124 #define PASSWORD_TEXT N_("Password")
125 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
127 /* if something goes wrong, we wait at least one minute before trying again */
128 #define DEFAULT_INTERVAL 60
130 /* last.fm client identifier */
131 #define CLIENT_NAME PACKAGE
132 #define CLIENT_VERSION VERSION
134 /* HTTP POST request : to submit data */
135 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
136 "Accept-Encoding: identity\n" \
137 "Content-length: %d\n" \
138 "Connection: close\n" \
139 "Content-type: application/x-www-form-urlencoded\n" \
141 "User-agent: VLC Media Player/%s\r\n" \
147 #define POST_DATA "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
148 "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
149 #define HTTPPOST_MAXLEN 2048
152 set_category( CAT_INTERFACE );
153 set_subcategory( SUBCAT_INTERFACE_CONTROL );
154 set_shortname( N_( "Audioscrobbler" ) );
155 set_description( N_("Audioscrobbler submission Plugin") );
156 add_string( "lastfm-username", "", NULL,
157 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
158 add_string( "lastfm-password", "", NULL,
159 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
160 set_capability( "interface", 0 );
161 set_callbacks( Open, Close );
164 /*****************************************************************************
165 * Open: initialize and create stuff
166 *****************************************************************************/
168 static int Open( vlc_object_t *p_this )
170 playlist_t *p_playlist;
171 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
172 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
175 free( p_sys->p_current_song ); \
176 free( p_sys->p_first_queue ); \
177 free( p_sys->psz_response_md5 ); \
187 vlc_mutex_init( p_this, &p_sys->lock );
189 p_sys_global = p_sys;
190 p_sys->psz_submit_host = NULL;
191 p_sys->psz_submit_file = NULL;
192 p_sys->b_handshaked = VLC_FALSE;
193 p_sys->i_interval = 0;
194 p_sys->time_last_interval = time( NULL );
195 p_sys->psz_username = NULL;
196 p_sys->b_paused = VLC_FALSE;
198 #define MALLOC_CHECK( a ) \
200 vlc_mutex_destroy( &p_sys->lock ); \
204 /* md5 response is 32 chars, + final \0 */
205 p_sys->psz_response_md5 = malloc( 33 );
206 MALLOC_CHECK( p_sys->psz_response_md5 )
208 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
209 MALLOC_CHECK( p_sys->p_first_queue )
211 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
212 MALLOC_CHECK( p_sys->p_current_song )
214 /* queues can't contain more than 10 songs */
215 p_sys->p_first_queue->p_queue =
216 malloc( 10 * sizeof( audioscrobbler_song_t ) );
217 MALLOC_CHECK( p_sys->p_current_song )
219 p_sys->p_first_queue->i_songs_nb = 0;
220 p_sys->p_first_queue->p_next_queue = NULL;
222 p_playlist = pl_Yield( p_intf );
224 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
226 pl_Release( p_playlist );
228 p_intf->pf_run = Run;
235 /*****************************************************************************
236 * Close: destroy interface stuff
237 *****************************************************************************/
238 static void Close( vlc_object_t *p_this )
240 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
241 playlist_t *p_playlist;
242 input_thread_t *p_input;
243 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
244 intf_sys_t *p_sys = p_intf->p_sys;
246 p_playlist = pl_Yield( p_intf );
248 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
250 p_input = p_playlist->p_input;
253 vlc_object_yield( p_input );
254 var_DelCallback( p_input, "state", PlayingChange, p_intf );
255 vlc_object_release( p_input );
259 pl_Release( p_playlist );
261 vlc_mutex_lock ( &p_sys->lock );
262 p_current_queue = p_sys->p_first_queue;
263 vlc_mutex_unlock ( &p_sys->lock );
265 while( ( p_current_queue->i_songs_nb == 10 ) &&
266 ( p_current_queue->p_next_queue != NULL ) )
268 p_next_queue = p_current_queue->p_next_queue;
269 DeleteQueue( p_current_queue );
270 free( p_current_queue );
271 p_current_queue = p_next_queue;
274 DeleteQueue( p_current_queue );
275 free( p_current_queue );
277 vlc_mutex_lock ( &p_sys->lock );
278 free( p_sys->psz_username );
279 free( p_sys->p_current_song );
280 free( p_sys->psz_submit_host );
281 free( p_sys->psz_submit_file );
282 free( p_sys->psz_response_md5 );
283 vlc_mutex_unlock ( &p_sys->lock );
284 vlc_mutex_destroy( &p_sys->lock );
288 /*****************************************************************************
289 * Run : call Handshake() then submit songs
290 *****************************************************************************/
291 static void Run( intf_thread_t *p_this )
293 char *psz_submit = NULL;
294 char *psz_submit_song = NULL;
297 playlist_t *p_playlist;
298 uint8_t *p_buffer = NULL;
299 char *p_buffer_pos = NULL;
300 audioscrobbler_queue_t *p_first_queue;
302 /* TODO: remove when meta_engine works */
305 p_this->p_sys = p_sys_global;
306 intf_sys_t *p_sys = p_this->p_sys;
309 free( psz_submit ); \
310 free( psz_submit_song ); \
312 msg_Err( p_this, "Out of memory" ); \
315 psz_submit = malloc( HTTPPOST_MAXLEN );
316 psz_submit_song = malloc( HTTPPOST_MAXLEN );
317 p_buffer = ( uint8_t* ) malloc( 1024 );
319 if( !psz_submit || !psz_submit_song || !p_buffer )
325 while( !p_this->b_die )
327 /* verify if there is data to submit
328 * and if waiting interval is elapsed */
329 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
331 ( p_sys->time_last_interval + p_sys->i_interval ) ) )
333 /* handshake if needed */
334 if( p_sys->b_handshaked == VLC_FALSE )
336 msg_Dbg( p_this, "Handshaking with last.fm ..." );
338 switch( Handshake( p_this ) )
345 /* username not set */
346 vlc_mutex_unlock ( &p_sys->lock );
347 intf_UserFatal( p_this, VLC_FALSE,
348 _("Last.fm username not set"),
349 _("Please set an username or disable"
350 "audioscrobbler plugin, and then restart VLC.\n"
351 "Visit https://www.last.fm/join/ to get an account")
354 free( psz_submit_song );
360 msg_Dbg( p_this, "Handshake successfull :)" );
361 vlc_mutex_lock ( &p_sys->lock );
362 p_sys->b_handshaked = VLC_TRUE;
363 vlc_mutex_unlock ( &p_sys->lock );
368 /* protocol error : we'll try later */
369 vlc_mutex_lock ( &p_sys->lock );
370 p_sys->i_interval = DEFAULT_INTERVAL;
371 time( &p_sys->time_last_interval );
372 vlc_mutex_unlock ( &p_sys->lock );
377 msg_Dbg( p_this, "Going to submit some data..." );
378 vlc_mutex_lock ( &p_sys->lock );
380 snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
381 p_sys->psz_username, p_sys->psz_response_md5 );
383 /* forge the HTTP POST request */
384 for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
387 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
388 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
389 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
390 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
391 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
392 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
393 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
395 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
398 i_post_socket = net_ConnectTCP( p_this,
399 p_sys->psz_submit_host, p_sys->i_submit_port);
401 /* we transmit the data */
402 i_net_ret = net_Printf(
403 VLC_OBJECT(p_this), i_post_socket, NULL,
404 POST_REQUEST, p_sys->psz_submit_file,
405 strlen( psz_submit ), p_sys->psz_submit_file,
409 if ( i_net_ret == -1 )
411 /* If connection fails, we assume we must handshake again */
412 p_sys->i_interval = DEFAULT_INTERVAL;
413 time( &p_sys->time_last_interval );
414 p_sys->b_handshaked = VLC_FALSE;
415 vlc_mutex_unlock( &p_sys->lock );
419 memset( p_buffer, '\0', 1024 );
421 i_net_ret = net_Read( p_this, i_post_socket, NULL,
422 p_buffer, 1024, VLC_FALSE );
423 if ( i_net_ret <= 0 )
425 /* if we get no answer, something went wrong : try again */
426 vlc_mutex_unlock( &p_sys->lock );
430 net_Close( i_post_socket );
432 /* record interval */
433 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
436 p_sys->i_interval = atoi( p_buffer_pos +
437 strlen( "INTERVAL " ) );
438 time( &p_sys->time_last_interval );
441 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
444 /* woops, submission failed */
445 msg_Dbg( p_this, p_buffer_pos );
446 vlc_mutex_unlock ( &p_sys->lock );
450 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
453 msg_Dbg( p_this, "Authentification failed, handshaking again" );
454 p_sys->b_handshaked = VLC_FALSE;
455 vlc_mutex_unlock ( &p_sys->lock );
459 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
462 if ( p_sys->p_first_queue->i_songs_nb == 10 )
464 /* if there are more than one queue, delete the 1st */
465 p_first_queue = p_sys->p_first_queue->p_next_queue;
466 DeleteQueue( p_sys->p_first_queue );
467 free( p_sys->p_first_queue );
468 p_sys->p_first_queue = p_first_queue;
472 DeleteQueue( p_sys->p_first_queue );
473 p_sys->p_first_queue->i_songs_nb = 0;
475 msg_Dbg( p_this, "Submission successfull!" );
477 vlc_mutex_unlock ( &p_sys->lock );
478 } /* data transmission finished or skipped */
480 msleep( INTF_IDLE_SLEEP );
482 p_playlist = pl_Yield( p_this );
484 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
486 /* if we stopped, we won't submit playing song */
487 vlc_mutex_lock( &p_sys->lock );
488 p_sys->b_queued = VLC_TRUE;
489 p_sys->b_metadata_read = VLC_TRUE;
490 vlc_mutex_unlock( &p_sys->lock );
493 pl_Release( p_playlist );
495 vlc_mutex_lock( &p_sys->lock );
496 if( p_sys->b_metadata_read == VLC_FALSE )
498 /* we read the metadata of the playing song */
499 /* TODO: remove when meta_engine works */
500 time( &played_time );
501 played_time -= p_sys->p_current_song->time_playing;
502 played_time -= p_sys->time_total_pauses;
504 vlc_mutex_unlock( &p_sys->lock );
506 /* TODO: remove when meta_engine works */
507 if( played_time > 10 )
509 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
517 /* we add the playing song into the queue */
518 if( ( p_sys->b_queued == VLC_FALSE )
519 && ( p_sys->b_paused == VLC_FALSE ) )
521 vlc_mutex_unlock( &p_sys->lock );
522 if( AddToQueue( p_this ) == VLC_ENOMEM )
529 vlc_mutex_unlock( &p_sys->lock );
536 /*****************************************************************************
537 * PlayingChange: Playing status change callback
538 *****************************************************************************/
539 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
540 vlc_value_t oldval, vlc_value_t newval, void *p_data )
542 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
543 intf_sys_t *p_sys = p_intf->p_sys;
545 (void)p_this; (void)psz_var; (void)oldval;
547 /* don't bother if song has already been queued */
548 if( p_sys->b_queued == VLC_TRUE )
551 vlc_mutex_lock( &p_sys->lock );
553 if( newval.i_int == PAUSE_S )
555 time( &p_sys->time_pause );
556 p_sys->b_paused = VLC_TRUE;
559 else if( newval.i_int == PLAYING_S )
561 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
562 p_sys->b_paused = VLC_FALSE;
565 vlc_mutex_unlock( &p_sys->lock );
570 /*****************************************************************************
571 * ItemChange: Playlist item change callback
572 *****************************************************************************/
573 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
574 vlc_value_t oldval, vlc_value_t newval, void *p_data )
576 playlist_t *p_playlist;
577 input_thread_t *p_input = NULL;
581 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
582 intf_sys_t *p_sys = p_intf->p_sys;
583 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
585 p_playlist = pl_Yield( p_intf );
587 p_input = p_playlist->p_input;
592 pl_Release( p_playlist );
593 vlc_mutex_lock( &p_sys->lock );
595 p_sys->b_queued = VLC_TRUE;
596 p_sys->b_metadata_read = VLC_TRUE;
598 vlc_mutex_unlock( &p_sys->lock );
602 vlc_object_yield( p_input );
604 pl_Release( p_playlist );
606 var_AddCallback( p_input, "state", PlayingChange, p_intf );
608 vlc_mutex_lock ( &p_sys->lock );
610 /* reset pause counter */
611 p_sys->time_total_pauses = 0;
613 /* we'll read metadata when it's present */
614 p_sys->b_metadata_read = VLC_FALSE;
615 p_sys->b_waiting_meta = VLC_FALSE;
618 epoch_tm = gmtime( &epoch );
619 snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
620 epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
621 epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
623 p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
624 p_sys->p_current_song->time_playing = epoch;
626 p_sys->b_paused = ( p_input->b_dead || !p_input->input.p_item->psz_name )
627 ? VLC_TRUE : VLC_FALSE;
629 vlc_mutex_unlock( &p_sys->lock );
631 vlc_object_release( p_input );
635 /*****************************************************************************
636 * AddToQueue: Add the played song to the queue to be submitted
637 *****************************************************************************/
638 static int AddToQueue ( intf_thread_t *p_this )
642 audioscrobbler_queue_t *p_queue = NULL,
643 *p_next_queue = NULL;
644 intf_sys_t *p_sys = p_this->p_sys;
646 /* wait for the user to listen enough before submitting */
647 time ( &played_time );
648 vlc_mutex_lock( &p_sys->lock );
650 played_time -= p_sys->p_current_song->time_playing;
651 played_time -= p_sys->time_total_pauses;
653 #define NO_SUBMISSION \
654 p_sys->b_queued = VLC_TRUE; \
655 vlc_mutex_unlock( &p_sys->lock ); \
658 if( ( played_time < 240 ) &&
659 ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
661 //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
662 vlc_mutex_unlock( &p_sys->lock );
666 if( p_sys->p_current_song->i_l < 30 )
668 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
672 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
674 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
678 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
680 /* go to last queue */
681 p_queue = p_sys->p_first_queue;
682 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
683 p_queue = p_queue->p_next_queue;
685 i_songs_nb = p_queue->i_songs_nb;
687 #define MALLOC_CHECK( a ) \
689 vlc_mutex_unlock( &p_sys->lock ); \
693 if( i_songs_nb == 10 )
695 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
696 MALLOC_CHECK( p_next_queue );
697 p_queue->p_next_queue = p_next_queue;
698 p_queue = p_next_queue;
700 p_queue->i_songs_nb = i_songs_nb;
703 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
704 MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
706 #define QUEUE_COPY( a ) \
707 p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
716 p_queue->i_songs_nb++;
717 p_sys->b_queued = VLC_TRUE;
719 vlc_mutex_unlock( &p_sys->lock );
727 /*****************************************************************************
728 * Handshake : Init audioscrobbler connection
729 *****************************************************************************/
730 static int Handshake( intf_thread_t *p_this )
732 char *psz_password = NULL;
733 struct md5_s *p_struct_md5 = NULL;
734 char *psz_password_md5 = NULL;
735 char *ps_challenge_md5 = NULL;
737 char *psz_handshake_url = NULL;
738 uint8_t *p_buffer = NULL;
739 char *p_buffer_pos = NULL;
740 char *psz_url_parser = NULL;
741 char *psz_buffer_substring;
744 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
745 intf_sys_t *p_sys = p_this->p_sys;
747 vlc_mutex_lock ( &p_sys->lock );
751 free( p_struct_md5 ); \
752 free( ps_challenge_md5 ); \
753 vlc_mutex_unlock( &p_sys->lock ); \
756 #define PROTOCOL_ERROR \
758 vlc_mutex_unlock( &p_sys->lock ); \
761 #define MALLOC_CHECK( a ) \
767 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
768 MALLOC_CHECK( p_sys->psz_username )
770 /* username has not been setup, ignoring */
771 if ( !*p_sys->psz_username )
774 psz_handshake_url = malloc( 1024 );
775 MALLOC_CHECK( p_sys->psz_username )
777 snprintf( psz_handshake_url, 1024,
778 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
779 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
781 /* send the http handshake request */
782 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
784 free( psz_handshake_url );
788 vlc_mutex_unlock ( &p_sys->lock );
792 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
795 stream_Delete( p_stream );
800 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
802 stream_Delete( p_stream );
806 stream_Delete( p_stream );
808 /* record interval before next submission */
809 p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
812 p_sys->i_interval = atoi( p_buffer_pos + strlen( "INTERVAL " ) );
813 time( &p_sys->time_last_interval );
816 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
819 /* handshake request failed, sorry */
820 msg_Dbg( p_this, p_buffer_pos );
824 p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
827 /* username does not exist on the server */
828 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
829 _("last.fm username is incorrect, please verify your settings")
834 p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
837 /* protocol has been updated, time to update the code */
838 msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
839 msg_Dbg( p_intf, p_buffer_pos );
844 p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
847 msg_Dbg( p_intf, "Can't recognize server protocol" );
852 psz_buffer_substring = strstr( p_buffer_pos, "\n" );
853 if( ( psz_buffer_substring == NULL ) || \
854 ( strlen( psz_buffer_substring + 1 ) < 32 ) )
856 msg_Dbg( p_intf, "Can't recognize server protocol" );
861 ps_challenge_md5 = malloc( 32 );
862 MALLOC_CHECK( ps_challenge_md5 )
863 memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
866 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
868 /* free old information */
869 free( p_sys->psz_submit_host );
870 free( p_sys->psz_submit_file );
872 psz_url_parser = p_buffer_pos + strlen( "http://" );
873 i_url_pos = strcspn( psz_url_parser, ":" );
875 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
876 MALLOC_CHECK( p_sys->psz_submit_host )
878 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
880 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
881 i_url_pos = strcspn( psz_url_parser, "\n" );
882 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
883 MALLOC_CHECK( p_sys->psz_submit_file )
887 p_struct_md5 = malloc( sizeof( struct md5_s ) );
888 MALLOC_CHECK( p_struct_md5 )
890 psz_password = config_GetPsz(p_this, "lastfm-password");
891 MALLOC_CHECK( psz_password )
893 /* generates a md5 hash of the password */
894 InitMD5( p_struct_md5 );
895 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
896 EndMD5( p_struct_md5 );
898 free( psz_password );
900 psz_password_md5 = malloc ( 33 );
901 MALLOC_CHECK( psz_password_md5 )
903 for ( i = 0; i < 4; i++ )
905 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
906 p_struct_md5->p_digest[i] & 0xff,
907 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
908 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
909 p_struct_md5->p_digest[i] >> 24
913 /* generates a md5 hash of :
914 * - md5 hash of the password, plus
915 * - md5 challenge sent by last.fm server
917 InitMD5( p_struct_md5 );
918 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
919 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
920 EndMD5( p_struct_md5 );
922 free( ps_challenge_md5 );
923 free( psz_password_md5 );
925 for ( i = 0; i < 4; i++ )
927 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
928 p_struct_md5->p_digest[i] & 0xff,
929 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
930 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
931 p_struct_md5->p_digest[i] >> 24
935 p_sys->psz_response_md5[32] = '\0';
937 vlc_mutex_unlock ( &p_sys->lock );
941 #undef PROTOCOL_ERROR
945 /*****************************************************************************
946 * DeleteQueue : Free all songs from an audioscrobbler_queue_t
947 *****************************************************************************/
948 void DeleteQueue( audioscrobbler_queue_t *p_queue )
952 for( i = 0; i < p_queue->i_songs_nb; i++ )
954 free( p_queue->p_queue[i]->psz_a );
955 free( p_queue->p_queue[i]->psz_b );
956 free( p_queue->p_queue[i]->psz_t );
957 free( p_queue->p_queue[i]->psz_i );
958 free( p_queue->p_queue[i] );
962 /*****************************************************************************
963 * ReadMetaData : Read meta data when parsed by vlc
964 * or wait for fetching if unavailable
965 *****************************************************************************/
966 static int ReadMetaData( intf_thread_t *p_this )
968 playlist_t *p_playlist;
969 input_thread_t *p_input = NULL;
970 vlc_value_t video_val;
972 intf_sys_t *p_sys = p_this->p_sys;
974 p_playlist = pl_Yield( p_this );
976 p_input = p_playlist->p_input;
981 pl_Release( p_playlist );
982 return( VLC_SUCCESS );
985 vlc_object_yield( p_input );
987 pl_Release( p_playlist );
989 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
990 if( ( video_val.i_int > 0 ) || \
991 ( p_input->input.p_item->i_type == ITEM_TYPE_NET ) )
993 msg_Dbg( p_this, "Not an audio only local file -> no submission");
994 vlc_object_release( p_input );
996 vlc_mutex_lock( &p_sys->lock );
997 p_sys->b_queued = VLC_TRUE;
998 p_sys->b_metadata_read = VLC_TRUE;
999 vlc_mutex_unlock( &p_sys->lock );
1004 return ReadLocalMetaData( p_this, p_input );
1007 /*****************************************************************************
1008 * ReadLocalMetaData : Puts current song's meta data in p_sys->p_current_song
1009 *****************************************************************************/
1010 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input )
1012 char *psz_title = NULL;
1013 char *psz_artist = NULL;
1014 char *psz_album = NULL;
1015 char *psz_trackid = NULL;
1017 vlc_bool_t b_waiting;
1018 intf_sys_t *p_sys = p_this->p_sys;
1021 i_status = p_input->input.p_item->p_meta->i_status;
1023 #define FREE_INPUT_AND_CHARS \
1024 vlc_object_release( p_input ); \
1025 free( psz_title ); \
1026 free( psz_artist ); \
1027 free( psz_album ); \
1028 free( psz_trackid );
1030 #define WAIT_METADATA_FETCHING( a ) \
1031 if ( b_waiting == VLC_TRUE ) \
1033 a = calloc( 1, 1 ); \
1037 vlc_object_release( p_input ); \
1038 vlc_mutex_lock( &p_sys->lock ); \
1039 p_sys->b_waiting_meta = VLC_TRUE; \
1040 vlc_mutex_unlock( &p_sys->lock ); \
1041 free( psz_artist ); \
1042 return VLC_SUCCESS; \
1045 #define ALLOC_ITEM_META( a, b ) \
1046 if ( p_input->input.p_item->b ) \
1048 a = encode_URI_component( \
1049 p_input->input.p_item->b ); \
1052 FREE_INPUT_AND_CHARS \
1053 return VLC_ENOMEM; \
1057 vlc_mutex_lock( &p_sys->lock );
1058 b_waiting = p_sys->b_waiting_meta;
1059 vlc_mutex_unlock( &p_sys->lock );
1061 /* TODO : replace 1 with ( i_status & ITEM_PREPARSED )
1062 * when meta_engine works */
1063 if ( ( b_waiting == VLC_FALSE ) ? 1 : ( i_status & ITEM_META_FETCHED ) )
1065 ALLOC_ITEM_META( psz_artist, p_meta->psz_artist )
1068 msg_Dbg( p_this, "No artist.." );
1069 WAIT_METADATA_FETCHING( psz_artist )
1072 ALLOC_ITEM_META( psz_title, psz_name )
1075 msg_Dbg( p_this, "No track name.." );
1076 WAIT_METADATA_FETCHING( psz_title );
1079 ALLOC_ITEM_META( psz_album, p_meta->psz_album )
1081 psz_album = calloc( 1, 1 );
1083 ALLOC_ITEM_META( psz_trackid, p_meta->psz_trackid )
1085 psz_trackid = calloc( 1, 1 );
1087 i_length = p_input->input.p_item->i_duration / 1000000;
1089 vlc_mutex_lock ( &p_sys->lock );
1091 p_sys->p_current_song->psz_a = strdup( psz_artist );
1092 p_sys->p_current_song->psz_t = strdup( psz_title );
1093 p_sys->p_current_song->psz_b = strdup( psz_album );
1094 p_sys->p_current_song->psz_m = strdup( psz_trackid );
1095 p_sys->p_current_song->i_l = i_length;
1096 p_sys->b_queued = VLC_FALSE;
1097 p_sys->b_metadata_read = VLC_TRUE;
1099 vlc_mutex_unlock( &p_sys->lock );
1101 msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1103 FREE_INPUT_AND_CHARS
1106 vlc_object_release( p_input );
1108 #undef FREE_INPUT_AND_CHARS
1109 #undef ALLOC_ITEM_META
1110 #undef WAIT_METADATA_FETCHING