1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright © 2006-2008 the VideoLAN team
7 * Author: Rafaël Carré <funman at videolanorg>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /* audioscrobbler protocol version: 1.2
25 * http://www.audioscrobbler.net/development/protocol/
27 * TODO: "Now Playing" feature (not mandatory)
29 /*****************************************************************************
31 *****************************************************************************/
38 #include <vlc_interface.h>
41 #include <vlc_block.h>
42 #include <vlc_stream.h>
44 #include <vlc_network.h>
45 #include <vlc_interface.h>
46 #include <vlc_playlist.h>
48 /*****************************************************************************
50 *****************************************************************************/
54 /* Keeps track of metadata to be submitted */
55 typedef struct audioscrobbler_song_t
57 char *psz_a; /**< track artist */
58 char *psz_t; /**< track title */
59 char *psz_b; /**< track album */
60 char *psz_n; /**< track number */
61 int i_l; /**< track length */
62 char *psz_m; /**< musicbrainz id */
63 time_t date; /**< date since epoch */
64 } audioscrobbler_song_t;
68 audioscrobbler_song_t p_queue[QUEUE_MAX]; /**< songs not submitted yet*/
69 int i_songs; /**< number of songs */
71 vlc_mutex_t lock; /**< p_sys mutex */
73 /* data about audioscrobbler session */
74 mtime_t next_exchange; /**< when can we send data */
75 unsigned int i_interval; /**< waiting interval (secs)*/
77 /* submission of played songs */
78 char *psz_submit_host; /**< where to submit data */
79 int i_submit_port; /**< port to which submit */
80 char *psz_submit_file; /**< file to which submit */
82 /* submission of playing song */
84 char *psz_nowp_host; /**< where to submit data */
85 int i_nowp_port; /**< port to which submit */
86 char *psz_nowp_file; /**< file to which submit */
88 vlc_bool_t b_handshaked; /**< are we authenticated ? */
89 char psz_auth_token[33]; /**< Authentication token */
91 /* data about song currently playing */
92 audioscrobbler_song_t p_current_song; /**< song being played */
94 mtime_t time_pause; /**< time when vlc paused */
95 mtime_t time_total_pauses; /**< total time in pause */
97 vlc_bool_t b_submit; /**< do we have to submit ? */
99 vlc_bool_t b_state_cb; /**< if we registered the
100 * "state" callback */
102 vlc_bool_t b_meta_read; /**< if we read the song's
103 * metadata already */
106 static int Open ( vlc_object_t * );
107 static void Close ( vlc_object_t * );
108 static void Unload ( intf_thread_t * );
109 static void Run ( intf_thread_t * );
111 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
112 vlc_value_t, void * );
113 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
114 vlc_value_t, void * );
116 static void AddToQueue ( intf_thread_t * );
117 static int Handshake ( intf_thread_t * );
118 static int ReadMetaData ( intf_thread_t * );
119 static void DeleteSong ( audioscrobbler_song_t* );
120 static int ParseURL ( char *, char **, char **, int * );
121 static void HandleInterval ( mtime_t *, unsigned int * );
123 /*****************************************************************************
125 ****************************************************************************/
127 #define USERNAME_TEXT N_("Username")
128 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
129 #define PASSWORD_TEXT N_("Password")
130 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
132 /* This error value is used when last.fm plugin has to be unloaded. */
133 #define VLC_AUDIOSCROBBLER_EFATAL -69
135 /* last.fm client identifier */
136 #define CLIENT_NAME PACKAGE
137 #define CLIENT_VERSION VERSION
139 /* HTTP POST request : to submit data */
140 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
141 "Accept-Encoding: identity\n" \
142 "Content-length: %u\n" \
143 "Connection: close\n" \
144 "Content-type: application/x-www-form-urlencoded\n" \
146 "User-agent: VLC Media Player/%s\r\n" \
152 set_category( CAT_INTERFACE );
153 set_subcategory( SUBCAT_INTERFACE_CONTROL );
154 set_shortname( N_( "Audioscrobbler" ) );
155 set_description( N_("Submission of played songs to last.fm") );
156 add_string( "lastfm-username", "", NULL,
157 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
158 add_password( "lastfm-password", "", NULL,
159 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
160 set_capability( "interface", 0 );
161 set_callbacks( Open, Close );
164 /*****************************************************************************
165 * Open: initialize and create stuff
166 *****************************************************************************/
167 static int Open( vlc_object_t *p_this )
169 playlist_t *p_playlist;
170 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
171 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
176 p_intf->p_sys = p_sys;
178 vlc_mutex_init( p_this, &p_sys->lock );
180 p_playlist = pl_Yield( p_intf );
182 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
184 pl_Release( p_playlist );
186 p_intf->pf_run = Run;
191 /*****************************************************************************
192 * Close: destroy interface stuff
193 *****************************************************************************/
194 static void Close( vlc_object_t *p_this )
196 playlist_t *p_playlist;
197 input_thread_t *p_input;
198 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
199 intf_sys_t *p_sys = p_intf->p_sys;
201 p_playlist = pl_Yield( p_intf );
204 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
206 p_input = p_playlist->p_input;
209 vlc_object_yield( p_input );
211 if( p_sys->b_state_cb )
212 var_DelCallback( p_input, "state", PlayingChange, p_intf );
214 vlc_object_release( p_input );
218 pl_Release( p_playlist );
220 p_intf->b_dead = VLC_TRUE;
221 /* we lock the mutex in case p_sys is being accessed from a callback */
222 vlc_mutex_lock ( &p_sys->lock );
224 for( i = 0; i < p_sys->i_songs; i++ )
225 DeleteSong( &p_sys->p_queue[i] );
226 free( p_sys->psz_submit_host );
227 free( p_sys->psz_submit_file );
229 free( p_sys->psz_nowp_host );
230 free( p_sys->psz_nowp_file );
232 vlc_mutex_unlock ( &p_sys->lock );
233 vlc_mutex_destroy( &p_sys->lock );
238 /*****************************************************************************
239 * Unload: Unloads the audioscrobbler when encountering fatal errors
240 *****************************************************************************/
241 static void Unload( intf_thread_t *p_this )
243 vlc_object_kill( p_this );
244 vlc_object_detach( p_this );
245 if( p_this->p_module )
246 module_Unneed( p_this, p_this->p_module );
247 vlc_mutex_destroy( &p_this->change_lock );
248 vlc_object_destroy( p_this );
251 /*****************************************************************************
252 * Run : call Handshake() then submit songs
253 *****************************************************************************/
254 static void Run( intf_thread_t *p_intf )
256 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
259 uint8_t p_buffer[1024];
263 intf_sys_t *p_sys = p_intf->p_sys;
268 vlc_bool_t b_die = VLC_FALSE, b_wait = VLC_FALSE;
270 vlc_object_lock( p_intf );
271 if( vlc_object_alive( p_intf ) )
273 if( mdate() < p_sys->next_exchange )
274 /* wait until we can resubmit, i.e. */
275 b_wait = !vlc_object_timedwait( p_intf,
276 p_sys->next_exchange );
278 /* wait for data to submit */
279 /* we are signaled each time there is a song to submit */
280 vlc_object_wait( p_intf );
282 b_die = !vlc_object_alive( p_intf );
283 vlc_object_unlock( p_intf );
287 msg_Dbg( p_intf, "audioscrobbler is dying");
291 continue; /* holding on until next_exchange */
293 /* handshake if needed */
294 if( p_sys->b_handshaked == VLC_FALSE )
296 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
298 switch( Handshake( p_intf ) )
305 /* username not set */
306 intf_UserFatal( p_intf, VLC_FALSE,
307 _("Last.fm username not set"),
308 _("Please set a username or disable the "
309 "audioscrobbler plugin, and restart VLC.\n"
310 "Visit http://www.last.fm/join/ to get an account.")
316 msg_Dbg( p_intf, "Handshake successfull :)" );
317 p_sys->b_handshaked = VLC_TRUE;
318 p_sys->i_interval = 0;
319 p_sys->next_exchange = mdate();
322 case VLC_AUDIOSCROBBLER_EFATAL:
323 msg_Warn( p_intf, "Unloading..." );
329 /* protocol error : we'll try later */
330 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
333 /* if handshake failed let's restart the loop */
334 if( p_sys->b_handshaked == VLC_FALSE )
338 msg_Dbg( p_intf, "Going to submit some data..." );
340 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
341 { /* Out of memory */
346 /* forge the HTTP POST request */
347 vlc_mutex_lock( &p_sys->lock );
348 audioscrobbler_song_t *p_song;
349 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
351 p_song = &p_sys->p_queue[i_song];
352 if( !asprintf( &psz_submit_song,
353 "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s"
354 "&i%%5B%d%%5D=%llu&o%%5B%d%%5D=P&r%%5B%d%%5D="
355 "&l%%5B%d%%5D=%d&b%%5B%d%%5D=%s"
356 "&n%%5B%d%%5D=%s&m%%5B%d%%5D=%s",
357 i_song, p_song->psz_a, i_song, p_song->psz_t,
358 i_song, (uintmax_t)p_song->date, i_song, i_song,
359 i_song, p_song->i_l, i_song, p_song->psz_b,
360 i_song, p_song->psz_n, i_song, p_song->psz_m
362 { /* Out of memory */
363 vlc_mutex_unlock( &p_sys->lock );
367 psz_submit_tmp = psz_submit;
368 if( !asprintf( &psz_submit, "%s%s",
369 psz_submit_tmp, psz_submit_song ) )
370 { /* Out of memory */
371 free( psz_submit_tmp );
372 free( psz_submit_song );
373 vlc_mutex_unlock( &p_sys->lock );
377 free( psz_submit_song );
378 free( psz_submit_tmp );
380 vlc_mutex_unlock( &p_sys->lock );
382 i_post_socket = net_ConnectTCP( p_intf,
383 p_sys->psz_submit_host, p_sys->i_submit_port );
385 if ( i_post_socket == -1 )
387 /* If connection fails, we assume we must handshake again */
388 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
389 p_sys->b_handshaked = VLC_FALSE;
394 /* we transmit the data */
395 i_net_ret = net_Printf(
396 VLC_OBJECT( p_intf ), i_post_socket, NULL,
397 POST_REQUEST, p_sys->psz_submit_file,
398 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
403 if ( i_net_ret == -1 )
405 /* If connection fails, we assume we must handshake again */
406 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
407 p_sys->b_handshaked = VLC_FALSE;
411 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
412 p_buffer, 1023, VLC_FALSE );
413 if ( i_net_ret <= 0 )
415 /* if we get no answer, something went wrong : try again */
419 net_Close( i_post_socket );
420 p_buffer[i_net_ret] = '\0';
422 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
425 msg_Warn( p_intf, "%s", p_buffer_pos );
426 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
430 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
433 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
434 p_sys->b_handshaked = VLC_FALSE;
435 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
439 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
443 for( i = 0; i < p_sys->i_songs; i++ )
444 DeleteSong( &p_sys->p_queue[i] );
446 p_sys->i_interval = 0;
447 p_sys->next_exchange = mdate();
448 msg_Dbg( p_intf, "Submission successful!" );
452 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
454 p_sys->b_handshaked = VLC_FALSE;
455 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
461 /*****************************************************************************
462 * PlayingChange: Playing status change callback
463 *****************************************************************************/
464 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
465 vlc_value_t oldval, vlc_value_t newval, void *p_data )
467 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
468 intf_sys_t *p_sys = p_intf->p_sys;
470 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
475 if( p_sys->b_meta_read == VLC_FALSE && newval.i_int >= PLAYING_S )
477 ReadMetaData( p_intf );
481 if( newval.i_int >= END_S )
482 AddToQueue( p_intf );
483 else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S )
484 p_sys->time_pause = mdate();
485 else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S )
486 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
491 /*****************************************************************************
492 * ItemChange: Playlist item change callback
493 *****************************************************************************/
494 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
495 vlc_value_t oldval, vlc_value_t newval, void *p_data )
497 playlist_t *p_playlist;
498 input_thread_t *p_input;
499 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
500 intf_sys_t *p_sys = p_intf->p_sys;
501 input_item_t *p_item;
502 vlc_value_t video_val;
504 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
505 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
510 p_sys->b_state_cb = VLC_FALSE;
511 p_sys->b_meta_read = VLC_FALSE;
512 p_sys->b_submit = VLC_FALSE;
514 p_playlist = pl_Yield( p_intf );
516 p_input = p_playlist->p_input;
518 if( !p_input || p_input->b_dead )
521 pl_Release( p_playlist );
525 vlc_object_yield( p_input );
527 pl_Release( p_playlist );
529 p_item = input_GetItem( p_input );
532 vlc_object_release( p_input );
536 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
537 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
539 msg_Dbg( p_this, "Not an audio local file, not submitting");
540 vlc_object_release( p_input );
544 p_sys->time_total_pauses = 0;
545 time( &p_sys->p_current_song.date );
547 var_AddCallback( p_input, "state", PlayingChange, p_intf );
548 p_sys->b_state_cb = VLC_TRUE;
550 if( input_item_IsPreparsed( p_item ) )
551 ReadMetaData( p_intf );
552 /* if the input item was not preparsed, we'll do it in PlayingChange()
553 * callback, when "state" == PLAYING_S */
555 vlc_object_release( p_input );
559 /*****************************************************************************
560 * AddToQueue: Add the played song to the queue to be submitted
561 *****************************************************************************/
562 static void AddToQueue ( intf_thread_t *p_this )
565 intf_sys_t *p_sys = p_this->p_sys;
567 vlc_mutex_lock( &p_sys->lock );
568 if( !p_sys->b_submit )
571 /* wait for the user to listen enough before submitting */
572 played_time = mdate();
573 played_time -= p_sys->p_current_song.date;
574 played_time -= p_sys->time_total_pauses;
575 played_time /= 1000000; /* µs → s */
577 if( ( played_time < 240 ) &&
578 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
580 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
584 if( p_sys->p_current_song.i_l < 30 )
586 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
590 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
591 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
593 msg_Dbg( p_this, "Missing artist or title, not submitting" );
594 /*XXX*/ msg_Dbg( p_this, "%s %s", p_sys->p_current_song.psz_a, p_sys->p_current_song.psz_t );
598 if( p_sys->i_songs >= QUEUE_MAX )
600 msg_Warn( p_this, "Submission queue is full, not submitting" );
604 msg_Dbg( p_this, "Song will be submitted." );
606 #define QUEUE_COPY( a ) \
607 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
609 #define QUEUE_COPY_NULL( a ) \
611 p_sys->p_current_song.a = NULL
614 QUEUE_COPY_NULL( psz_n );
615 QUEUE_COPY_NULL( psz_a );
616 QUEUE_COPY_NULL( psz_t );
617 QUEUE_COPY_NULL( psz_b );
618 QUEUE_COPY_NULL( psz_m );
620 #undef QUEUE_COPY_NULL
625 /* signal the main loop we have something to submit */
626 vlc_object_signal( VLC_OBJECT( p_this ) );
629 DeleteSong( &p_sys->p_current_song );
630 p_sys->b_submit = VLC_FALSE;
631 vlc_mutex_unlock( &p_sys->lock );
634 /*****************************************************************************
635 * ParseURL : Split an http:// URL into host, file, and port
637 * Example: "62.216.251.205:80/protocol_1.2"
638 * will be split into "62.216.251.205", 80, "protocol_1.2"
640 * psz_url will be freed before returning
641 * *psz_file & *psz_host will be freed before use
644 * VLC_ENOMEM Out Of Memory
645 * VLC_EGENERIC Invalid url provided
646 * VLC_SUCCESS Success
647 *****************************************************************************/
648 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
652 int i_len = strlen( psz_url );
653 FREENULL( *psz_host );
654 FREENULL( *psz_file );
656 i_pos = strcspn( psz_url, ":" );
660 *psz_host = strndup( psz_url, i_pos );
664 i_pos++; /* skip the ':' */
665 *i_port = atoi( psz_url + i_pos );
668 FREENULL( *psz_host );
672 i_pos = strcspn( psz_url, "/" );
677 i_pos++; /* skip the '/' */
678 *psz_file = strdup( psz_url + i_pos );
681 FREENULL( *psz_host );
689 /*****************************************************************************
690 * Handshake : Init audioscrobbler connection
691 *****************************************************************************/
692 static int Handshake( intf_thread_t *p_this )
694 char *psz_username, *psz_password;
696 char psz_timestamp[33];
698 struct md5_s p_struct_md5;
701 char *psz_handshake_url;
702 uint8_t p_buffer[1024];
708 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
709 intf_sys_t *p_sys = p_this->p_sys;
711 psz_username = config_GetPsz( p_this, "lastfm-username" );
715 psz_password = config_GetPsz( p_this, "lastfm-password" );
718 free( psz_username );
722 /* username or password have not been setup */
723 if ( !*psz_username || !*psz_password )
725 free( psz_username );
726 free( psz_password );
732 /* generates a md5 hash of the password */
733 InitMD5( &p_struct_md5 );
734 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
735 EndMD5( &p_struct_md5 );
737 free( psz_password );
739 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
740 if( !psz_password_md5 )
742 free( psz_username );
746 snprintf( psz_timestamp, 33, "%llu", (uintmax_t)timestamp );
748 /* generates a md5 hash of :
749 * - md5 hash of the password, plus
750 * - timestamp in clear text
752 InitMD5( &p_struct_md5 );
753 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
754 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
755 EndMD5( &p_struct_md5 );
756 free( psz_password_md5 );
758 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
759 if( !psz_auth_token )
761 free( psz_username );
764 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
765 free( psz_auth_token );
767 if( !asprintf( &psz_handshake_url,
768 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
769 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
770 p_sys->psz_auth_token ) )
772 free( psz_username );
775 free( psz_username );
777 /* send the http handshake request */
778 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
779 free( psz_handshake_url );
785 i_ret = stream_Read( p_stream, p_buffer, 1023 );
788 stream_Delete( p_stream );
791 p_buffer[i_ret] = '\0';
792 stream_Delete( p_stream );
794 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
797 /* handshake request failed, sorry */
798 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
802 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
805 /* authentication failed, bad username/password combination */
806 intf_UserFatal( p_this, VLC_FALSE,
807 _("last.fm: Authentication failed"),
808 _("last.fm username or password is incorrect. "
809 "Please verify your settings and relaunch VLC." ) );
810 return VLC_AUDIOSCROBBLER_EFATAL;
813 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
816 /* oops, our version of vlc has been banned by last.fm servers */
817 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
818 "You should upgrade VLC, or disable the last.fm plugin." );
819 return VLC_AUDIOSCROBBLER_EFATAL;
822 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
825 /* The system clock isn't good */
826 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
827 "much shifted. Please correct it, and relaunch VLC." );
828 return VLC_AUDIOSCROBBLER_EFATAL;
831 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
835 p_buffer_pos = strstr( p_buffer_pos, "\n" );
836 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
838 p_buffer_pos++; /* we skip the '\n' */
840 /* save the session ID */
841 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
843 p_buffer_pos = strstr( p_buffer_pos, "http://" );
844 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
847 /* We need to read the nowplaying url */
848 p_buffer_pos += 7; /* we skip "http://" */
850 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
854 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
855 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
866 p_buffer_pos = strstr( p_buffer_pos, "http://" );
867 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
870 /* We need to read the submission url */
871 p_buffer_pos += 7; /* we skip "http://" */
872 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
876 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
877 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
894 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
898 /*****************************************************************************
899 * DeleteSong : Delete the char pointers in a song
900 *****************************************************************************/
901 static void DeleteSong( audioscrobbler_song_t* p_song )
903 FREENULL( p_song->psz_a );
904 FREENULL( p_song->psz_b );
905 FREENULL( p_song->psz_t );
906 FREENULL( p_song->psz_m );
907 FREENULL( p_song->psz_n );
910 /*****************************************************************************
911 * ReadMetaData : Read meta data when parsed by vlc
912 *****************************************************************************/
913 static int ReadMetaData( intf_thread_t *p_this )
915 playlist_t *p_playlist;
916 input_thread_t *p_input;
917 input_item_t *p_item;
919 intf_sys_t *p_sys = p_this->p_sys;
921 p_playlist = pl_Yield( p_this );
923 p_input = p_playlist->p_input;
927 pl_Release( p_playlist );
928 return( VLC_SUCCESS );
931 vlc_object_yield( p_input );
933 pl_Release( p_playlist );
935 p_item = input_GetItem( p_input );
940 #define ALLOC_ITEM_META( a, b ) \
941 psz_meta = input_item_Get##b( p_item ); \
942 if( psz_meta && *psz_meta ) \
944 a = encode_URI_component( psz_meta ); \
953 vlc_mutex_lock( &p_sys->lock );
955 p_sys->b_meta_read = VLC_TRUE;
957 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
960 vlc_mutex_unlock( &p_sys->lock );
961 msg_Dbg( p_this, "No artist.." );
962 vlc_object_release( p_input );
967 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
970 vlc_mutex_unlock( &p_sys->lock );
971 msg_Dbg( p_this, "No track name.." );
972 vlc_object_release( p_input );
973 free( p_sys->p_current_song.psz_a );
978 /* Now we have read the mandatory meta data, so we can submit that info */
979 p_sys->b_submit = VLC_TRUE;
981 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
983 p_sys->p_current_song.psz_b = calloc( 1, 1 );
985 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
987 p_sys->p_current_song.psz_m = calloc( 1, 1 );
989 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
991 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
993 p_sys->p_current_song.psz_n = calloc( 1, 1 );
994 #undef ALLOC_ITEM_META
996 msg_Dbg( p_this, "Meta data registered" );
998 vlc_mutex_unlock( &p_sys->lock );
999 vlc_object_release( p_input );
1004 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1006 if( *i_interval == 0 )
1008 /* first interval is 1 minute */
1013 /* else we double the previous interval, up to 120 minutes */
1015 if( *i_interval > 120 )
1018 *next = mdate() + ( *i_interval * 1000000 * 60 );