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 #include <vlc_interface.h>
44 #include <vlc_block.h>
45 #include <vlc_stream.h>
47 #include <vlc_network.h>
48 #include <vlc_interface.h>
49 #include <vlc_playlist.h>
51 /*****************************************************************************
53 *****************************************************************************/
55 /* Keeps track of metadata to be 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 time_t time_next_exchange; /* when can we send data? */
83 char *psz_submit_host; /* where to submit data ? */
84 int i_submit_port; /* at which port ? */
85 char *psz_submit_file; /* in which file ? */
86 char *psz_username; /* last.fm username */
87 vlc_bool_t b_handshaked; /* did we handshake ? */
88 char *psz_response_md5; /* md5 response to use */
90 /* data about song currently playing */
91 audioscrobbler_song_t *p_current_song; /* song being played */
92 time_t time_pause; /* time when vlc paused */
93 time_t time_total_pauses; /* sum of time in pause */
94 vlc_bool_t b_queued; /* has it been queud ? */
95 vlc_bool_t b_metadata_read; /* did we read metadata ? */
96 vlc_bool_t b_paused; /* is vlc paused ? */
97 vlc_bool_t b_waiting_meta; /* we need fetched data? */
100 intf_sys_t *p_sys_global; /* to retrieve p_sys in Run() thread */
102 static int Open ( vlc_object_t * );
103 static void Close ( vlc_object_t * );
104 static void Run ( intf_thread_t * );
105 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
106 vlc_value_t, void * );
107 static int PlayingChange( vlc_object_t *, const char *, vlc_value_t,
108 vlc_value_t, void * );
109 static int AddToQueue ( intf_thread_t *p_this );
110 static int Handshake ( intf_thread_t *p_sd );
111 static int ReadMetaData ( intf_thread_t *p_this );
112 void DeleteQueue ( audioscrobbler_queue_t *p_queue );
114 /*****************************************************************************
116 ****************************************************************************/
118 #define USERNAME_TEXT N_("Username")
119 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
120 #define PASSWORD_TEXT N_("Password")
121 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
123 /* if something goes wrong, we wait at least one minute before trying again */
124 #define DEFAULT_INTERVAL 60
126 /* last.fm client identifier */
127 #define CLIENT_NAME PACKAGE
128 #define CLIENT_VERSION VERSION
130 /* HTTP POST request : to submit data */
131 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
132 "Accept-Encoding: identity\n" \
133 "Content-length: %d\n" \
134 "Connection: close\n" \
135 "Content-type: application/x-www-form-urlencoded\n" \
137 "User-agent: VLC Media Player/%s\r\n" \
143 #define POST_DATA "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s&b%%5B%d%%5D=%s" \
144 "&m%%5B%d%%5D=%s&l%%5B%d%%5D=%d&i%%5B%d%%5D=%s"
145 #define HTTPPOST_MAXLEN 2048
148 set_category( CAT_INTERFACE );
149 set_subcategory( SUBCAT_INTERFACE_CONTROL );
150 set_shortname( N_( "Audioscrobbler" ) );
151 set_description( N_("Audioscrobbler submission Plugin") );
152 add_string( "lastfm-username", "", NULL,
153 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
154 add_string( "lastfm-password", "", NULL,
155 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
156 set_capability( "interface", 0 );
157 set_callbacks( Open, Close );
160 /*****************************************************************************
161 * Open: initialize and create stuff
162 *****************************************************************************/
164 static int Open( vlc_object_t *p_this )
166 playlist_t *p_playlist;
167 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
168 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
171 free( p_sys->p_current_song ); \
172 free( p_sys->p_first_queue ); \
173 free( p_sys->psz_response_md5 ); \
183 p_intf->p_sys = p_sys;
185 vlc_mutex_init( p_this, &p_sys->lock );
187 p_sys_global = p_sys;
188 p_sys->psz_submit_host = NULL;
189 p_sys->psz_submit_file = NULL;
190 p_sys->b_handshaked = VLC_FALSE;
191 p_sys->time_next_exchange = time( NULL );
192 p_sys->psz_username = NULL;
193 p_sys->b_paused = VLC_FALSE;
195 #define MALLOC_CHECK( a ) \
197 vlc_mutex_destroy( &p_sys->lock ); \
201 /* md5 response is 32 chars, + final \0 */
202 p_sys->psz_response_md5 = malloc( 33 );
203 MALLOC_CHECK( p_sys->psz_response_md5 )
205 p_sys->p_first_queue = malloc( sizeof( audioscrobbler_queue_t ) );
206 MALLOC_CHECK( p_sys->p_first_queue )
208 p_sys->p_current_song = malloc( sizeof( audioscrobbler_song_t ) );
209 MALLOC_CHECK( p_sys->p_current_song )
210 time( &p_sys->p_current_song->time_playing );
212 /* queues can't contain more than 10 songs */
213 p_sys->p_first_queue->p_queue =
214 malloc( 10 * sizeof( audioscrobbler_song_t ) );
215 MALLOC_CHECK( p_sys->p_current_song )
217 p_sys->p_first_queue->i_songs_nb = 0;
218 p_sys->p_first_queue->p_next_queue = NULL;
220 p_playlist = pl_Yield( p_intf );
222 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
224 pl_Release( p_playlist );
226 p_intf->pf_run = Run;
233 /*****************************************************************************
234 * Close: destroy interface stuff
235 *****************************************************************************/
236 static void Close( vlc_object_t *p_this )
238 audioscrobbler_queue_t *p_current_queue, *p_next_queue;
239 playlist_t *p_playlist;
240 input_thread_t *p_input;
241 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
242 intf_sys_t *p_sys = p_intf->p_sys;
244 p_playlist = pl_Yield( p_intf );
246 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
248 p_input = p_playlist->p_input;
251 vlc_object_yield( p_input );
252 var_DelCallback( p_input, "state", PlayingChange, p_intf );
253 vlc_object_release( p_input );
257 pl_Release( p_playlist );
259 vlc_mutex_lock ( &p_sys->lock );
260 p_current_queue = p_sys->p_first_queue;
261 vlc_mutex_unlock ( &p_sys->lock );
263 while( ( p_current_queue->i_songs_nb == 10 ) &&
264 ( p_current_queue->p_next_queue != NULL ) )
266 p_next_queue = p_current_queue->p_next_queue;
267 DeleteQueue( p_current_queue );
268 free( p_current_queue );
269 p_current_queue = p_next_queue;
272 DeleteQueue( p_current_queue );
273 free( p_current_queue );
275 vlc_mutex_lock ( &p_sys->lock );
276 free( p_sys->psz_username );
277 free( p_sys->p_current_song );
278 free( p_sys->psz_submit_host );
279 free( p_sys->psz_submit_file );
280 free( p_sys->psz_response_md5 );
281 vlc_mutex_unlock ( &p_sys->lock );
282 vlc_mutex_destroy( &p_sys->lock );
286 /*****************************************************************************
287 * Run : call Handshake() then submit songs
288 *****************************************************************************/
289 static void Run( intf_thread_t *p_this )
291 char *psz_submit = NULL;
292 char *psz_submit_song = NULL;
295 playlist_t *p_playlist;
296 uint8_t *p_buffer = NULL;
297 char *p_buffer_pos = NULL;
298 audioscrobbler_queue_t *p_first_queue;
302 p_this->p_sys = p_sys_global;
303 intf_sys_t *p_sys = p_this->p_sys;
306 free( psz_submit ); \
307 free( psz_submit_song ); \
309 msg_Err( p_this, "Out of memory" ); \
312 psz_submit = malloc( HTTPPOST_MAXLEN );
313 psz_submit_song = malloc( HTTPPOST_MAXLEN );
314 p_buffer = ( uint8_t* ) malloc( 1024 );
316 if( !psz_submit || !psz_submit_song || !p_buffer )
322 while( !p_this->b_die )
324 /* verify if there is data to submit
325 * and if waiting interval is elapsed */
326 if ( ( p_sys->p_first_queue->i_songs_nb > 0 ) &&
327 ( time( NULL ) >= p_sys->time_next_exchange ) )
329 /* handshake if needed */
330 if( p_sys->b_handshaked == VLC_FALSE )
332 msg_Dbg( p_this, "Handshaking with last.fm ..." );
334 switch( Handshake( p_this ) )
341 /* username not set */
342 vlc_mutex_unlock ( &p_sys->lock );
343 intf_UserFatal( p_this, VLC_FALSE,
344 _("Last.fm username not set"),
345 _("Please set an username or disable "
346 "audioscrobbler plugin, and then restart VLC.\n"
347 "Visit https://www.last.fm/join/ to get an account")
350 free( psz_submit_song );
356 msg_Dbg( p_this, "Handshake successfull :)" );
357 vlc_mutex_lock ( &p_sys->lock );
358 p_sys->b_handshaked = VLC_TRUE;
359 vlc_mutex_unlock ( &p_sys->lock );
364 /* protocol error : we'll try later */
365 vlc_mutex_lock ( &p_sys->lock );
366 time( &p_sys->time_next_exchange );
367 p_sys->time_next_exchange += DEFAULT_INTERVAL;
368 vlc_mutex_unlock ( &p_sys->lock );
373 msg_Dbg( p_this, "Going to submit some data..." );
374 vlc_mutex_lock ( &p_sys->lock );
376 snprintf( psz_submit, HTTPPOST_MAXLEN, "u=%s&s=%s",
377 p_sys->psz_username, p_sys->psz_response_md5 );
379 /* forge the HTTP POST request */
380 for (i_song = 0 ; i_song < p_sys->p_first_queue->i_songs_nb ;
383 snprintf( psz_submit_song, HTTPPOST_MAXLEN -1, POST_DATA,
384 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_a,
385 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_t,
386 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_b,
387 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_m,
388 i_song, p_sys->p_first_queue->p_queue[i_song]->i_l,
389 i_song, p_sys->p_first_queue->p_queue[i_song]->psz_i
391 strncat( psz_submit, psz_submit_song, HTTPPOST_MAXLEN - 1 );
394 i_post_socket = net_ConnectTCP( p_this,
395 p_sys->psz_submit_host, p_sys->i_submit_port);
397 if ( i_post_socket == -1 )
399 /* If connection fails, we assume we must handshake again */
400 time( &p_sys->time_next_exchange );
401 p_sys->time_next_exchange += DEFAULT_INTERVAL;
402 p_sys->b_handshaked = VLC_FALSE;
403 vlc_mutex_unlock( &p_sys->lock );
407 /* we transmit the data */
408 i_net_ret = net_Printf(
409 VLC_OBJECT(p_this), i_post_socket, NULL,
410 POST_REQUEST, p_sys->psz_submit_file,
411 strlen( psz_submit ), p_sys->psz_submit_file,
415 if ( i_net_ret == -1 )
417 /* If connection fails, we assume we must handshake again */
418 time( &p_sys->time_next_exchange );
419 p_sys->time_next_exchange += DEFAULT_INTERVAL;
420 p_sys->b_handshaked = VLC_FALSE;
421 vlc_mutex_unlock( &p_sys->lock );
425 memset( p_buffer, '\0', 1024 );
427 i_net_ret = net_Read( p_this, i_post_socket, NULL,
428 p_buffer, 1024, VLC_FALSE );
429 if ( i_net_ret <= 0 )
431 /* if we get no answer, something went wrong : try again */
432 vlc_mutex_unlock( &p_sys->lock );
436 net_Close( i_post_socket );
438 /* record interval */
439 p_buffer_pos = strstr( ( char * ) p_buffer, "INTERVAL" );
442 time( &p_sys->time_next_exchange );
443 p_sys->time_next_exchange += atoi( p_buffer_pos +
444 strlen( "INTERVAL " ) );
447 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
450 /* woops, submission failed */
451 msg_Dbg( p_this, "%s", p_buffer_pos );
452 vlc_mutex_unlock ( &p_sys->lock );
456 p_buffer_pos = strstr( ( char * ) p_buffer, "BADAUTH" );
459 msg_Dbg( p_this, "Authentification failed, handshaking again" );
460 p_sys->b_handshaked = VLC_FALSE;
461 vlc_mutex_unlock ( &p_sys->lock );
465 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
468 if ( p_sys->p_first_queue->i_songs_nb == 10 )
470 /* if there are more than one queue, delete the 1st */
471 p_first_queue = p_sys->p_first_queue->p_next_queue;
472 DeleteQueue( p_sys->p_first_queue );
473 free( p_sys->p_first_queue );
474 p_sys->p_first_queue = p_first_queue;
478 DeleteQueue( p_sys->p_first_queue );
479 p_sys->p_first_queue->i_songs_nb = 0;
481 msg_Dbg( p_this, "Submission successfull!" );
483 vlc_mutex_unlock ( &p_sys->lock );
484 } /* data transmission finished or skipped */
486 msleep( INTF_IDLE_SLEEP );
488 p_playlist = pl_Yield( p_this );
490 if( p_playlist->request.i_status == PLAYLIST_STOPPED )
492 /* if we stopped, we won't submit playing song */
493 vlc_mutex_lock( &p_sys->lock );
494 p_sys->b_queued = VLC_TRUE;
495 p_sys->b_metadata_read = VLC_TRUE;
496 vlc_mutex_unlock( &p_sys->lock );
499 pl_Release( p_playlist );
501 vlc_mutex_lock( &p_sys->lock );
502 if( p_sys->b_metadata_read == VLC_FALSE )
504 /* we read the metadata of the playing song */
505 time( &played_time );
506 played_time -= p_sys->p_current_song->time_playing;
507 played_time -= p_sys->time_total_pauses;
509 vlc_mutex_unlock( &p_sys->lock );
511 if( played_time > 20 )
512 /* wait at least 20 secondes before reading the meta data
513 * since the songs must be at least 30s */
515 if ( ReadMetaData( p_this ) == VLC_ENOMEM )
523 /* we add the playing song into the queue */
524 if( ( p_sys->b_queued == VLC_FALSE )
525 && ( p_sys->b_paused == VLC_FALSE ) )
527 vlc_mutex_unlock( &p_sys->lock );
528 if( AddToQueue( p_this ) == VLC_ENOMEM )
535 vlc_mutex_unlock( &p_sys->lock );
542 /*****************************************************************************
543 * PlayingChange: Playing status change callback
544 *****************************************************************************/
545 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
546 vlc_value_t oldval, vlc_value_t newval, void *p_data )
548 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
549 intf_sys_t *p_sys = p_intf->p_sys;
551 (void)p_this; (void)psz_var; (void)oldval;
553 /* don't bother if song has already been queued */
554 if( p_sys->b_queued == VLC_TRUE )
557 vlc_mutex_lock( &p_sys->lock );
559 if( newval.i_int == PAUSE_S )
561 time( &p_sys->time_pause );
562 p_sys->b_paused = VLC_TRUE;
565 else if( newval.i_int == PLAYING_S )
567 p_sys->time_total_pauses += time( NULL ) - p_sys->time_pause;
568 p_sys->b_paused = VLC_FALSE;
571 vlc_mutex_unlock( &p_sys->lock );
576 /*****************************************************************************
577 * ItemChange: Playlist item change callback
578 *****************************************************************************/
579 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
580 vlc_value_t oldval, vlc_value_t newval, void *p_data )
582 playlist_t *p_playlist;
583 input_thread_t *p_input = NULL;
587 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
588 intf_sys_t *p_sys = p_intf->p_sys;
589 (void)p_this; (void)psz_var; (void)oldval; (void)newval;
591 p_playlist = pl_Yield( p_intf );
593 p_input = p_playlist->p_input;
598 pl_Release( p_playlist );
600 vlc_mutex_lock( &p_sys->lock );
602 p_sys->b_queued = VLC_TRUE;
603 p_sys->b_metadata_read = VLC_TRUE;
605 vlc_mutex_unlock( &p_sys->lock );
610 vlc_object_yield( p_input );
612 pl_Release( p_playlist );
614 var_AddCallback( p_input, "state", PlayingChange, p_intf );
616 vlc_mutex_lock ( &p_sys->lock );
618 /* reset pause counter */
619 p_sys->time_total_pauses = 0;
621 /* we'll read metadata when it's present */
622 p_sys->b_metadata_read = VLC_FALSE;
623 p_sys->b_waiting_meta = VLC_FALSE;
626 epoch_tm = gmtime( &epoch );
627 snprintf( psz_date, 20, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
628 epoch_tm->tm_year+1900, epoch_tm->tm_mon+1, epoch_tm->tm_mday,
629 epoch_tm->tm_hour, epoch_tm->tm_min, epoch_tm->tm_sec );
631 p_sys->p_current_song->psz_i = encode_URI_component( psz_date );
632 p_sys->p_current_song->time_playing = epoch;
634 p_sys->b_paused = ( p_input->b_dead || !input_GetItem(p_input)->psz_name )
635 ? VLC_TRUE : VLC_FALSE;
637 vlc_mutex_unlock( &p_sys->lock );
639 vlc_object_release( p_input );
643 /*****************************************************************************
644 * AddToQueue: Add the played song to the queue to be submitted
645 *****************************************************************************/
646 static int AddToQueue ( intf_thread_t *p_this )
650 audioscrobbler_queue_t *p_queue = NULL,
651 *p_next_queue = NULL;
652 intf_sys_t *p_sys = p_this->p_sys;
654 /* wait for the user to listen enough before submitting */
655 time ( &played_time );
656 vlc_mutex_lock( &p_sys->lock );
658 played_time -= p_sys->p_current_song->time_playing;
659 played_time -= p_sys->time_total_pauses;
661 #define NO_SUBMISSION \
662 p_sys->b_queued = VLC_TRUE; \
663 vlc_mutex_unlock( &p_sys->lock ); \
666 if( ( played_time < 240 ) &&
667 ( played_time < ( p_sys->p_current_song->i_l / 2 ) ) )
669 //msg_Dbg( p_this, "Song not listened long enough -> waiting" );
670 vlc_mutex_unlock( &p_sys->lock );
674 if( p_sys->p_current_song->i_l < 30 )
676 msg_Dbg( p_this, "Song too short (< 30s) -> not submitting" );
680 if( !*p_sys->p_current_song->psz_a || !*p_sys->p_current_song->psz_t )
682 msg_Dbg( p_this, "Missing artist or title -> not submitting" );
686 msg_Dbg( p_this, "Ok. We'll put it in the queue for submission" );
688 /* go to last queue */
689 p_queue = p_sys->p_first_queue;
690 while( ( p_queue->i_songs_nb == 10 ) && ( p_queue->p_next_queue != NULL ) )
691 p_queue = p_queue->p_next_queue;
693 i_songs_nb = p_queue->i_songs_nb;
695 #define MALLOC_CHECK( a ) \
697 vlc_mutex_unlock( &p_sys->lock ); \
701 if( i_songs_nb == 10 )
703 p_next_queue = malloc( sizeof( audioscrobbler_queue_t ) );
704 MALLOC_CHECK( p_next_queue );
705 p_queue->p_next_queue = p_next_queue;
706 p_queue = p_next_queue;
708 p_queue->i_songs_nb = i_songs_nb;
711 p_queue->p_queue[i_songs_nb] = malloc( sizeof( audioscrobbler_song_t ) );
712 MALLOC_CHECK( p_queue->p_queue[i_songs_nb] );
714 #define QUEUE_COPY( a ) \
715 p_queue->p_queue[i_songs_nb]->a = p_sys->p_current_song->a
724 p_queue->i_songs_nb++;
725 p_sys->b_queued = VLC_TRUE;
727 vlc_mutex_unlock( &p_sys->lock );
735 /*****************************************************************************
736 * Handshake : Init audioscrobbler connection
737 *****************************************************************************/
738 static int Handshake( intf_thread_t *p_this )
740 char *psz_password = NULL;
741 struct md5_s *p_struct_md5 = NULL;
742 char *psz_password_md5 = NULL;
743 char *ps_challenge_md5 = NULL;
745 char *psz_handshake_url = NULL;
746 uint8_t *p_buffer = NULL;
747 char *p_buffer_pos = NULL;
748 char *psz_url_parser = NULL;
749 char *psz_buffer_substring;
752 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
753 intf_sys_t *p_sys = p_this->p_sys;
755 vlc_mutex_lock ( &p_sys->lock );
759 free( p_struct_md5 ); \
760 free( ps_challenge_md5 ); \
761 vlc_mutex_unlock( &p_sys->lock ); \
764 #define PROTOCOL_ERROR \
766 vlc_mutex_unlock( &p_sys->lock ); \
769 #define MALLOC_CHECK( a ) \
775 p_sys->psz_username = config_GetPsz(p_this, "lastfm-username");
776 MALLOC_CHECK( p_sys->psz_username )
778 /* username has not been setup, ignoring */
779 if ( !*p_sys->psz_username )
782 psz_handshake_url = malloc( 1024 );
783 MALLOC_CHECK( p_sys->psz_username )
785 snprintf( psz_handshake_url, 1024,
786 "http://post.audioscrobbler.com/?hs=true&p=1.1&c=%s&v=%s&u=%s",
787 CLIENT_NAME, CLIENT_VERSION, p_sys->psz_username );
789 /* send the http handshake request */
790 p_stream = stream_UrlNew( p_intf, psz_handshake_url);
792 free( psz_handshake_url );
796 vlc_mutex_unlock ( &p_sys->lock );
800 p_buffer = ( uint8_t* ) calloc( 1, 1024 );
803 stream_Delete( p_stream );
808 if ( stream_Read( p_stream, p_buffer, 1024 ) == 0 )
810 stream_Delete( p_stream );
814 stream_Delete( p_stream );
816 /* record interval before next submission */
817 p_buffer_pos = strstr( ( char* ) p_buffer, "INTERVAL" );
820 time( &p_sys->time_next_exchange );
821 p_sys->time_next_exchange +=
822 atoi( p_buffer_pos + strlen( "INTERVAL " ) );
825 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED" );
828 /* handshake request failed, sorry */
829 msg_Dbg( p_this, "%s", p_buffer_pos );
833 p_buffer_pos = strstr( ( char* ) p_buffer, "BADUSER" );
836 /* username does not exist on the server */
837 intf_UserFatal( p_this, VLC_FALSE, _("Bad last.fm Username"),
838 _("last.fm username is incorrect, please verify your settings")
843 p_buffer_pos = strstr( ( char* ) p_buffer, "UPDATE" );
846 /* protocol has been updated, time to update the code */
847 msg_Dbg( p_intf, "Protocol updated : plugin may be outdated" );
848 msg_Dbg( p_intf, "%s", p_buffer_pos );
853 p_buffer_pos = strstr( ( char* ) p_buffer, "UPTODATE" );
856 msg_Dbg( p_intf, "Can't recognize server protocol" );
861 psz_buffer_substring = strstr( p_buffer_pos, "\n" );
862 if( ( psz_buffer_substring == NULL ) || \
863 ( strlen( psz_buffer_substring + 1 ) < 32 ) )
865 msg_Dbg( p_intf, "Can't recognize server protocol" );
870 ps_challenge_md5 = malloc( 32 );
871 MALLOC_CHECK( ps_challenge_md5 )
872 memcpy( ps_challenge_md5, psz_buffer_substring + 1, 32 );
875 p_buffer_pos = ( void* ) strstr( ( char* ) p_buffer, "http://" );
877 /* free old information */
878 free( p_sys->psz_submit_host );
879 free( p_sys->psz_submit_file );
881 psz_url_parser = p_buffer_pos + strlen( "http://" );
882 i_url_pos = strcspn( psz_url_parser, ":" );
884 p_sys->psz_submit_host = strndup( psz_url_parser, i_url_pos );
885 MALLOC_CHECK( p_sys->psz_submit_host )
887 p_sys->i_submit_port = atoi( psz_url_parser + i_url_pos + 1 );
889 psz_url_parser += strcspn( psz_url_parser , "/" ) + 1;
890 i_url_pos = strcspn( psz_url_parser, "\n" );
891 p_sys->psz_submit_file = strndup( psz_url_parser, i_url_pos );
892 MALLOC_CHECK( p_sys->psz_submit_file )
896 p_struct_md5 = malloc( sizeof( struct md5_s ) );
897 MALLOC_CHECK( p_struct_md5 )
899 psz_password = config_GetPsz(p_this, "lastfm-password");
900 MALLOC_CHECK( psz_password )
902 /* generates a md5 hash of the password */
903 InitMD5( p_struct_md5 );
904 AddMD5( p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
905 EndMD5( p_struct_md5 );
907 free( psz_password );
909 psz_password_md5 = malloc ( 33 );
910 MALLOC_CHECK( psz_password_md5 )
912 for ( i = 0; i < 4; i++ )
914 sprintf( &psz_password_md5[8*i], "%02x%02x%02x%02x",
915 p_struct_md5->p_digest[i] & 0xff,
916 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
917 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
918 p_struct_md5->p_digest[i] >> 24
922 /* generates a md5 hash of :
923 * - md5 hash of the password, plus
924 * - md5 challenge sent by last.fm server
926 InitMD5( p_struct_md5 );
927 AddMD5( p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
928 AddMD5( p_struct_md5, ( uint8_t* ) ps_challenge_md5, 32 );
929 EndMD5( p_struct_md5 );
931 free( ps_challenge_md5 );
932 free( psz_password_md5 );
934 for ( i = 0; i < 4; i++ )
936 sprintf( &p_sys->psz_response_md5[8*i], "%02x%02x%02x%02x",
937 p_struct_md5->p_digest[i] & 0xff,
938 ( p_struct_md5->p_digest[i] >> 8 ) & 0xff,
939 ( p_struct_md5->p_digest[i] >> 16 ) & 0xff,
940 p_struct_md5->p_digest[i] >> 24
944 p_sys->psz_response_md5[32] = '\0';
946 vlc_mutex_unlock ( &p_sys->lock );
950 #undef PROTOCOL_ERROR
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 char *psz_title = NULL;
982 char *psz_artist = NULL;
983 char *psz_album = NULL;
984 char *psz_trackid = NULL;
986 vlc_bool_t b_waiting;
987 intf_sys_t *p_sys = p_this->p_sys;
990 p_playlist = pl_Yield( p_this );
992 p_input = p_playlist->p_input;
997 pl_Release( p_playlist );
998 return( VLC_SUCCESS );
1001 vlc_object_yield( p_input );
1003 pl_Release( p_playlist );
1005 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
1006 if( ( video_val.i_int > 0 ) || \
1007 ( input_GetItem(p_input)->i_type == ITEM_TYPE_NET ) )
1009 msg_Dbg( p_this, "Not an audio only local file -> no submission");
1010 vlc_object_release( p_input );
1012 vlc_mutex_lock( &p_sys->lock );
1013 p_sys->b_queued = VLC_TRUE;
1014 p_sys->b_metadata_read = VLC_TRUE;
1015 vlc_mutex_unlock( &p_sys->lock );
1020 #define FREE_INPUT_AND_CHARS \
1021 vlc_object_release( p_input ); \
1022 free( psz_title ); \
1023 free( psz_artist ); \
1024 free( psz_album ); \
1025 free( psz_trackid );
1027 #define WAIT_METADATA_FETCHING( a ) \
1028 if ( b_waiting == VLC_TRUE ) \
1030 a = calloc( 1, 1 ); \
1034 vlc_object_release( p_input ); \
1035 vlc_mutex_lock( &p_sys->lock ); \
1036 p_sys->b_waiting_meta = VLC_TRUE; \
1037 vlc_mutex_unlock( &p_sys->lock ); \
1038 free( psz_artist ); \
1039 return VLC_SUCCESS; \
1042 #define ALLOC_ITEM_META( a, b ) \
1043 if ( input_GetItem(p_input)->b ) \
1045 a = encode_URI_component( \
1046 input_GetItem(p_input)->b ); \
1049 FREE_INPUT_AND_CHARS \
1050 return VLC_ENOMEM; \
1054 i_status = input_GetItem(p_input)->p_meta->i_status;
1056 b_waiting = VLC_TRUE;
1057 /* TODO: meta data fetching doesn't work atm
1058 vlc_mutex_lock( &p_sys->lock );
1059 b_waiting = p_sys->b_waiting_meta;
1060 vlc_mutex_unlock( &p_sys->lock );
1063 /* TODO: item preparsing is buggy on drag&drop and command line arguments
1064 if( i_status & ( !b_waiting ? ITEM_PREPARSED : ITEM_META_FETCHED ) )
1068 ALLOC_ITEM_META( psz_artist, p_meta->psz_artist )
1071 msg_Dbg( p_this, "No artist.." );
1072 WAIT_METADATA_FETCHING( psz_artist )
1075 ALLOC_ITEM_META( psz_title, psz_name )
1078 msg_Dbg( p_this, "No track name.." );
1079 WAIT_METADATA_FETCHING( psz_title );
1082 ALLOC_ITEM_META( psz_album, p_meta->psz_album )
1084 psz_album = calloc( 1, 1 );
1086 ALLOC_ITEM_META( psz_trackid, p_meta->psz_trackid )
1088 psz_trackid = calloc( 1, 1 );
1090 i_length = input_GetItem(p_input)->i_duration / 1000000;
1092 vlc_mutex_lock ( &p_sys->lock );
1094 p_sys->p_current_song->psz_a = strdup( psz_artist );
1095 p_sys->p_current_song->psz_t = strdup( psz_title );
1096 p_sys->p_current_song->psz_b = strdup( psz_album );
1097 p_sys->p_current_song->psz_m = strdup( psz_trackid );
1098 p_sys->p_current_song->i_l = i_length;
1099 p_sys->b_queued = VLC_FALSE;
1100 p_sys->b_metadata_read = VLC_TRUE;
1102 vlc_mutex_unlock( &p_sys->lock );
1104 msg_Dbg( p_this, "Meta data registered, waiting to be queued" );
1107 FREE_INPUT_AND_CHARS
1109 #undef FREE_INPUT_AND_CHARS
1110 #undef ALLOC_ITEM_META
1111 #undef WAIT_METADATA_FETCHING