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 /*****************************************************************************
27 *****************************************************************************/
37 * implement musicbrainz unique track identifier in p_meta
38 * check meta_engine's state, and remove delaying of metadata reading
39 * check md5 operations on BIGENDIAN and 64 bits architectures
45 #include <vlc_block.h>
46 #include <vlc_stream.h>
49 #include <vlc_interaction.h>
51 /*****************************************************************************
53 *****************************************************************************/
55 /* Keeps track of metadata to be submitted, and if song has been submitted */
56 typedef struct audioscrobbler_song_t
58 char *psz_a; /* track artist */
59 char *psz_t; /* track title */
60 char *psz_b; /* track album */
61 int i_l; /* track length */
62 char *psz_m; /* musicbrainz id */
63 char *psz_i; /* date */
64 time_t time_playing; /* date (epoch) */
65 } audioscrobbler_song_t;
68 /* Queue to be submitted to server, 10 songs max */
69 typedef struct audioscrobbler_queue_t
71 audioscrobbler_song_t **p_queue; /* contains up to 10 songs */
72 int i_songs_nb; /* number of songs */
73 void *p_next_queue; /* if queue full, pointer to next */
74 } audioscrobbler_queue_t;
78 audioscrobbler_queue_t *p_first_queue; /* 1st queue */
79 vlc_mutex_t lock; /* p_sys mutex */
81 /* data about audioscrobbler session */
82 int i_interval; /* last interval recorded */
83 time_t time_last_interval; /* when was it recorded ? */
84 char *psz_submit_host; /* where to submit data ? */
85 int i_submit_port; /* at which port ? */
86 char *psz_submit_file; /* in which file ? */
87 char *psz_username; /* last.fm username */
88 vlc_bool_t b_handshaked; /* did we handshake ? */
89 int i_post_socket; /* socket for submission */
90 char *psz_response_md5; /* md5 response to use */
92 /* data about input elements */
93 audioscrobbler_song_t *p_current_song; /* song being played */
94 time_t time_pause; /* time when vlc paused */
95 time_t time_total_pauses; /* sum of time in pause */
96 vlc_bool_t b_queued; /* has it been queud ? */
97 vlc_bool_t b_metadata_read; /* did we read metadata ? */
98 vlc_bool_t b_paused; /* are we playing ? */
99 vlc_bool_t b_waiting_meta; /* we need fetched data? */
102 intf_sys_t *p_sys_global; /* to retrieve p_sys in Run() thread */
104 static int Open ( vlc_object_t * );
105 static void Close ( vlc_object_t * );
106 static void Run ( intf_thread_t * );
107 static int ItemChange ( vlc_object_t *, const char *,
108 vlc_value_t, vlc_value_t, void * );
109 static int PlayingChange( vlc_object_t *, const char *,
110 vlc_value_t, vlc_value_t, void * );
111 static int AddToQueue ( intf_thread_t *p_this );
112 static int Handshake ( intf_thread_t *p_sd );
113 static int ReadMetaData ( intf_thread_t *p_this );
114 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input );
115 void DeleteQueue( audioscrobbler_queue_t *p_queue );
117 /*****************************************************************************
119 ****************************************************************************/
122 #define APPLICATION_NAME "VLC media player"
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
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 "u=%s&s=%s&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s" \
148 "&b%%5B%d%%5D=%s&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
151 set_category( CAT_INTERFACE );
152 set_subcategory( SUBCAT_INTERFACE_CONTROL );
153 set_shortname( N_( "Audioscrobbler" ) );
154 set_description( N_("Audioscrobbler submission Plugin") );
155 add_string( "lastfm-username", "", NULL,
156 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
157 add_string( "lastfm-password", "", NULL,
158 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
159 set_capability( "interface", 0 );
160 set_callbacks( Open, Close );
163 /*****************************************************************************
164 * Open: initialize and create stuff
165 *****************************************************************************/
167 static int Open( vlc_object_t *p_this )
169 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 ) );
177 vlc_mutex_init( p_this, &p_sys->lock );
179 p_sys_global = p_sys;
180 p_sys->psz_submit_host = NULL;
181 p_sys->psz_submit_file = NULL;
182 p_sys->b_handshaked = VLC_FALSE;
183 p_sys->i_interval = 0;
184 p_sys->time_last_interval = time( NULL );
185 p_sys->psz_username = NULL;
186 p_sys->b_paused = VLC_FALSE;
188 /* md5 response is 32 chars, + final \0 */
189 p_sys->psz_response_md5 = malloc( sizeof( char ) * 33 );
190 if( !p_sys->psz_response_md5 )
192 vlc_mutex_destroy ( &p_sys->lock );
196 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
197 if( !p_sys->p_first_queue )
199 vlc_mutex_destroy( &p_sys->lock );
203 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
204 if( !p_sys->p_current_song )
206 vlc_mutex_destroy( &p_sys->lock );
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 if( !p_sys->p_current_song )
215 vlc_mutex_destroy( &p_sys->lock );
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 );
223 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
224 pl_Release( p_playlist );
226 p_intf->pf_run = Run;
231 free( p_sys->p_current_song );
232 free( p_sys->p_first_queue );
233 free( p_sys->psz_response_md5 );
239 /*****************************************************************************
240 * Close: destroy interface stuff
241 *****************************************************************************/
242 static void Close( vlc_object_t *p_this )
244 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
245 playlist_t *p_playlist;
246 input_thread_t *p_input;
248 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
249 intf_sys_t *p_sys = p_intf->p_sys;
251 p_playlist = pl_Yield( p_intf );
253 p_input = p_playlist->p_input;
254 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
258 vlc_object_yield( p_input );
259 var_DelCallback( p_input, "state", PlayingChange, p_intf );
260 vlc_object_release( p_input );
264 pl_Release( p_playlist );
266 vlc_mutex_lock ( &p_sys->lock );
267 p_current_queue = p_sys->p_first_queue;
268 vlc_mutex_unlock ( &p_sys->lock );
270 while( ( p_current_queue->i_songs_nb == 10 ) &&
271 ( p_current_queue->p_next_queue != NULL ) )
273 p_next_queue = p_current_queue->p_next_queue;
274 DeleteQueue( p_current_queue );
275 free( p_current_queue );
276 p_current_queue = p_next_queue;
279 DeleteQueue( p_current_queue );
280 free( p_current_queue );
282 vlc_mutex_lock ( &p_sys->lock );
283 free( p_sys->psz_username );
284 free( p_sys->p_current_song );
285 vlc_mutex_unlock ( &p_sys->lock );
286 vlc_mutex_destroy( &p_sys->lock );
290 /*****************************************************************************
291 * Run : Handshake with audioscrobbler, then submit songs
292 *****************************************************************************/
293 static void Run( intf_thread_t *p_this )
295 char *psz_submit_string = 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;
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 while( !p_this->b_die )
311 /* verify if there is data to submit
312 * and if waiting interval is finished */
313 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
315 ( p_sys->time_last_interval + p_sys->i_interval ) ) )
317 /* handshake if needed */
318 if( p_sys->b_handshaked == VLC_FALSE )
320 msg_Dbg( p_this, "Handshaking with last.fm ..." );
322 switch( Handshake( p_this ) )
325 msg_Err( p_this, "Out of memory" );
330 /* username not set */
331 vlc_mutex_unlock ( &p_sys->lock );
332 intf_UserFatal( p_this, VLC_FALSE,
333 _("last.fm username not set"),
334 _("You have to set a username,"
335 " and then restart VLC.\n"
336 "Visit https://www.last.fm/join/"
337 " if you don't have one.")
343 msg_Dbg( p_this, "Handshake successfull :)" );
344 vlc_mutex_lock ( &p_sys->lock );
345 p_sys->b_handshaked = VLC_TRUE;
346 vlc_mutex_unlock ( &p_sys->lock );
351 /* VLC_EGENERIC : we'll try later */
352 vlc_mutex_lock ( &p_sys->lock );
353 p_sys->i_interval = DEFAULT_INTERVAL;
354 time( &p_sys->time_last_interval );
355 vlc_mutex_unlock ( &p_sys->lock );
360 msg_Dbg( p_this, "Going to submit some data..." );
361 vlc_mutex_lock ( &p_sys->lock );
362 psz_submit_string = malloc( 2048 * sizeof( char ) );
364 if (!psz_submit_string)
366 msg_Err( p_this, "Out of memory" );
367 vlc_mutex_unlock ( &p_sys->lock );
371 /* forge the HTTP POST request */
372 for (i_song = 0; i_song < p_sys->p_first_queue->i_songs_nb ;
375 snprintf( psz_submit_string, 2048, POST_DATA,
376 p_sys->psz_username, p_sys->psz_response_md5,
377 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
378 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
379 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
380 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
381 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
382 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
386 p_sys->i_post_socket = net_ConnectTCP( p_this,
387 p_sys->psz_submit_host, p_sys->i_submit_port);
389 /* we transmit the data */
390 i_net_ret = net_Printf(
391 VLC_OBJECT(p_this), p_sys->i_post_socket, NULL,
392 POST_REQUEST, p_sys->psz_submit_file,
393 strlen( psz_submit_string), p_sys->psz_submit_file,
394 VERSION, psz_submit_string
397 if ( i_net_ret == -1 )
399 /* If connection fails, we assume we must handshake again */
400 p_sys->i_interval = DEFAULT_INTERVAL;
401 time( &p_sys->time_last_interval );
402 p_sys->b_handshaked = VLC_FALSE;
403 vlc_mutex_unlock( &p_sys->lock );
407 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
410 msg_Err( p_this, "Out of memory" );
411 vlc_mutex_unlock ( &p_sys->lock );
415 i_net_ret = net_Read( p_this, p_sys->i_post_socket, NULL,
416 p_buffer, 1024, VLC_FALSE );
417 if ( i_net_ret <= 0 )
419 /* if we get no answer, something went wrong : try again */
420 vlc_mutex_unlock( &p_sys->lock );
424 net_Close( p_sys->i_post_socket );
426 /* record interval */
427 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
430 p_sys->i_interval = atoi( p_buffer_pos +
431 strlen( "INTERVAL " ) );
432 time( &p_sys->time_last_interval );
435 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
438 /* woops, something failed */
439 msg_Dbg( p_this, p_buffer_pos );
440 vlc_mutex_unlock ( &p_sys->lock );
444 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
447 /* too much time elapsed after last handshake? */
448 msg_Dbg( p_this, "Authentification failed, handshaking again" );
449 p_sys->b_handshaked = VLC_FALSE;
450 vlc_mutex_unlock ( &p_sys->lock );
454 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
457 if ( p_sys->p_first_queue->i_songs_nb == 10 )
459 /* if there are more than one queue, delete the 1st */
460 p_first_queue = p_sys->p_first_queue->p_next_queue;
461 DeleteQueue( p_sys->p_first_queue );
462 free( p_sys->p_first_queue );
463 p_sys->p_first_queue = p_first_queue;
467 DeleteQueue( p_sys->p_first_queue );
468 p_sys->p_first_queue->i_songs_nb = 0;
470 msg_Dbg( p_this, "Submission successfull!" );
472 vlc_mutex_unlock ( &p_sys->lock );
473 } /* data transmission finished or skipped */
475 msleep( INTF_IDLE_SLEEP );
477 p_playlist = pl_Yield( p_this );
479 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
482 pl_Release( p_playlist );
483 /* if we stopped, we won't submit playing song */
484 vlc_mutex_lock( &p_sys->lock );
485 p_sys->b_queued = VLC_TRUE;
486 p_sys->b_metadata_read = VLC_TRUE;
487 vlc_mutex_unlock( &p_sys->lock );
492 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 )
511 msg_Err( p_this, "Out of memory" );
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 )
525 msg_Err( p_this, "Out of memory" );
531 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 vlc_mutex_lock( &p_sys->lock );
550 if( newval.i_int == PAUSE_S )
552 time( &p_sys->time_pause );
553 p_sys->b_paused = VLC_TRUE;
556 if( newval.i_int == PLAYING_S )
558 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
559 p_sys->b_paused = VLC_FALSE;
562 vlc_mutex_unlock( &p_sys->lock );
567 /*****************************************************************************
568 * ItemChange: Playlist item change callback
569 *****************************************************************************/
570 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
571 vlc_value_t oldval, vlc_value_t newval, void *p_data )
573 playlist_t *p_playlist;
574 input_thread_t *p_input = NULL;
579 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
581 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
582 intf_sys_t *p_sys = p_intf->p_sys;
584 p_playlist = pl_Yield( p_intf );
586 p_input = p_playlist->p_input;
591 pl_Release( p_playlist );
592 vlc_mutex_lock( &p_sys->lock );
594 /* we won't read p_input */
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, *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 played_time -= p_sys->p_current_song->time_playing;
649 played_time -= p_sys->time_total_pauses;
651 vlc_mutex_lock( &p_sys->lock );
652 if( ( played_time < 240 )
653 && ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
655 vlc_mutex_unlock ( &p_sys->lock );
659 if( p_sys->p_current_song->i_l < 30 )
661 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
662 p_sys->b_queued = VLC_TRUE;
663 vlc_mutex_unlock ( &p_sys->lock );
667 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
669 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
670 p_sys->b_queued = VLC_TRUE;
671 vlc_mutex_unlock ( &p_sys->lock );
675 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
677 /* go to last queue */
678 p_queue = p_sys->p_first_queue;
679 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
681 p_queue = p_queue->p_next_queue;
684 i_songs_nb = p_queue->i_songs_nb;
686 if( i_songs_nb == 10 )
688 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
691 vlc_mutex_unlock ( &p_sys->lock );
694 p_queue->p_next_queue = p_next_queue;
696 p_queue = p_next_queue;
697 p_queue->i_songs_nb = i_songs_nb;
700 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
702 p_queue->p_queue[i_songs_nb]->i_l = p_sys->p_current_song->i_l;
704 p_queue->p_queue[i_songs_nb]->psz_a =
705 strdup( p_sys->p_current_song->psz_a );
707 p_queue->p_queue[i_songs_nb]->psz_t =
708 strdup( p_sys->p_current_song->psz_t );
710 p_queue->p_queue[i_songs_nb]->psz_b =
711 strdup( p_sys->p_current_song->psz_b );
713 p_queue->p_queue[i_songs_nb]->psz_m =
714 strdup( p_sys->p_current_song->psz_m );
716 p_queue->p_queue[i_songs_nb]->psz_i =
717 strdup( p_sys->p_current_song->psz_i );
719 p_queue->i_songs_nb++;
720 p_sys->b_queued = VLC_TRUE;
722 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;
738 char *psz_handshake_url = NULL;
740 uint8_t *p_buffer = NULL;
741 char *p_buffer_pos = NULL;
742 char *psz_buffer_substring = NULL;
743 char *psz_url_parser = NULL;
746 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
747 intf_sys_t *p_sys = p_this->p_sys;
749 vlc_mutex_lock ( &p_sys->lock );
751 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
752 if ( !p_sys->psz_username )
757 if ( !*p_sys->psz_username )
762 psz_handshake_url = malloc( 1024 );
763 if ( !psz_handshake_url )
768 snprintf( psz_handshake_url, 1024,
769 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
770 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
772 /* send the http handshake request */
773 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
775 free( psz_handshake_url );
779 vlc_mutex_unlock ( &p_sys->lock );
783 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
786 stream_Delete( p_stream );
791 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
793 stream_Delete( p_stream );
797 stream_Delete( p_stream );
799 /* record interval before next submission */
800 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
803 p_sys->i_interval = atoi( p_buffer_pos + strlen( "INTERVAL " ) );
804 time( &p_sys->time_last_interval );
807 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
810 /* handshake request failed */
811 msg_Dbg( p_this, p_buffer_pos );
815 p_buffer_pos = strstr( ( char * ) p_buffer, "BADUSER" );
818 /* username does not exist */
819 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
820 _("last.fm username is incorrect, please verify your settings")
825 p_buffer_pos = strstr( ( char * ) p_buffer, "UPDATE" );
828 /* protocol has been updated, developers need to work :) */
829 msg_Dbg( p_intf, "Protocol updated" );
830 msg_Dbg( p_intf, p_buffer_pos );
835 p_buffer_pos = strstr( ( char * ) p_buffer, "UPTODATE" );
838 msg_Dbg( p_intf, "Protocol error" );
843 psz_buffer_substring = strndup( strstr( p_buffer_pos, "\n" ) + 1, 32 );
844 if ( !psz_buffer_substring )
850 ps_challenge_md5 = malloc( sizeof( char ) * 32 );
851 if ( !ps_challenge_md5 )
855 memcpy( ps_challenge_md5, psz_buffer_substring, 32 );
856 free( psz_buffer_substring );
859 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
861 free( p_sys->psz_submit_host );
862 free( p_sys->psz_submit_file );
864 psz_url_parser = p_buffer_pos + strlen( "http://" );
866 i_url_pos = strcspn( psz_url_parser, ":" );
867 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
869 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
871 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
872 i_url_pos = strcspn( psz_url_parser, "\n" );
873 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
877 p_struct_md5 = malloc( sizeof( struct md5_s ) );
883 psz_password = config_GetPsz(p_this, "lastfm-password");
889 /* generates a md5 hash of the password */
890 InitMD5( p_struct_md5 );
891 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
892 EndMD5( p_struct_md5 );
894 free( psz_password );
896 psz_password_md5 = malloc ( 33 * sizeof( char ) );
897 if ( !psz_password_md5 )
902 for ( i = 0; i < 4; i++ )
904 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
905 p_struct_md5->p_digest[i] & 0xff,
906 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
907 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
908 p_struct_md5->p_digest[i] >> 24
912 /* generates a md5 hash of :
913 * - md5 hash of the password
914 * - md5 challenge sent by last.fm server
916 InitMD5( p_struct_md5 );
917 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
918 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
919 EndMD5( p_struct_md5 );
921 free( ps_challenge_md5 );
922 free( psz_password_md5 );
924 for ( i = 0; i < 4; i++ )
926 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
927 p_struct_md5->p_digest[i] & 0xff,
928 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
929 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
930 p_struct_md5->p_digest[i] >> 24
934 p_sys->psz_response_md5[32] = 0;
936 vlc_mutex_unlock ( &p_sys->lock );
942 vlc_mutex_unlock( &p_sys->lock );
947 free( p_struct_md5 );
948 free( psz_buffer_substring );
950 vlc_mutex_unlock( &p_sys->lock );
954 /*****************************************************************************
955 * DeleteQueue : Free all songs from an audioscrobbler_queue_t
956 *****************************************************************************/
957 void DeleteQueue( audioscrobbler_queue_t *p_queue )
961 for( i = 0; i < p_queue->i_songs_nb; i++ )
963 free( p_queue->p_queue[i]->psz_a );
964 free( p_queue->p_queue[i]->psz_b );
965 free( p_queue->p_queue[i]->psz_t );
966 free( p_queue->p_queue[i]->psz_i );
967 free( p_queue->p_queue[i] );
971 /*****************************************************************************
972 * ReadMetaData : Read meta data when parsed by vlc
973 * or wait for fetching if unavailable
974 *****************************************************************************/
975 static int ReadMetaData( intf_thread_t *p_this )
977 playlist_t *p_playlist;
978 input_thread_t *p_input = NULL;
979 vlc_value_t video_val;
981 intf_sys_t *p_sys = p_this->p_sys;
983 p_playlist = pl_Yield( p_this );
985 p_input = p_playlist->p_input;
990 pl_Release( p_playlist );
991 return( VLC_SUCCESS );
994 vlc_object_yield( p_input );
996 pl_Release( p_playlist );
998 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
999 if( ( video_val.i_int > 0 ) || \
1000 ( p_input->input.p_item->i_type == ITEM_TYPE_NET ) )
1002 msg_Dbg( p_this, "Not an audio file -> no submission");
1003 vlc_object_release( p_input );
1005 vlc_mutex_lock( &p_sys->lock );
1006 p_sys->b_queued = VLC_TRUE;
1007 p_sys->b_metadata_read = VLC_TRUE;
1008 vlc_mutex_unlock( &p_sys->lock );
1013 return ReadLocalMetaData( p_this, p_input );
1016 /*****************************************************************************
1017 * ReadLocalMetaData : Puts current song's meta data in p_sys->p_current_song
1018 *****************************************************************************/
1019 static int ReadLocalMetaData( intf_thread_t *p_this, input_thread_t *p_input )
1021 char *psz_title = NULL;
1022 char *psz_artist = NULL;
1023 char *psz_album = NULL;
1024 char *psz_trackid = NULL;
1026 vlc_bool_t b_waiting;
1029 intf_sys_t *p_sys = p_this->p_sys;
1031 i_status = p_input->input.p_item->p_meta->i_status;
1033 vlc_mutex_lock( &p_sys->lock );
1034 b_waiting = p_sys->b_waiting_meta;
1035 vlc_mutex_unlock( &p_sys->lock );
1037 /* TODO : remove if (1) when meta_engine works */
1038 if ( (1/*( i_status & ITEM_PREPARSED )*/&& ( b_waiting == VLC_FALSE ) ) || \
1039 ( ( i_status & ITEM_META_FETCHED ) && ( b_waiting == VLC_TRUE ) ) )
1041 if ( p_input->input.p_item->p_meta->psz_artist )
1043 psz_artist = encode_URI_component(
1044 p_input->input.p_item->p_meta->psz_artist );
1052 msg_Dbg( p_this, "No artist.." );
1053 if ( b_waiting == VLC_TRUE )
1055 psz_artist = calloc( 1, sizeof( char ) );
1059 goto waiting_meta_data_fetching;
1062 if ( p_input->input.p_item->psz_name )
1064 psz_title = encode_URI_component( p_input->input.p_item->psz_name );
1072 msg_Dbg( p_this, "No track name.." );
1073 if ( b_waiting == VLC_TRUE )
1075 psz_title = calloc( 1, sizeof( char ) );
1079 goto waiting_meta_data_fetching;
1083 if ( p_input->input.p_item->p_meta->psz_trackid )
1085 psz_trackid = strdup( p_input->input.p_item->p_meta->psz_trackid );
1093 psz_trackid = calloc( 1, sizeof( char ) );
1096 if ( p_input->input.p_item->p_meta->psz_album )
1098 psz_album = encode_URI_component(
1099 p_input->input.p_item->p_meta->psz_album );
1107 psz_album = calloc( 1, sizeof( char ) );
1110 i_length = p_input->input.p_item->i_duration / 1000000;
1112 vlc_object_release( p_input );
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" );
1131 free( psz_trackid );
1138 waiting_meta_data_fetching:
1139 vlc_object_release( p_input );
1141 vlc_mutex_lock( &p_sys->lock );
1142 p_sys->b_waiting_meta = VLC_TRUE;
1143 vlc_mutex_unlock( &p_sys->lock );
1152 vlc_object_release( p_input );
1157 free( psz_trackid );