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 *****************************************************************************/
39 #include <vlc_interface.h>
42 #include <vlc_block.h>
43 #include <vlc_stream.h>
45 #include <vlc_network.h>
46 #include <vlc_interface.h>
47 #include <vlc_playlist.h>
49 /*****************************************************************************
51 *****************************************************************************/
53 /* Keeps track of metadata to be submitted */
54 typedef struct audioscrobbler_song_t
56 char *psz_a; /* track artist */
57 char *psz_t; /* track title */
58 char *psz_b; /* track album */
59 int i_l; /* track length */
60 char *psz_m; /* musicbrainz id */
61 char *psz_i; /* date */
62 time_t time_playing; /* date (epoch) */
63 } audioscrobbler_song_t;
66 /* Queue to be submitted to server, 10 songs max */
67 typedef struct audioscrobbler_queue_t
69 audioscrobbler_song_t **p_queue; /* contains up to 10 songs */
70 int i_songs_nb; /* number of songs */
71 void *p_next_queue; /* if queue full, pointer to next */
72 } audioscrobbler_queue_t;
76 audioscrobbler_queue_t *p_first_queue; /* 1st queue */
77 vlc_mutex_t lock; /* p_sys mutex */
79 /* data about audioscrobbler session */
80 time_t time_next_exchange; /* when can we send data? */
81 char *psz_submit_host; /* where to submit data ? */
82 int i_submit_port; /* at which port ? */
83 char *psz_submit_file; /* in which file ? */
84 char *psz_username; /* last.fm username */
85 vlc_bool_t b_handshaked; /* did we handshake ? */
86 char *psz_response_md5; /* md5 response to use */
88 /* data about song currently playing */
89 audioscrobbler_song_t *p_current_song; /* song being played */
90 time_t time_pause; /* time when vlc paused */
91 time_t time_total_pauses; /* sum of time in pause */
92 vlc_bool_t b_queued; /* has it been queud ? */
93 vlc_bool_t b_metadata_read; /* did we read metadata ? */
94 vlc_bool_t b_paused; /* is vlc paused ? */
95 vlc_bool_t b_waiting_meta; /* we need fetched data? */
98 intf_sys_t *p_sys_global; /* to retrieve p_sys in Run() thread */
100 static int Open ( vlc_object_t * );
101 static void Close ( vlc_object_t * );
102 static void Run ( intf_thread_t * );
103 static void Main ( intf_thread_t * );
104 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
105 vlc_value_t, void * );
106 static int PlayingChange( vlc_object_t *, const char *, vlc_value_t,
107 vlc_value_t, void * );
108 static int AddToQueue ( intf_thread_t *p_this );
109 static int Handshake ( intf_thread_t *p_sd );
110 static int ReadMetaData ( intf_thread_t *p_this );
111 void DeleteQueue ( audioscrobbler_queue_t *p_queue );
113 /*****************************************************************************
115 ****************************************************************************/
117 #define USERNAME_TEXT N_("Username")
118 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
119 #define PASSWORD_TEXT N_("Password")
120 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
122 /* if something goes wrong, we wait at least one minute before trying again */
123 #define DEFAULT_INTERVAL 60
125 /* last.fm client identifier */
126 #define CLIENT_NAME PACKAGE
127 #define CLIENT_VERSION VERSION
129 /* HTTP POST request : to submit data */
130 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
131 "Accept-Encoding: identity\n" \
132 "Content-length: %d\n" \
133 "Connection: close\n" \
134 "Content-type: application/x-www-form-urlencoded\n" \
136 "User-agent: VLC Media Player/%s\r\n" \
142 #define POST_DATA "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
143 "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
144 #define HTTPPOST_MAXLEN 2048
147 set_category( CAT_INTERFACE );
148 set_subcategory( SUBCAT_INTERFACE_CONTROL );
149 set_shortname( N_( "Audioscrobbler" ) );
150 set_description( N_("Audioscrobbler submission Plugin") );
151 add_string( "lastfm-username", "", NULL,
152 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
153 add_password( "lastfm-password", "", NULL,
154 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
155 set_capability( "interface", 0 );
156 set_callbacks( Open, Close );
159 /*****************************************************************************
160 * Open: initialize and create stuff
161 *****************************************************************************/
163 static int Open( vlc_object_t *p_this )
165 playlist_t *p_playlist;
166 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
167 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
170 free( p_sys->p_current_song ); \
171 free( p_sys->p_first_queue ); \
172 free( p_sys->psz_response_md5 ); \
181 p_intf->p_sys = p_sys;
183 vlc_mutex_init( p_this, &p_sys->lock );
185 p_sys_global = p_sys;
186 p_sys->psz_submit_host = NULL;
187 p_sys->psz_submit_file = NULL;
188 p_sys->b_handshaked = VLC_FALSE;
189 p_sys->time_next_exchange = time( NULL );
190 p_sys->psz_username = NULL;
191 p_sys->b_paused = VLC_FALSE;
193 #define MALLOC_CHECK( a ) \
195 vlc_mutex_destroy( &p_sys->lock ); \
199 /* md5 response is 32 chars, + final \0 */
200 p_sys->psz_response_md5 = malloc( 33 );
201 MALLOC_CHECK( p_sys->psz_response_md5 )
203 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
204 MALLOC_CHECK( p_sys->p_first_queue )
206 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
207 MALLOC_CHECK( p_sys->p_current_song )
208 time( &p_sys->p_current_song->time_playing );
210 /* queues can't contain more than 10 songs */
211 p_sys->p_first_queue->p_queue =
212 malloc( 10 * sizeof( audioscrobbler_song_t ) );
213 MALLOC_CHECK( p_sys->p_current_song )
215 p_sys->p_first_queue->i_songs_nb = 0;
216 p_sys->p_first_queue->p_next_queue = NULL;
218 p_playlist = pl_Yield( p_intf );
220 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
222 pl_Release( p_playlist );
224 p_intf->pf_run = Run;
231 /*****************************************************************************
232 * Close: destroy interface stuff
233 *****************************************************************************/
234 static void Close( vlc_object_t *p_this )
236 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
237 playlist_t *p_playlist;
238 input_thread_t *p_input;
239 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
240 intf_sys_t *p_sys = p_intf->p_sys;
242 p_playlist = pl_Yield( p_intf );
244 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
246 p_input = p_playlist->p_input;
249 vlc_object_yield( p_input );
250 var_DelCallback( p_input, "state", PlayingChange, p_intf );
251 vlc_object_release( p_input );
255 pl_Release( p_playlist );
257 vlc_mutex_lock ( &p_sys->lock );
258 p_current_queue = p_sys->p_first_queue;
259 vlc_mutex_unlock ( &p_sys->lock );
261 while( ( p_current_queue->i_songs_nb == 10 ) &&
262 ( p_current_queue->p_next_queue != NULL ) )
264 p_next_queue = p_current_queue->p_next_queue;
265 DeleteQueue( p_current_queue );
266 free( p_current_queue );
267 p_current_queue = p_next_queue;
270 DeleteQueue( p_current_queue );
271 free( p_current_queue );
273 vlc_mutex_lock ( &p_sys->lock );
274 free( p_sys->psz_username );
275 free( p_sys->p_current_song );
276 free( p_sys->psz_submit_host );
277 free( p_sys->psz_submit_file );
278 free( p_sys->psz_response_md5 );
279 vlc_mutex_unlock ( &p_sys->lock );
280 vlc_mutex_destroy( &p_sys->lock );
284 /****************************************************************************
285 * Run : create Main() thread
286 * **************************************************************************/
287 static void Run( intf_thread_t *p_intf )
289 if( vlc_thread_create( p_intf, "Audioscrobbler", Main, 0, VLC_TRUE ) )
290 msg_Err( p_intf, "failed to create Audioscrobbler thread" );
293 /*****************************************************************************
294 * Main : call Handshake() then submit songs
295 *****************************************************************************/
296 static void Main( intf_thread_t *p_this )
298 char *psz_submit = NULL;
299 char *psz_submit_song = NULL;
302 playlist_t *p_playlist;
303 uint8_t *p_buffer = NULL;
304 char *p_buffer_pos = NULL;
305 audioscrobbler_queue_t *p_first_queue;
309 vlc_thread_ready( p_this );
311 p_this->p_sys = p_sys_global;
312 intf_sys_t *p_sys = p_this->p_sys;
315 free( psz_submit ); \
316 free( psz_submit_song ); \
318 msg_Err( p_this, "Out of memory" ); \
321 psz_submit = malloc( HTTPPOST_MAXLEN );
322 psz_submit_song = malloc( HTTPPOST_MAXLEN );
323 p_buffer = ( uint8_t* ) malloc( 1024 );
325 if( !psz_submit || !psz_submit_song || !p_buffer )
331 while( !p_this->b_die )
333 /* verify if there is data to submit
334 * and if waiting interval is elapsed */
335 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
336 ( time( NULL ) >= p_sys->time_next_exchange ) )
338 /* handshake if needed */
339 if( p_sys->b_handshaked == VLC_FALSE )
341 msg_Dbg( p_this, "Handshaking with last.fm ..." );
343 switch( Handshake( p_this ) )
350 /* username not set */
351 vlc_mutex_unlock ( &p_sys->lock );
352 intf_UserFatal( p_this, VLC_FALSE,
353 _("Last.fm username not set"),
354 _("Please set an username or disable "
355 "audioscrobbler plugin, and then restart VLC.\n"
356 "Visit https://www.last.fm/join/ to get an account")
359 free( psz_submit_song );
365 msg_Dbg( p_this, "Handshake successfull :)" );
366 vlc_mutex_lock ( &p_sys->lock );
367 p_sys->b_handshaked = VLC_TRUE;
368 vlc_mutex_unlock ( &p_sys->lock );
373 /* protocol error : we'll try later */
374 vlc_mutex_lock ( &p_sys->lock );
375 time( &p_sys->time_next_exchange );
376 p_sys->time_next_exchange += DEFAULT_INTERVAL;
377 vlc_mutex_unlock ( &p_sys->lock );
380 /* handshake is done or failed, lets start from
381 * beginning to check it out and wait INTERVAL if needed
386 msg_Dbg( p_this, "Going to submit some data..." );
387 vlc_mutex_lock ( &p_sys->lock );
389 snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
390 p_sys->psz_username, p_sys->psz_response_md5 );
392 /* forge the HTTP POST request */
393 for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
396 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
397 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
398 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
399 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
400 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
401 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
402 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
404 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
407 i_post_socket = net_ConnectTCP( p_this,
408 p_sys->psz_submit_host, p_sys->i_submit_port);
410 if ( i_post_socket == -1 )
412 /* If connection fails, we assume we must handshake again */
413 time( &p_sys->time_next_exchange );
414 p_sys->time_next_exchange += DEFAULT_INTERVAL;
415 p_sys->b_handshaked = VLC_FALSE;
416 vlc_mutex_unlock( &p_sys->lock );
420 /* we transmit the data */
421 i_net_ret = net_Printf(
422 VLC_OBJECT(p_this), i_post_socket, NULL,
423 POST_REQUEST, p_sys->psz_submit_file,
424 strlen( psz_submit ), p_sys->psz_submit_file,
428 if ( i_net_ret == -1 )
430 /* If connection fails, we assume we must handshake again */
431 time( &p_sys->time_next_exchange );
432 p_sys->time_next_exchange += DEFAULT_INTERVAL;
433 p_sys->b_handshaked = VLC_FALSE;
434 vlc_mutex_unlock( &p_sys->lock );
438 memset( p_buffer, '\0', 1024 );
440 i_net_ret = net_Read( p_this, i_post_socket, NULL,
441 p_buffer, 1024, VLC_FALSE );
442 if ( i_net_ret <= 0 )
444 /* if we get no answer, something went wrong : try again */
445 vlc_mutex_unlock( &p_sys->lock );
449 net_Close( i_post_socket );
451 /* record interval */
452 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
455 time( &p_sys->time_next_exchange );
456 p_sys->time_next_exchange += atoi( p_buffer_pos +
457 strlen( "INTERVAL " ) );
460 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
463 /* woops, submission failed */
464 msg_Dbg( p_this, "%s", p_buffer_pos );
465 vlc_mutex_unlock ( &p_sys->lock );
469 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
472 msg_Dbg( p_this, "Authentification failed, handshaking again" );
473 p_sys->b_handshaked = VLC_FALSE;
474 vlc_mutex_unlock ( &p_sys->lock );
478 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
481 if ( p_sys->p_first_queue->i_songs_nb == 10 )
483 /* if there are more than one queue, delete the 1st */
484 p_first_queue = p_sys->p_first_queue->p_next_queue;
485 DeleteQueue( p_sys->p_first_queue );
486 free( p_sys->p_first_queue );
487 p_sys->p_first_queue = p_first_queue;
491 DeleteQueue( p_sys->p_first_queue );
492 p_sys->p_first_queue->i_songs_nb = 0;
494 msg_Dbg( p_this, "Submission successfull!" );
496 vlc_mutex_unlock ( &p_sys->lock );
497 } /* data transmission finished or skipped */
499 msleep( INTF_IDLE_SLEEP );
501 p_playlist = pl_Yield( p_this );
503 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
505 /* if we stopped, we won't submit playing song */
506 vlc_mutex_lock( &p_sys->lock );
507 p_sys->b_queued = VLC_TRUE;
508 p_sys->b_metadata_read = VLC_TRUE;
509 vlc_mutex_unlock( &p_sys->lock );
512 pl_Release( p_playlist );
514 vlc_mutex_lock( &p_sys->lock );
515 if( p_sys->b_metadata_read == VLC_FALSE )
517 /* we read the metadata of the playing song */
518 time( &played_time );
519 played_time -= p_sys->p_current_song->time_playing;
520 played_time -= p_sys->time_total_pauses;
522 vlc_mutex_unlock( &p_sys->lock );
524 if( played_time > 20 )
525 /* wait at least 20 secondes before reading the meta data
526 * since the songs must be at least 30s */
528 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
536 /* we add the playing song into the queue */
537 if( ( p_sys->b_queued == VLC_FALSE )
538 && ( p_sys->b_paused == VLC_FALSE ) )
540 vlc_mutex_unlock( &p_sys->lock );
541 if( AddToQueue( p_this ) == VLC_ENOMEM )
548 vlc_mutex_unlock( &p_sys->lock );
555 /*****************************************************************************
556 * PlayingChange: Playing status change callback
557 *****************************************************************************/
558 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
559 vlc_value_t oldval, vlc_value_t newval, void *p_data )
561 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
562 intf_sys_t *p_sys = p_intf->p_sys;
564 (void)p_this; (void)psz_var; (void)oldval;
566 /* don't bother if song has already been queued */
567 if( p_sys->b_queued == VLC_TRUE )
570 vlc_mutex_lock( &p_sys->lock );
572 if( newval.i_int == PAUSE_S )
574 time( &p_sys->time_pause );
575 p_sys->b_paused = VLC_TRUE;
578 else if( newval.i_int == PLAYING_S )
580 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
581 p_sys->b_paused = VLC_FALSE;
584 vlc_mutex_unlock( &p_sys->lock );
589 /*****************************************************************************
590 * ItemChange: Playlist item change callback
591 *****************************************************************************/
592 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
593 vlc_value_t oldval, vlc_value_t newval, void *p_data )
595 playlist_t *p_playlist;
596 input_thread_t *p_input = NULL;
600 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
601 intf_sys_t *p_sys = p_intf->p_sys;
602 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
604 p_playlist = pl_Yield( p_intf );
606 p_input = p_playlist->p_input;
611 pl_Release( p_playlist );
613 vlc_mutex_lock( &p_sys->lock );
615 p_sys->b_queued = VLC_TRUE;
616 p_sys->b_metadata_read = VLC_TRUE;
618 vlc_mutex_unlock( &p_sys->lock );
623 vlc_object_yield( p_input );
625 pl_Release( p_playlist );
627 var_AddCallback( p_input, "state", PlayingChange, p_intf );
629 vlc_mutex_lock ( &p_sys->lock );
631 /* reset pause counter */
632 p_sys->time_total_pauses = 0;
634 /* we'll read metadata when it's present */
635 p_sys->b_metadata_read = VLC_FALSE;
636 p_sys->b_waiting_meta = VLC_FALSE;
639 epoch_tm = gmtime( &epoch );
640 snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
641 epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
642 epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
644 p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
645 p_sys->p_current_song->time_playing = epoch;
647 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
648 p_sys->b_paused = ( p_input->b_dead || !psz_name )
649 ? VLC_TRUE : VLC_FALSE;
652 vlc_mutex_unlock( &p_sys->lock );
654 vlc_object_release( p_input );
658 /*****************************************************************************
659 * AddToQueue: Add the played song to the queue to be submitted
660 *****************************************************************************/
661 static int AddToQueue ( intf_thread_t *p_this )
665 audioscrobbler_queue_t *p_queue = NULL,
666 *p_next_queue = NULL;
667 intf_sys_t *p_sys = p_this->p_sys;
669 /* wait for the user to listen enough before submitting */
670 time ( &played_time );
671 vlc_mutex_lock( &p_sys->lock );
673 played_time -= p_sys->p_current_song->time_playing;
674 played_time -= p_sys->time_total_pauses;
676 #define NO_SUBMISSION \
677 p_sys->b_queued = VLC_TRUE; \
678 vlc_mutex_unlock( &p_sys->lock ); \
681 if( ( played_time < 240 ) &&
682 ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
684 //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
685 vlc_mutex_unlock( &p_sys->lock );
689 if( p_sys->p_current_song->i_l < 30 )
691 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
695 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
697 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
701 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
703 /* go to last queue */
704 p_queue = p_sys->p_first_queue;
705 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
706 p_queue = p_queue->p_next_queue;
708 i_songs_nb = p_queue->i_songs_nb;
710 #define MALLOC_CHECK( a ) \
712 vlc_mutex_unlock( &p_sys->lock ); \
716 if( i_songs_nb == 10 )
718 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
719 MALLOC_CHECK( p_next_queue );
720 p_queue->p_next_queue = p_next_queue;
721 p_queue = p_next_queue;
723 p_queue->i_songs_nb = i_songs_nb;
726 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
727 MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
729 #define QUEUE_COPY( a ) \
730 p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
739 p_queue->i_songs_nb++;
740 p_sys->b_queued = VLC_TRUE;
742 vlc_mutex_unlock( &p_sys->lock );
750 /*****************************************************************************
751 * Handshake : Init audioscrobbler connection
752 *****************************************************************************/
753 static int Handshake( intf_thread_t *p_this )
755 char *psz_password = NULL;
756 struct md5_s *p_struct_md5 = NULL;
757 char *psz_password_md5 = NULL;
758 char *ps_challenge_md5 = NULL;
760 char *psz_handshake_url = NULL;
761 uint8_t *p_buffer = NULL;
762 char *p_buffer_pos = NULL;
763 char *psz_url_parser = NULL;
764 char *psz_buffer_substring;
767 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
768 intf_sys_t *p_sys = p_this->p_sys;
770 vlc_mutex_lock ( &p_sys->lock );
774 free( p_struct_md5 ); \
775 free( ps_challenge_md5 ); \
776 vlc_mutex_unlock( &p_sys->lock ); \
779 #define PROTOCOL_ERROR \
781 vlc_mutex_unlock( &p_sys->lock ); \
784 #define MALLOC_CHECK( a ) \
790 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
791 MALLOC_CHECK( p_sys->psz_username )
793 /* username has not been setup, ignoring */
794 if ( !*p_sys->psz_username )
797 psz_handshake_url = malloc( 1024 );
798 MALLOC_CHECK( p_sys->psz_username )
800 snprintf( psz_handshake_url, 1024,
801 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
802 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
804 /* send the http handshake request */
805 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
807 free( psz_handshake_url );
811 vlc_mutex_unlock ( &p_sys->lock );
815 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
818 stream_Delete( p_stream );
823 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
825 stream_Delete( p_stream );
829 stream_Delete( p_stream );
831 /* record interval before next submission */
832 p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
835 time( &p_sys->time_next_exchange );
836 p_sys->time_next_exchange +=
837 atoi( p_buffer_pos + strlen( "INTERVAL " ) );
840 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
843 /* handshake request failed, sorry */
844 msg_Dbg( p_this, "%s", p_buffer_pos );
848 p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
851 /* username does not exist on the server */
852 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
853 _("last.fm username is incorrect, please verify your settings")
858 p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
861 /* protocol has been updated, time to update the code */
862 msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
863 msg_Dbg( p_intf, "%s", p_buffer_pos );
868 p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
871 msg_Dbg( p_intf, "Can't recognize server protocol" );
876 psz_buffer_substring = strstr( p_buffer_pos, "\n" );
877 if( ( psz_buffer_substring == NULL ) || \
878 ( strlen( psz_buffer_substring + 1 ) < 32 ) )
880 msg_Dbg( p_intf, "Can't recognize server protocol" );
885 ps_challenge_md5 = malloc( 32 );
886 MALLOC_CHECK( ps_challenge_md5 )
887 memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
890 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
892 /* free old information */
893 free( p_sys->psz_submit_host );
894 free( p_sys->psz_submit_file );
896 psz_url_parser = p_buffer_pos + strlen( "http://" );
897 i_url_pos = strcspn( psz_url_parser, ":" );
899 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
900 MALLOC_CHECK( p_sys->psz_submit_host )
902 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
904 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
905 i_url_pos = strcspn( psz_url_parser, "\n" );
906 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
907 MALLOC_CHECK( p_sys->psz_submit_file )
911 p_struct_md5 = malloc( sizeof( struct md5_s ) );
912 MALLOC_CHECK( p_struct_md5 )
914 psz_password = config_GetPsz(p_this, "lastfm-password");
915 MALLOC_CHECK( psz_password )
917 /* generates a md5 hash of the password */
918 InitMD5( p_struct_md5 );
919 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
920 EndMD5( p_struct_md5 );
922 free( psz_password );
924 psz_password_md5 = malloc ( 33 );
925 MALLOC_CHECK( psz_password_md5 )
927 for ( i = 0; i < 4; i++ )
929 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
930 p_struct_md5->p_digest[i] & 0xff,
931 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
932 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
933 p_struct_md5->p_digest[i] >> 24
937 /* generates a md5 hash of :
938 * - md5 hash of the password, plus
939 * - md5 challenge sent by last.fm server
941 InitMD5( p_struct_md5 );
942 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
943 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
944 EndMD5( p_struct_md5 );
946 free( ps_challenge_md5 );
947 free( psz_password_md5 );
949 for ( i = 0; i < 4; i++ )
951 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
952 p_struct_md5->p_digest[i] & 0xff,
953 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
954 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
955 p_struct_md5->p_digest[i] >> 24
959 p_sys->psz_response_md5[32] = '\0';
961 vlc_mutex_unlock ( &p_sys->lock );
965 #undef PROTOCOL_ERROR
969 /*****************************************************************************
970 * DeleteQueue : Free all songs from an audioscrobbler_queue_t
971 *****************************************************************************/
972 void DeleteQueue( audioscrobbler_queue_t *p_queue )
976 for( i = 0; i < p_queue->i_songs_nb; i++ )
978 free( p_queue->p_queue[i]->psz_a );
979 free( p_queue->p_queue[i]->psz_b );
980 free( p_queue->p_queue[i]->psz_t );
981 free( p_queue->p_queue[i]->psz_i );
982 free( p_queue->p_queue[i] );
986 /*****************************************************************************
987 * ReadMetaData : Read meta data when parsed by vlc
988 * or wait for fetching if unavailable
989 *****************************************************************************/
990 static int ReadMetaData( intf_thread_t *p_this )
992 playlist_t *p_playlist;
993 input_thread_t *p_input = NULL;
994 vlc_value_t video_val;
996 char *psz_title = NULL;
997 char *psz_artist = NULL;
998 char *psz_album = NULL;
999 char *psz_trackid = NULL;
1001 vlc_bool_t b_waiting;
1002 intf_sys_t *p_sys = p_this->p_sys;
1005 p_playlist = pl_Yield( p_this );
1007 p_input = p_playlist->p_input;
1012 pl_Release( p_playlist );
1013 return( VLC_SUCCESS );
1016 vlc_object_yield( p_input );
1018 pl_Release( p_playlist );
1020 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
1021 if( ( video_val.i_int > 0 ) || \
1022 ( input_GetItem( p_input )->i_type == ITEM_TYPE_NET ) )
1024 msg_Dbg( p_this, "Not an audio only local file -> no submission");
1025 vlc_object_release( p_input );
1027 vlc_mutex_lock( &p_sys->lock );
1028 p_sys->b_queued = VLC_TRUE;
1029 p_sys->b_metadata_read = VLC_TRUE;
1030 vlc_mutex_unlock( &p_sys->lock );
1035 #define FREE_INPUT_AND_CHARS \
1036 vlc_object_release( p_input ); \
1037 free( psz_title ); \
1038 free( psz_artist ); \
1039 free( psz_album ); \
1040 free( psz_trackid );
1042 #define WAIT_METADATA_FETCHING( a ) \
1043 if ( b_waiting == VLC_TRUE ) \
1045 a = calloc( 1, 1 ); \
1049 vlc_object_release( p_input ); \
1050 vlc_mutex_lock( &p_sys->lock ); \
1051 p_sys->b_waiting_meta = VLC_TRUE; \
1052 vlc_mutex_unlock( &p_sys->lock ); \
1053 free( psz_artist ); \
1054 return VLC_SUCCESS; \
1058 #define ALLOC_ITEM_META( a, b ) \
1059 psz_meta = input_item_Get##b( input_GetItem( p_input ) ); \
1062 a = encode_URI_component( psz_meta ); \
1066 FREE_INPUT_AND_CHARS \
1067 return VLC_ENOMEM; \
1072 i_status = input_GetItem(p_input)->p_meta->i_status;
1074 vlc_mutex_lock( &p_sys->lock );
1075 b_waiting = p_sys->b_waiting_meta;
1076 vlc_mutex_unlock( &p_sys->lock );
1078 if( i_status & ( !b_waiting ? ITEM_PREPARSED : ITEM_META_FETCHED ) )
1080 ALLOC_ITEM_META( psz_artist, Artist )
1083 msg_Dbg( p_this, "No artist.." );
1084 WAIT_METADATA_FETCHING( psz_artist )
1086 psz_meta = input_item_GetTitle( input_GetItem( p_input ) );
1089 psz_title = encode_URI_component( psz_meta );
1093 FREE_INPUT_AND_CHARS
1100 msg_Dbg( p_this, "No track name.." );
1101 WAIT_METADATA_FETCHING( psz_title );
1104 ALLOC_ITEM_META( psz_album, Album )
1106 psz_album = calloc( 1, 1 );
1108 ALLOC_ITEM_META( psz_trackid, TrackID )
1110 psz_trackid = calloc( 1, 1 );
1112 i_length = input_item_GetDuration( input_GetItem( p_input ) ) / 1000000;
1114 vlc_mutex_lock ( &p_sys->lock );
1116 p_sys->p_current_song->psz_a = strdup( psz_artist );
1117 p_sys->p_current_song->psz_t = strdup( psz_title );
1118 p_sys->p_current_song->psz_b = strdup( psz_album );
1119 p_sys->p_current_song->psz_m = strdup( psz_trackid );
1120 p_sys->p_current_song->i_l = i_length;
1121 p_sys->b_queued = VLC_FALSE;
1122 p_sys->b_metadata_read = VLC_TRUE;
1124 vlc_mutex_unlock( &p_sys->lock );
1126 msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1129 FREE_INPUT_AND_CHARS
1131 #undef FREE_INPUT_AND_CHARS
1132 #undef ALLOC_ITEM_META
1133 #undef WAIT_METADATA_FETCHING