1 /*****************************************************************************
2 * fingerprinter.c: Audio fingerprinter module
3 *****************************************************************************
4 * Copyright (C) 2012 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
24 #include <vlc_common.h>
25 #include <vlc_plugin.h>
26 #include <vlc_stream.h>
27 #include <vlc_modules.h>
32 #include <vlc_input.h>
33 #include <vlc_fingerprinter.h>
34 #include <webservices/acoustid.h>
35 #include <../stream_out/chromaprint_data.h>
37 /*****************************************************************************
39 *****************************************************************************/
41 struct fingerprinter_sys_t
49 } incoming, processing, results;
51 vlc_cond_t incoming_queue_filled;
60 /* tracked in sys for cancelability */
62 input_thread_t *p_input;
63 chromaprint_fingerprint_t chroma_fingerprint;
66 /* clobberable by cleanups */
71 static int Open (vlc_object_t *);
72 static void Close (vlc_object_t *);
73 static void Run (fingerprinter_thread_t *);
75 /*****************************************************************************
77 ****************************************************************************/
79 set_category(CAT_ADVANCED)
80 set_subcategory(SUBCAT_ADVANCED_MISC)
81 set_shortname(N_("acoustid"))
82 set_description(N_("Track fingerprinter (based on Acoustid)"))
83 set_capability("fingerprinter", 10)
84 set_callbacks(Open, Close)
87 /*****************************************************************************
89 *****************************************************************************/
91 static void EnqueueRequest( fingerprinter_thread_t *f, fingerprint_request_t *r )
93 fingerprinter_sys_t *p_sys = f->p_sys;
94 vlc_mutex_lock( &p_sys->incoming.lock );
95 vlc_array_append( p_sys->incoming.queue, r );
96 vlc_mutex_unlock( &p_sys->incoming.lock );
97 vlc_cond_signal( &p_sys->incoming_queue_filled );
100 static void QueueIncomingRequests( fingerprinter_sys_t *p_sys )
102 vlc_mutex_lock( &p_sys->incoming.lock );
103 int i = vlc_array_count( p_sys->incoming.queue );
104 if ( i == 0 ) goto end;
105 vlc_mutex_lock( &p_sys->processing.lock );
107 vlc_array_append( p_sys->processing.queue,
108 vlc_array_item_at_index( p_sys->incoming.queue, --i ) );
109 vlc_array_clear( p_sys->incoming.queue );
110 vlc_mutex_unlock( &p_sys->processing.lock );
112 vlc_mutex_unlock(&p_sys->incoming.lock);
115 static fingerprint_request_t * GetResult( fingerprinter_thread_t *f )
117 fingerprint_request_t *r = NULL;
118 fingerprinter_sys_t *p_sys = f->p_sys;
119 vlc_mutex_lock( &p_sys->results.lock );
120 if ( vlc_array_count( p_sys->results.queue ) )
122 r = vlc_array_item_at_index( p_sys->results.queue, 0 );
123 vlc_array_remove( p_sys->results.queue, 0 );
125 vlc_mutex_unlock( &p_sys->results.lock );
129 static void ApplyResult( fingerprint_request_t *p_r, int i_resultid )
131 if ( i_resultid >= vlc_array_count( & p_r->results.metas_array ) ) return;
133 vlc_meta_t *p_meta = (vlc_meta_t *)
134 vlc_array_item_at_index( & p_r->results.metas_array, i_resultid );
135 input_item_t *p_item = p_r->p_item;
136 vlc_mutex_lock( &p_item->lock );
137 vlc_meta_Merge( p_item->p_meta, p_meta );
138 vlc_mutex_unlock( &p_item->lock );
141 static void cancelDoFingerprint( void *p_arg )
143 fingerprinter_sys_t *p_sys = ( fingerprinter_sys_t * ) p_arg;
144 if ( p_sys->p_input )
146 input_Stop( p_sys->p_input, true );
147 input_Close( p_sys->p_input );
149 /* cleanup temporary result */
150 if ( p_sys->chroma_fingerprint.psz_fingerprint )
151 FREENULL( p_sys->chroma_fingerprint.psz_fingerprint );
153 input_item_Release( p_sys->p_item );
156 static int inputStateCallback( vlc_object_t *obj, const char *var,
157 vlc_value_t old, vlc_value_t cur, void *p_data )
159 VLC_UNUSED(obj);VLC_UNUSED(var);VLC_UNUSED(old);
160 fingerprinter_sys_t *p_sys = (fingerprinter_sys_t *) p_data;
161 if ( cur.i_int != INPUT_EVENT_STATE ) return VLC_SUCCESS;
162 p_sys->condwait.i_input_state = var_GetInteger( p_sys->p_input, "state" );
163 vlc_cond_signal( & p_sys->condwait.wait );
167 static void DoFingerprint( vlc_object_t *p_this, fingerprinter_sys_t *p_sys, acoustid_fingerprint_t *fp )
169 p_sys->p_input = NULL;
170 p_sys->p_item = NULL;
171 p_sys->chroma_fingerprint.psz_fingerprint = NULL;
172 vlc_cleanup_push( cancelDoFingerprint, p_sys );
174 p_sys->p_item = input_item_New( NULL, NULL );
175 if ( ! p_sys->p_item ) goto end;
177 char *psz_sout_option;
178 /* Note: need at -max- 2 channels, but we can't guess it before playing */
179 /* the stereo upmix could make the mono tracks fingerprint to differ :/ */
180 if ( asprintf( &psz_sout_option,
181 "sout=#transcode{acodec=%s,channels=2}:chromaprint",
182 ( VLC_CODEC_S16L == VLC_CODEC_S16N ) ? "s16l" : "s16b" )
184 input_item_AddOption( p_sys->p_item, psz_sout_option, VLC_INPUT_OPTION_TRUSTED );
185 free( psz_sout_option );
186 input_item_AddOption( p_sys->p_item, "vout=dummy", VLC_INPUT_OPTION_TRUSTED );
187 input_item_AddOption( p_sys->p_item, "aout=dummy", VLC_INPUT_OPTION_TRUSTED );
188 if ( fp->i_duration )
190 if ( asprintf( &psz_sout_option, "stop-time=%u", fp->i_duration ) == -1 ) goto end;
191 input_item_AddOption( p_sys->p_item, psz_sout_option, VLC_INPUT_OPTION_TRUSTED );
192 free( psz_sout_option );
194 input_item_SetURI( p_sys->p_item, p_sys->psz_uri ) ;
196 p_sys->p_input = input_Create( p_this, p_sys->p_item, "fingerprinter", NULL );
197 if ( p_sys->p_input )
199 p_sys->chroma_fingerprint.i_duration = fp->i_duration;
200 var_Create( p_sys->p_input, "fingerprint-data", VLC_VAR_ADDRESS );
201 var_SetAddress( p_sys->p_input, "fingerprint-data", & p_sys->chroma_fingerprint );
203 input_Start( p_sys->p_input );
205 /* Wait for input to start && end */
206 p_sys->condwait.i_input_state = var_GetInteger( p_sys->p_input, "state" );
208 if ( likely( var_AddCallback( p_sys->p_input, "intf-event",
209 inputStateCallback, p_sys ) == VLC_SUCCESS ) )
211 while( p_sys->condwait.i_input_state <= PAUSE_S )
213 vlc_mutex_lock( &p_sys->condwait.lock );
214 mutex_cleanup_push( &p_sys->condwait.lock );
215 vlc_cond_wait( &p_sys->condwait.wait, &p_sys->condwait.lock );
218 var_DelCallback( p_sys->p_input, "intf-event", inputStateCallback, p_sys );
220 input_Stop( p_sys->p_input, true );
221 input_Close( p_sys->p_input );
222 p_sys->p_input = NULL;
224 if ( p_sys->chroma_fingerprint.psz_fingerprint )
226 fp->psz_fingerprint = strdup( p_sys->chroma_fingerprint.psz_fingerprint );
227 if ( ! fp->i_duration ) /* had not given hint */
228 fp->i_duration = p_sys->chroma_fingerprint.i_duration;
235 /*****************************************************************************
237 *****************************************************************************/
238 static int Open(vlc_object_t *p_this)
240 fingerprinter_thread_t *p_fingerprinter = (fingerprinter_thread_t*) p_this;
241 fingerprinter_sys_t *p_sys = calloc(1, sizeof(fingerprinter_sys_t));
246 p_fingerprinter->p_sys = p_sys;
248 p_sys->incoming.queue = vlc_array_new();
249 vlc_mutex_init( &p_sys->incoming.lock );
250 vlc_cond_init( &p_sys->incoming_queue_filled );
252 p_sys->processing.queue = vlc_array_new();
253 vlc_mutex_init( &p_sys->processing.lock );
255 p_sys->results.queue = vlc_array_new();
256 vlc_mutex_init( &p_sys->results.lock );
258 vlc_mutex_init( &p_sys->condwait.lock );
259 vlc_cond_init( &p_sys->condwait.wait );
261 p_sys->psz_uri = NULL;
263 p_fingerprinter->pf_run = Run;
264 p_fingerprinter->pf_enqueue = EnqueueRequest;
265 p_fingerprinter->pf_getresults = GetResult;
266 p_fingerprinter->pf_apply = ApplyResult;
268 var_Create( p_fingerprinter, "results-available", VLC_VAR_BOOL );
269 if( p_fingerprinter->pf_run
270 && vlc_clone( &p_sys->thread,
271 (void *(*) (void *)) p_fingerprinter->pf_run,
272 p_fingerprinter, VLC_THREAD_PRIORITY_LOW ) )
274 msg_Err( p_fingerprinter, "cannot spawn fingerprinter thread" );
285 /*****************************************************************************
287 *****************************************************************************/
288 static void Close(vlc_object_t *p_this)
290 fingerprinter_thread_t *p_fingerprinter = (fingerprinter_thread_t*) p_this;
291 fingerprinter_sys_t *p_sys = p_fingerprinter->p_sys;
293 vlc_cancel( p_sys->thread );
294 vlc_join( p_sys->thread, NULL );
296 vlc_mutex_destroy( &p_sys->condwait.lock );
297 vlc_cond_destroy( &p_sys->condwait.wait );
299 for ( int i = 0; i < vlc_array_count( p_sys->incoming.queue ); i++ )
300 fingerprint_request_Delete( vlc_array_item_at_index( p_sys->incoming.queue, i ) );
301 vlc_array_destroy( p_sys->incoming.queue );
302 vlc_mutex_destroy( &p_sys->incoming.lock );
303 vlc_cond_destroy( &p_sys->incoming_queue_filled );
305 for ( int i = 0; i < vlc_array_count( p_sys->processing.queue ); i++ )
306 fingerprint_request_Delete( vlc_array_item_at_index( p_sys->processing.queue, i ) );
307 vlc_array_destroy( p_sys->processing.queue );
308 vlc_mutex_destroy( &p_sys->processing.lock );
310 for ( int i = 0; i < vlc_array_count( p_sys->results.queue ); i++ )
311 fingerprint_request_Delete( vlc_array_item_at_index( p_sys->results.queue, i ) );
312 vlc_array_destroy( p_sys->results.queue );
313 vlc_mutex_destroy( &p_sys->results.lock );
318 static void fill_metas_with_results( fingerprint_request_t *p_r, acoustid_fingerprint_t *p_f )
320 for( unsigned int i=0 ; i < p_f->results.count; i++ )
322 acoustid_result_t *p_result = & p_f->results.p_results[ i ];
323 for ( unsigned int j=0 ; j < p_result->recordings.count; j++ )
325 musicbrainz_recording_t *p_record = & p_result->recordings.p_recordings[ j ];
326 vlc_meta_t *p_meta = vlc_meta_New();
329 vlc_meta_Set( p_meta, vlc_meta_Title, p_record->psz_title );
330 vlc_meta_Set( p_meta, vlc_meta_Artist, p_record->psz_artist );
331 vlc_meta_AddExtra( p_meta, "musicbrainz-id", p_record->sz_musicbrainz_id );
332 vlc_array_append( & p_r->results.metas_array, p_meta );
338 /*****************************************************************************
340 *****************************************************************************/
341 static void cancelRun( void * p_arg )
343 fingerprinter_sys_t *p_sys = ( fingerprinter_sys_t * ) p_arg;
344 if ( vlc_array_count( p_sys->processing.queue ) )
345 vlc_array_clear( p_sys->processing.queue );
346 if ( p_sys->psz_uri )
347 free( p_sys->psz_uri );
350 static void clearPrint( void * p_arg )
352 acoustid_fingerprint_t *acoustid_print = ( acoustid_fingerprint_t * ) p_arg;
353 for( unsigned int j=0 ; j < acoustid_print->results.count; j++ )
354 free_acoustid_result_t( &acoustid_print->results.p_results[j] );
355 if ( acoustid_print->results.count )
356 free( acoustid_print->results.p_results );
357 if ( acoustid_print->psz_fingerprint )
358 free( acoustid_print->psz_fingerprint );
361 static void Run( fingerprinter_thread_t *p_fingerprinter )
363 fingerprinter_sys_t *p_sys = p_fingerprinter->p_sys;
368 vlc_mutex_lock( &p_sys->processing.lock );
369 mutex_cleanup_push( &p_sys->processing.lock );
370 vlc_cond_timedwait( &p_sys->incoming_queue_filled, &p_sys->processing.lock, mdate() + 1000000 );
373 QueueIncomingRequests( p_sys );
375 vlc_mutex_lock( &p_sys->processing.lock ); // L0
376 mutex_cleanup_push( &p_sys->processing.lock );
377 vlc_cleanup_push( cancelRun, p_sys ); // C1
379 for ( p_sys->i = 0 ; p_sys->i < vlc_array_count( p_sys->processing.queue ); p_sys->i++ )
381 fingerprint_request_t *p_data = vlc_array_item_at_index( p_sys->processing.queue, p_sys->i );
382 acoustid_fingerprint_t acoustid_print;
383 memset( &acoustid_print , 0, sizeof(acoustid_fingerprint_t) );
384 vlc_cleanup_push( clearPrint, &acoustid_print ); // C2
385 p_sys->psz_uri = input_item_GetURI( p_data->p_item );
386 if ( p_sys->psz_uri )
388 /* overwrite with hint, as in this case, fingerprint's session will be truncated */
389 if ( p_data->i_duration ) acoustid_print.i_duration = p_data->i_duration;
391 DoFingerprint( VLC_OBJECT(p_fingerprinter), p_sys, &acoustid_print );
393 DoAcoustIdWebRequest( VLC_OBJECT(p_fingerprinter), &acoustid_print );
394 fill_metas_with_results( p_data, &acoustid_print );
395 FREENULL( p_sys->psz_uri );
397 vlc_cleanup_run( ); // C2
400 vlc_mutex_lock( &p_sys->results.lock );
401 vlc_array_append( p_sys->results.queue, p_data );
402 vlc_mutex_unlock( &p_sys->results.lock );
407 if ( vlc_array_count( p_sys->processing.queue ) )
409 var_TriggerCallback( p_fingerprinter, "results-available" );
410 vlc_array_clear( p_sys->processing.queue );
412 vlc_cleanup_pop( ); // C1
414 vlc_cleanup_run(); // L0