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 int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
104 vlc_value_t, void * );
105 static int PlayingChange( vlc_object_t *, const char *, vlc_value_t,
106 vlc_value_t, void * );
107 static int AddToQueue ( intf_thread_t *p_this );
108 static int Handshake ( intf_thread_t *p_sd );
109 static int ReadMetaData ( intf_thread_t *p_this );
110 void DeleteQueue ( audioscrobbler_queue_t *p_queue );
112 /*****************************************************************************
114 ****************************************************************************/
116 #define USERNAME_TEXT N_("Username")
117 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
118 #define PASSWORD_TEXT N_("Password")
119 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
121 /* if something goes wrong, we wait at least one minute before trying again */
122 #define DEFAULT_INTERVAL 60
124 /* last.fm client identifier */
125 #define CLIENT_NAME PACKAGE
126 #define CLIENT_VERSION VERSION
128 /* HTTP POST request : to submit data */
129 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
130 "Accept-Encoding: identity\n" \
131 "Content-length: %d\n" \
132 "Connection: close\n" \
133 "Content-type: application/x-www-form-urlencoded\n" \
135 "User-agent: VLC Media Player/%s\r\n" \
141 #define POST_DATA "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
142 "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
143 #define HTTPPOST_MAXLEN 2048
146 set_category( CAT_INTERFACE );
147 set_subcategory( SUBCAT_INTERFACE_CONTROL );
148 set_shortname( N_( "Audioscrobbler" ) );
149 set_description( N_("Audioscrobbler submission Plugin") );
150 add_string( "lastfm-username", "", NULL,
151 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
152 add_password( "lastfm-password", "", NULL,
153 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
154 set_capability( "interface", 0 );
155 set_callbacks( Open, Close );
158 /*****************************************************************************
159 * Open: initialize and create stuff
160 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 playlist_t *p_playlist;
165 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
166 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
169 free( p_sys->p_current_song ); \
170 free( p_sys->p_first_queue ); \
171 free( p_sys->psz_response_md5 ); \
180 p_intf->p_sys = p_sys;
182 vlc_mutex_init( p_this, &p_sys->lock );
184 p_sys_global = p_sys;
185 p_sys->psz_submit_host = NULL;
186 p_sys->psz_submit_file = NULL;
187 p_sys->b_handshaked = VLC_FALSE;
188 p_sys->time_next_exchange = time( NULL );
189 p_sys->psz_username = NULL;
190 p_sys->b_paused = VLC_FALSE;
192 #define MALLOC_CHECK( a ) \
194 vlc_mutex_destroy( &p_sys->lock ); \
198 /* md5 response is 32 chars, + final \0 */
199 p_sys->psz_response_md5 = malloc( 33 );
200 MALLOC_CHECK( p_sys->psz_response_md5 )
202 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
203 MALLOC_CHECK( p_sys->p_first_queue )
205 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
206 MALLOC_CHECK( p_sys->p_current_song )
207 time( &p_sys->p_current_song->time_playing );
209 /* queues can't contain more than 10 songs */
210 p_sys->p_first_queue->p_queue =
211 malloc( 10 * sizeof( audioscrobbler_song_t ) );
212 MALLOC_CHECK( p_sys->p_current_song )
214 p_sys->p_first_queue->i_songs_nb = 0;
215 p_sys->p_first_queue->p_next_queue = NULL;
217 p_playlist = pl_Yield( p_intf );
219 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
221 pl_Release( p_playlist );
223 p_intf->pf_run = Run;
230 /*****************************************************************************
231 * Close: destroy interface stuff
232 *****************************************************************************/
233 static void Close( vlc_object_t *p_this )
235 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
236 playlist_t *p_playlist;
237 input_thread_t *p_input;
238 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
239 intf_sys_t *p_sys = p_intf->p_sys;
241 p_playlist = pl_Yield( p_intf );
243 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
245 p_input = p_playlist->p_input;
248 vlc_object_yield( p_input );
249 var_DelCallback( p_input, "state", PlayingChange, p_intf );
250 vlc_object_release( p_input );
254 pl_Release( p_playlist );
256 vlc_mutex_lock ( &p_sys->lock );
257 p_current_queue = p_sys->p_first_queue;
258 vlc_mutex_unlock ( &p_sys->lock );
260 while( ( p_current_queue->i_songs_nb == 10 ) &&
261 ( p_current_queue->p_next_queue != NULL ) )
263 p_next_queue = p_current_queue->p_next_queue;
264 DeleteQueue( p_current_queue );
265 free( p_current_queue );
266 p_current_queue = p_next_queue;
269 DeleteQueue( p_current_queue );
270 free( p_current_queue );
272 vlc_mutex_lock ( &p_sys->lock );
273 free( p_sys->psz_username );
274 free( p_sys->p_current_song );
275 free( p_sys->psz_submit_host );
276 free( p_sys->psz_submit_file );
277 free( p_sys->psz_response_md5 );
278 vlc_mutex_unlock ( &p_sys->lock );
279 vlc_mutex_destroy( &p_sys->lock );
283 /*****************************************************************************
284 * Run : call Handshake() then submit songs
285 *****************************************************************************/
286 static void Run( intf_thread_t *p_this )
288 char *psz_submit = NULL;
289 char *psz_submit_song = NULL;
292 playlist_t *p_playlist;
293 uint8_t *p_buffer = NULL;
294 char *p_buffer_pos = NULL;
295 audioscrobbler_queue_t *p_first_queue;
299 p_this->p_sys = p_sys_global;
300 intf_sys_t *p_sys = p_this->p_sys;
303 free( psz_submit ); \
304 free( psz_submit_song ); \
306 msg_Err( p_this, "Out of memory" ); \
309 psz_submit = malloc( HTTPPOST_MAXLEN );
310 psz_submit_song = malloc( HTTPPOST_MAXLEN );
311 p_buffer = ( uint8_t* ) malloc( 1024 );
313 if( !psz_submit || !psz_submit_song || !p_buffer )
319 while( !p_this->b_die )
321 /* verify if there is data to submit
322 * and if waiting interval is elapsed */
323 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
324 ( time( NULL ) >= p_sys->time_next_exchange ) )
326 /* handshake if needed */
327 if( p_sys->b_handshaked == VLC_FALSE )
329 msg_Dbg( p_this, "Handshaking with last.fm ..." );
331 switch( Handshake( p_this ) )
338 /* username not set */
339 vlc_mutex_unlock ( &p_sys->lock );
340 intf_UserFatal( p_this, VLC_FALSE,
341 _("Last.fm username not set"),
342 _("Please set an username or disable "
343 "audioscrobbler plugin, and then restart VLC.\n"
344 "Visit https://www.last.fm/join/ to get an account")
347 free( psz_submit_song );
353 msg_Dbg( p_this, "Handshake successfull :)" );
354 vlc_mutex_lock ( &p_sys->lock );
355 p_sys->b_handshaked = VLC_TRUE;
356 vlc_mutex_unlock ( &p_sys->lock );
361 /* protocol error : we'll try later */
362 vlc_mutex_lock ( &p_sys->lock );
363 time( &p_sys->time_next_exchange );
364 p_sys->time_next_exchange += DEFAULT_INTERVAL;
365 vlc_mutex_unlock ( &p_sys->lock );
370 msg_Dbg( p_this, "Going to submit some data..." );
371 vlc_mutex_lock ( &p_sys->lock );
373 snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
374 p_sys->psz_username, p_sys->psz_response_md5 );
376 /* forge the HTTP POST request */
377 for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
380 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
381 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
382 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
383 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
384 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
385 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
386 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
388 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
391 i_post_socket = net_ConnectTCP( p_this,
392 p_sys->psz_submit_host, p_sys->i_submit_port);
394 if ( i_post_socket == -1 )
396 /* If connection fails, we assume we must handshake again */
397 time( &p_sys->time_next_exchange );
398 p_sys->time_next_exchange += DEFAULT_INTERVAL;
399 p_sys->b_handshaked = VLC_FALSE;
400 vlc_mutex_unlock( &p_sys->lock );
404 /* we transmit the data */
405 i_net_ret = net_Printf(
406 VLC_OBJECT(p_this), i_post_socket, NULL,
407 POST_REQUEST, p_sys->psz_submit_file,
408 strlen( psz_submit ), p_sys->psz_submit_file,
412 if ( i_net_ret == -1 )
414 /* If connection fails, we assume we must handshake again */
415 time( &p_sys->time_next_exchange );
416 p_sys->time_next_exchange += DEFAULT_INTERVAL;
417 p_sys->b_handshaked = VLC_FALSE;
418 vlc_mutex_unlock( &p_sys->lock );
422 memset( p_buffer, '\0', 1024 );
424 i_net_ret = net_Read( p_this, i_post_socket, NULL,
425 p_buffer, 1024, VLC_FALSE );
426 if ( i_net_ret <= 0 )
428 /* if we get no answer, something went wrong : try again */
429 vlc_mutex_unlock( &p_sys->lock );
433 net_Close( i_post_socket );
435 /* record interval */
436 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
439 time( &p_sys->time_next_exchange );
440 p_sys->time_next_exchange += atoi( p_buffer_pos +
441 strlen( "INTERVAL " ) );
444 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
447 /* woops, submission failed */
448 msg_Dbg( p_this, "%s", p_buffer_pos );
449 vlc_mutex_unlock ( &p_sys->lock );
453 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
456 msg_Dbg( p_this, "Authentification failed, handshaking again" );
457 p_sys->b_handshaked = VLC_FALSE;
458 vlc_mutex_unlock ( &p_sys->lock );
462 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
465 if ( p_sys->p_first_queue->i_songs_nb == 10 )
467 /* if there are more than one queue, delete the 1st */
468 p_first_queue = p_sys->p_first_queue->p_next_queue;
469 DeleteQueue( p_sys->p_first_queue );
470 free( p_sys->p_first_queue );
471 p_sys->p_first_queue = p_first_queue;
475 DeleteQueue( p_sys->p_first_queue );
476 p_sys->p_first_queue->i_songs_nb = 0;
478 msg_Dbg( p_this, "Submission successfull!" );
480 vlc_mutex_unlock ( &p_sys->lock );
481 } /* data transmission finished or skipped */
483 msleep( INTF_IDLE_SLEEP );
485 p_playlist = pl_Yield( p_this );
487 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
489 /* if we stopped, we won't submit playing song */
490 vlc_mutex_lock( &p_sys->lock );
491 p_sys->b_queued = VLC_TRUE;
492 p_sys->b_metadata_read = VLC_TRUE;
493 vlc_mutex_unlock( &p_sys->lock );
496 pl_Release( p_playlist );
498 vlc_mutex_lock( &p_sys->lock );
499 if( p_sys->b_metadata_read == VLC_FALSE )
501 /* we read the metadata of the playing song */
502 time( &played_time );
503 played_time -= p_sys->p_current_song->time_playing;
504 played_time -= p_sys->time_total_pauses;
506 vlc_mutex_unlock( &p_sys->lock );
508 if( played_time > 20 )
509 /* wait at least 20 secondes before reading the meta data
510 * since the songs must be at least 30s */
512 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
520 /* we add the playing song into the queue */
521 if( ( p_sys->b_queued == VLC_FALSE )
522 && ( p_sys->b_paused == VLC_FALSE ) )
524 vlc_mutex_unlock( &p_sys->lock );
525 if( AddToQueue( p_this ) == VLC_ENOMEM )
532 vlc_mutex_unlock( &p_sys->lock );
539 /*****************************************************************************
540 * PlayingChange: Playing status change callback
541 *****************************************************************************/
542 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
543 vlc_value_t oldval, vlc_value_t newval, void *p_data )
545 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
546 intf_sys_t *p_sys = p_intf->p_sys;
548 (void)p_this; (void)psz_var; (void)oldval;
550 /* don't bother if song has already been queued */
551 if( p_sys->b_queued == VLC_TRUE )
554 vlc_mutex_lock( &p_sys->lock );
556 if( newval.i_int == PAUSE_S )
558 time( &p_sys->time_pause );
559 p_sys->b_paused = VLC_TRUE;
562 else if( newval.i_int == PLAYING_S )
564 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
565 p_sys->b_paused = VLC_FALSE;
568 vlc_mutex_unlock( &p_sys->lock );
573 /*****************************************************************************
574 * ItemChange: Playlist item change callback
575 *****************************************************************************/
576 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
577 vlc_value_t oldval, vlc_value_t newval, void *p_data )
579 playlist_t *p_playlist;
580 input_thread_t *p_input = NULL;
584 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
585 intf_sys_t *p_sys = p_intf->p_sys;
586 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
588 p_playlist = pl_Yield( p_intf );
590 p_input = p_playlist->p_input;
595 pl_Release( p_playlist );
597 vlc_mutex_lock( &p_sys->lock );
599 p_sys->b_queued = VLC_TRUE;
600 p_sys->b_metadata_read = VLC_TRUE;
602 vlc_mutex_unlock( &p_sys->lock );
607 vlc_object_yield( p_input );
609 pl_Release( p_playlist );
611 var_AddCallback( p_input, "state", PlayingChange, p_intf );
613 vlc_mutex_lock ( &p_sys->lock );
615 /* reset pause counter */
616 p_sys->time_total_pauses = 0;
618 /* we'll read metadata when it's present */
619 p_sys->b_metadata_read = VLC_FALSE;
620 p_sys->b_waiting_meta = VLC_FALSE;
623 epoch_tm = gmtime( &epoch );
624 snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
625 epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
626 epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
628 p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
629 p_sys->p_current_song->time_playing = epoch;
631 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
632 p_sys->b_paused = ( p_input->b_dead || !psz_name )
633 ? VLC_TRUE : VLC_FALSE;
636 vlc_mutex_unlock( &p_sys->lock );
638 vlc_object_release( p_input );
642 /*****************************************************************************
643 * AddToQueue: Add the played song to the queue to be submitted
644 *****************************************************************************/
645 static int AddToQueue ( intf_thread_t *p_this )
649 audioscrobbler_queue_t *p_queue = NULL,
650 *p_next_queue = NULL;
651 intf_sys_t *p_sys = p_this->p_sys;
653 /* wait for the user to listen enough before submitting */
654 time ( &played_time );
655 vlc_mutex_lock( &p_sys->lock );
657 played_time -= p_sys->p_current_song->time_playing;
658 played_time -= p_sys->time_total_pauses;
660 #define NO_SUBMISSION \
661 p_sys->b_queued = VLC_TRUE; \
662 vlc_mutex_unlock( &p_sys->lock ); \
665 if( ( played_time < 240 ) &&
666 ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
668 //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
669 vlc_mutex_unlock( &p_sys->lock );
673 if( p_sys->p_current_song->i_l < 30 )
675 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
679 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
681 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
685 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
687 /* go to last queue */
688 p_queue = p_sys->p_first_queue;
689 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
690 p_queue = p_queue->p_next_queue;
692 i_songs_nb = p_queue->i_songs_nb;
694 #define MALLOC_CHECK( a ) \
696 vlc_mutex_unlock( &p_sys->lock ); \
700 if( i_songs_nb == 10 )
702 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
703 MALLOC_CHECK( p_next_queue );
704 p_queue->p_next_queue = p_next_queue;
705 p_queue = p_next_queue;
707 p_queue->i_songs_nb = i_songs_nb;
710 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
711 MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
713 #define QUEUE_COPY( a ) \
714 p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
723 p_queue->i_songs_nb++;
724 p_sys->b_queued = VLC_TRUE;
726 vlc_mutex_unlock( &p_sys->lock );
734 /*****************************************************************************
735 * Handshake : Init audioscrobbler connection
736 *****************************************************************************/
737 static int Handshake( intf_thread_t *p_this )
739 char *psz_password = NULL;
740 struct md5_s *p_struct_md5 = NULL;
741 char *psz_password_md5 = NULL;
742 char *ps_challenge_md5 = NULL;
744 char *psz_handshake_url = NULL;
745 uint8_t *p_buffer = NULL;
746 char *p_buffer_pos = NULL;
747 char *psz_url_parser = NULL;
748 char *psz_buffer_substring;
751 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
752 intf_sys_t *p_sys = p_this->p_sys;
754 vlc_mutex_lock ( &p_sys->lock );
758 free( p_struct_md5 ); \
759 free( ps_challenge_md5 ); \
760 vlc_mutex_unlock( &p_sys->lock ); \
763 #define PROTOCOL_ERROR \
765 vlc_mutex_unlock( &p_sys->lock ); \
768 #define MALLOC_CHECK( a ) \
774 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
775 MALLOC_CHECK( p_sys->psz_username )
777 /* username has not been setup, ignoring */
778 if ( !*p_sys->psz_username )
781 psz_handshake_url = malloc( 1024 );
782 MALLOC_CHECK( p_sys->psz_username )
784 snprintf( psz_handshake_url, 1024,
785 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
786 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
788 /* send the http handshake request */
789 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
791 free( psz_handshake_url );
795 vlc_mutex_unlock ( &p_sys->lock );
799 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
802 stream_Delete( p_stream );
807 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
809 stream_Delete( p_stream );
813 stream_Delete( p_stream );
815 /* record interval before next submission */
816 p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
819 time( &p_sys->time_next_exchange );
820 p_sys->time_next_exchange +=
821 atoi( p_buffer_pos + strlen( "INTERVAL " ) );
824 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
827 /* handshake request failed, sorry */
828 msg_Dbg( p_this, "%s", p_buffer_pos );
832 p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
835 /* username does not exist on the server */
836 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
837 _("last.fm username is incorrect, please verify your settings")
842 p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
845 /* protocol has been updated, time to update the code */
846 msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
847 msg_Dbg( p_intf, "%s", p_buffer_pos );
852 p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
855 msg_Dbg( p_intf, "Can't recognize server protocol" );
860 psz_buffer_substring = strstr( p_buffer_pos, "\n" );
861 if( ( psz_buffer_substring == NULL ) || \
862 ( strlen( psz_buffer_substring + 1 ) < 32 ) )
864 msg_Dbg( p_intf, "Can't recognize server protocol" );
869 ps_challenge_md5 = malloc( 32 );
870 MALLOC_CHECK( ps_challenge_md5 )
871 memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
874 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
876 /* free old information */
877 free( p_sys->psz_submit_host );
878 free( p_sys->psz_submit_file );
880 psz_url_parser = p_buffer_pos + strlen( "http://" );
881 i_url_pos = strcspn( psz_url_parser, ":" );
883 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
884 MALLOC_CHECK( p_sys->psz_submit_host )
886 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
888 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
889 i_url_pos = strcspn( psz_url_parser, "\n" );
890 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
891 MALLOC_CHECK( p_sys->psz_submit_file )
895 p_struct_md5 = malloc( sizeof( struct md5_s ) );
896 MALLOC_CHECK( p_struct_md5 )
898 psz_password = config_GetPsz(p_this, "lastfm-password");
899 MALLOC_CHECK( psz_password )
901 /* generates a md5 hash of the password */
902 InitMD5( p_struct_md5 );
903 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
904 EndMD5( p_struct_md5 );
906 free( psz_password );
908 psz_password_md5 = malloc ( 33 );
909 MALLOC_CHECK( psz_password_md5 )
911 for ( i = 0; i < 4; i++ )
913 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
914 p_struct_md5->p_digest[i] & 0xff,
915 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
916 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
917 p_struct_md5->p_digest[i] >> 24
921 /* generates a md5 hash of :
922 * - md5 hash of the password, plus
923 * - md5 challenge sent by last.fm server
925 InitMD5( p_struct_md5 );
926 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
927 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
928 EndMD5( p_struct_md5 );
930 free( ps_challenge_md5 );
931 free( psz_password_md5 );
933 for ( i = 0; i < 4; i++ )
935 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
936 p_struct_md5->p_digest[i] & 0xff,
937 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
938 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
939 p_struct_md5->p_digest[i] >> 24
943 p_sys->psz_response_md5[32] = '\0';
945 vlc_mutex_unlock ( &p_sys->lock );
949 #undef PROTOCOL_ERROR
953 /*****************************************************************************
954 * DeleteQueue : Free all songs from an audioscrobbler_queue_t
955 *****************************************************************************/
956 void DeleteQueue( audioscrobbler_queue_t *p_queue )
960 for( i = 0; i < p_queue->i_songs_nb; i++ )
962 free( p_queue->p_queue[i]->psz_a );
963 free( p_queue->p_queue[i]->psz_b );
964 free( p_queue->p_queue[i]->psz_t );
965 free( p_queue->p_queue[i]->psz_i );
966 free( p_queue->p_queue[i] );
970 /*****************************************************************************
971 * ReadMetaData : Read meta data when parsed by vlc
972 * or wait for fetching if unavailable
973 *****************************************************************************/
974 static int ReadMetaData( intf_thread_t *p_this )
976 playlist_t *p_playlist;
977 input_thread_t *p_input = NULL;
978 vlc_value_t video_val;
980 char *psz_title = NULL;
981 char *psz_artist = NULL;
982 char *psz_album = NULL;
983 char *psz_trackid = NULL;
985 vlc_bool_t b_waiting;
986 intf_sys_t *p_sys = p_this->p_sys;
989 p_playlist = pl_Yield( p_this );
991 p_input = p_playlist->p_input;
996 pl_Release( p_playlist );
997 return( VLC_SUCCESS );
1000 vlc_object_yield( p_input );
1002 pl_Release( p_playlist );
1004 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
1005 if( ( video_val.i_int > 0 ) || \
1006 ( input_GetItem( p_input )->i_type == ITEM_TYPE_NET ) )
1008 msg_Dbg( p_this, "Not an audio only local file -> no submission");
1009 vlc_object_release( p_input );
1011 vlc_mutex_lock( &p_sys->lock );
1012 p_sys->b_queued = VLC_TRUE;
1013 p_sys->b_metadata_read = VLC_TRUE;
1014 vlc_mutex_unlock( &p_sys->lock );
1019 #define FREE_INPUT_AND_CHARS \
1020 vlc_object_release( p_input ); \
1021 free( psz_title ); \
1022 free( psz_artist ); \
1023 free( psz_album ); \
1024 free( psz_trackid );
1026 #define WAIT_METADATA_FETCHING( a ) \
1027 if ( b_waiting == VLC_TRUE ) \
1029 a = calloc( 1, 1 ); \
1033 vlc_object_release( p_input ); \
1034 vlc_mutex_lock( &p_sys->lock ); \
1035 p_sys->b_waiting_meta = VLC_TRUE; \
1036 vlc_mutex_unlock( &p_sys->lock ); \
1037 free( psz_artist ); \
1038 return VLC_SUCCESS; \
1042 #define ALLOC_ITEM_META( a, b ) \
1043 psz_meta = input_item_Get##b( input_GetItem( p_input ) ); \
1046 a = encode_URI_component( psz_meta ); \
1050 FREE_INPUT_AND_CHARS \
1051 return VLC_ENOMEM; \
1056 i_status = input_GetItem(p_input)->p_meta->i_status;
1058 vlc_mutex_lock( &p_sys->lock );
1059 b_waiting = p_sys->b_waiting_meta;
1060 vlc_mutex_unlock( &p_sys->lock );
1062 if( i_status & ( !b_waiting ? ITEM_PREPARSED : ITEM_META_FETCHED ) )
1064 ALLOC_ITEM_META( psz_artist, Artist )
1067 msg_Dbg( p_this, "No artist.." );
1068 WAIT_METADATA_FETCHING( psz_artist )
1070 psz_meta = input_item_GetName( input_GetItem( p_input ) );
1073 psz_title = encode_URI_component( psz_meta );
1077 FREE_INPUT_AND_CHARS
1084 msg_Dbg( p_this, "No track name.." );
1085 WAIT_METADATA_FETCHING( psz_title );
1088 ALLOC_ITEM_META( psz_album, Album )
1090 psz_album = calloc( 1, 1 );
1092 ALLOC_ITEM_META( psz_trackid, TrackID )
1094 psz_trackid = calloc( 1, 1 );
1096 i_length = input_item_GetDuration( input_GetItem( p_input ) ) / 1000000;
1098 vlc_mutex_lock ( &p_sys->lock );
1100 p_sys->p_current_song->psz_a = strdup( psz_artist );
1101 p_sys->p_current_song->psz_t = strdup( psz_title );
1102 p_sys->p_current_song->psz_b = strdup( psz_album );
1103 p_sys->p_current_song->psz_m = strdup( psz_trackid );
1104 p_sys->p_current_song->i_l = i_length;
1105 p_sys->b_queued = VLC_FALSE;
1106 p_sys->b_metadata_read = VLC_TRUE;
1108 vlc_mutex_unlock( &p_sys->lock );
1110 msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1113 FREE_INPUT_AND_CHARS
1115 #undef FREE_INPUT_AND_CHARS
1116 #undef ALLOC_ITEM_META
1117 #undef WAIT_METADATA_FETCHING