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
44 #include <vlc_interface.h>
47 #include <vlc_block.h>
48 #include <vlc_stream.h>
50 #include <vlc_network.h>
51 #include <vlc_interface.h>
52 #include <vlc_playlist.h>
54 /*****************************************************************************
56 *****************************************************************************/
58 /* Keeps track of metadata to be submitted */
59 typedef struct audioscrobbler_song_t
61 char *psz_a; /* track artist */
62 char *psz_t; /* track title */
63 char *psz_b; /* track album */
64 int i_l; /* track length */
65 char *psz_m; /* musicbrainz id */
66 char *psz_i; /* date */
67 time_t time_playing; /* date (epoch) */
68 } audioscrobbler_song_t;
71 /* Queue to be submitted to server, 10 songs max */
72 typedef struct audioscrobbler_queue_t
74 audioscrobbler_song_t **p_queue; /* contains up to 10 songs */
75 int i_songs_nb; /* number of songs */
76 void *p_next_queue; /* if queue full, pointer to next */
77 } audioscrobbler_queue_t;
81 audioscrobbler_queue_t *p_first_queue; /* 1st queue */
82 vlc_mutex_t lock; /* p_sys mutex */
84 /* data about audioscrobbler session */
85 int i_interval; /* last interval recorded */
86 time_t time_last_interval; /* when was it recorded ? */
87 char *psz_submit_host; /* where to submit data ? */
88 int i_submit_port; /* at which port ? */
89 char *psz_submit_file; /* in which file ? */
90 char *psz_username; /* last.fm username */
91 vlc_bool_t b_handshaked; /* did we handshake ? */
92 char *psz_response_md5; /* md5 response to use */
94 /* data about song currently playing */
95 audioscrobbler_song_t *p_current_song; /* song being played */
96 time_t time_pause; /* time when vlc paused */
97 time_t time_total_pauses; /* sum of time in pause */
98 vlc_bool_t b_queued; /* has it been queud ? */
99 vlc_bool_t b_metadata_read; /* did we read metadata ? */
100 vlc_bool_t b_paused; /* is vlc paused ? */
101 vlc_bool_t b_waiting_meta; /* we need fetched data? */
104 intf_sys_t *p_sys_global; /* to retrieve p_sys in Run() thread */
106 static int Open ( vlc_object_t * );
107 static void Close ( vlc_object_t * );
108 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 * );
113 static int AddToQueue ( intf_thread_t *p_this );
114 static int Handshake ( intf_thread_t *p_sd );
115 static int ReadMetaData ( intf_thread_t *p_this );
116 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input );
117 void DeleteQueue ( audioscrobbler_queue_t *p_queue );
119 /*****************************************************************************
121 ****************************************************************************/
123 #define USERNAME_TEXT N_("Username")
124 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
125 #define PASSWORD_TEXT N_("Password")
126 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
128 /* if something goes wrong, we wait at least one minute before trying again */
129 #define DEFAULT_INTERVAL 60
131 /* last.fm client identifier */
132 #define CLIENT_NAME PACKAGE
133 #define CLIENT_VERSION VERSION
135 /* HTTP POST request : to submit data */
136 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
137 "Accept-Encoding: identity\n" \
138 "Content-length: %d\n" \
139 "Connection: close\n" \
140 "Content-type: application/x-www-form-urlencoded\n" \
142 "User-agent: VLC Media Player/%s\r\n" \
148 #define POST_DATA "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
149 "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
150 #define HTTPPOST_MAXLEN 2048
153 set_category( CAT_INTERFACE );
154 set_subcategory( SUBCAT_INTERFACE_CONTROL );
155 set_shortname( N_( "Audioscrobbler" ) );
156 set_description( N_("Audioscrobbler submission Plugin") );
157 add_string( "lastfm-username", "", NULL,
158 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
159 add_string( "lastfm-password", "", NULL,
160 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
161 set_capability( "interface", 0 );
162 set_callbacks( Open, Close );
165 /*****************************************************************************
166 * Open: initialize and create stuff
167 *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
171 playlist_t *p_playlist;
172 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
173 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
176 free( p_sys->p_current_song ); \
177 free( p_sys->p_first_queue ); \
178 free( p_sys->psz_response_md5 ); \
188 vlc_mutex_init( p_this, &p_sys->lock );
190 p_sys_global = p_sys;
191 p_sys->psz_submit_host = NULL;
192 p_sys->psz_submit_file = NULL;
193 p_sys->b_handshaked = VLC_FALSE;
194 p_sys->i_interval = 0;
195 p_sys->time_last_interval = time( NULL );
196 p_sys->psz_username = NULL;
197 p_sys->b_paused = VLC_FALSE;
199 #define MALLOC_CHECK( a ) \
201 vlc_mutex_destroy( &p_sys->lock ); \
205 /* md5 response is 32 chars, + final \0 */
206 p_sys->psz_response_md5 = malloc( 33 );
207 MALLOC_CHECK( p_sys->psz_response_md5 )
209 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
210 MALLOC_CHECK( p_sys->p_first_queue )
212 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
213 MALLOC_CHECK( p_sys->p_current_song )
215 /* queues can't contain more than 10 songs */
216 p_sys->p_first_queue->p_queue =
217 malloc( 10 * sizeof( audioscrobbler_song_t ) );
218 MALLOC_CHECK( p_sys->p_current_song )
220 p_sys->p_first_queue->i_songs_nb = 0;
221 p_sys->p_first_queue->p_next_queue = NULL;
223 p_playlist = pl_Yield( p_intf );
225 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
227 pl_Release( p_playlist );
229 p_intf->pf_run = Run;
236 /*****************************************************************************
237 * Close: destroy interface stuff
238 *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
241 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
242 playlist_t *p_playlist;
243 input_thread_t *p_input;
244 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
245 intf_sys_t *p_sys = p_intf->p_sys;
247 p_playlist = pl_Yield( p_intf );
249 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
251 p_input = p_playlist->p_input;
254 vlc_object_yield( p_input );
255 var_DelCallback( p_input, "state", PlayingChange, p_intf );
256 vlc_object_release( p_input );
260 pl_Release( p_playlist );
262 vlc_mutex_lock ( &p_sys->lock );
263 p_current_queue = p_sys->p_first_queue;
264 vlc_mutex_unlock ( &p_sys->lock );
266 while( ( p_current_queue->i_songs_nb == 10 ) &&
267 ( p_current_queue->p_next_queue != NULL ) )
269 p_next_queue = p_current_queue->p_next_queue;
270 DeleteQueue( p_current_queue );
271 free( p_current_queue );
272 p_current_queue = p_next_queue;
275 DeleteQueue( p_current_queue );
276 free( p_current_queue );
278 vlc_mutex_lock ( &p_sys->lock );
279 free( p_sys->psz_username );
280 free( p_sys->p_current_song );
281 free( p_sys->psz_submit_host );
282 free( p_sys->psz_submit_file );
283 free( p_sys->psz_response_md5 );
284 vlc_mutex_unlock ( &p_sys->lock );
285 vlc_mutex_destroy( &p_sys->lock );
289 /*****************************************************************************
290 * Run : call Handshake() then submit songs
291 *****************************************************************************/
292 static void Run( intf_thread_t *p_this )
294 char *psz_submit = NULL;
295 char *psz_submit_song = NULL;
298 playlist_t *p_playlist;
299 uint8_t *p_buffer = NULL;
300 char *p_buffer_pos = NULL;
301 audioscrobbler_queue_t *p_first_queue;
303 /* TODO: remove when meta_engine works */
306 p_this->p_sys = p_sys_global;
307 intf_sys_t *p_sys = p_this->p_sys;
310 free( psz_submit ); \
311 free( psz_submit_song ); \
313 msg_Err( p_this, "Out of memory" ); \
316 psz_submit = malloc( HTTPPOST_MAXLEN );
317 psz_submit_song = malloc( HTTPPOST_MAXLEN );
318 p_buffer = ( uint8_t* ) malloc( 1024 );
320 if( !psz_submit || !psz_submit_song || !p_buffer )
326 while( !p_this->b_die )
328 /* verify if there is data to submit
329 * and if waiting interval is elapsed */
330 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
332 ( p_sys->time_last_interval + p_sys->i_interval ) ) )
334 /* handshake if needed */
335 if( p_sys->b_handshaked == VLC_FALSE )
337 msg_Dbg( p_this, "Handshaking with last.fm ..." );
339 switch( Handshake( p_this ) )
346 /* username not set */
347 vlc_mutex_unlock ( &p_sys->lock );
348 intf_UserFatal( p_this, VLC_FALSE,
349 _("Last.fm username not set"),
350 _("Please set an username or disable"
351 "audioscrobbler plugin, and then restart VLC.\n"
352 "Visit https://www.last.fm/join/ to get an account")
355 free( psz_submit_song );
361 msg_Dbg( p_this, "Handshake successfull :)" );
362 vlc_mutex_lock ( &p_sys->lock );
363 p_sys->b_handshaked = VLC_TRUE;
364 vlc_mutex_unlock ( &p_sys->lock );
369 /* protocol error : we'll try later */
370 vlc_mutex_lock ( &p_sys->lock );
371 p_sys->i_interval = DEFAULT_INTERVAL;
372 time( &p_sys->time_last_interval );
373 vlc_mutex_unlock ( &p_sys->lock );
378 msg_Dbg( p_this, "Going to submit some data..." );
379 vlc_mutex_lock ( &p_sys->lock );
381 snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
382 p_sys->psz_username, p_sys->psz_response_md5 );
384 /* forge the HTTP POST request */
385 for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
388 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
389 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
390 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
391 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
392 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
393 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
394 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
396 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
399 i_post_socket = net_ConnectTCP( p_this,
400 p_sys->psz_submit_host, p_sys->i_submit_port);
402 /* we transmit the data */
403 i_net_ret = net_Printf(
404 VLC_OBJECT(p_this), i_post_socket, NULL,
405 POST_REQUEST, p_sys->psz_submit_file,
406 strlen( psz_submit ), p_sys->psz_submit_file,
410 if ( i_net_ret == -1 )
412 /* If connection fails, we assume we must handshake again */
413 p_sys->i_interval = DEFAULT_INTERVAL;
414 time( &p_sys->time_last_interval );
415 p_sys->b_handshaked = VLC_FALSE;
416 vlc_mutex_unlock( &p_sys->lock );
420 memset( p_buffer, '\0', 1024 );
422 i_net_ret = net_Read( p_this, i_post_socket, NULL,
423 p_buffer, 1024, VLC_FALSE );
424 if ( i_net_ret <= 0 )
426 /* if we get no answer, something went wrong : try again */
427 vlc_mutex_unlock( &p_sys->lock );
431 net_Close( i_post_socket );
433 /* record interval */
434 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
437 p_sys->i_interval = atoi( p_buffer_pos +
438 strlen( "INTERVAL " ) );
439 time( &p_sys->time_last_interval );
442 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
445 /* woops, submission failed */
446 msg_Dbg( p_this, p_buffer_pos );
447 vlc_mutex_unlock ( &p_sys->lock );
451 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
454 msg_Dbg( p_this, "Authentification failed, handshaking again" );
455 p_sys->b_handshaked = VLC_FALSE;
456 vlc_mutex_unlock ( &p_sys->lock );
460 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
463 if ( p_sys->p_first_queue->i_songs_nb == 10 )
465 /* if there are more than one queue, delete the 1st */
466 p_first_queue = p_sys->p_first_queue->p_next_queue;
467 DeleteQueue( p_sys->p_first_queue );
468 free( p_sys->p_first_queue );
469 p_sys->p_first_queue = p_first_queue;
473 DeleteQueue( p_sys->p_first_queue );
474 p_sys->p_first_queue->i_songs_nb = 0;
476 msg_Dbg( p_this, "Submission successfull!" );
478 vlc_mutex_unlock ( &p_sys->lock );
479 } /* data transmission finished or skipped */
481 msleep( INTF_IDLE_SLEEP );
483 p_playlist = pl_Yield( p_this );
485 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
487 /* if we stopped, we won't submit playing song */
488 vlc_mutex_lock( &p_sys->lock );
489 p_sys->b_queued = VLC_TRUE;
490 p_sys->b_metadata_read = VLC_TRUE;
491 vlc_mutex_unlock( &p_sys->lock );
494 pl_Release( p_playlist );
496 vlc_mutex_lock( &p_sys->lock );
497 if( p_sys->b_metadata_read == VLC_FALSE )
499 /* we read the metadata of the playing song */
500 /* TODO: remove when meta_engine works */
501 time( &played_time );
502 played_time -= p_sys->p_current_song->time_playing;
503 played_time -= p_sys->time_total_pauses;
505 vlc_mutex_unlock( &p_sys->lock );
507 /* TODO: remove when meta_engine works */
508 if( played_time > 10 )
510 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
518 /* we add the playing song into the queue */
519 if( ( p_sys->b_queued == VLC_FALSE )
520 && ( p_sys->b_paused == VLC_FALSE ) )
522 vlc_mutex_unlock( &p_sys->lock );
523 if( AddToQueue( p_this ) == VLC_ENOMEM )
530 vlc_mutex_unlock( &p_sys->lock );
537 /*****************************************************************************
538 * PlayingChange: Playing status change callback
539 *****************************************************************************/
540 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
541 vlc_value_t oldval, vlc_value_t newval, void *p_data )
543 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
544 intf_sys_t *p_sys = p_intf->p_sys;
546 (void)p_this; (void)psz_var; (void)oldval;
548 /* don't bother if song has already been queued */
549 if( p_sys->b_queued == VLC_TRUE )
552 vlc_mutex_lock( &p_sys->lock );
554 if( newval.i_int == PAUSE_S )
556 time( &p_sys->time_pause );
557 p_sys->b_paused = VLC_TRUE;
560 else if( newval.i_int == PLAYING_S )
562 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
563 p_sys->b_paused = VLC_FALSE;
566 vlc_mutex_unlock( &p_sys->lock );
571 /*****************************************************************************
572 * ItemChange: Playlist item change callback
573 *****************************************************************************/
574 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
575 vlc_value_t oldval, vlc_value_t newval, void *p_data )
577 playlist_t *p_playlist;
578 input_thread_t *p_input = NULL;
582 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
583 intf_sys_t *p_sys = p_intf->p_sys;
584 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
586 p_playlist = pl_Yield( p_intf );
588 p_input = p_playlist->p_input;
593 pl_Release( p_playlist );
594 vlc_mutex_lock( &p_sys->lock );
596 p_sys->b_queued = VLC_TRUE;
597 p_sys->b_metadata_read = VLC_TRUE;
599 vlc_mutex_unlock( &p_sys->lock );
603 vlc_object_yield( p_input );
605 pl_Release( p_playlist );
607 var_AddCallback( p_input, "state", PlayingChange, p_intf );
609 vlc_mutex_lock ( &p_sys->lock );
611 /* reset pause counter */
612 p_sys->time_total_pauses = 0;
614 /* we'll read metadata when it's present */
615 p_sys->b_metadata_read = VLC_FALSE;
616 p_sys->b_waiting_meta = VLC_FALSE;
619 epoch_tm = gmtime( &epoch );
620 snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
621 epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
622 epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
624 p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
625 p_sys->p_current_song->time_playing = epoch;
627 p_sys->b_paused = ( p_input->b_dead || !input_GetItem(p_input)->psz_name )
628 ? VLC_TRUE : VLC_FALSE;
630 vlc_mutex_unlock( &p_sys->lock );
632 vlc_object_release( p_input );
636 /*****************************************************************************
637 * AddToQueue: Add the played song to the queue to be submitted
638 *****************************************************************************/
639 static int AddToQueue ( intf_thread_t *p_this )
643 audioscrobbler_queue_t *p_queue = NULL,
644 *p_next_queue = NULL;
645 intf_sys_t *p_sys = p_this->p_sys;
647 /* wait for the user to listen enough before submitting */
648 time ( &played_time );
649 vlc_mutex_lock( &p_sys->lock );
651 played_time -= p_sys->p_current_song->time_playing;
652 played_time -= p_sys->time_total_pauses;
654 #define NO_SUBMISSION \
655 p_sys->b_queued = VLC_TRUE; \
656 vlc_mutex_unlock( &p_sys->lock ); \
659 if( ( played_time < 240 ) &&
660 ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
662 //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
663 vlc_mutex_unlock( &p_sys->lock );
667 if( p_sys->p_current_song->i_l < 30 )
669 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
673 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
675 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
679 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
681 /* go to last queue */
682 p_queue = p_sys->p_first_queue;
683 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
684 p_queue = p_queue->p_next_queue;
686 i_songs_nb = p_queue->i_songs_nb;
688 #define MALLOC_CHECK( a ) \
690 vlc_mutex_unlock( &p_sys->lock ); \
694 if( i_songs_nb == 10 )
696 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
697 MALLOC_CHECK( p_next_queue );
698 p_queue->p_next_queue = p_next_queue;
699 p_queue = p_next_queue;
701 p_queue->i_songs_nb = i_songs_nb;
704 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
705 MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
707 #define QUEUE_COPY( a ) \
708 p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
717 p_queue->i_songs_nb++;
718 p_sys->b_queued = VLC_TRUE;
720 vlc_mutex_unlock( &p_sys->lock );
728 /*****************************************************************************
729 * Handshake : Init audioscrobbler connection
730 *****************************************************************************/
731 static int Handshake( intf_thread_t *p_this )
733 char *psz_password = NULL;
734 struct md5_s *p_struct_md5 = NULL;
735 char *psz_password_md5 = NULL;
736 char *ps_challenge_md5 = NULL;
738 char *psz_handshake_url = NULL;
739 uint8_t *p_buffer = NULL;
740 char *p_buffer_pos = NULL;
741 char *psz_url_parser = NULL;
742 char *psz_buffer_substring;
745 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
746 intf_sys_t *p_sys = p_this->p_sys;
748 vlc_mutex_lock ( &p_sys->lock );
752 free( p_struct_md5 ); \
753 free( ps_challenge_md5 ); \
754 vlc_mutex_unlock( &p_sys->lock ); \
757 #define PROTOCOL_ERROR \
759 vlc_mutex_unlock( &p_sys->lock ); \
762 #define MALLOC_CHECK( a ) \
768 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
769 MALLOC_CHECK( p_sys->psz_username )
771 /* username has not been setup, ignoring */
772 if ( !*p_sys->psz_username )
775 psz_handshake_url = malloc( 1024 );
776 MALLOC_CHECK( p_sys->psz_username )
778 snprintf( psz_handshake_url, 1024,
779 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
780 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
782 /* send the http handshake request */
783 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
785 free( psz_handshake_url );
789 vlc_mutex_unlock ( &p_sys->lock );
793 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
796 stream_Delete( p_stream );
801 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
803 stream_Delete( p_stream );
807 stream_Delete( p_stream );
809 /* record interval before next submission */
810 p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
813 p_sys->i_interval = atoi( p_buffer_pos + strlen( "INTERVAL " ) );
814 time( &p_sys->time_last_interval );
817 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
820 /* handshake request failed, sorry */
821 msg_Dbg( p_this, p_buffer_pos );
825 p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
828 /* username does not exist on the server */
829 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
830 _("last.fm username is incorrect, please verify your settings")
835 p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
838 /* protocol has been updated, time to update the code */
839 msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
840 msg_Dbg( p_intf, p_buffer_pos );
845 p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
848 msg_Dbg( p_intf, "Can't recognize server protocol" );
853 psz_buffer_substring = strstr( p_buffer_pos, "\n" );
854 if( ( psz_buffer_substring == NULL ) || \
855 ( strlen( psz_buffer_substring + 1 ) < 32 ) )
857 msg_Dbg( p_intf, "Can't recognize server protocol" );
862 ps_challenge_md5 = malloc( 32 );
863 MALLOC_CHECK( ps_challenge_md5 )
864 memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
867 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
869 /* free old information */
870 free( p_sys->psz_submit_host );
871 free( p_sys->psz_submit_file );
873 psz_url_parser = p_buffer_pos + strlen( "http://" );
874 i_url_pos = strcspn( psz_url_parser, ":" );
876 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
877 MALLOC_CHECK( p_sys->psz_submit_host )
879 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
881 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
882 i_url_pos = strcspn( psz_url_parser, "\n" );
883 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
884 MALLOC_CHECK( p_sys->psz_submit_file )
888 p_struct_md5 = malloc( sizeof( struct md5_s ) );
889 MALLOC_CHECK( p_struct_md5 )
891 psz_password = config_GetPsz(p_this, "lastfm-password");
892 MALLOC_CHECK( psz_password )
894 /* generates a md5 hash of the password */
895 InitMD5( p_struct_md5 );
896 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
897 EndMD5( p_struct_md5 );
899 free( psz_password );
901 psz_password_md5 = malloc ( 33 );
902 MALLOC_CHECK( psz_password_md5 )
904 for ( i = 0; i < 4; i++ )
906 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
907 p_struct_md5->p_digest[i] & 0xff,
908 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
909 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
910 p_struct_md5->p_digest[i] >> 24
914 /* generates a md5 hash of :
915 * - md5 hash of the password, plus
916 * - md5 challenge sent by last.fm server
918 InitMD5( p_struct_md5 );
919 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
920 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
921 EndMD5( p_struct_md5 );
923 free( ps_challenge_md5 );
924 free( psz_password_md5 );
926 for ( i = 0; i < 4; i++ )
928 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
929 p_struct_md5->p_digest[i] & 0xff,
930 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
931 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
932 p_struct_md5->p_digest[i] >> 24
936 p_sys->psz_response_md5[32] = '\0';
938 vlc_mutex_unlock ( &p_sys->lock );
942 #undef PROTOCOL_ERROR
946 /*****************************************************************************
947 * DeleteQueue : Free all songs from an audioscrobbler_queue_t
948 *****************************************************************************/
949 void DeleteQueue( audioscrobbler_queue_t *p_queue )
953 for( i = 0; i < p_queue->i_songs_nb; i++ )
955 free( p_queue->p_queue[i]->psz_a );
956 free( p_queue->p_queue[i]->psz_b );
957 free( p_queue->p_queue[i]->psz_t );
958 free( p_queue->p_queue[i]->psz_i );
959 free( p_queue->p_queue[i] );
963 /*****************************************************************************
964 * ReadMetaData : Read meta data when parsed by vlc
965 * or wait for fetching if unavailable
966 *****************************************************************************/
967 static int ReadMetaData( intf_thread_t *p_this )
969 playlist_t *p_playlist;
970 input_thread_t *p_input = NULL;
971 vlc_value_t video_val;
973 intf_sys_t *p_sys = p_this->p_sys;
975 p_playlist = pl_Yield( p_this );
977 p_input = p_playlist->p_input;
982 pl_Release( p_playlist );
983 return( VLC_SUCCESS );
986 vlc_object_yield( p_input );
988 pl_Release( p_playlist );
990 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
991 if( ( video_val.i_int > 0 ) || \
992 ( input_GetItem(p_input)->i_type == ITEM_TYPE_NET ) )
994 msg_Dbg( p_this, "Not an audio only local file -> no submission");
995 vlc_object_release( p_input );
997 vlc_mutex_lock( &p_sys->lock );
998 p_sys->b_queued = VLC_TRUE;
999 p_sys->b_metadata_read = VLC_TRUE;
1000 vlc_mutex_unlock( &p_sys->lock );
1005 return ReadLocalMetaData( p_this, p_input );
1008 /*****************************************************************************
1009 * ReadLocalMetaData : Puts current song's meta data in p_sys->p_current_song
1010 *****************************************************************************/
1011 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input )
1013 char *psz_title = NULL;
1014 char *psz_artist = NULL;
1015 char *psz_album = NULL;
1016 char *psz_trackid = NULL;
1018 vlc_bool_t b_waiting;
1019 intf_sys_t *p_sys = p_this->p_sys;
1022 i_status = input_GetItem(p_input)->p_meta->i_status;
1024 #define FREE_INPUT_AND_CHARS \
1025 vlc_object_release( p_input ); \
1026 free( psz_title ); \
1027 free( psz_artist ); \
1028 free( psz_album ); \
1029 free( psz_trackid );
1031 #define WAIT_METADATA_FETCHING( a ) \
1032 if ( b_waiting == VLC_TRUE ) \
1034 a = calloc( 1, 1 ); \
1038 vlc_object_release( p_input ); \
1039 vlc_mutex_lock( &p_sys->lock ); \
1040 p_sys->b_waiting_meta = VLC_TRUE; \
1041 vlc_mutex_unlock( &p_sys->lock ); \
1042 free( psz_artist ); \
1043 return VLC_SUCCESS; \
1046 #define ALLOC_ITEM_META( a, b ) \
1047 if ( input_GetItem(p_input)->b ) \
1049 a = encode_URI_component( \
1050 input_GetItem(p_input)->b ); \
1053 FREE_INPUT_AND_CHARS \
1054 return VLC_ENOMEM; \
1058 vlc_mutex_lock( &p_sys->lock );
1059 b_waiting = p_sys->b_waiting_meta;
1060 vlc_mutex_unlock( &p_sys->lock );
1062 /* TODO : replace 1 with ( i_status & ITEM_PREPARSED )
1063 * when meta_engine works */
1064 if ( ( b_waiting == VLC_FALSE ) ? 1 : ( i_status & ITEM_META_FETCHED ) )
1066 ALLOC_ITEM_META( psz_artist, p_meta->psz_artist )
1069 msg_Dbg( p_this, "No artist.." );
1070 WAIT_METADATA_FETCHING( psz_artist )
1073 ALLOC_ITEM_META( psz_title, psz_name )
1076 msg_Dbg( p_this, "No track name.." );
1077 WAIT_METADATA_FETCHING( psz_title );
1080 ALLOC_ITEM_META( psz_album, p_meta->psz_album )
1082 psz_album = calloc( 1, 1 );
1084 ALLOC_ITEM_META( psz_trackid, p_meta->psz_trackid )
1086 psz_trackid = calloc( 1, 1 );
1088 i_length = input_GetItem(p_input)->i_duration / 1000000;
1090 vlc_mutex_lock ( &p_sys->lock );
1092 p_sys->p_current_song->psz_a = strdup( psz_artist );
1093 p_sys->p_current_song->psz_t = strdup( psz_title );
1094 p_sys->p_current_song->psz_b = strdup( psz_album );
1095 p_sys->p_current_song->psz_m = strdup( psz_trackid );
1096 p_sys->p_current_song->i_l = i_length;
1097 p_sys->b_queued = VLC_FALSE;
1098 p_sys->b_metadata_read = VLC_TRUE;
1100 vlc_mutex_unlock( &p_sys->lock );
1102 msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1104 FREE_INPUT_AND_CHARS
1107 vlc_object_release( p_input );
1109 #undef FREE_INPUT_AND_CHARS
1110 #undef ALLOC_ITEM_META
1111 #undef WAIT_METADATA_FETCHING