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 *****************************************************************************/
42 #include <vlc_interface.h>
45 #include <vlc_block.h>
46 #include <vlc_stream.h>
48 #include <vlc_network.h>
49 #include <vlc_interface.h>
50 #include <vlc_playlist.h>
52 /*****************************************************************************
54 *****************************************************************************/
58 /* Keeps track of metadata to be submitted */
59 typedef struct audioscrobbler_song_t
61 char *psz_a; /**< track artist */
62 char *psz_t; /**< track title */
63 char *psz_b; /**< track album */
64 char *psz_n; /**< track number */
65 int i_l; /**< track length */
66 char *psz_m; /**< musicbrainz id */
67 time_t date; /**< date since epoch */
68 } audioscrobbler_song_t;
72 audioscrobbler_song_t p_queue[QUEUE_MAX]; /**< songs not submitted yet*/
73 int i_songs; /**< number of songs */
75 vlc_mutex_t lock; /**< p_sys mutex */
77 /* data about audioscrobbler session */
78 mtime_t next_exchange; /**< when can we send data */
79 unsigned int i_interval; /**< waiting interval (secs)*/
81 /* submission of played songs */
82 char *psz_submit_host; /**< where to submit data */
83 int i_submit_port; /**< port to which submit */
84 char *psz_submit_file; /**< file to which submit */
86 /* submission of playing song */
88 char *psz_nowp_host; /**< where to submit data */
89 int i_nowp_port; /**< port to which submit */
90 char *psz_nowp_file; /**< file to which submit */
92 vlc_bool_t b_handshaked; /**< are we authenticated ? */
93 char psz_auth_token[33]; /**< Authentication token */
95 /* data about song currently playing */
96 audioscrobbler_song_t p_current_song; /**< song being played */
98 mtime_t time_pause; /**< time when vlc paused */
99 mtime_t time_total_pauses; /**< total time in pause */
101 vlc_bool_t b_submit; /**< do we have to submit ? */
103 vlc_bool_t b_state_cb; /**< if we registered the
104 * "state" callback */
106 vlc_bool_t b_meta_read; /**< if we read the song's
107 * metadata already */
110 static int Open ( vlc_object_t * );
111 static void Close ( vlc_object_t * );
112 static void Unload ( intf_thread_t * );
113 static void Run ( intf_thread_t * );
115 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
116 vlc_value_t, void * );
117 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
118 vlc_value_t, void * );
120 static void AddToQueue ( intf_thread_t * );
121 static int Handshake ( intf_thread_t * );
122 static int ReadMetaData ( intf_thread_t * );
123 static void DeleteSong ( audioscrobbler_song_t* );
124 static int ParseURL ( char *, char **, char **, int * );
125 static void HandleInterval ( mtime_t *, unsigned int * );
127 /*****************************************************************************
129 ****************************************************************************/
131 #define USERNAME_TEXT N_("Username")
132 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
133 #define PASSWORD_TEXT N_("Password")
134 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
136 /* This error value is used when last.fm plugin has to be unloaded. */
137 #define VLC_AUDIOSCROBBLER_EFATAL -69
139 /* last.fm client identifier */
140 #define CLIENT_NAME PACKAGE
141 #define CLIENT_VERSION VERSION
143 /* HTTP POST request : to submit data */
144 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
145 "Accept-Encoding: identity\n" \
146 "Content-length: %u\n" \
147 "Connection: close\n" \
148 "Content-type: application/x-www-form-urlencoded\n" \
150 "User-agent: VLC Media Player/%s\r\n" \
156 set_category( CAT_INTERFACE );
157 set_subcategory( SUBCAT_INTERFACE_CONTROL );
158 set_shortname( N_( "Audioscrobbler" ) );
159 set_description( N_("Submission of played songs to last.fm") );
160 add_string( "lastfm-username", "", NULL,
161 USERNAME_TEXT, USERNAME_LONGTEXT, VLC_FALSE );
162 add_password( "lastfm-password", "", NULL,
163 PASSWORD_TEXT, PASSWORD_LONGTEXT, VLC_FALSE );
164 set_capability( "interface", 0 );
165 set_callbacks( Open, Close );
168 /*****************************************************************************
169 * Open: initialize and create stuff
170 *****************************************************************************/
171 static int Open( vlc_object_t *p_this )
173 playlist_t *p_playlist;
174 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
175 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
180 p_intf->p_sys = p_sys;
182 vlc_mutex_init( p_this, &p_sys->lock );
184 p_playlist = pl_Yield( p_intf );
186 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
188 pl_Release( p_playlist );
190 p_intf->pf_run = Run;
195 /*****************************************************************************
196 * Close: destroy interface stuff
197 *****************************************************************************/
198 static void Close( vlc_object_t *p_this )
200 playlist_t *p_playlist;
201 input_thread_t *p_input;
202 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
203 intf_sys_t *p_sys = p_intf->p_sys;
205 p_playlist = pl_Yield( p_intf );
208 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
210 p_input = p_playlist->p_input;
213 vlc_object_yield( p_input );
215 if( p_sys->b_state_cb )
216 var_DelCallback( p_input, "state", PlayingChange, p_intf );
218 vlc_object_release( p_input );
222 pl_Release( p_playlist );
224 p_intf->b_dead = VLC_TRUE;
225 /* we lock the mutex in case p_sys is being accessed from a callback */
226 vlc_mutex_lock ( &p_sys->lock );
228 for( i = 0; i < p_sys->i_songs; i++ )
229 DeleteSong( &p_sys->p_queue[i] );
230 free( p_sys->psz_submit_host );
231 free( p_sys->psz_submit_file );
233 free( p_sys->psz_nowp_host );
234 free( p_sys->psz_nowp_file );
236 vlc_mutex_unlock ( &p_sys->lock );
237 vlc_mutex_destroy( &p_sys->lock );
242 /*****************************************************************************
243 * Unload: Unloads the audioscrobbler when encountering fatal errors
244 *****************************************************************************/
245 static void Unload( intf_thread_t *p_this )
247 vlc_object_kill( p_this );
248 vlc_object_detach( p_this );
249 if( p_this->p_module )
250 module_Unneed( p_this, p_this->p_module );
251 vlc_mutex_destroy( &p_this->change_lock );
252 vlc_object_destroy( p_this );
255 /*****************************************************************************
256 * Run : call Handshake() then submit songs
257 *****************************************************************************/
258 static void Run( intf_thread_t *p_intf )
260 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
263 uint8_t p_buffer[1024];
267 intf_sys_t *p_sys = p_intf->p_sys;
272 vlc_bool_t b_die = VLC_FALSE, b_wait = VLC_FALSE;
274 vlc_object_lock( p_intf );
275 if( vlc_object_alive( p_intf ) )
277 if( mdate() < p_sys->next_exchange )
278 /* wait until we can resubmit, i.e. */
279 b_wait = !vlc_object_timedwait( p_intf,
280 p_sys->next_exchange );
282 /* wait for data to submit */
283 /* we are signaled each time there is a song to submit */
284 vlc_object_wait( p_intf );
286 b_die = !vlc_object_alive( p_intf );
287 vlc_object_unlock( p_intf );
291 msg_Dbg( p_intf, "audioscrobbler is dying");
295 continue; /* holding on until next_exchange */
297 /* handshake if needed */
298 if( p_sys->b_handshaked == VLC_FALSE )
300 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
302 switch( Handshake( p_intf ) )
309 /* username not set */
310 intf_UserFatal( p_intf, VLC_FALSE,
311 _("Last.fm username not set"),
312 _("Please set a username or disable the "
313 "audioscrobbler plugin, and restart VLC.\n"
314 "Visit http://www.last.fm/join/ to get an account.")
320 msg_Dbg( p_intf, "Handshake successfull :)" );
321 p_sys->b_handshaked = VLC_TRUE;
322 p_sys->i_interval = 0;
323 p_sys->next_exchange = mdate();
326 case VLC_AUDIOSCROBBLER_EFATAL:
327 msg_Warn( p_intf, "Unloading..." );
333 /* protocol error : we'll try later */
334 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
337 /* if handshake failed let's restart the loop */
338 if( p_sys->b_handshaked == VLC_FALSE )
342 msg_Dbg( p_intf, "Going to submit some data..." );
344 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
345 { /* Out of memory */
350 /* forge the HTTP POST request */
351 vlc_mutex_lock( &p_sys->lock );
352 audioscrobbler_song_t *p_song;
353 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
355 p_song = &p_sys->p_queue[i_song];
356 if( !asprintf( &psz_submit_song,
357 "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s"
358 "&i%%5B%d%%5D=%llu&o%%5B%d%%5D=P&r%%5B%d%%5D="
359 "&l%%5B%d%%5D=%d&b%%5B%d%%5D=%s"
360 "&n%%5B%d%%5D=%s&m%%5B%d%%5D=%s",
361 i_song, p_song->psz_a, i_song, p_song->psz_t,
362 i_song, (uintmax_t)p_song->date, i_song, i_song,
363 i_song, p_song->i_l, i_song, p_song->psz_b,
364 i_song, p_song->psz_n, i_song, p_song->psz_m
366 { /* Out of memory */
367 vlc_mutex_unlock( &p_sys->lock );
371 psz_submit_tmp = psz_submit;
372 if( !asprintf( &psz_submit, "%s%s",
373 psz_submit_tmp, psz_submit_song ) )
374 { /* Out of memory */
375 free( psz_submit_tmp );
376 free( psz_submit_song );
377 vlc_mutex_unlock( &p_sys->lock );
381 free( psz_submit_song );
382 free( psz_submit_tmp );
384 vlc_mutex_unlock( &p_sys->lock );
386 i_post_socket = net_ConnectTCP( p_intf,
387 p_sys->psz_submit_host, p_sys->i_submit_port );
389 if ( i_post_socket == -1 )
391 /* If connection fails, we assume we must handshake again */
392 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
393 p_sys->b_handshaked = VLC_FALSE;
398 /* we transmit the data */
399 i_net_ret = net_Printf(
400 VLC_OBJECT( p_intf ), i_post_socket, NULL,
401 POST_REQUEST, p_sys->psz_submit_file,
402 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
407 if ( i_net_ret == -1 )
409 /* If connection fails, we assume we must handshake again */
410 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
411 p_sys->b_handshaked = VLC_FALSE;
415 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
416 p_buffer, 1023, VLC_FALSE );
417 if ( i_net_ret <= 0 )
419 /* if we get no answer, something went wrong : try again */
423 net_Close( i_post_socket );
424 p_buffer[i_net_ret] = '\0';
426 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
429 msg_Warn( p_intf, "%s", p_buffer_pos );
430 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
434 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
437 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
438 p_sys->b_handshaked = VLC_FALSE;
439 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
443 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
447 for( i = 0; i < p_sys->i_songs; i++ )
448 DeleteSong( &p_sys->p_queue[i] );
450 p_sys->i_interval = 0;
451 p_sys->next_exchange = mdate();
452 msg_Dbg( p_intf, "Submission successful!" );
456 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
458 p_sys->b_handshaked = VLC_FALSE;
459 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
465 /*****************************************************************************
466 * PlayingChange: Playing status change callback
467 *****************************************************************************/
468 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
469 vlc_value_t oldval, vlc_value_t newval, void *p_data )
471 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
472 intf_sys_t *p_sys = p_intf->p_sys;
474 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
479 if( p_sys->b_meta_read == VLC_FALSE && newval.i_int >= PLAYING_S )
481 ReadMetaData( p_intf );
485 if( newval.i_int >= END_S )
486 AddToQueue( p_intf );
487 else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S )
488 p_sys->time_pause = mdate();
489 else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S )
490 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
495 /*****************************************************************************
496 * ItemChange: Playlist item change callback
497 *****************************************************************************/
498 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
499 vlc_value_t oldval, vlc_value_t newval, void *p_data )
501 playlist_t *p_playlist;
502 input_thread_t *p_input;
503 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
504 intf_sys_t *p_sys = p_intf->p_sys;
505 input_item_t *p_item;
506 vlc_value_t video_val;
508 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
509 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
514 p_sys->b_state_cb = VLC_FALSE;
515 p_sys->b_meta_read = VLC_FALSE;
516 p_sys->b_submit = VLC_FALSE;
518 p_playlist = pl_Yield( p_intf );
520 p_input = p_playlist->p_input;
522 if( !p_input || p_input->b_dead )
525 pl_Release( p_playlist );
529 vlc_object_yield( p_input );
531 pl_Release( p_playlist );
533 p_item = input_GetItem( p_input );
536 vlc_object_release( p_input );
540 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
541 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
543 msg_Dbg( p_this, "Not an audio local file, not submitting");
544 vlc_object_release( p_input );
548 p_sys->time_total_pauses = 0;
549 time( &p_sys->p_current_song.date );
551 var_AddCallback( p_input, "state", PlayingChange, p_intf );
552 p_sys->b_state_cb = VLC_TRUE;
554 if( input_item_IsPreparsed( p_item ) )
555 ReadMetaData( p_intf );
556 /* if the input item was not preparsed, we'll do it in PlayingChange()
557 * callback, when "state" == PLAYING_S */
559 vlc_object_release( p_input );
563 /*****************************************************************************
564 * AddToQueue: Add the played song to the queue to be submitted
565 *****************************************************************************/
566 static void AddToQueue ( intf_thread_t *p_this )
569 intf_sys_t *p_sys = p_this->p_sys;
571 vlc_mutex_lock( &p_sys->lock );
572 if( !p_sys->b_submit )
575 /* wait for the user to listen enough before submitting */
576 played_time = mdate();
577 played_time -= p_sys->p_current_song.date;
578 played_time -= p_sys->time_total_pauses;
579 played_time /= 1000000; /* µs → s */
581 if( ( played_time < 240 ) &&
582 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
584 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
588 if( p_sys->p_current_song.i_l < 30 )
590 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
594 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
595 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
597 msg_Dbg( p_this, "Missing artist or title, not submitting" );
598 /*XXX*/ msg_Dbg( p_this, "%s %s", p_sys->p_current_song.psz_a, p_sys->p_current_song.psz_t );
602 if( p_sys->i_songs >= QUEUE_MAX )
604 msg_Warn( p_this, "Submission queue is full, not submitting" );
608 msg_Dbg( p_this, "Song will be submitted." );
610 #define QUEUE_COPY( a ) \
611 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
613 #define QUEUE_COPY_NULL( a ) \
615 p_sys->p_current_song.a = NULL
618 QUEUE_COPY_NULL( psz_n );
619 QUEUE_COPY_NULL( psz_a );
620 QUEUE_COPY_NULL( psz_t );
621 QUEUE_COPY_NULL( psz_b );
622 QUEUE_COPY_NULL( psz_m );
624 #undef QUEUE_COPY_NULL
629 /* signal the main loop we have something to submit */
630 vlc_object_signal( VLC_OBJECT( p_this ) );
633 DeleteSong( &p_sys->p_current_song );
634 p_sys->b_submit = VLC_FALSE;
635 vlc_mutex_unlock( &p_sys->lock );
638 /*****************************************************************************
639 * ParseURL : Split an http:// URL into host, file, and port
641 * Example: "62.216.251.205:80/protocol_1.2"
642 * will be split into "62.216.251.205", 80, "protocol_1.2"
644 * psz_url will be freed before returning
645 * *psz_file & *psz_host will be freed before use
648 * VLC_ENOMEM Out Of Memory
649 * VLC_EGENERIC Invalid url provided
650 * VLC_SUCCESS Success
651 *****************************************************************************/
652 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
656 int i_len = strlen( psz_url );
657 FREENULL( *psz_host );
658 FREENULL( *psz_file );
660 i_pos = strcspn( psz_url, ":" );
664 *psz_host = strndup( psz_url, i_pos );
668 i_pos++; /* skip the ':' */
669 *i_port = atoi( psz_url + i_pos );
672 FREENULL( *psz_host );
676 i_pos = strcspn( psz_url, "/" );
681 i_pos++; /* skip the '/' */
682 *psz_file = strdup( psz_url + i_pos );
685 FREENULL( *psz_host );
693 /*****************************************************************************
694 * Handshake : Init audioscrobbler connection
695 *****************************************************************************/
696 static int Handshake( intf_thread_t *p_this )
698 char *psz_username, *psz_password;
700 char psz_timestamp[33];
702 struct md5_s p_struct_md5;
705 char *psz_handshake_url;
706 uint8_t p_buffer[1024];
712 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
713 intf_sys_t *p_sys = p_this->p_sys;
715 psz_username = config_GetPsz( p_this, "lastfm-username" );
719 psz_password = config_GetPsz( p_this, "lastfm-password" );
722 free( psz_username );
726 /* username or password have not been setup */
727 if ( !*psz_username || !*psz_password )
729 free( psz_username );
730 free( psz_password );
736 /* generates a md5 hash of the password */
737 InitMD5( &p_struct_md5 );
738 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
739 EndMD5( &p_struct_md5 );
741 free( psz_password );
743 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
744 if( !psz_password_md5 )
746 free( psz_username );
750 snprintf( psz_timestamp, 33, "%llu", (uintmax_t)timestamp );
752 /* generates a md5 hash of :
753 * - md5 hash of the password, plus
754 * - timestamp in clear text
756 InitMD5( &p_struct_md5 );
757 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
758 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
759 EndMD5( &p_struct_md5 );
760 free( psz_password_md5 );
762 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
763 if( !psz_auth_token )
765 free( psz_username );
768 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
769 free( psz_auth_token );
771 if( !asprintf( &psz_handshake_url,
772 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
773 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
774 p_sys->psz_auth_token ) )
776 free( psz_username );
779 free( psz_username );
781 /* send the http handshake request */
782 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
783 free( psz_handshake_url );
789 i_ret = stream_Read( p_stream, p_buffer, 1023 );
792 stream_Delete( p_stream );
795 p_buffer[i_ret] = '\0';
796 stream_Delete( p_stream );
798 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
801 /* handshake request failed, sorry */
802 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
806 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
809 /* authentication failed, bad username/password combination */
810 intf_UserFatal( p_this, VLC_FALSE,
811 _("last.fm: Authentication failed"),
812 _("last.fm username or password is incorrect. "
813 "Please verify your settings and relaunch VLC." ) );
814 return VLC_AUDIOSCROBBLER_EFATAL;
817 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
820 /* oops, our version of vlc has been banned by last.fm servers */
821 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
822 "You should upgrade VLC, or disable the last.fm plugin." );
823 return VLC_AUDIOSCROBBLER_EFATAL;
826 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
829 /* The system clock isn't good */
830 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
831 "much shifted. Please correct it, and relaunch VLC." );
832 return VLC_AUDIOSCROBBLER_EFATAL;
835 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
839 p_buffer_pos = strstr( p_buffer_pos, "\n" );
840 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
842 p_buffer_pos++; /* we skip the '\n' */
844 /* save the session ID */
845 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
847 p_buffer_pos = strstr( p_buffer_pos, "http://" );
848 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
851 /* We need to read the nowplaying url */
852 p_buffer_pos += 7; /* we skip "http://" */
854 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
858 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
859 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
870 p_buffer_pos = strstr( p_buffer_pos, "http://" );
871 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
874 /* We need to read the submission url */
875 p_buffer_pos += 7; /* we skip "http://" */
876 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
880 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
881 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
898 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
902 /*****************************************************************************
903 * DeleteSong : Delete the char pointers in a song
904 *****************************************************************************/
905 static void DeleteSong( audioscrobbler_song_t* p_song )
907 FREENULL( p_song->psz_a );
908 FREENULL( p_song->psz_b );
909 FREENULL( p_song->psz_t );
910 FREENULL( p_song->psz_m );
911 FREENULL( p_song->psz_n );
914 /*****************************************************************************
915 * ReadMetaData : Read meta data when parsed by vlc
916 *****************************************************************************/
917 static int ReadMetaData( intf_thread_t *p_this )
919 playlist_t *p_playlist;
920 input_thread_t *p_input;
921 input_item_t *p_item;
923 intf_sys_t *p_sys = p_this->p_sys;
925 p_playlist = pl_Yield( p_this );
927 p_input = p_playlist->p_input;
931 pl_Release( p_playlist );
932 return( VLC_SUCCESS );
935 vlc_object_yield( p_input );
937 pl_Release( p_playlist );
939 p_item = input_GetItem( p_input );
944 #define ALLOC_ITEM_META( a, b ) \
945 psz_meta = input_item_Get##b( p_item ); \
946 if( psz_meta && *psz_meta ) \
948 a = encode_URI_component( psz_meta ); \
957 vlc_mutex_lock( &p_sys->lock );
959 p_sys->b_meta_read = VLC_TRUE;
961 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
964 vlc_mutex_unlock( &p_sys->lock );
965 msg_Dbg( p_this, "No artist.." );
966 vlc_object_release( p_input );
971 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
974 vlc_mutex_unlock( &p_sys->lock );
975 msg_Dbg( p_this, "No track name.." );
976 vlc_object_release( p_input );
977 free( p_sys->p_current_song.psz_a );
982 /* Now we have read the mandatory meta data, so we can submit that info */
983 p_sys->b_submit = VLC_TRUE;
985 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
987 p_sys->p_current_song.psz_b = calloc( 1, 1 );
989 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
991 p_sys->p_current_song.psz_m = calloc( 1, 1 );
993 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
995 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
997 p_sys->p_current_song.psz_n = calloc( 1, 1 );
998 #undef ALLOC_ITEM_META
1000 msg_Dbg( p_this, "Meta data registered" );
1002 vlc_mutex_unlock( &p_sys->lock );
1003 vlc_object_release( p_input );
1008 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1010 if( *i_interval == 0 )
1012 /* first interval is 1 minute */
1017 /* else we double the previous interval, up to 120 minutes */
1019 if( *i_interval > 120 )
1022 *next = mdate() + ( *i_interval * 1000000 * 60 );