]> git.sesse.net Git - vlc/blob - modules/misc/fingerprinter.c
0daebcc0a1d53dcf6444c999e089dfcc604bcac9
[vlc] / modules / misc / fingerprinter.c
1 /*****************************************************************************
2  * fingerprinter.c: Audio fingerprinter module
3  *****************************************************************************
4  * Copyright (C) 2012 VLC authors and VideoLAN
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <vlc_common.h>
25 #include <vlc_plugin.h>
26 #include <vlc_stream.h>
27 #include <vlc_modules.h>
28 #include <vlc_meta.h>
29 #include <vlc_url.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_input.h>
33 #include <vlc_fingerprinter.h>
34 #include <webservices/acoustid.h>
35 #include <../stream_out/chromaprint_data.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 struct fingerprinter_sys_t
42 {
43     vlc_thread_t thread;
44
45     struct
46     {
47         vlc_array_t         *queue;
48         vlc_mutex_t         lock;
49     } incoming, processing, results;
50
51     vlc_cond_t              incoming_queue_filled;
52
53     struct
54     {
55         vlc_mutex_t         lock;
56         vlc_cond_t          wait;
57         int                 i_input_state;
58     } condwait;
59
60     /* tracked in sys for cancelability */
61     input_item_t            *p_item;
62     input_thread_t          *p_input;
63     chromaprint_fingerprint_t chroma_fingerprint;
64     char                    *psz_uri;
65
66     /* clobberable by cleanups */
67     int                     i_cancel_state;
68     int                     i;
69 };
70
71 static int  Open            (vlc_object_t *);
72 static void Close           (vlc_object_t *);
73 static void Run             (fingerprinter_thread_t *);
74
75 /*****************************************************************************
76  * Module descriptor
77  ****************************************************************************/
78 vlc_module_begin ()
79     set_category(CAT_ADVANCED)
80     set_shortname(N_("acoustid"))
81     set_description(N_("Track fingerprinter (based on Acoustid)"))
82     set_capability("fingerprinter", 1)
83     set_callbacks(Open, Close)
84 vlc_module_end ()
85
86 /*****************************************************************************
87  * Requests lifecycle
88  *****************************************************************************/
89
90 static void EnqueueRequest( fingerprinter_thread_t *f, fingerprint_request_t *r )
91 {
92     fingerprinter_sys_t *p_sys = f->p_sys;
93     vlc_mutex_lock( &p_sys->incoming.lock );
94     vlc_array_append( p_sys->incoming.queue, r );
95     vlc_mutex_unlock( &p_sys->incoming.lock );
96     vlc_cond_signal( &p_sys->incoming_queue_filled );
97 }
98
99 static void QueueIncomingRequests( fingerprinter_sys_t *p_sys )
100 {
101     vlc_mutex_lock( &p_sys->incoming.lock );
102     int i = vlc_array_count( p_sys->incoming.queue );
103     if ( i == 0 ) goto end;
104     vlc_mutex_lock( &p_sys->processing.lock );
105     while( i )
106         vlc_array_append( p_sys->processing.queue,
107                           vlc_array_item_at_index( p_sys->incoming.queue, --i ) );
108     vlc_array_clear( p_sys->incoming.queue );
109     vlc_mutex_unlock( &p_sys->processing.lock );
110 end:
111     vlc_mutex_unlock(&p_sys->incoming.lock);
112 }
113
114 static fingerprint_request_t * GetResult( fingerprinter_thread_t *f )
115 {
116     fingerprint_request_t *r = NULL;
117     fingerprinter_sys_t *p_sys = f->p_sys;
118     vlc_mutex_lock( &p_sys->results.lock );
119     if ( vlc_array_count( p_sys->results.queue ) )
120     {
121         r = vlc_array_item_at_index( p_sys->results.queue, 0 );
122         vlc_array_remove( p_sys->results.queue, 0 );
123     }
124     vlc_mutex_unlock( &p_sys->results.lock );
125     return r;
126 }
127
128 static void ApplyResult( fingerprint_request_t *p_r, int i_resultid )
129 {
130     if ( i_resultid >= vlc_array_count( & p_r->results.metas_array ) ) return;
131
132     vlc_meta_t *p_meta = (vlc_meta_t *)
133             vlc_array_item_at_index( & p_r->results.metas_array, i_resultid );
134     input_item_t *p_item = p_r->p_item;
135     vlc_mutex_lock( &p_item->lock );
136     vlc_meta_Merge( p_item->p_meta, p_meta );
137     vlc_mutex_unlock( &p_item->lock );
138 }
139
140 static void cancelDoFingerprint( void *p_arg )
141 {
142     fingerprinter_sys_t *p_sys = ( fingerprinter_sys_t * ) p_arg;
143     if ( p_sys->p_input )
144     {
145         input_Stop( p_sys->p_input, true );
146         input_Close( p_sys->p_input );
147     }
148     /* cleanup temporary result */
149     if ( p_sys->chroma_fingerprint.psz_fingerprint )
150         FREENULL( p_sys->chroma_fingerprint.psz_fingerprint );
151     if ( p_sys->p_item )
152         input_item_Release( p_sys->p_item );
153 }
154
155 static int inputStateCallback( vlc_object_t *obj, const char *var,
156                                vlc_value_t old, vlc_value_t cur, void *p_data )
157 {
158     VLC_UNUSED(obj);VLC_UNUSED(var);VLC_UNUSED(old);
159     fingerprinter_sys_t *p_sys = (fingerprinter_sys_t *) p_data;
160     if ( cur.i_int != INPUT_EVENT_STATE ) return VLC_SUCCESS;
161     p_sys->condwait.i_input_state = var_GetInteger( p_sys->p_input, "state" );
162     vlc_cond_signal( & p_sys->condwait.wait );
163     return VLC_SUCCESS;
164 }
165
166 static void DoFingerprint( vlc_object_t *p_this, fingerprinter_sys_t *p_sys, acoustid_fingerprint_t *fp )
167 {
168     p_sys->p_input = NULL;
169     p_sys->p_item = NULL;
170     p_sys->chroma_fingerprint.psz_fingerprint = NULL;
171     vlc_cleanup_push( cancelDoFingerprint, p_sys );
172
173     p_sys->p_item = input_item_New( NULL, NULL );
174     if ( ! p_sys->p_item ) goto end;
175
176     char *psz_sout_option;
177     /* Note: need at -max- 2 channels, but we can't guess it before playing */
178     /* the stereo upmix could make the mono tracks fingerprint to differ :/ */
179     if ( asprintf( &psz_sout_option,
180                    "sout=#transcode{acodec=%s,channels=2}:chromaprint",
181                    ( VLC_CODEC_S16L == VLC_CODEC_S16N ) ? "s16l" : "s16b" )
182          == -1 ) goto end;
183     input_item_AddOption( p_sys->p_item, psz_sout_option, VLC_INPUT_OPTION_TRUSTED );
184     free( psz_sout_option );
185     input_item_AddOption( p_sys->p_item, "vout=dummy", VLC_INPUT_OPTION_TRUSTED );
186     input_item_AddOption( p_sys->p_item, "aout=dummy", VLC_INPUT_OPTION_TRUSTED );
187     if ( fp->i_duration )
188     {
189         if ( asprintf( &psz_sout_option, "stop-time=%u", fp->i_duration ) == -1 ) goto end;
190         input_item_AddOption( p_sys->p_item, psz_sout_option, VLC_INPUT_OPTION_TRUSTED );
191         free( psz_sout_option );
192     }
193     input_item_SetURI( p_sys->p_item, p_sys->psz_uri ) ;
194
195     p_sys->p_input = input_Create( p_this, p_sys->p_item, "fingerprinter", NULL );
196     if ( p_sys->p_input )
197     {
198         p_sys->chroma_fingerprint.i_duration = fp->i_duration;
199         var_Create( p_sys->p_input, "fingerprint-data", VLC_VAR_ADDRESS );
200         var_SetAddress( p_sys->p_input, "fingerprint-data", & p_sys->chroma_fingerprint );
201
202         input_Start( p_sys->p_input );
203
204         /* Wait for input to start && end */
205         p_sys->condwait.i_input_state = var_GetInteger( p_sys->p_input, "state" );
206
207         if ( likely( var_AddCallback( p_sys->p_input, "intf-event",
208                             inputStateCallback, p_sys ) == VLC_SUCCESS ) )
209         {
210             while( p_sys->condwait.i_input_state <= PAUSE_S )
211             {
212                 vlc_mutex_lock( &p_sys->condwait.lock );
213                 mutex_cleanup_push( &p_sys->condwait.lock );
214                 vlc_cond_wait( &p_sys->condwait.wait, &p_sys->condwait.lock );
215                 vlc_cleanup_run();
216             }
217             var_DelCallback( p_sys->p_input, "intf-event", inputStateCallback, p_sys );
218         }
219         input_Stop( p_sys->p_input, true );
220         input_Close( p_sys->p_input );
221         p_sys->p_input = NULL;
222
223         if ( p_sys->chroma_fingerprint.psz_fingerprint )
224         {
225             fp->psz_fingerprint = strdup( p_sys->chroma_fingerprint.psz_fingerprint );
226             if ( ! fp->i_duration ) /* had not given hint */
227                 fp->i_duration = p_sys->chroma_fingerprint.i_duration;
228         }
229     }
230 end:
231     vlc_cleanup_run( );
232 }
233
234 /*****************************************************************************
235  * Open:
236  *****************************************************************************/
237 static int Open(vlc_object_t *p_this)
238 {
239     fingerprinter_thread_t *p_fingerprinter = (fingerprinter_thread_t*) p_this;
240     fingerprinter_sys_t *p_sys = calloc(1, sizeof(fingerprinter_sys_t));
241
242     if ( !p_sys )
243         return VLC_ENOMEM;
244
245     p_fingerprinter->p_sys = p_sys;
246
247     p_sys->incoming.queue = vlc_array_new();
248     vlc_mutex_init( &p_sys->incoming.lock );
249     vlc_cond_init( &p_sys->incoming_queue_filled );
250
251     p_sys->processing.queue = vlc_array_new();
252     vlc_mutex_init( &p_sys->processing.lock );
253
254     p_sys->results.queue = vlc_array_new();
255     vlc_mutex_init( &p_sys->results.lock );
256
257     vlc_mutex_init( &p_sys->condwait.lock );
258     vlc_cond_init( &p_sys->condwait.wait );
259
260     p_sys->psz_uri = NULL;
261
262     p_fingerprinter->pf_run = Run;
263     p_fingerprinter->pf_enqueue = EnqueueRequest;
264     p_fingerprinter->pf_getresults = GetResult;
265     p_fingerprinter->pf_apply = ApplyResult;
266
267     var_Create( p_fingerprinter, "results-available", VLC_VAR_BOOL );
268     if( p_fingerprinter->pf_run
269      && vlc_clone( &p_sys->thread,
270                    (void *(*) (void *)) p_fingerprinter->pf_run,
271                    p_fingerprinter, VLC_THREAD_PRIORITY_LOW ) )
272     {
273         msg_Err( p_fingerprinter, "cannot spawn fingerprinter thread" );
274         goto error;
275     }
276
277     return VLC_SUCCESS;
278
279 error:
280     free( p_sys );
281     return VLC_EGENERIC;
282 }
283
284 /*****************************************************************************
285  * Close:
286  *****************************************************************************/
287 static void Close(vlc_object_t *p_this)
288 {
289     fingerprinter_thread_t   *p_fingerprinter = (fingerprinter_thread_t*) p_this;
290     fingerprinter_sys_t *p_sys = p_fingerprinter->p_sys;
291
292     vlc_cancel( p_sys->thread );
293     vlc_join( p_sys->thread, NULL );
294
295     vlc_mutex_destroy( &p_sys->condwait.lock );
296     vlc_cond_destroy( &p_sys->condwait.wait );
297
298     for ( int i = 0; i < vlc_array_count( p_sys->incoming.queue ); i++ )
299         fingerprint_request_Delete( vlc_array_item_at_index( p_sys->incoming.queue, i ) );
300     vlc_array_destroy( p_sys->incoming.queue );
301     vlc_mutex_destroy( &p_sys->incoming.lock );
302     vlc_cond_destroy( &p_sys->incoming_queue_filled );
303
304     for ( int i = 0; i < vlc_array_count( p_sys->processing.queue ); i++ )
305         fingerprint_request_Delete( vlc_array_item_at_index( p_sys->processing.queue, i ) );
306     vlc_array_destroy( p_sys->processing.queue );
307     vlc_mutex_destroy( &p_sys->processing.lock );
308
309     for ( int i = 0; i < vlc_array_count( p_sys->results.queue ); i++ )
310         fingerprint_request_Delete( vlc_array_item_at_index( p_sys->results.queue, i ) );
311     vlc_array_destroy( p_sys->results.queue );
312     vlc_mutex_destroy( &p_sys->results.lock );
313
314     free( p_sys );
315 }
316
317 static void fill_metas_with_results( fingerprint_request_t *p_r, acoustid_fingerprint_t *p_f )
318 {
319     for( unsigned int i=0 ; i < p_f->results.count; i++ )
320     {
321         acoustid_result_t *p_result = & p_f->results.p_results[ i ];
322         for ( unsigned int j=0 ; j < p_result->recordings.count; j++ )
323         {
324             musicbrainz_recording_t *p_record = & p_result->recordings.p_recordings[ j ];
325             vlc_meta_t *p_meta = vlc_meta_New();
326             if ( p_meta )
327             {
328                 vlc_meta_Set( p_meta, vlc_meta_Title, p_record->psz_title );
329                 vlc_meta_Set( p_meta, vlc_meta_Artist, p_record->psz_artist );
330                 vlc_meta_AddExtra( p_meta, "musicbrainz-id", p_record->sz_musicbrainz_id );
331                 vlc_array_append( & p_r->results.metas_array, p_meta );
332             }
333         }
334     }
335 }
336
337 /*****************************************************************************
338  * Run :
339  *****************************************************************************/
340 static void cancelRun( void * p_arg )
341 {
342     fingerprinter_sys_t *p_sys = ( fingerprinter_sys_t * ) p_arg;
343     if ( vlc_array_count( p_sys->processing.queue ) )
344         vlc_array_clear( p_sys->processing.queue );
345     if ( p_sys->psz_uri )
346         free( p_sys->psz_uri );
347 }
348
349 static void clearPrint( void * p_arg )
350 {
351     acoustid_fingerprint_t *acoustid_print = ( acoustid_fingerprint_t * ) p_arg;
352     for( unsigned int j=0 ; j < acoustid_print->results.count; j++ )
353         free_acoustid_result_t( &acoustid_print->results.p_results[j] );
354     if ( acoustid_print->results.count )
355         free( acoustid_print->results.p_results );
356     if ( acoustid_print->psz_fingerprint )
357         free( acoustid_print->psz_fingerprint );
358 }
359
360 static void Run( fingerprinter_thread_t *p_fingerprinter )
361 {
362     fingerprinter_sys_t *p_sys = p_fingerprinter->p_sys;
363
364     /* main loop */
365     for (;;)
366     {
367         vlc_mutex_lock( &p_sys->processing.lock );
368         mutex_cleanup_push( &p_sys->processing.lock );
369         vlc_cond_timedwait( &p_sys->incoming_queue_filled, &p_sys->processing.lock, mdate() + 1000000 );
370         vlc_cleanup_run();
371
372         QueueIncomingRequests( p_sys );
373
374         vlc_mutex_lock( &p_sys->processing.lock ); // L0
375         mutex_cleanup_push( &p_sys->processing.lock );
376         vlc_cleanup_push( cancelRun, p_sys ); // C1
377 //**
378         for ( p_sys->i = 0 ; p_sys->i < vlc_array_count( p_sys->processing.queue ); p_sys->i++ )
379         {
380             fingerprint_request_t *p_data = vlc_array_item_at_index( p_sys->processing.queue, p_sys->i );
381             acoustid_fingerprint_t acoustid_print;
382             memset( &acoustid_print , 0, sizeof(acoustid_fingerprint_t) );
383             vlc_cleanup_push( clearPrint, &acoustid_print ); // C2
384             p_sys->psz_uri = input_item_GetURI( p_data->p_item );
385             if ( p_sys->psz_uri )
386             {
387                 /* overwrite with hint, as in this case, fingerprint's session will be truncated */
388                 if ( p_data->i_duration ) acoustid_print.i_duration = p_data->i_duration;
389
390                 DoFingerprint( VLC_OBJECT(p_fingerprinter), p_sys, &acoustid_print );
391
392                 DoAcoustIdWebRequest( VLC_OBJECT(p_fingerprinter), &acoustid_print );
393                 fill_metas_with_results( p_data, &acoustid_print );
394                 FREENULL( p_sys->psz_uri );
395             }
396             vlc_cleanup_run( ); // C2
397
398             /* copy results */
399             vlc_mutex_lock( &p_sys->results.lock );
400             vlc_array_append( p_sys->results.queue, p_data );
401             vlc_mutex_unlock( &p_sys->results.lock );
402
403             vlc_testcancel();
404         }
405
406         if ( vlc_array_count( p_sys->processing.queue ) )
407         {
408             var_TriggerCallback( p_fingerprinter, "results-available" );
409             vlc_array_clear( p_sys->processing.queue );
410         }
411         vlc_cleanup_pop( ); // C1
412 //**
413         vlc_cleanup_run(); // L0
414     }
415 }