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>
47 #include <vlc_block.h>
48 #include <vlc_stream.h>
50 #include <vlc_network.h>
51 #include <vlc_interface.h>
52 #include <vlc_playlist.h>
54 /*****************************************************************************
56 *****************************************************************************/
60 /* Keeps track of metadata to be submitted */
61 typedef struct audioscrobbler_song_t
63 char *psz_a; /**< track artist */
64 char *psz_t; /**< track title */
65 char *psz_b; /**< track album */
66 char *psz_n; /**< track number */
67 int i_l; /**< track length */
68 char *psz_m; /**< musicbrainz id */
69 time_t date; /**< date since epoch */
70 mtime_t i_start; /**< playing start */
71 } audioscrobbler_song_t;
75 audioscrobbler_song_t p_queue[QUEUE_MAX]; /**< songs not submitted yet*/
76 int i_songs; /**< number of songs */
78 vlc_mutex_t lock; /**< p_sys mutex */
79 vlc_cond_t wait; /**< song to submit event */
81 /* data about audioscrobbler session */
82 mtime_t next_exchange; /**< when can we send data */
83 unsigned int i_interval; /**< waiting interval (secs)*/
85 /* submission of played songs */
86 char *psz_submit_host; /**< where to submit data */
87 int i_submit_port; /**< port to which submit */
88 char *psz_submit_file; /**< file to which submit */
90 /* submission of playing song */
92 char *psz_nowp_host; /**< where to submit data */
93 int i_nowp_port; /**< port to which submit */
94 char *psz_nowp_file; /**< file to which submit */
96 bool b_handshaked; /**< are we authenticated ? */
97 char psz_auth_token[33]; /**< Authentication token */
99 /* data about song currently playing */
100 audioscrobbler_song_t p_current_song; /**< song being played */
102 mtime_t time_pause; /**< time when vlc paused */
103 mtime_t time_total_pauses; /**< total time in pause */
105 bool b_submit; /**< do we have to submit ? */
107 bool b_state_cb; /**< if we registered the
108 * "state" callback */
110 bool b_meta_read; /**< if we read the song's
111 * metadata already */
114 static int Open ( vlc_object_t * );
115 static void Close ( vlc_object_t * );
116 static void Run ( intf_thread_t * );
118 static int ItemChange ( vlc_object_t *, const char *, vlc_value_t,
119 vlc_value_t, void * );
120 static int PlayingChange ( vlc_object_t *, const char *, vlc_value_t,
121 vlc_value_t, void * );
123 static void AddToQueue ( intf_thread_t * );
124 static int Handshake ( intf_thread_t * );
125 static int ReadMetaData ( intf_thread_t * );
126 static void DeleteSong ( audioscrobbler_song_t* );
127 static int ParseURL ( char *, char **, char **, int * );
128 static void HandleInterval ( mtime_t *, unsigned int * );
130 /*****************************************************************************
132 ****************************************************************************/
134 #define USERNAME_TEXT N_("Username")
135 #define USERNAME_LONGTEXT N_("The username of your last.fm account")
136 #define PASSWORD_TEXT N_("Password")
137 #define PASSWORD_LONGTEXT N_("The password of your last.fm account")
139 /* This error value is used when last.fm plugin has to be unloaded. */
140 #define VLC_AUDIOSCROBBLER_EFATAL -69
142 /* last.fm client identifier */
143 #define CLIENT_NAME PACKAGE
144 #define CLIENT_VERSION VERSION
146 /* HTTP POST request : to submit data */
147 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
148 "Accept-Encoding: identity\n" \
149 "Content-length: %u\n" \
150 "Connection: close\n" \
151 "Content-type: application/x-www-form-urlencoded\n" \
153 "User-agent: VLC Media Player/%s\r\n" \
159 set_category( CAT_INTERFACE )
160 set_subcategory( SUBCAT_INTERFACE_CONTROL )
161 set_shortname( N_( "Audioscrobbler" ) )
162 set_description( N_("Submission of played songs to last.fm") )
163 add_string( "lastfm-username", "", NULL,
164 USERNAME_TEXT, USERNAME_LONGTEXT, false );
165 add_password( "lastfm-password", "", NULL,
166 PASSWORD_TEXT, PASSWORD_LONGTEXT, false );
167 set_capability( "interface", 0 )
168 set_callbacks( Open, Close )
171 /*****************************************************************************
172 * Open: initialize and create stuff
173 *****************************************************************************/
174 static int Open( vlc_object_t *p_this )
176 playlist_t *p_playlist;
177 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
178 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
183 p_intf->p_sys = p_sys;
185 vlc_mutex_init( &p_sys->lock );
186 vlc_cond_init( &p_sys->wait );
188 p_playlist = pl_Hold( p_intf );
190 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
192 pl_Release( p_intf );
194 p_intf->pf_run = Run;
199 /*****************************************************************************
200 * Close: destroy interface stuff
201 *****************************************************************************/
202 static void Close( vlc_object_t *p_this )
204 playlist_t *p_playlist;
205 input_thread_t *p_input;
206 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
207 intf_sys_t *p_sys = p_intf->p_sys;
209 p_playlist = pl_Hold( p_intf );
213 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
215 p_input = playlist_CurrentInput( p_playlist );
218 if( p_sys->b_state_cb )
219 var_DelCallback( p_input, "intf-event", PlayingChange, p_intf );
221 vlc_object_release( p_input );
224 pl_Release( p_intf );
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_cond_destroy( &p_sys->wait );
237 vlc_mutex_destroy( &p_sys->lock );
242 /*****************************************************************************
243 * Run : call Handshake() then submit songs
244 *****************************************************************************/
245 static void Run( intf_thread_t *p_intf )
247 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
250 uint8_t p_buffer[1024];
253 int canc = vlc_savecancel();
255 intf_sys_t *p_sys = p_intf->p_sys;
263 vlc_restorecancel( canc );
264 vlc_mutex_lock( &p_sys->lock );
265 mutex_cleanup_push( &p_sys->lock );
267 if( mdate() < p_sys->next_exchange )
268 /* wait until we can resubmit, i.e. */
269 b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock,
270 p_sys->next_exchange ) == 0;
272 /* wait for data to submit */
273 /* we are signaled each time there is a song to submit */
274 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
276 canc = vlc_savecancel();
279 continue; /* holding on until next_exchange */
281 /* handshake if needed */
282 if( p_sys->b_handshaked == false )
284 msg_Dbg( p_intf, "Handshaking with last.fm ..." );
286 switch( Handshake( p_intf ) )
292 /* username not set */
293 intf_UserFatal( p_intf, false,
294 _("Last.fm username not set"),
295 _("Please set a username or disable the "
296 "audioscrobbler plugin, and restart VLC.\n"
297 "Visit http://www.last.fm/join/ to get an account.")
302 msg_Dbg( p_intf, "Handshake successfull :)" );
303 p_sys->b_handshaked = true;
304 p_sys->i_interval = 0;
305 p_sys->next_exchange = mdate();
308 case VLC_AUDIOSCROBBLER_EFATAL:
309 msg_Warn( p_intf, "Exiting..." );
314 /* protocol error : we'll try later */
315 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
318 /* if handshake failed let's restart the loop */
319 if( p_sys->b_handshaked == false )
323 msg_Dbg( p_intf, "Going to submit some data..." );
325 /* The session may be invalid if there is a trailing \n */
326 char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' );
330 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
331 { /* Out of memory */
335 /* forge the HTTP POST request */
336 vlc_mutex_lock( &p_sys->lock );
337 audioscrobbler_song_t *p_song;
338 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
340 p_song = &p_sys->p_queue[i_song];
341 if( !asprintf( &psz_submit_song,
351 i_song, p_song->psz_a,
352 i_song, p_song->psz_t,
353 i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
357 i_song, p_song->psz_b,
358 i_song, p_song->psz_n,
359 i_song, p_song->psz_m
361 { /* Out of memory */
362 vlc_mutex_unlock( &p_sys->lock );
365 psz_submit_tmp = psz_submit;
366 if( !asprintf( &psz_submit, "%s%s",
367 psz_submit_tmp, psz_submit_song ) )
368 { /* Out of memory */
369 free( psz_submit_tmp );
370 free( psz_submit_song );
371 vlc_mutex_unlock( &p_sys->lock );
374 free( psz_submit_song );
375 free( psz_submit_tmp );
377 vlc_mutex_unlock( &p_sys->lock );
379 i_post_socket = net_ConnectTCP( p_intf,
380 p_sys->psz_submit_host, p_sys->i_submit_port );
382 if ( i_post_socket == -1 )
384 /* If connection fails, we assume we must handshake again */
385 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
386 p_sys->b_handshaked = false;
391 /* we transmit the data */
392 i_net_ret = net_Printf(
393 VLC_OBJECT( p_intf ), i_post_socket, NULL,
394 POST_REQUEST, p_sys->psz_submit_file,
395 (unsigned)strlen( psz_submit ), p_sys->psz_submit_file,
400 if ( i_net_ret == -1 )
402 /* If connection fails, we assume we must handshake again */
403 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
404 p_sys->b_handshaked = false;
408 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
409 p_buffer, 1023, false );
410 if ( i_net_ret <= 0 )
412 /* if we get no answer, something went wrong : try again */
416 net_Close( i_post_socket );
417 p_buffer[i_net_ret] = '\0';
419 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
422 msg_Warn( p_intf, "%s", p_buffer_pos );
423 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
427 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
430 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
431 p_sys->b_handshaked = false;
432 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
436 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
440 for( i = 0; i < p_sys->i_songs; i++ )
441 DeleteSong( &p_sys->p_queue[i] );
443 p_sys->i_interval = 0;
444 p_sys->next_exchange = mdate();
445 msg_Dbg( p_intf, "Submission successful!" );
449 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
451 p_sys->b_handshaked = false;
452 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
456 vlc_restorecancel( canc );
459 /*****************************************************************************
460 * PlayingChange: Playing status change callback
461 *****************************************************************************/
462 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
463 vlc_value_t oldval, vlc_value_t newval, void *p_data )
465 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
466 intf_sys_t *p_sys = p_intf->p_sys;
467 input_thread_t *p_input = ( input_thread_t* )p_this;
468 vlc_value_t state_value;
470 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
472 if( newval.i_int != INPUT_EVENT_STATE ) return VLC_SUCCESS;
474 state_value.i_int = 0;
476 var_Get( p_input, "state", &state_value );
479 if( p_sys->b_meta_read == false && state_value.i_int >= PLAYING_S )
481 ReadMetaData( p_intf );
486 if( state_value.i_int >= END_S )
487 AddToQueue( p_intf );
488 else if( state_value.i_int == PAUSE_S )
489 p_sys->time_pause = mdate();
490 else if( p_sys->time_pause > 0 && state_value.i_int == PLAYING_S )
491 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
496 /*****************************************************************************
497 * ItemChange: Playlist item change callback
498 *****************************************************************************/
499 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
500 vlc_value_t oldval, vlc_value_t newval, void *p_data )
502 playlist_t *p_playlist;
503 input_thread_t *p_input;
504 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
505 intf_sys_t *p_sys = p_intf->p_sys;
506 input_item_t *p_item;
507 vlc_value_t video_val;
509 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
510 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
512 p_sys->b_state_cb = false;
513 p_sys->b_meta_read = false;
514 p_sys->b_submit = false;
516 p_playlist = pl_Hold( p_intf );
517 p_input = playlist_CurrentInput( p_playlist );
519 if( !p_input || p_input->b_dead )
521 pl_Release( p_intf );
525 pl_Release( p_intf );
527 p_item = input_GetItem( p_input );
530 vlc_object_release( p_input );
534 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
535 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
537 msg_Dbg( p_this, "Not an audio local file, not submitting");
538 vlc_object_release( p_input );
542 p_sys->time_total_pauses = 0;
543 time( &p_sys->p_current_song.date ); /* to be sent to last.fm */
544 p_sys->p_current_song.i_start = mdate(); /* only used locally */
546 var_AddCallback( p_input, "intf-event", PlayingChange, p_intf );
547 p_sys->b_state_cb = true;
549 if( input_item_IsPreparsed( p_item ) )
550 ReadMetaData( p_intf );
551 /* if the input item was not preparsed, we'll do it in PlayingChange()
552 * callback, when "state" == PLAYING_S */
554 vlc_object_release( p_input );
558 /*****************************************************************************
559 * AddToQueue: Add the played song to the queue to be submitted
560 *****************************************************************************/
561 static void AddToQueue ( intf_thread_t *p_this )
564 intf_sys_t *p_sys = p_this->p_sys;
566 vlc_mutex_lock( &p_sys->lock );
567 if( !p_sys->b_submit )
570 /* wait for the user to listen enough before submitting */
571 played_time = mdate() - p_sys->p_current_song.i_start -
572 p_sys->time_total_pauses;
573 played_time /= 1000000; /* µs → s */
575 /*HACK: it seam that the preparsing sometime fail,
576 so use the playing time as the song length */
577 if( p_sys->p_current_song.i_l == 0 )
578 p_sys->p_current_song.i_l = played_time;
580 /* Don't send song shorter than 30s */
581 if( p_sys->p_current_song.i_l < 30 )
583 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
587 /* Send if the user had listen more than 240s OR half the track length */
588 if( ( played_time < 240 ) &&
589 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
591 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
595 /* Check that all meta are present */
596 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
597 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
599 msg_Dbg( p_this, "Missing artist or title, not submitting" );
603 if( p_sys->i_songs >= QUEUE_MAX )
605 msg_Warn( p_this, "Submission queue is full, not submitting" );
609 msg_Dbg( p_this, "Song will be submitted." );
611 #define QUEUE_COPY( a ) \
612 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
614 #define QUEUE_COPY_NULL( a ) \
616 p_sys->p_current_song.a = NULL
619 QUEUE_COPY_NULL( psz_n );
620 QUEUE_COPY_NULL( psz_a );
621 QUEUE_COPY_NULL( psz_t );
622 QUEUE_COPY_NULL( psz_b );
623 QUEUE_COPY_NULL( psz_m );
625 #undef QUEUE_COPY_NULL
630 /* signal the main loop we have something to submit */
631 vlc_cond_signal( &p_sys->wait );
634 DeleteSong( &p_sys->p_current_song );
635 p_sys->b_submit = false;
636 vlc_mutex_unlock( &p_sys->lock );
639 /*****************************************************************************
640 * ParseURL : Split an http:// URL into host, file, and port
642 * Example: "62.216.251.205:80/protocol_1.2"
643 * will be split into "62.216.251.205", 80, "protocol_1.2"
645 * psz_url will be freed before returning
646 * *psz_file & *psz_host will be freed before use
649 * VLC_ENOMEM Out Of Memory
650 * VLC_EGENERIC Invalid url provided
651 * VLC_SUCCESS Success
652 *****************************************************************************/
653 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
657 int i_len = strlen( psz_url );
658 FREENULL( *psz_host );
659 FREENULL( *psz_file );
661 i_pos = strcspn( psz_url, ":" );
665 *psz_host = strndup( psz_url, i_pos );
669 i_pos++; /* skip the ':' */
670 *i_port = atoi( psz_url + i_pos );
673 FREENULL( *psz_host );
677 i_pos = strcspn( psz_url, "/" );
682 i_pos++; /* skip the '/' */
683 *psz_file = strdup( psz_url + i_pos );
686 FREENULL( *psz_host );
694 /*****************************************************************************
695 * Handshake : Init audioscrobbler connection
696 *****************************************************************************/
697 static int Handshake( intf_thread_t *p_this )
699 char *psz_username, *psz_password;
701 char psz_timestamp[21];
703 struct md5_s p_struct_md5;
706 char *psz_handshake_url;
707 uint8_t p_buffer[1024];
713 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
714 intf_sys_t *p_sys = p_this->p_sys;
716 psz_username = config_GetPsz( p_this, "lastfm-username" );
720 psz_password = config_GetPsz( p_this, "lastfm-password" );
723 free( psz_username );
727 /* username or password have not been setup */
728 if ( !*psz_username || !*psz_password )
730 free( psz_username );
731 free( psz_password );
737 /* generates a md5 hash of the password */
738 InitMD5( &p_struct_md5 );
739 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
740 EndMD5( &p_struct_md5 );
742 free( psz_password );
744 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
745 if( !psz_password_md5 )
747 free( psz_username );
751 snprintf( psz_timestamp, sizeof( psz_timestamp ), "%"PRIu64,
752 (uint64_t)timestamp );
754 /* generates a md5 hash of :
755 * - md5 hash of the password, plus
756 * - timestamp in clear text
758 InitMD5( &p_struct_md5 );
759 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
760 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
761 EndMD5( &p_struct_md5 );
762 free( psz_password_md5 );
764 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
765 if( !psz_auth_token )
767 free( psz_username );
770 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
771 free( psz_auth_token );
773 if( !asprintf( &psz_handshake_url,
774 "http://post.audioscrobbler.com/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s",
775 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
776 p_sys->psz_auth_token ) )
778 free( psz_username );
781 free( psz_username );
783 /* send the http handshake request */
784 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
785 free( psz_handshake_url );
791 i_ret = stream_Read( p_stream, p_buffer, 1023 );
794 stream_Delete( p_stream );
797 p_buffer[i_ret] = '\0';
798 stream_Delete( p_stream );
800 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
803 /* handshake request failed, sorry */
804 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
808 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
811 /* authentication failed, bad username/password combination */
812 intf_UserFatal( p_this, false,
813 _("last.fm: Authentication failed"),
814 _("last.fm username or password is incorrect. "
815 "Please verify your settings and relaunch VLC." ) );
816 return VLC_AUDIOSCROBBLER_EFATAL;
819 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
822 /* oops, our version of vlc has been banned by last.fm servers */
823 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
824 "You should upgrade VLC, or disable the last.fm plugin." );
825 return VLC_AUDIOSCROBBLER_EFATAL;
828 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
831 /* The system clock isn't good */
832 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
833 "much shifted. Please correct it, and relaunch VLC." );
834 return VLC_AUDIOSCROBBLER_EFATAL;
837 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
841 p_buffer_pos = strstr( p_buffer_pos, "\n" );
842 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
844 p_buffer_pos++; /* we skip the '\n' */
846 /* save the session ID */
847 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
849 p_buffer_pos = strstr( p_buffer_pos, "http://" );
850 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
853 /* We need to read the nowplaying url */
854 p_buffer_pos += 7; /* we skip "http://" */
856 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
860 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
861 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
872 p_buffer_pos = strstr( p_buffer_pos, "http://" );
873 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
876 /* We need to read the submission url */
877 p_buffer_pos += 7; /* we skip "http://" */
878 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
882 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
883 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
900 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
904 /*****************************************************************************
905 * DeleteSong : Delete the char pointers in a song
906 *****************************************************************************/
907 static void DeleteSong( audioscrobbler_song_t* p_song )
909 FREENULL( p_song->psz_a );
910 FREENULL( p_song->psz_b );
911 FREENULL( p_song->psz_t );
912 FREENULL( p_song->psz_m );
913 FREENULL( p_song->psz_n );
916 /*****************************************************************************
917 * ReadMetaData : Read meta data when parsed by vlc
918 *****************************************************************************/
919 static int ReadMetaData( intf_thread_t *p_this )
921 playlist_t *p_playlist;
922 input_thread_t *p_input;
923 input_item_t *p_item;
925 intf_sys_t *p_sys = p_this->p_sys;
927 p_playlist = pl_Hold( p_this );
928 p_input = playlist_CurrentInput( p_playlist );
931 pl_Release( p_this );
932 return( VLC_SUCCESS );
935 pl_Release( p_this );
937 p_item = input_GetItem( p_input );
942 #define ALLOC_ITEM_META( a, b ) \
943 psz_meta = input_item_Get##b( p_item ); \
944 if( psz_meta && *psz_meta ) \
946 a = encode_URI_component( psz_meta ); \
949 vlc_mutex_unlock( &p_sys->lock ); \
950 vlc_object_release( p_input ); \
956 vlc_mutex_lock( &p_sys->lock );
958 p_sys->b_meta_read = true;
960 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
963 vlc_mutex_unlock( &p_sys->lock );
964 msg_Dbg( p_this, "No artist.." );
965 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 );
983 /* Now we have read the mandatory meta data, so we can submit that info */
984 p_sys->b_submit = true;
986 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
988 p_sys->p_current_song.psz_b = calloc( 1, 1 );
991 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
993 p_sys->p_current_song.psz_m = calloc( 1, 1 );
996 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
998 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
1000 p_sys->p_current_song.psz_n = calloc( 1, 1 );
1002 #undef ALLOC_ITEM_META
1004 msg_Dbg( p_this, "Meta data registered" );
1006 vlc_mutex_unlock( &p_sys->lock );
1007 vlc_object_release( p_input );
1012 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1014 if( *i_interval == 0 )
1016 /* first interval is 1 minute */
1021 /* else we double the previous interval, up to 120 minutes */
1023 if( *i_interval > 120 )
1026 *next = mdate() + ( *i_interval * 1000000 * 60 );