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 */
79 /* data about audioscrobbler session */
80 mtime_t next_exchange; /**< when can we send data */
81 unsigned int i_interval; /**< waiting interval (secs)*/
83 /* submission of played songs */
84 char *psz_submit_host; /**< where to submit data */
85 int i_submit_port; /**< port to which submit */
86 char *psz_submit_file; /**< file to which submit */
88 /* submission of playing song */
90 char *psz_nowp_host; /**< where to submit data */
91 int i_nowp_port; /**< port to which submit */
92 char *psz_nowp_file; /**< file to which submit */
94 bool b_handshaked; /**< are we authenticated ? */
95 char psz_auth_token[33]; /**< Authentication token */
97 /* data about song currently playing */
98 audioscrobbler_song_t p_current_song; /**< song being played */
100 mtime_t time_pause; /**< time when vlc paused */
101 mtime_t time_total_pauses; /**< total time in pause */
103 bool b_submit; /**< do we have to submit ? */
105 bool b_state_cb; /**< if we registered the
106 * "state" callback */
108 bool b_meta_read; /**< if we read the song's
109 * metadata already */
112 static int Open ( vlc_object_t * );
113 static void Close ( vlc_object_t * );
114 static void Run ( intf_thread_t * );
116 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
117 vlc_value_t, void * );
118 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
119 vlc_value_t, void * );
121 static void AddToQueue ( intf_thread_t * );
122 static int Handshake ( intf_thread_t * );
123 static int ReadMetaData ( intf_thread_t * );
124 static void DeleteSong ( audioscrobbler_song_t* );
125 static int ParseURL ( char *, char **, char **, int * );
126 static void HandleInterval ( mtime_t *, unsigned int * );
128 /*****************************************************************************
130 ****************************************************************************/
132 #define USERNAME_TEXT N_("Username")
133 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
134 #define PASSWORD_TEXT N_("Password")
135 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
137 /* This error value is used when last.fm plugin has to be unloaded. */
138 #define VLC_AUDIOSCROBBLER_EFATAL -69
140 /* last.fm client identifier */
141 #define CLIENT_NAME PACKAGE
142 #define CLIENT_VERSION VERSION
144 /* HTTP POST request : to submit data */
145 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
146 "Accept-Encoding: identity\n" \
147 "Content-length: %u\n" \
148 "Connection: close\n" \
149 "Content-type: application/x-www-form-urlencoded\n" \
151 "User-agent: VLC Media Player/%s\r\n" \
157 set_category( CAT_INTERFACE );
158 set_subcategory( SUBCAT_INTERFACE_CONTROL );
159 set_shortname( N_( "Audioscrobbler" ) );
160 set_description( N_("Submission of played songs to last.fm") );
161 add_string( "lastfm-username", "", NULL,
162 USERNAME_TEXT, USERNAME_LONGTEXT, false );
163 add_password( "lastfm-password", "", NULL,
164 PASSWORD_TEXT, PASSWORD_LONGTEXT, false );
165 set_capability( "interface", 0 );
166 set_callbacks( Open, Close );
169 /*****************************************************************************
170 * Open: initialize and create stuff
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 playlist_t *p_playlist;
175 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
176 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
181 p_intf->p_sys = p_sys;
183 vlc_mutex_init( &p_sys->lock );
185 p_playlist = pl_Yield( p_intf );
187 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
189 pl_Release( p_intf );
191 p_intf->pf_run = Run;
196 /*****************************************************************************
197 * Close: destroy interface stuff
198 *****************************************************************************/
199 static void Close( vlc_object_t *p_this )
201 playlist_t *p_playlist;
202 input_thread_t *p_input;
203 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
204 intf_sys_t *p_sys = p_intf->p_sys;
206 p_playlist = pl_Yield( p_intf );
211 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
213 p_input = p_playlist->p_input;
216 vlc_object_yield( p_input );
218 if( p_sys->b_state_cb )
219 var_DelCallback( p_input, "state", PlayingChange, p_intf );
221 vlc_object_release( p_input );
225 pl_Release( p_intf );
228 p_intf->b_dead = true;
229 /* we lock the mutex in case p_sys is being accessed from a callback */
230 vlc_mutex_lock ( &p_sys->lock );
232 for( i = 0; i < p_sys->i_songs; i++ )
233 DeleteSong( &p_sys->p_queue[i] );
234 free( p_sys->psz_submit_host );
235 free( p_sys->psz_submit_file );
237 free( p_sys->psz_nowp_host );
238 free( p_sys->psz_nowp_file );
240 vlc_mutex_unlock ( &p_sys->lock );
241 vlc_mutex_destroy( &p_sys->lock );
246 /*****************************************************************************
247 * Run : call Handshake() then submit songs
248 *****************************************************************************/
249 static void Run( intf_thread_t *p_intf )
251 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
254 uint8_t p_buffer[1024];
258 intf_sys_t *p_sys = p_intf->p_sys;
265 vlc_object_lock( p_intf );
266 if( !vlc_object_alive( p_intf ) )
268 vlc_object_unlock( p_intf );
269 msg_Dbg( p_intf, "audioscrobbler is dying");
272 if( mdate() < p_sys->next_exchange )
273 /* wait until we can resubmit, i.e. */
274 b_wait = vlc_object_timedwait( p_intf, p_sys->next_exchange ) == 0;
276 /* wait for data to submit */
277 /* we are signaled each time there is a song to submit */
278 vlc_object_wait( p_intf );
279 vlc_object_unlock( p_intf );
282 continue; /* holding on until next_exchange */
284 /* handshake if needed */
285 if( p_sys->b_handshaked == false )
287 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
289 switch( Handshake( p_intf ) )
295 /* username not set */
296 intf_UserFatal( p_intf, false,
297 _("Last.fm username not set"),
298 _("Please set a username or disable the "
299 "audioscrobbler plugin, and restart VLC.\n"
300 "Visit http://www.last.fm/join/ to get an account.")
305 msg_Dbg( p_intf, "Handshake successfull :)" );
306 p_sys->b_handshaked = true;
307 p_sys->i_interval = 0;
308 p_sys->next_exchange = mdate();
311 case VLC_AUDIOSCROBBLER_EFATAL:
312 msg_Warn( p_intf, "Exiting..." );
317 /* protocol error : we'll try later */
318 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
321 /* if handshake failed let's restart the loop */
322 if( p_sys->b_handshaked == false )
326 msg_Dbg( p_intf, "Going to submit some data..." );
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,
340 "&a%%5B%d%%5D=%s&t%%5B%d%%5D=%s"
341 "&i%%5B%d%%5D=%ju&o%%5B%d%%5D=P&r%%5B%d%%5D="
342 "&l%%5B%d%%5D=%d&b%%5B%d%%5D=%s"
343 "&n%%5B%d%%5D=%s&m%%5B%d%%5D=%s",
344 i_song, p_song->psz_a, i_song, p_song->psz_t,
345 i_song, (uintmax_t)p_song->date, i_song, i_song,
346 i_song, p_song->i_l, i_song, p_song->psz_b,
347 i_song, p_song->psz_n, i_song, p_song->psz_m
349 { /* Out of memory */
350 vlc_mutex_unlock( &p_sys->lock );
353 psz_submit_tmp = psz_submit;
354 if( !asprintf( &psz_submit, "%s%s",
355 psz_submit_tmp, psz_submit_song ) )
356 { /* Out of memory */
357 free( psz_submit_tmp );
358 free( psz_submit_song );
359 vlc_mutex_unlock( &p_sys->lock );
362 free( psz_submit_song );
363 free( psz_submit_tmp );
365 vlc_mutex_unlock( &p_sys->lock );
367 i_post_socket = net_ConnectTCP( p_intf,
368 p_sys->psz_submit_host, p_sys->i_submit_port );
370 if ( i_post_socket == -1 )
372 /* If connection fails, we assume we must handshake again */
373 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
374 p_sys->b_handshaked = false;
379 /* we transmit the data */
380 i_net_ret = net_Printf(
381 VLC_OBJECT( p_intf ), i_post_socket, NULL,
382 POST_REQUEST, p_sys->psz_submit_file,
383 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
388 if ( i_net_ret == -1 )
390 /* If connection fails, we assume we must handshake again */
391 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
392 p_sys->b_handshaked = false;
396 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
397 p_buffer, 1023, false );
398 if ( i_net_ret <= 0 )
400 /* if we get no answer, something went wrong : try again */
404 net_Close( i_post_socket );
405 p_buffer[i_net_ret] = '\0';
407 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
410 msg_Warn( p_intf, "%s", p_buffer_pos );
411 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
415 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
418 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
419 p_sys->b_handshaked = false;
420 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
424 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
428 for( i = 0; i < p_sys->i_songs; i++ )
429 DeleteSong( &p_sys->p_queue[i] );
431 p_sys->i_interval = 0;
432 p_sys->next_exchange = mdate();
433 msg_Dbg( p_intf, "Submission successful!" );
437 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
439 p_sys->b_handshaked = false;
440 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
446 /*****************************************************************************
447 * PlayingChange: Playing status change callback
448 *****************************************************************************/
449 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
450 vlc_value_t oldval, vlc_value_t newval, void *p_data )
452 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
453 intf_sys_t *p_sys = p_intf->p_sys;
455 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
460 if( p_sys->b_meta_read == false && newval.i_int >= PLAYING_S )
462 ReadMetaData( p_intf );
466 if( newval.i_int >= END_S )
467 AddToQueue( p_intf );
468 else if( oldval.i_int == PLAYING_S && newval.i_int == PAUSE_S )
469 p_sys->time_pause = mdate();
470 else if( oldval.i_int == PAUSE_S && newval.i_int == PLAYING_S )
471 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
476 /*****************************************************************************
477 * ItemChange: Playlist item change callback
478 *****************************************************************************/
479 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
480 vlc_value_t oldval, vlc_value_t newval, void *p_data )
482 playlist_t *p_playlist;
483 input_thread_t *p_input;
484 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
485 intf_sys_t *p_sys = p_intf->p_sys;
486 input_item_t *p_item;
487 vlc_value_t video_val;
489 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
490 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
495 p_sys->b_state_cb = false;
496 p_sys->b_meta_read = false;
497 p_sys->b_submit = false;
499 p_playlist = pl_Yield( p_intf );
501 p_input = p_playlist->p_input;
503 if( !p_input || p_input->b_dead )
506 pl_Release( p_intf );
510 vlc_object_yield( p_input );
512 pl_Release( p_intf );
514 p_item = input_GetItem( p_input );
517 vlc_object_release( p_input );
521 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
522 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
524 msg_Dbg( p_this, "Not an audio local file, not submitting");
525 vlc_object_release( p_input );
529 p_sys->time_total_pauses = 0;
530 time( &p_sys->p_current_song.date ); /* to be sent to last.fm */
531 p_sys->p_current_song.i_start = mdate(); /* only used locally */
533 var_AddCallback( p_input, "state", PlayingChange, p_intf );
534 p_sys->b_state_cb = true;
536 if( input_item_IsPreparsed( p_item ) )
537 ReadMetaData( p_intf );
538 /* if the input item was not preparsed, we'll do it in PlayingChange()
539 * callback, when "state" == PLAYING_S */
541 vlc_object_release( p_input );
545 /*****************************************************************************
546 * AddToQueue: Add the played song to the queue to be submitted
547 *****************************************************************************/
548 static void AddToQueue ( intf_thread_t *p_this )
551 intf_sys_t *p_sys = p_this->p_sys;
553 vlc_mutex_lock( &p_sys->lock );
554 if( !p_sys->b_submit )
557 /* wait for the user to listen enough before submitting */
558 played_time = mdate() - p_sys->p_current_song.i_start -
559 p_sys->time_total_pauses;
560 played_time /= 1000000; /* µs → s */
562 if( ( played_time < 240 ) &&
563 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
565 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
569 if( p_sys->p_current_song.i_l < 30 )
571 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
575 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
576 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
578 msg_Dbg( p_this, "Missing artist or title, not submitting" );
579 /*XXX*/ msg_Dbg( p_this, "%s %s", p_sys->p_current_song.psz_a, p_sys->p_current_song.psz_t );
583 if( p_sys->i_songs >= QUEUE_MAX )
585 msg_Warn( p_this, "Submission queue is full, not submitting" );
589 msg_Dbg( p_this, "Song will be submitted." );
591 #define QUEUE_COPY( a ) \
592 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
594 #define QUEUE_COPY_NULL( a ) \
596 p_sys->p_current_song.a = NULL
599 QUEUE_COPY_NULL( psz_n );
600 QUEUE_COPY_NULL( psz_a );
601 QUEUE_COPY_NULL( psz_t );
602 QUEUE_COPY_NULL( psz_b );
603 QUEUE_COPY_NULL( psz_m );
605 #undef QUEUE_COPY_NULL
610 /* signal the main loop we have something to submit */
611 vlc_object_signal( VLC_OBJECT( p_this ) );
614 DeleteSong( &p_sys->p_current_song );
615 p_sys->b_submit = false;
616 vlc_mutex_unlock( &p_sys->lock );
619 /*****************************************************************************
620 * ParseURL : Split an http:// URL into host, file, and port
622 * Example: "62.216.251.205:80/protocol_1.2"
623 * will be split into "62.216.251.205", 80, "protocol_1.2"
625 * psz_url will be freed before returning
626 * *psz_file & *psz_host will be freed before use
629 * VLC_ENOMEM Out Of Memory
630 * VLC_EGENERIC Invalid url provided
631 * VLC_SUCCESS Success
632 *****************************************************************************/
633 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
637 int i_len = strlen( psz_url );
638 FREENULL( *psz_host );
639 FREENULL( *psz_file );
641 i_pos = strcspn( psz_url, ":" );
645 *psz_host = strndup( psz_url, i_pos );
649 i_pos++; /* skip the ':' */
650 *i_port = atoi( psz_url + i_pos );
653 FREENULL( *psz_host );
657 i_pos = strcspn( psz_url, "/" );
662 i_pos++; /* skip the '/' */
663 *psz_file = strdup( psz_url + i_pos );
666 FREENULL( *psz_host );
674 /*****************************************************************************
675 * Handshake : Init audioscrobbler connection
676 *****************************************************************************/
677 static int Handshake( intf_thread_t *p_this )
679 char *psz_username, *psz_password;
681 char psz_timestamp[21];
683 struct md5_s p_struct_md5;
686 char *psz_handshake_url;
687 uint8_t p_buffer[1024];
693 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
694 intf_sys_t *p_sys = p_this->p_sys;
696 psz_username = config_GetPsz( p_this, "lastfm-username" );
700 psz_password = config_GetPsz( p_this, "lastfm-password" );
703 free( psz_username );
707 /* username or password have not been setup */
708 if ( !*psz_username || !*psz_password )
710 free( psz_username );
711 free( psz_password );
717 /* generates a md5 hash of the password */
718 InitMD5( &p_struct_md5 );
719 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
720 EndMD5( &p_struct_md5 );
722 free( psz_password );
724 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
725 if( !psz_password_md5 )
727 free( psz_username );
731 snprintf( psz_timestamp, sizeof( psz_timestamp ), "%"PRIu64,
732 (uint64_t)timestamp );
734 /* generates a md5 hash of :
735 * - md5 hash of the password, plus
736 * - timestamp in clear text
738 InitMD5( &p_struct_md5 );
739 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
740 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
741 EndMD5( &p_struct_md5 );
742 free( psz_password_md5 );
744 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
745 if( !psz_auth_token )
747 free( psz_username );
750 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
751 free( psz_auth_token );
753 if( !asprintf( &psz_handshake_url,
754 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
755 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
756 p_sys->psz_auth_token ) )
758 free( psz_username );
761 free( psz_username );
763 /* send the http handshake request */
764 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
765 free( psz_handshake_url );
771 i_ret = stream_Read( p_stream, p_buffer, 1023 );
774 stream_Delete( p_stream );
777 p_buffer[i_ret] = '\0';
778 stream_Delete( p_stream );
780 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
783 /* handshake request failed, sorry */
784 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
788 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
791 /* authentication failed, bad username/password combination */
792 intf_UserFatal( p_this, false,
793 _("last.fm: Authentication failed"),
794 _("last.fm username or password is incorrect. "
795 "Please verify your settings and relaunch VLC." ) );
796 return VLC_AUDIOSCROBBLER_EFATAL;
799 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
802 /* oops, our version of vlc has been banned by last.fm servers */
803 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
804 "You should upgrade VLC, or disable the last.fm plugin." );
805 return VLC_AUDIOSCROBBLER_EFATAL;
808 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
811 /* The system clock isn't good */
812 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
813 "much shifted. Please correct it, and relaunch VLC." );
814 return VLC_AUDIOSCROBBLER_EFATAL;
817 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
821 p_buffer_pos = strstr( p_buffer_pos, "\n" );
822 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
824 p_buffer_pos++; /* we skip the '\n' */
826 /* save the session ID */
827 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
829 p_buffer_pos = strstr( p_buffer_pos, "http://" );
830 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
833 /* We need to read the nowplaying url */
834 p_buffer_pos += 7; /* we skip "http://" */
836 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
840 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
841 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
852 p_buffer_pos = strstr( p_buffer_pos, "http://" );
853 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
856 /* We need to read the submission url */
857 p_buffer_pos += 7; /* we skip "http://" */
858 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
862 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
863 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
880 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
884 /*****************************************************************************
885 * DeleteSong : Delete the char pointers in a song
886 *****************************************************************************/
887 static void DeleteSong( audioscrobbler_song_t* p_song )
889 FREENULL( p_song->psz_a );
890 FREENULL( p_song->psz_b );
891 FREENULL( p_song->psz_t );
892 FREENULL( p_song->psz_m );
893 FREENULL( p_song->psz_n );
896 /*****************************************************************************
897 * ReadMetaData : Read meta data when parsed by vlc
898 *****************************************************************************/
899 static int ReadMetaData( intf_thread_t *p_this )
901 playlist_t *p_playlist;
902 input_thread_t *p_input;
903 input_item_t *p_item;
905 intf_sys_t *p_sys = p_this->p_sys;
907 p_playlist = pl_Yield( p_this );
909 p_input = p_playlist->p_input;
913 pl_Release( p_this );
914 return( VLC_SUCCESS );
917 vlc_object_yield( p_input );
919 pl_Release( p_this );
921 p_item = input_GetItem( p_input );
926 #define ALLOC_ITEM_META( a, b ) \
927 psz_meta = input_item_Get##b( p_item ); \
928 if( psz_meta && *psz_meta ) \
930 a = encode_URI_component( psz_meta ); \
938 vlc_mutex_lock( &p_sys->lock );
940 p_sys->b_meta_read = true;
942 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
945 vlc_mutex_unlock( &p_sys->lock );
946 msg_Dbg( p_this, "No artist.." );
947 vlc_object_release( p_input );
953 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
956 vlc_mutex_unlock( &p_sys->lock );
957 msg_Dbg( p_this, "No track name.." );
958 vlc_object_release( p_input );
959 free( p_sys->p_current_song.psz_a );
965 /* Now we have read the mandatory meta data, so we can submit that info */
966 p_sys->b_submit = true;
968 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
970 p_sys->p_current_song.psz_b = calloc( 1, 1 );
973 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
975 p_sys->p_current_song.psz_m = calloc( 1, 1 );
978 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
980 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
982 p_sys->p_current_song.psz_n = calloc( 1, 1 );
984 #undef ALLOC_ITEM_META
986 msg_Dbg( p_this, "Meta data registered" );
988 vlc_mutex_unlock( &p_sys->lock );
989 vlc_object_release( p_input );
994 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
996 if( *i_interval == 0 )
998 /* first interval is 1 minute */
1003 /* else we double the previous interval, up to 120 minutes */
1005 if( *i_interval > 120 )
1008 *next = mdate() + ( *i_interval * 1000000 * 60 );