1 /*****************************************************************************
2 * audioscrobbler.c : audioscrobbler submission plugin
3 *****************************************************************************
4 * Copyright © 2006-2009 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")
137 #define URL_TEXT N_("Scrobbler URL")
138 #define URL_LONGTEXT N_("The URL set for an alternative scrobbler engine")
140 /* This error value is used when last.fm plugin has to be unloaded. */
141 #define VLC_AUDIOSCROBBLER_EFATAL -69
143 /* last.fm client identifier */
144 #define CLIENT_NAME PACKAGE
145 #define CLIENT_VERSION VERSION
147 /* HTTP POST request : to submit data */
148 #define POST_REQUEST "POST /%s HTTP/1.1\n" \
149 "Accept-Encoding: identity\n" \
150 "Content-length: %u\n" \
151 "Connection: close\n" \
152 "Content-type: application/x-www-form-urlencoded\n" \
154 "User-agent: VLC media player/%s\r\n" \
160 set_category( CAT_INTERFACE )
161 set_subcategory( SUBCAT_INTERFACE_CONTROL )
162 set_shortname( N_( "Audioscrobbler" ) )
163 set_description( N_("Submission of played songs to last.fm") )
164 add_string( "lastfm-username", "", NULL,
165 USERNAME_TEXT, USERNAME_LONGTEXT, false )
166 add_password( "lastfm-password", "", NULL,
167 PASSWORD_TEXT, PASSWORD_LONGTEXT, false )
168 add_string( "scrobbler-url", "post.audioscrobbler.com", NULL,
169 URL_TEXT, URL_LONGTEXT, false )
170 set_capability( "interface", 0 )
171 set_callbacks( Open, Close )
174 /*****************************************************************************
175 * Open: initialize and create stuff
176 *****************************************************************************/
177 static int Open( vlc_object_t *p_this )
179 playlist_t *p_playlist;
180 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
181 intf_sys_t *p_sys = calloc( 1, sizeof( intf_sys_t ) );
186 p_intf->p_sys = p_sys;
188 vlc_mutex_init( &p_sys->lock );
189 vlc_cond_init( &p_sys->wait );
191 p_playlist = pl_Hold( p_intf );
193 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
195 pl_Release( p_intf );
197 p_intf->pf_run = Run;
202 /*****************************************************************************
203 * Close: destroy interface stuff
204 *****************************************************************************/
205 static void Close( vlc_object_t *p_this )
207 playlist_t *p_playlist;
208 input_thread_t *p_input;
209 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
210 intf_sys_t *p_sys = p_intf->p_sys;
212 p_playlist = pl_Hold( p_intf );
216 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
218 p_input = playlist_CurrentInput( p_playlist );
221 if( p_sys->b_state_cb )
222 var_DelCallback( p_input, "intf-event", PlayingChange, p_intf );
224 vlc_object_release( p_input );
227 pl_Release( p_intf );
231 for( i = 0; i < p_sys->i_songs; i++ )
232 DeleteSong( &p_sys->p_queue[i] );
233 free( p_sys->psz_submit_host );
234 free( p_sys->psz_submit_file );
236 free( p_sys->psz_nowp_host );
237 free( p_sys->psz_nowp_file );
239 vlc_cond_destroy( &p_sys->wait );
240 vlc_mutex_destroy( &p_sys->lock );
245 /*****************************************************************************
246 * Run : call Handshake() then submit songs
247 *****************************************************************************/
248 static void Run( intf_thread_t *p_intf )
250 char *psz_submit, *psz_submit_song, *psz_submit_tmp;
253 uint8_t p_buffer[1024];
256 int canc = vlc_savecancel();
258 intf_sys_t *p_sys = p_intf->p_sys;
266 vlc_restorecancel( canc );
267 vlc_mutex_lock( &p_sys->lock );
268 mutex_cleanup_push( &p_sys->lock );
270 if( mdate() < p_sys->next_exchange )
271 /* wait until we can resubmit, i.e. */
272 b_wait = vlc_cond_timedwait( &p_sys->wait, &p_sys->lock,
273 p_sys->next_exchange ) == 0;
275 /* wait for data to submit */
276 /* we are signaled each time there is a song to submit */
277 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
279 canc = vlc_savecancel();
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 dialog_Fatal( p_intf,
297 _("Last.fm username not set"),
298 "%s", _("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 /* The session may be invalid if there is a trailing \n */
329 char *psz_ln = strrchr( p_sys->psz_auth_token, '\n' );
333 if( !asprintf( &psz_submit, "s=%s", p_sys->psz_auth_token ) )
334 { /* Out of memory */
338 /* forge the HTTP POST request */
339 vlc_mutex_lock( &p_sys->lock );
340 audioscrobbler_song_t *p_song;
341 for( i_song = 0 ; i_song < p_sys->i_songs ; i_song++ )
343 p_song = &p_sys->p_queue[i_song];
344 if( !asprintf( &psz_submit_song,
354 i_song, p_song->psz_a,
355 i_song, p_song->psz_t,
356 i_song, (unsigned)p_song->date, /* HACK: %ju (uintmax_t) unsupported on Windows */
360 i_song, p_song->psz_b,
361 i_song, p_song->psz_n,
362 i_song, p_song->psz_m
364 { /* Out of memory */
365 vlc_mutex_unlock( &p_sys->lock );
368 psz_submit_tmp = psz_submit;
369 if( !asprintf( &psz_submit, "%s%s",
370 psz_submit_tmp, psz_submit_song ) )
371 { /* Out of memory */
372 free( psz_submit_tmp );
373 free( psz_submit_song );
374 vlc_mutex_unlock( &p_sys->lock );
377 free( psz_submit_song );
378 free( psz_submit_tmp );
380 vlc_mutex_unlock( &p_sys->lock );
382 i_post_socket = net_ConnectTCP( p_intf,
383 p_sys->psz_submit_host, p_sys->i_submit_port );
385 if ( i_post_socket == -1 )
387 /* If connection fails, we assume we must handshake again */
388 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
389 p_sys->b_handshaked = false;
394 /* we transmit the data */
395 i_net_ret = net_Printf(
396 VLC_OBJECT( p_intf ), i_post_socket, NULL,
397 POST_REQUEST, p_sys->psz_submit_file,
398 (unsigned)strlen( psz_submit ), p_sys->psz_submit_host,
403 if ( i_net_ret == -1 )
405 /* If connection fails, we assume we must handshake again */
406 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
407 p_sys->b_handshaked = false;
411 i_net_ret = net_Read( p_intf, i_post_socket, NULL,
412 p_buffer, 1023, false );
413 if ( i_net_ret <= 0 )
415 /* if we get no answer, something went wrong : try again */
419 net_Close( i_post_socket );
420 p_buffer[i_net_ret] = '\0';
422 p_buffer_pos = strstr( ( char * ) p_buffer, "FAILED" );
425 msg_Warn( p_intf, "%s", p_buffer_pos );
426 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
430 p_buffer_pos = strstr( ( char * ) p_buffer, "BADSESSION" );
433 msg_Err( p_intf, "Authentication failed (BADSESSION), are you connected to last.fm with another program ?" );
434 p_sys->b_handshaked = false;
435 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
439 p_buffer_pos = strstr( ( char * ) p_buffer, "OK" );
443 for( i = 0; i < p_sys->i_songs; i++ )
444 DeleteSong( &p_sys->p_queue[i] );
446 p_sys->i_interval = 0;
447 p_sys->next_exchange = mdate();
448 msg_Dbg( p_intf, "Submission successful!" );
452 msg_Err( p_intf, "Authentication failed, handshaking again (%s)",
454 p_sys->b_handshaked = false;
455 HandleInterval( &p_sys->next_exchange, &p_sys->i_interval );
459 vlc_restorecancel( canc );
462 /*****************************************************************************
463 * PlayingChange: Playing status change callback
464 *****************************************************************************/
465 static int PlayingChange( vlc_object_t *p_this, const char *psz_var,
466 vlc_value_t oldval, vlc_value_t newval, void *p_data )
468 VLC_UNUSED( oldval );
470 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
471 intf_sys_t *p_sys = p_intf->p_sys;
472 input_thread_t *p_input = ( input_thread_t* )p_this;
473 vlc_value_t state_value;
475 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
477 if( newval.i_int != INPUT_EVENT_STATE ) return VLC_SUCCESS;
479 state_value.i_int = 0;
481 var_Get( p_input, "state", &state_value );
484 if( p_sys->b_meta_read == false && state_value.i_int >= PLAYING_S )
486 ReadMetaData( p_intf );
491 if( state_value.i_int >= END_S )
492 AddToQueue( p_intf );
493 else if( state_value.i_int == PAUSE_S )
494 p_sys->time_pause = mdate();
495 else if( p_sys->time_pause > 0 && state_value.i_int == PLAYING_S )
497 p_sys->time_total_pauses += ( mdate() - p_sys->time_pause );
498 p_sys->time_pause = 0;
504 /*****************************************************************************
505 * ItemChange: Playlist item change callback
506 *****************************************************************************/
507 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
508 vlc_value_t oldval, vlc_value_t newval, void *p_data )
510 playlist_t *p_playlist;
511 input_thread_t *p_input;
512 intf_thread_t *p_intf = ( intf_thread_t* ) p_data;
513 intf_sys_t *p_sys = p_intf->p_sys;
514 input_item_t *p_item;
515 vlc_value_t video_val;
517 VLC_UNUSED( p_this ); VLC_UNUSED( psz_var );
518 VLC_UNUSED( oldval ); VLC_UNUSED( newval );
520 p_sys->b_state_cb = false;
521 p_sys->b_meta_read = false;
522 p_sys->b_submit = false;
524 p_playlist = pl_Hold( p_intf );
525 p_input = playlist_CurrentInput( p_playlist );
527 if( !p_input || p_input->b_dead )
529 pl_Release( p_intf );
533 pl_Release( p_intf );
535 p_item = input_GetItem( p_input );
538 vlc_object_release( p_input );
542 var_Change( p_input, "video-es", VLC_VAR_CHOICESCOUNT, &video_val, NULL );
543 if( ( video_val.i_int > 0 ) || p_item->i_type == ITEM_TYPE_NET )
545 msg_Dbg( p_this, "Not an audio local file, not submitting");
546 vlc_object_release( p_input );
550 p_sys->time_total_pauses = 0;
551 time( &p_sys->p_current_song.date ); /* to be sent to last.fm */
552 p_sys->p_current_song.i_start = mdate(); /* only used locally */
554 var_AddCallback( p_input, "intf-event", PlayingChange, p_intf );
555 p_sys->b_state_cb = true;
557 if( input_item_IsPreparsed( p_item ) )
558 ReadMetaData( p_intf );
559 /* if the input item was not preparsed, we'll do it in PlayingChange()
560 * callback, when "state" == PLAYING_S */
562 vlc_object_release( p_input );
566 /*****************************************************************************
567 * AddToQueue: Add the played song to the queue to be submitted
568 *****************************************************************************/
569 static void AddToQueue ( intf_thread_t *p_this )
572 intf_sys_t *p_sys = p_this->p_sys;
574 vlc_mutex_lock( &p_sys->lock );
575 if( !p_sys->b_submit )
578 /* wait for the user to listen enough before submitting */
579 played_time = mdate() - p_sys->p_current_song.i_start -
580 p_sys->time_total_pauses;
581 played_time /= 1000000; /* µs → s */
583 /*HACK: it seam that the preparsing sometime fail,
584 so use the playing time as the song length */
585 if( p_sys->p_current_song.i_l == 0 )
586 p_sys->p_current_song.i_l = played_time;
588 /* Don't send song shorter than 30s */
589 if( p_sys->p_current_song.i_l < 30 )
591 msg_Dbg( p_this, "Song too short (< 30s), not submitting" );
595 /* Send if the user had listen more than 240s OR half the track length */
596 if( ( played_time < 240 ) &&
597 ( played_time < ( p_sys->p_current_song.i_l / 2 ) ) )
599 msg_Dbg( p_this, "Song not listened long enough, not submitting" );
603 /* Check that all meta are present */
604 if( !p_sys->p_current_song.psz_a || !*p_sys->p_current_song.psz_a ||
605 !p_sys->p_current_song.psz_t || !*p_sys->p_current_song.psz_t )
607 msg_Dbg( p_this, "Missing artist or title, not submitting" );
611 if( p_sys->i_songs >= QUEUE_MAX )
613 msg_Warn( p_this, "Submission queue is full, not submitting" );
617 msg_Dbg( p_this, "Song will be submitted." );
619 #define QUEUE_COPY( a ) \
620 p_sys->p_queue[p_sys->i_songs].a = p_sys->p_current_song.a
622 #define QUEUE_COPY_NULL( a ) \
624 p_sys->p_current_song.a = NULL
627 QUEUE_COPY_NULL( psz_n );
628 QUEUE_COPY_NULL( psz_a );
629 QUEUE_COPY_NULL( psz_t );
630 QUEUE_COPY_NULL( psz_b );
631 QUEUE_COPY_NULL( psz_m );
633 #undef QUEUE_COPY_NULL
638 /* signal the main loop we have something to submit */
639 vlc_cond_signal( &p_sys->wait );
642 DeleteSong( &p_sys->p_current_song );
643 p_sys->b_submit = false;
644 vlc_mutex_unlock( &p_sys->lock );
647 /*****************************************************************************
648 * ParseURL : Split an http:// URL into host, file, and port
650 * Example: "62.216.251.205:80/protocol_1.2"
651 * will be split into "62.216.251.205", 80, "protocol_1.2"
653 * psz_url will be freed before returning
654 * *psz_file & *psz_host will be freed before use
657 * VLC_ENOMEM Out Of Memory
658 * VLC_EGENERIC Invalid url provided
659 * VLC_SUCCESS Success
660 *****************************************************************************/
661 static int ParseURL( char *psz_url, char **psz_host, char **psz_file,
665 int i_len = strlen( psz_url );
666 bool b_no_port = false;
667 FREENULL( *psz_host );
668 FREENULL( *psz_file );
670 i_pos = strcspn( psz_url, ":" );
674 i_pos = strcspn( psz_url, "/" );
678 *psz_host = strndup( psz_url, i_pos );
684 i_pos++; /* skip the ':' */
685 *i_port = atoi( psz_url + i_pos );
688 FREENULL( *psz_host );
692 i_pos = strcspn( psz_url, "/" );
698 i_pos++; /* skip the '/' */
699 *psz_file = strdup( psz_url + i_pos );
702 FREENULL( *psz_host );
710 /*****************************************************************************
711 * Handshake : Init audioscrobbler connection
712 *****************************************************************************/
713 static int Handshake( intf_thread_t *p_this )
715 char *psz_username, *psz_password;
716 char *psz_scrobbler_url;
718 char psz_timestamp[21];
720 struct md5_s p_struct_md5;
723 char *psz_handshake_url;
724 uint8_t p_buffer[1024];
730 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
731 intf_sys_t *p_sys = p_this->p_sys;
733 psz_username = config_GetPsz( p_this, "lastfm-username" );
737 psz_password = config_GetPsz( p_this, "lastfm-password" );
740 free( psz_username );
744 /* username or password have not been setup */
745 if ( !*psz_username || !*psz_password )
747 free( psz_username );
748 free( psz_password );
754 /* generates a md5 hash of the password */
755 InitMD5( &p_struct_md5 );
756 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password, strlen( psz_password ) );
757 EndMD5( &p_struct_md5 );
759 free( psz_password );
761 char *psz_password_md5 = psz_md5_hash( &p_struct_md5 );
762 if( !psz_password_md5 )
764 free( psz_username );
768 snprintf( psz_timestamp, sizeof( psz_timestamp ), "%"PRIu64,
769 (uint64_t)timestamp );
771 /* generates a md5 hash of :
772 * - md5 hash of the password, plus
773 * - timestamp in clear text
775 InitMD5( &p_struct_md5 );
776 AddMD5( &p_struct_md5, ( uint8_t* ) psz_password_md5, 32 );
777 AddMD5( &p_struct_md5, ( uint8_t* ) psz_timestamp, strlen( psz_timestamp ));
778 EndMD5( &p_struct_md5 );
779 free( psz_password_md5 );
781 char *psz_auth_token = psz_md5_hash( &p_struct_md5 );
782 if( !psz_auth_token )
784 free( psz_username );
787 strncpy( p_sys->psz_auth_token, psz_auth_token, 33 );
788 free( psz_auth_token );
790 psz_scrobbler_url = config_GetPsz( p_this, "scrobbler-url" );
791 if( !psz_scrobbler_url )
793 free( psz_username );
797 if( !asprintf( &psz_handshake_url,
798 "http://%s/?hs=true&p=1.2&c=%s&v=%s&u=%s&t=%s&a=%s", psz_scrobbler_url,
799 CLIENT_NAME, CLIENT_VERSION, psz_username, psz_timestamp,
800 p_sys->psz_auth_token ) )
802 free( psz_scrobbler_url );
803 free( psz_username );
806 free( psz_scrobbler_url );
807 free( psz_username );
809 /* send the http handshake request */
810 p_stream = stream_UrlNew( p_intf, psz_handshake_url );
811 free( psz_handshake_url );
817 i_ret = stream_Read( p_stream, p_buffer, 1023 );
820 stream_Delete( p_stream );
823 p_buffer[i_ret] = '\0';
824 stream_Delete( p_stream );
826 p_buffer_pos = strstr( ( char* ) p_buffer, "FAILED " );
829 /* handshake request failed, sorry */
830 msg_Err( p_this, "last.fm handshake failed: %s", p_buffer_pos + 7 );
834 p_buffer_pos = strstr( ( char* ) p_buffer, "BADAUTH" );
837 /* authentication failed, bad username/password combination */
838 dialog_Fatal( p_this,
839 _("last.fm: Authentication failed"),
840 "%s", _("last.fm username or password is incorrect. "
841 "Please verify your settings and relaunch VLC." ) );
842 return VLC_AUDIOSCROBBLER_EFATAL;
845 p_buffer_pos = strstr( ( char* ) p_buffer, "BANNED" );
848 /* oops, our version of vlc has been banned by last.fm servers */
849 msg_Err( p_intf, "This version of VLC has been banned by last.fm. "
850 "You should upgrade VLC, or disable the last.fm plugin." );
851 return VLC_AUDIOSCROBBLER_EFATAL;
854 p_buffer_pos = strstr( ( char* ) p_buffer, "BADTIME" );
857 /* The system clock isn't good */
858 msg_Err( p_intf, "last.fm handshake failed because your clock is too "
859 "much shifted. Please correct it, and relaunch VLC." );
860 return VLC_AUDIOSCROBBLER_EFATAL;
863 p_buffer_pos = strstr( ( char* ) p_buffer, "OK" );
867 p_buffer_pos = strstr( p_buffer_pos, "\n" );
868 if( !p_buffer_pos || strlen( p_buffer_pos ) < 34 )
870 p_buffer_pos++; /* we skip the '\n' */
872 /* save the session ID */
873 snprintf( p_sys->psz_auth_token, 33, "%s", p_buffer_pos );
875 p_buffer_pos = strstr( p_buffer_pos, "http://" );
876 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
879 /* We need to read the nowplaying url */
880 p_buffer_pos += 7; /* we skip "http://" */
882 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
886 switch( ParseURL( psz_url, &p_sys->psz_nowp_host,
887 &p_sys->psz_nowp_file, &p_sys->i_nowp_port ) )
898 p_buffer_pos = strstr( p_buffer_pos, "http://" );
899 if( !p_buffer_pos || strlen( p_buffer_pos ) == 7 )
902 /* We need to read the submission url */
903 p_buffer_pos += 7; /* we skip "http://" */
904 psz_url = strndup( p_buffer_pos, strcspn( p_buffer_pos, "\n" ) );
908 switch( ParseURL( psz_url, &p_sys->psz_submit_host,
909 &p_sys->psz_submit_file, &p_sys->i_submit_port ) )
926 msg_Err( p_intf, "Handshake: can't recognize server protocol" );
930 /*****************************************************************************
931 * DeleteSong : Delete the char pointers in a song
932 *****************************************************************************/
933 static void DeleteSong( audioscrobbler_song_t* p_song )
935 FREENULL( p_song->psz_a );
936 FREENULL( p_song->psz_b );
937 FREENULL( p_song->psz_t );
938 FREENULL( p_song->psz_m );
939 FREENULL( p_song->psz_n );
942 /*****************************************************************************
943 * ReadMetaData : Read meta data when parsed by vlc
944 *****************************************************************************/
945 static int ReadMetaData( intf_thread_t *p_this )
947 playlist_t *p_playlist;
948 input_thread_t *p_input;
949 input_item_t *p_item;
951 intf_sys_t *p_sys = p_this->p_sys;
953 p_playlist = pl_Hold( p_this );
954 p_input = playlist_CurrentInput( p_playlist );
957 pl_Release( p_this );
958 return( VLC_SUCCESS );
961 pl_Release( p_this );
963 p_item = input_GetItem( p_input );
968 #define ALLOC_ITEM_META( a, b ) \
969 psz_meta = input_item_Get##b( p_item ); \
970 if( psz_meta && *psz_meta ) \
972 a = encode_URI_component( psz_meta ); \
975 vlc_mutex_unlock( &p_sys->lock ); \
976 vlc_object_release( p_input ); \
982 vlc_mutex_lock( &p_sys->lock );
984 p_sys->b_meta_read = true;
986 ALLOC_ITEM_META( p_sys->p_current_song.psz_a, Artist )
989 vlc_mutex_unlock( &p_sys->lock );
990 msg_Dbg( p_this, "No artist.." );
991 vlc_object_release( p_input );
997 ALLOC_ITEM_META( p_sys->p_current_song.psz_t, Title )
1000 vlc_mutex_unlock( &p_sys->lock );
1001 msg_Dbg( p_this, "No track name.." );
1002 vlc_object_release( p_input );
1003 free( p_sys->p_current_song.psz_a );
1005 return VLC_EGENERIC;
1009 /* Now we have read the mandatory meta data, so we can submit that info */
1010 p_sys->b_submit = true;
1012 ALLOC_ITEM_META( p_sys->p_current_song.psz_b, Album )
1014 p_sys->p_current_song.psz_b = calloc( 1, 1 );
1017 ALLOC_ITEM_META( p_sys->p_current_song.psz_m, TrackID )
1019 p_sys->p_current_song.psz_m = calloc( 1, 1 );
1022 p_sys->p_current_song.i_l = input_item_GetDuration( p_item ) / 1000000;
1024 ALLOC_ITEM_META( p_sys->p_current_song.psz_n, TrackNum )
1026 p_sys->p_current_song.psz_n = calloc( 1, 1 );
1028 #undef ALLOC_ITEM_META
1030 msg_Dbg( p_this, "Meta data registered" );
1032 vlc_mutex_unlock( &p_sys->lock );
1033 vlc_object_release( p_input );
1038 static void HandleInterval( mtime_t *next, unsigned int *i_interval )
1040 if( *i_interval == 0 )
1042 /* first interval is 1 minute */
1047 /* else we double the previous interval, up to 120 minutes */
1049 if( *i_interval > 120 )
1052 *next = mdate() + ( *i_interval * 1000000 * 60 );