1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright © 2006-2008 the VideoLAN team
7 * Author: Rafaël Carré <funman at videolanorg>
8 * Ilkka Ollakka <ileoo at videolan org>
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.2
26 * http://www.audioscrobbler.net/development/protocol/
28 * TODO: "Now Playing" feature (not mandatory)
30 /*****************************************************************************
32 *****************************************************************************/
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_interface.h>
45 #include <vlc_dialog.h>
48 #include <vlc_stream.h>
50 #include <vlc_network.h>
51 #include <vlc_playlist.h>
53 /*****************************************************************************
55 *****************************************************************************/
59 /* Keeps track of metadata to be submitted */
60 typedef struct audioscrobbler_song_t
62 char *psz_a; /**< track artist */
63 char *psz_t; /**< track title */
64 char *psz_b; /**< track album */
65 char *psz_n; /**< track number */
66 int i_l; /**< track length */
67 char *psz_m; /**< musicbrainz id */
68 time_t date; /**< date since epoch */
69 mtime_t i_start; /**< playing start */
70 } audioscrobbler_song_t;
74 audioscrobbler_song_t p_queue[QUEUE_MAX]; /**< songs not submitted yet*/
75 int i_songs; /**< number of songs */
77 vlc_mutex_t lock; /**< p_sys mutex */
78 vlc_cond_t wait; /**< song to submit event */
80 /* data about audioscrobbler session */
81 mtime_t next_exchange; /**< when can we send data */
82 unsigned int i_interval; /**< waiting interval (secs)*/
84 /* submission of played songs */
85 char *psz_submit_host; /**< where to submit data */
86 int i_submit_port; /**< port to which submit */
87 char *psz_submit_file; /**< file to which submit */
89 /* submission of playing song */
91 char *psz_nowp_host; /**< where to submit data */
92 int i_nowp_port; /**< port to which submit */
93 char *psz_nowp_file; /**< file to which submit */
95 bool b_handshaked; /**< are we authenticated ? */
96 char psz_auth_token[33]; /**< Authentication token */
98 /* data about song currently playing */
99 audioscrobbler_song_t p_current_song; /**< song being played */
101 mtime_t time_pause; /**< time when vlc paused */
102 mtime_t time_total_pauses; /**< total time in pause */
104 bool b_submit; /**< do we have to submit ? */
106 bool b_state_cb; /**< if we registered the
107 * "state" callback */
109 bool b_meta_read; /**< if we read the song's
110 * metadata already */
113 static int Open ( vlc_object_t * );
114 static void Close ( vlc_object_t * );
115 static void Run ( intf_thread_t * );
117 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
118 vlc_value_t, void * );
119 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
120 vlc_value_t, void * );
122 static void AddToQueue ( intf_thread_t * );
123 static int Handshake ( intf_thread_t * );
124 static int ReadMetaData ( intf_thread_t * );
125 static void DeleteSong ( audioscrobbler_song_t* );
126 static int ParseURL ( char *, char **, char **, int * );
127 static void HandleInterval ( mtime_t *, unsigned int * );
129 /*****************************************************************************
131 ****************************************************************************/
133 #define USERNAME_TEXT N_("Username")
134 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
135 #define PASSWORD_TEXT N_("Password")
136 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
138 /* This error value is used when last.fm plugin has to be unloaded. */
139 #define VLC_AUDIOSCROBBLER_EFATAL -69
141 /* last.fm client identifier */
142 #define CLIENT_NAME PACKAGE
143 #define CLIENT_VERSION VERSION
145 /* HTTP POST request : to submit data */
146 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
147 "Accept-Encoding: identity\n" \
148 "Content-length: %u\n" \
149 "Connection: close\n" \
150 "Content-type: application/x-www-form-urlencoded\n" \
152 "User-agent: VLC Media Player/%s\r\n" \
158 set_category( CAT_INTERFACE )
159 set_subcategory( SUBCAT_INTERFACE_CONTROL )
160 set_shortname( N_( "Audioscrobbler" ) )
161 set_description( N_("Submission of played songs to last.fm") )
162 add_string( "lastfm-username", "", NULL,
163 USERNAME_TEXT, USERNAME_LONGTEXT, false )
164 add_password( "lastfm-password", "", NULL,
165 PASSWORD_TEXT, PASSWORD_LONGTEXT, false )
166 set_capability( "interface", 0 )
167 set_callbacks( Open, Close )
170 /*****************************************************************************
171 * Open: initialize and create stuff
172 *****************************************************************************/
173 static int Open( vlc_object_t *p_this )
175 playlist_t *p_playlist;
176 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
177 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
182 p_intf->p_sys = p_sys;
184 vlc_mutex_init( &p_sys->lock );
185 vlc_cond_init( &p_sys->wait );
187 p_playlist = pl_Hold( p_intf );
189 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
191 pl_Release( p_intf );
193 p_intf->pf_run = Run;
198 /*****************************************************************************
199 * Close: destroy interface stuff
200 *****************************************************************************/
201 static void Close( vlc_object_t *p_this )
203 playlist_t *p_playlist;
204 input_thread_t *p_input;
205 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
206 intf_sys_t *p_sys = p_intf->p_sys;
208 p_playlist = pl_Hold( p_intf );
212 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
214 p_input = playlist_CurrentInput( p_playlist );
217 if( p_sys->b_state_cb )
218 var_DelCallback( p_input, "intf-event", PlayingChange, p_intf );
220 vlc_object_release( p_input );
223 pl_Release( p_intf );
227 for( i = 0; i < p_sys->i_songs; i++ )
228 DeleteSong( &p_sys->p_queue[i] );
229 free( p_sys->psz_submit_host );
230 free( p_sys->psz_submit_file );
232 free( p_sys->psz_nowp_host );
233 free( p_sys->psz_nowp_file );
235 vlc_cond_destroy( &p_sys->wait );
236 vlc_mutex_destroy( &p_sys->lock );
241 /*****************************************************************************
242 * Run : call Handshake() then submit songs
243 *****************************************************************************/
244 static void Run( intf_thread_t *p_intf )
246 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
249 uint8_t p_buffer[1024];
252 int canc = vlc_savecancel();
254 intf_sys_t *p_sys = p_intf->p_sys;
262 vlc_restorecancel( canc );
263 vlc_mutex_lock( &p_sys->lock );
264 mutex_cleanup_push( &p_sys->lock );
266 if( mdate() < p_sys->next_exchange )
267 /* wait until we can resubmit, i.e. */
268 b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock,
269 p_sys->next_exchange ) == 0;
271 /* wait for data to submit */
272 /* we are signaled each time there is a song to submit */
273 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
275 canc = vlc_savecancel();
278 continue; /* holding on until next_exchange */
280 /* handshake if needed */
281 if( p_sys->b_handshaked == false )
283 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
285 switch( Handshake( p_intf ) )
291 /* username not set */
292 dialog_Fatal( p_intf,
293 _("Last.fm username not set"),
294 _("Please set a username or disable the "
295 "audioscrobbler plugin, and restart VLC.\n"
296 "Visit http://www.last.fm/join/ to get an account.")
301 msg_Dbg( p_intf, "Handshake successfull :)" );
302 p_sys->b_handshaked = true;
303 p_sys->i_interval = 0;
304 p_sys->next_exchange = mdate();
307 case VLC_AUDIOSCROBBLER_EFATAL:
308 msg_Warn( p_intf, "Exiting..." );
313 /* protocol error : we'll try later */
314 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
317 /* if handshake failed let's restart the loop */
318 if( p_sys->b_handshaked == false )
322 msg_Dbg( p_intf, "Going to submit some data..." );
324 /* The session may be invalid if there is a trailing \n */
325 char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' );
329 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
330 { /* Out of memory */
334 /* forge the HTTP POST request */
335 vlc_mutex_lock( &p_sys->lock );
336 audioscrobbler_song_t *p_song;
337 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
339 p_song = &p_sys->p_queue[i_song];
340 if( !asprintf( &psz_submit_song,
350 i_song, p_song->psz_a,
351 i_song, p_song->psz_t,
352 i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
356 i_song, p_song->psz_b,
357 i_song, p_song->psz_n,
358 i_song, p_song->psz_m
360 { /* Out of memory */
361 vlc_mutex_unlock( &p_sys->lock );
364 psz_submit_tmp = psz_submit;
365 if( !asprintf( &psz_submit, "%s%s",
366 psz_submit_tmp, psz_submit_song ) )
367 { /* Out of memory */
368 free( psz_submit_tmp );
369 free( psz_submit_song );
370 vlc_mutex_unlock( &p_sys->lock );
373 free( psz_submit_song );
374 free( psz_submit_tmp );
376 vlc_mutex_unlock( &p_sys->lock );
378 i_post_socket = net_ConnectTCP( p_intf,
379 p_sys->psz_submit_host, p_sys->i_submit_port );
381 if ( i_post_socket == -1 )
383 /* If connection fails, we assume we must handshake again */
384 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
385 p_sys->b_handshaked = false;
390 /* we transmit the data */
391 i_net_ret = net_Printf(
392 VLC_OBJECT( p_intf ), i_post_socket, NULL,
393 POST_REQUEST, p_sys->psz_submit_file,
394 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
399 if ( i_net_ret == -1 )
401 /* If connection fails, we assume we must handshake again */
402 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
403 p_sys->b_handshaked = false;
407 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
408 p_buffer, 1023, false );
409 if ( i_net_ret <= 0 )
411 /* if we get no answer, something went wrong : try again */
415 net_Close( i_post_socket );
416 p_buffer[i_net_ret] = '\0';
418 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
421 msg_Warn( p_intf, "%s", p_buffer_pos );
422 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
426 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
429 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
430 p_sys->b_handshaked = false;
431 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
435 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
439 for( i = 0; i < p_sys->i_songs; i++ )
440 DeleteSong( &p_sys->p_queue[i] );
442 p_sys->i_interval = 0;
443 p_sys->next_exchange = mdate();
444 msg_Dbg( p_intf, "Submission successful!" );
448 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
450 p_sys->b_handshaked = false;
451 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
455 vlc_restorecancel( canc );
458 /*****************************************************************************
459 * PlayingChange: Playing status change callback
460 *****************************************************************************/
461 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
462 vlc_value_t oldval, vlc_value_t newval, void *p_data )
464 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
465 intf_sys_t *p_sys = p_intf->p_sys;
466 input_thread_t *p_input = ( input_thread_t* )p_this;
467 vlc_value_t state_value;
469 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
471 if( newval.i_int != INPUT_EVENT_STATE ) return VLC_SUCCESS;
473 state_value.i_int = 0;
475 var_Get( p_input, "state", &state_value );
478 if( p_sys->b_meta_read == false && state_value.i_int >= PLAYING_S )
480 ReadMetaData( p_intf );
485 if( state_value.i_int >= END_S )
486 AddToQueue( p_intf );
487 else if( state_value.i_int == PAUSE_S )
488 p_sys->time_pause = mdate();
489 else if( p_sys->time_pause > 0 && state_value.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 );
511 p_sys->b_state_cb = false;
512 p_sys->b_meta_read = false;
513 p_sys->b_submit = false;
515 p_playlist = pl_Hold( p_intf );
516 p_input = playlist_CurrentInput( p_playlist );
518 if( !p_input || p_input->b_dead )
520 pl_Release( p_intf );
524 pl_Release( p_intf );
526 p_item = input_GetItem( p_input );
529 vlc_object_release( p_input );
533 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
534 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
536 msg_Dbg( p_this, "Not an audio local file, not submitting");
537 vlc_object_release( p_input );
541 p_sys->time_total_pauses = 0;
542 time( &p_sys->p_current_song.date ); /* to be sent to last.fm */
543 p_sys->p_current_song.i_start = mdate(); /* only used locally */
545 var_AddCallback( p_input, "intf-event", PlayingChange, p_intf );
546 p_sys->b_state_cb = true;
548 if( input_item_IsPreparsed( p_item ) )
549 ReadMetaData( p_intf );
550 /* if the input item was not preparsed, we'll do it in PlayingChange()
551 * callback, when "state" == PLAYING_S */
553 vlc_object_release( p_input );
557 /*****************************************************************************
558 * AddToQueue: Add the played song to the queue to be submitted
559 *****************************************************************************/
560 static void AddToQueue ( intf_thread_t *p_this )
563 intf_sys_t *p_sys = p_this->p_sys;
565 vlc_mutex_lock( &p_sys->lock );
566 if( !p_sys->b_submit )
569 /* wait for the user to listen enough before submitting */
570 played_time = mdate() - p_sys->p_current_song.i_start -
571 p_sys->time_total_pauses;
572 played_time /= 1000000; /* µs → s */
574 /*HACK: it seam that the preparsing sometime fail,
575 so use the playing time as the song length */
576 if( p_sys->p_current_song.i_l == 0 )
577 p_sys->p_current_song.i_l = played_time;
579 /* Don't send song shorter than 30s */
580 if( p_sys->p_current_song.i_l < 30 )
582 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
586 /* Send if the user had listen more than 240s OR half the track length */
587 if( ( played_time < 240 ) &&
588 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
590 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
594 /* Check that all meta are present */
595 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
596 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
598 msg_Dbg( p_this, "Missing artist or title, not submitting" );
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_cond_signal( &p_sys->wait );
633 DeleteSong( &p_sys->p_current_song );
634 p_sys->b_submit = 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[21];
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, sizeof( psz_timestamp ), "%"PRIu64,
751 (uint64_t)timestamp );
753 /* generates a md5 hash of :
754 * - md5 hash of the password, plus
755 * - timestamp in clear text
757 InitMD5( &p_struct_md5 );
758 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
759 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
760 EndMD5( &p_struct_md5 );
761 free( psz_password_md5 );
763 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
764 if( !psz_auth_token )
766 free( psz_username );
769 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
770 free( psz_auth_token );
772 if( !asprintf( &psz_handshake_url,
773 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
774 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
775 p_sys->psz_auth_token ) )
777 free( psz_username );
780 free( psz_username );
782 /* send the http handshake request */
783 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
784 free( psz_handshake_url );
790 i_ret = stream_Read( p_stream, p_buffer, 1023 );
793 stream_Delete( p_stream );
796 p_buffer[i_ret] = '\0';
797 stream_Delete( p_stream );
799 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
802 /* handshake request failed, sorry */
803 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
807 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
810 /* authentication failed, bad username/password combination */
811 dialog_Fatal( p_this,
812 _("last.fm: Authentication failed"),
813 _("last.fm username or password is incorrect. "
814 "Please verify your settings and relaunch VLC." ) );
815 return VLC_AUDIOSCROBBLER_EFATAL;
818 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
821 /* oops, our version of vlc has been banned by last.fm servers */
822 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
823 "You should upgrade VLC, or disable the last.fm plugin." );
824 return VLC_AUDIOSCROBBLER_EFATAL;
827 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
830 /* The system clock isn't good */
831 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
832 "much shifted. Please correct it, and relaunch VLC." );
833 return VLC_AUDIOSCROBBLER_EFATAL;
836 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
840 p_buffer_pos = strstr( p_buffer_pos, "\n" );
841 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
843 p_buffer_pos++; /* we skip the '\n' */
845 /* save the session ID */
846 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
848 p_buffer_pos = strstr( p_buffer_pos, "http://" );
849 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
852 /* We need to read the nowplaying url */
853 p_buffer_pos += 7; /* we skip "http://" */
855 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
859 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
860 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
871 p_buffer_pos = strstr( p_buffer_pos, "http://" );
872 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
875 /* We need to read the submission url */
876 p_buffer_pos += 7; /* we skip "http://" */
877 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
881 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
882 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
899 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
903 /*****************************************************************************
904 * DeleteSong : Delete the char pointers in a song
905 *****************************************************************************/
906 static void DeleteSong( audioscrobbler_song_t* p_song )
908 FREENULL( p_song->psz_a );
909 FREENULL( p_song->psz_b );
910 FREENULL( p_song->psz_t );
911 FREENULL( p_song->psz_m );
912 FREENULL( p_song->psz_n );
915 /*****************************************************************************
916 * ReadMetaData : Read meta data when parsed by vlc
917 *****************************************************************************/
918 static int ReadMetaData( intf_thread_t *p_this )
920 playlist_t *p_playlist;
921 input_thread_t *p_input;
922 input_item_t *p_item;
924 intf_sys_t *p_sys = p_this->p_sys;
926 p_playlist = pl_Hold( p_this );
927 p_input = playlist_CurrentInput( p_playlist );
930 pl_Release( p_this );
931 return( VLC_SUCCESS );
934 pl_Release( p_this );
936 p_item = input_GetItem( p_input );
941 #define ALLOC_ITEM_META( a, b ) \
942 psz_meta = input_item_Get##b( p_item ); \
943 if( psz_meta && *psz_meta ) \
945 a = encode_URI_component( psz_meta ); \
948 vlc_mutex_unlock( &p_sys->lock ); \
949 vlc_object_release( p_input ); \
955 vlc_mutex_lock( &p_sys->lock );
957 p_sys->b_meta_read = true;
959 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
962 vlc_mutex_unlock( &p_sys->lock );
963 msg_Dbg( p_this, "No artist.." );
964 vlc_object_release( p_input );
970 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
973 vlc_mutex_unlock( &p_sys->lock );
974 msg_Dbg( p_this, "No track name.." );
975 vlc_object_release( p_input );
976 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 = true;
985 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
987 p_sys->p_current_song.psz_b = calloc( 1, 1 );
990 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
992 p_sys->p_current_song.psz_m = calloc( 1, 1 );
995 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
997 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
999 p_sys->p_current_song.psz_n = calloc( 1, 1 );
1001 #undef ALLOC_ITEM_META
1003 msg_Dbg( p_this, "Meta data registered" );
1005 vlc_mutex_unlock( &p_sys->lock );
1006 vlc_object_release( p_input );
1011 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1013 if( *i_interval == 0 )
1015 /* first interval is 1 minute */
1020 /* else we double the previous interval, up to 120 minutes */
1022 if( *i_interval > 120 )
1025 *next = mdate() + ( *i_interval * 1000000 * 60 );