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 *****************************************************************************/
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_interface.h>
46 #include <vlc_block.h>
47 #include <vlc_stream.h>
49 #include <vlc_network.h>
50 #include <vlc_interface.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, "playlist-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, "playlist-current", ItemChange, p_intf );
214 p_input = playlist_CurrentInput( p_playlist );
217 if( p_sys->b_state_cb )
218 var_DelCallback( p_input, "state", 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;
261 vlc_restorecancel( canc );
262 vlc_mutex_lock( &p_sys->lock );
263 mutex_cleanup_push( &p_sys->lock );
265 if( mdate() < p_sys->next_exchange )
266 /* wait until we can resubmit, i.e. */
267 b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock,
268 p_sys->next_exchange ) == 0;
270 /* wait for data to submit */
271 /* we are signaled each time there is a song to submit */
272 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
274 canc = vlc_savecancel();
277 continue; /* holding on until next_exchange */
279 /* handshake if needed */
280 if( p_sys->b_handshaked == false )
282 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
284 switch( Handshake( p_intf ) )
290 /* username not set */
291 intf_UserFatal( p_intf, false,
292 _("Last.fm username not set"),
293 _("Please set a username or disable the "
294 "audioscrobbler plugin, and restart VLC.\n"
295 "Visit http://www.last.fm/join/ to get an account.")
300 msg_Dbg( p_intf, "Handshake successfull :)" );
301 p_sys->b_handshaked = true;
302 p_sys->i_interval = 0;
303 p_sys->next_exchange = mdate();
306 case VLC_AUDIOSCROBBLER_EFATAL:
307 msg_Warn( p_intf, "Exiting..." );
312 /* protocol error : we'll try later */
313 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
316 /* if handshake failed let's restart the loop */
317 if( p_sys->b_handshaked == false )
321 msg_Dbg( p_intf, "Going to submit some data..." );
323 /* The session may be invalid if there is a trailing \n */
324 char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' );
328 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
329 { /* Out of memory */
333 /* forge the HTTP POST request */
334 vlc_mutex_lock( &p_sys->lock );
335 audioscrobbler_song_t *p_song;
336 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
338 p_song = &p_sys->p_queue[i_song];
339 if( !asprintf( &psz_submit_song,
349 i_song, p_song->psz_a,
350 i_song, p_song->psz_t,
351 i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
355 i_song, p_song->psz_b,
356 i_song, p_song->psz_n,
357 i_song, p_song->psz_m
359 { /* Out of memory */
360 vlc_mutex_unlock( &p_sys->lock );
363 psz_submit_tmp = psz_submit;
364 if( !asprintf( &psz_submit, "%s%s",
365 psz_submit_tmp, psz_submit_song ) )
366 { /* Out of memory */
367 free( psz_submit_tmp );
368 free( psz_submit_song );
369 vlc_mutex_unlock( &p_sys->lock );
372 free( psz_submit_song );
373 free( psz_submit_tmp );
375 vlc_mutex_unlock( &p_sys->lock );
377 i_post_socket = net_ConnectTCP( p_intf,
378 p_sys->psz_submit_host, p_sys->i_submit_port );
380 if ( i_post_socket == -1 )
382 /* If connection fails, we assume we must handshake again */
383 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
384 p_sys->b_handshaked = false;
389 /* we transmit the data */
390 i_net_ret = net_Printf(
391 VLC_OBJECT( p_intf ), i_post_socket, NULL,
392 POST_REQUEST, p_sys->psz_submit_file,
393 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
398 if ( i_net_ret == -1 )
400 /* If connection fails, we assume we must handshake again */
401 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
402 p_sys->b_handshaked = false;
406 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
407 p_buffer, 1023, false );
408 if ( i_net_ret <= 0 )
410 /* if we get no answer, something went wrong : try again */
414 net_Close( i_post_socket );
415 p_buffer[i_net_ret] = '\0';
417 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
420 msg_Warn( p_intf, "%s", p_buffer_pos );
421 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
425 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
428 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
429 p_sys->b_handshaked = false;
430 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
434 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
438 for( i = 0; i < p_sys->i_songs; i++ )
439 DeleteSong( &p_sys->p_queue[i] );
441 p_sys->i_interval = 0;
442 p_sys->next_exchange = mdate();
443 msg_Dbg( p_intf, "Submission successful!" );
447 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
449 p_sys->b_handshaked = false;
450 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
454 vlc_restorecancel( canc );
457 /*****************************************************************************
458 * PlayingChange: Playing status change callback
459 *****************************************************************************/
460 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
461 vlc_value_t oldval, vlc_value_t newval, void *p_data )
463 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
464 intf_sys_t *p_sys = p_intf->p_sys;
466 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
468 if( p_sys->b_meta_read == false && newval.i_int >= PLAYING_S )
470 ReadMetaData( p_intf );
474 if( newval.i_int >= END_S )
475 AddToQueue( p_intf );
476 else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S )
477 p_sys->time_pause = mdate();
478 else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S )
479 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
484 /*****************************************************************************
485 * ItemChange: Playlist item change callback
486 *****************************************************************************/
487 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
488 vlc_value_t oldval, vlc_value_t newval, void *p_data )
490 playlist_t *p_playlist;
491 input_thread_t *p_input;
492 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
493 intf_sys_t *p_sys = p_intf->p_sys;
494 input_item_t *p_item;
495 vlc_value_t video_val;
497 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
498 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
500 p_sys->b_state_cb = false;
501 p_sys->b_meta_read = false;
502 p_sys->b_submit = false;
504 p_playlist = pl_Hold( p_intf );
505 p_input = playlist_CurrentInput( p_playlist );
507 if( !p_input || p_input->b_dead )
509 pl_Release( p_intf );
513 pl_Release( p_intf );
515 p_item = input_GetItem( p_input );
518 vlc_object_release( p_input );
522 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
523 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
525 msg_Dbg( p_this, "Not an audio local file, not submitting");
526 vlc_object_release( p_input );
530 p_sys->time_total_pauses = 0;
531 time( &p_sys->p_current_song.date ); /* to be sent to last.fm */
532 p_sys->p_current_song.i_start = mdate(); /* only used locally */
534 var_AddCallback( p_input, "state", PlayingChange, p_intf );
535 p_sys->b_state_cb = true;
537 if( input_item_IsPreparsed( p_item ) )
538 ReadMetaData( p_intf );
539 /* if the input item was not preparsed, we'll do it in PlayingChange()
540 * callback, when "state" == PLAYING_S */
542 vlc_object_release( p_input );
546 /*****************************************************************************
547 * AddToQueue: Add the played song to the queue to be submitted
548 *****************************************************************************/
549 static void AddToQueue ( intf_thread_t *p_this )
552 intf_sys_t *p_sys = p_this->p_sys;
554 vlc_mutex_lock( &p_sys->lock );
555 if( !p_sys->b_submit )
558 /* wait for the user to listen enough before submitting */
559 played_time = mdate() - p_sys->p_current_song.i_start -
560 p_sys->time_total_pauses;
561 played_time /= 1000000; /* µs → s */
563 /*HACK: it seam that the preparsing sometime fail,
564 so use the playing time as the song length */
565 if( p_sys->p_current_song.i_l == 0 )
566 p_sys->p_current_song.i_l = played_time;
568 /* Don't send song shorter than 30s */
569 if( p_sys->p_current_song.i_l < 30 )
571 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
575 /* Send if the user had listen more than 240s OR half the track length */
576 if( ( played_time < 240 ) &&
577 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
579 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
583 /* Check that all meta are present */
584 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
585 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
587 msg_Dbg( p_this, "Missing artist or title, not submitting" );
591 if( p_sys->i_songs >= QUEUE_MAX )
593 msg_Warn( p_this, "Submission queue is full, not submitting" );
597 msg_Dbg( p_this, "Song will be submitted." );
599 #define QUEUE_COPY( a ) \
600 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
602 #define QUEUE_COPY_NULL( a ) \
604 p_sys->p_current_song.a = NULL
607 QUEUE_COPY_NULL( psz_n );
608 QUEUE_COPY_NULL( psz_a );
609 QUEUE_COPY_NULL( psz_t );
610 QUEUE_COPY_NULL( psz_b );
611 QUEUE_COPY_NULL( psz_m );
613 #undef QUEUE_COPY_NULL
618 /* signal the main loop we have something to submit */
619 vlc_cond_signal( &p_sys->wait );
622 DeleteSong( &p_sys->p_current_song );
623 p_sys->b_submit = false;
624 vlc_mutex_unlock( &p_sys->lock );
627 /*****************************************************************************
628 * ParseURL : Split an http:// URL into host, file, and port
630 * Example: "62.216.251.205:80/protocol_1.2"
631 * will be split into "62.216.251.205", 80, "protocol_1.2"
633 * psz_url will be freed before returning
634 * *psz_file & *psz_host will be freed before use
637 * VLC_ENOMEM Out Of Memory
638 * VLC_EGENERIC Invalid url provided
639 * VLC_SUCCESS Success
640 *****************************************************************************/
641 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
645 int i_len = strlen( psz_url );
646 FREENULL( *psz_host );
647 FREENULL( *psz_file );
649 i_pos = strcspn( psz_url, ":" );
653 *psz_host = strndup( psz_url, i_pos );
657 i_pos++; /* skip the ':' */
658 *i_port = atoi( psz_url + i_pos );
661 FREENULL( *psz_host );
665 i_pos = strcspn( psz_url, "/" );
670 i_pos++; /* skip the '/' */
671 *psz_file = strdup( psz_url + i_pos );
674 FREENULL( *psz_host );
682 /*****************************************************************************
683 * Handshake : Init audioscrobbler connection
684 *****************************************************************************/
685 static int Handshake( intf_thread_t *p_this )
687 char *psz_username, *psz_password;
689 char psz_timestamp[21];
691 struct md5_s p_struct_md5;
694 char *psz_handshake_url;
695 uint8_t p_buffer[1024];
701 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
702 intf_sys_t *p_sys = p_this->p_sys;
704 psz_username = config_GetPsz( p_this, "lastfm-username" );
708 psz_password = config_GetPsz( p_this, "lastfm-password" );
711 free( psz_username );
715 /* username or password have not been setup */
716 if ( !*psz_username || !*psz_password )
718 free( psz_username );
719 free( psz_password );
725 /* generates a md5 hash of the password */
726 InitMD5( &p_struct_md5 );
727 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
728 EndMD5( &p_struct_md5 );
730 free( psz_password );
732 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
733 if( !psz_password_md5 )
735 free( psz_username );
739 snprintf( psz_timestamp, sizeof( psz_timestamp ), "%"PRIu64,
740 (uint64_t)timestamp );
742 /* generates a md5 hash of :
743 * - md5 hash of the password, plus
744 * - timestamp in clear text
746 InitMD5( &p_struct_md5 );
747 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
748 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
749 EndMD5( &p_struct_md5 );
750 free( psz_password_md5 );
752 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
753 if( !psz_auth_token )
755 free( psz_username );
758 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
759 free( psz_auth_token );
761 if( !asprintf( &psz_handshake_url,
762 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
763 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
764 p_sys->psz_auth_token ) )
766 free( psz_username );
769 free( psz_username );
771 /* send the http handshake request */
772 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
773 free( psz_handshake_url );
779 i_ret = stream_Read( p_stream, p_buffer, 1023 );
782 stream_Delete( p_stream );
785 p_buffer[i_ret] = '\0';
786 stream_Delete( p_stream );
788 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
791 /* handshake request failed, sorry */
792 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
796 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
799 /* authentication failed, bad username/password combination */
800 intf_UserFatal( p_this, false,
801 _("last.fm: Authentication failed"),
802 _("last.fm username or password is incorrect. "
803 "Please verify your settings and relaunch VLC." ) );
804 return VLC_AUDIOSCROBBLER_EFATAL;
807 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
810 /* oops, our version of vlc has been banned by last.fm servers */
811 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
812 "You should upgrade VLC, or disable the last.fm plugin." );
813 return VLC_AUDIOSCROBBLER_EFATAL;
816 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
819 /* The system clock isn't good */
820 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
821 "much shifted. Please correct it, and relaunch VLC." );
822 return VLC_AUDIOSCROBBLER_EFATAL;
825 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
829 p_buffer_pos = strstr( p_buffer_pos, "\n" );
830 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
832 p_buffer_pos++; /* we skip the '\n' */
834 /* save the session ID */
835 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
837 p_buffer_pos = strstr( p_buffer_pos, "http://" );
838 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
841 /* We need to read the nowplaying url */
842 p_buffer_pos += 7; /* we skip "http://" */
844 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
848 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
849 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
860 p_buffer_pos = strstr( p_buffer_pos, "http://" );
861 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
864 /* We need to read the submission url */
865 p_buffer_pos += 7; /* we skip "http://" */
866 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
870 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
871 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
888 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
892 /*****************************************************************************
893 * DeleteSong : Delete the char pointers in a song
894 *****************************************************************************/
895 static void DeleteSong( audioscrobbler_song_t* p_song )
897 FREENULL( p_song->psz_a );
898 FREENULL( p_song->psz_b );
899 FREENULL( p_song->psz_t );
900 FREENULL( p_song->psz_m );
901 FREENULL( p_song->psz_n );
904 /*****************************************************************************
905 * ReadMetaData : Read meta data when parsed by vlc
906 *****************************************************************************/
907 static int ReadMetaData( intf_thread_t *p_this )
909 playlist_t *p_playlist;
910 input_thread_t *p_input;
911 input_item_t *p_item;
913 intf_sys_t *p_sys = p_this->p_sys;
915 p_playlist = pl_Hold( p_this );
916 p_input = playlist_CurrentInput( p_playlist );
919 pl_Release( p_this );
920 return( VLC_SUCCESS );
923 pl_Release( p_this );
925 p_item = input_GetItem( p_input );
930 #define ALLOC_ITEM_META( a, b ) \
931 psz_meta = input_item_Get##b( p_item ); \
932 if( psz_meta && *psz_meta ) \
934 a = encode_URI_component( psz_meta ); \
937 vlc_mutex_unlock( &p_sys->lock ); \
938 vlc_object_release( p_input ); \
944 vlc_mutex_lock( &p_sys->lock );
946 p_sys->b_meta_read = true;
948 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
951 vlc_mutex_unlock( &p_sys->lock );
952 msg_Dbg( p_this, "No artist.." );
953 vlc_object_release( p_input );
959 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
962 vlc_mutex_unlock( &p_sys->lock );
963 msg_Dbg( p_this, "No track name.." );
964 vlc_object_release( p_input );
965 free( p_sys->p_current_song.psz_a );
971 /* Now we have read the mandatory meta data, so we can submit that info */
972 p_sys->b_submit = true;
974 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
976 p_sys->p_current_song.psz_b = calloc( 1, 1 );
979 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
981 p_sys->p_current_song.psz_m = calloc( 1, 1 );
984 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
986 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
988 p_sys->p_current_song.psz_n = calloc( 1, 1 );
990 #undef ALLOC_ITEM_META
992 msg_Dbg( p_this, "Meta data registered" );
994 vlc_mutex_unlock( &p_sys->lock );
995 vlc_object_release( p_input );
1000 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1002 if( *i_interval == 0 )
1004 /* first interval is 1 minute */
1009 /* else we double the previous interval, up to 120 minutes */
1011 if( *i_interval > 120 )
1014 *next = mdate() + ( *i_interval * 1000000 * 60 );