]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
bc5332a273713ef4a0467e00ccf16e53eb507935
[vlc] / modules / access / cdda / access.c
1 /*****************************************************************************
2  * cddax.c : CD digital audio input module for vlc using libcdio
3  *****************************************************************************
4  * Copyright (C) 2000, 2003, 2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Rocky Bernstein <rocky@panix.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "callback.h"      /* FIXME - reorganize callback.h, cdda.h better */
30 #include "cdda.h"          /* private structures. Also #includes vlc things */
31 #include <vlc_playlist.h>  /* Has to come *after* cdda.h */
32 #include "vlc_keys.h"
33
34 #include <cdio/cdio.h>
35 #include <cdio/logging.h>
36 #include <cdio/cd_types.h>
37
38 #include <stdio.h>
39
40 /* #ifdef variables below are defined via config.h via #include vlc above. */
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44
45 #ifdef HAVE_SYS_TYPES_H
46 #include <sys/types.h>
47 #endif
48
49 #ifdef HAVE_STRING_H
50 #include <string.h>
51 #endif
52
53 #ifdef HAVE_UNISTD_H
54 #   include <unistd.h>
55 #endif
56
57 #ifdef HAVE_ERRNO_H
58 #   include <errno.h>
59 #endif
60
61 #define CDDA_MRL_PREFIX "cddax://"
62
63 /* Frequency of sample in bits per second. */
64 #define CDDA_FREQUENCY_SAMPLE 44100
65
66 /* FIXME: This variable is a hack. Would be nice to eliminate. */
67 access_t *p_cdda_input = NULL;
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static block_t *CDDAReadBlocks( access_t * p_access );
73 static int      CDDASeek( access_t * p_access, int64_t i_pos );
74 static int      CDDAControl( access_t *p_access, int i_query, 
75                              va_list args );
76 static void     CDDAMetaInfo( access_t *p_access  );
77 static int      CDDAFixupPlaylist( access_t *p_access, cdda_data_t *p_cdda, 
78                                    const char *psz_source, 
79                                    vlc_bool_t b_single_track );
80 static void     CDDACreatePlaylistItem(const access_t *p_access, 
81                                        cdda_data_t *p_cdda,
82                                        playlist_t *p_playlist, 
83                                        track_t i_track,
84                                        char *psz_mrl, int psz_mrl_max,
85                                        const char *psz_source, 
86                                        int playlist_operation,
87                                        int i_pos);
88
89 static int      GetCDInfo( access_t *p_access, cdda_data_t *p_cdda ) ;
90
91
92
93
94 /****************************************************************************
95  * Private functions
96  ****************************************************************************/
97
98 /* process messages that originate from libcdio. */
99 static void
100 cdio_log_handler (cdio_log_level_t level, const char message[])
101 {
102   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
103   switch (level) {
104   case CDIO_LOG_DEBUG:
105   case CDIO_LOG_INFO:
106     if (p_cdda->i_debug & INPUT_DBG_CDIO)
107       msg_Dbg( p_cdda_input, message);
108     break;
109   case CDIO_LOG_WARN:
110     msg_Warn( p_cdda_input, message);
111     break;
112   case CDIO_LOG_ERROR:
113   case CDIO_LOG_ASSERT:
114     msg_Err( p_cdda_input, message);
115     break;
116   default:
117     msg_Warn( p_cdda_input, message,
118             _("The above message had unknown cdio log level"),
119             level);
120   }
121   return;
122 }
123
124
125 #ifdef HAVE_LIBCDDB
126 /*! This routine is called by libcddb routines on error.
127    Setup is done by init_input_plugin.
128 */
129 static void
130 cddb_log_handler (cddb_log_level_t level, const char message[])
131 {
132   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
133   switch (level) {
134   case CDDB_LOG_DEBUG:
135   case CDDB_LOG_INFO:
136     if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
137     /* Fall through if to warn case */
138   default:
139     cdio_log_handler (level, message);
140   }
141 }
142 #endif /*HAVE_LIBCDDB*/
143
144
145 /*! This routine is when xine is not fully set up (before full initialization)
146    or is not around (before finalization).
147 */
148 static void
149 uninit_log_handler (cdio_log_level_t level, const char message[])
150 {
151   cdda_data_t *p_cdda = NULL;
152
153   if (p_cdda_input)
154     p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
155
156   switch (level) {
157   case CDIO_LOG_DEBUG:
158   case CDIO_LOG_INFO:
159     if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
160       return;
161     /* Fall through if to warn case */
162   case CDIO_LOG_WARN:
163     fprintf(stderr, "WARN: %s\n", message);
164     break;
165   case CDIO_LOG_ERROR:
166     fprintf(stderr, "ERROR: %s\n", message);
167     break;
168   case CDIO_LOG_ASSERT:
169     fprintf(stderr, "ASSERT ERROR: %s\n", message);
170     break;
171   default:
172     fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
173             message,
174             _("The above message had unknown cdio log level"),
175             level);
176   }
177
178   /* gl_default_cdio_log_handler (level, message); */
179 }
180
181 /*****************************************************************************
182  * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
183  * an allocated pointer to the data. NULL is returned if no data
184  * read. It is also possible if we haven't read a RIFF header in which
185  * case one that we creaded during Open/Initialization is returned.
186  *****************************************************************************/
187 static block_t *
188 CDDAReadBlocks( access_t * p_access )
189 {
190     block_t     *p_block;
191     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
192     int          i_blocks = p_cdda->i_blocks_per_read;
193
194     dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d", 
195               p_cdda->i_lsn);
196
197     /* Check end of file */
198     if( p_access->info.b_eof ) return NULL;
199
200     if( !p_cdda->b_header )
201       {
202         /* Return only the dummy RIFF header we created in Open/Init */
203         p_block = block_New( p_access, sizeof( WAVEHEADER ) );
204         memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
205         p_cdda->b_header = VLC_TRUE;
206         return p_block;
207     }
208
209     /* Check end of track */
210     while( p_cdda->i_lsn >= p_cdda->p_lsns[p_access->info.i_title + 1] )
211     {
212         if( p_access->info.i_title + 1 >= p_cdda->i_tracks )
213         {
214             p_access->info.b_eof = VLC_TRUE;
215             return NULL;
216         }
217
218         p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
219         p_access->info.i_title++;
220         p_access->info.i_size = 
221           p_cdda->p_title[p_access->info.i_title]->i_size;
222         p_access->info.i_pos = 0;
223         p_cdda->i_track++;
224     }
225
226     /* Possibly adjust i_blocks so we don't read past the end of a track. */
227     if( p_cdda->i_lsn + i_blocks >=
228         p_cdda->p_lsns[p_access->info.i_title + 1] )
229     {
230         i_blocks = p_cdda->p_lsns[p_access->info.i_title + 1 ] -
231                    p_cdda->i_lsn;
232     }
233
234     /* Do the actual reading */
235     p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
236     if( !p_block)
237     {
238       msg_Err( p_access, _("Cannot get a new block of size: %i"),
239                i_blocks * CDIO_CD_FRAMESIZE_RAW );
240       return NULL;
241     }
242
243     if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
244                                  p_cdda->i_lsn, i_blocks) != 0 )
245         {
246           msg_Err( p_access, _("could not read sector %lu"), 
247                    (long unsigned int) p_cdda->i_lsn );
248           block_Release( p_block );
249
250           /* If we had problems above, assume the problem is with
251              the first sector of the read and set to skip it.  In
252              the future libcdio may have cdparanoia support.
253           */
254           p_cdda->i_lsn++;
255           p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
256           return NULL;
257         }
258     
259     p_cdda->i_lsn     += i_blocks;
260     p_access->info.i_pos += p_block->i_buffer;
261
262     return p_block;
263 }
264
265 /****************************************************************************
266  * CDDASeek - change position for subsequent reads. For example, this
267  * can happen if the user moves a position slider bar in a GUI.
268  ****************************************************************************/
269 static int 
270 CDDASeek( access_t * p_access, int64_t i_pos )
271 {
272     cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
273
274     p_cdda->i_lsn = p_cdda->p_lsns[p_access->info.i_title]
275                   + (i_pos / CDIO_CD_FRAMESIZE_RAW);
276     p_access->info.i_pos = i_pos;
277
278     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
279                "lsn %lu, offset: %lld",  
280                (long unsigned int) p_cdda->i_lsn, i_pos );
281     return VLC_SUCCESS;
282 }
283
284 #ifdef HAVE_LIBCDDB
285
286 #define free_and_dup(var, val) \
287   if (var) free(var);          \
288   if (val) var=strdup(val);
289
290
291 static void
292 GetCDDBInfo( access_t *p_access, cdda_data_t *p_cdda )
293 {
294
295   dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
296
297   if (config_GetInt( p_access, MODULE_STRING "-cddb-enabled" )) {
298     int i, i_matches;
299     cddb_conn_t  *conn = cddb_new();
300     const CdIo *p_cdio = p_cdda->p_cdio;
301
302
303     cddb_log_set_handler (uninit_log_handler);
304
305     if (!conn) {
306       msg_Warn( p_access, _("Unable to initialize libcddb") );
307       goto cddb_destroy;
308     }
309
310     cddb_set_email_address( conn,
311                             config_GetPsz( p_access,
312                                            MODULE_STRING "-cddb-email") );
313
314     cddb_set_server_name( conn,
315                           config_GetPsz( p_access,
316                                          MODULE_STRING "-cddb-server") );
317
318     cddb_set_server_port(conn,
319                           config_GetInt( p_access,
320                                          MODULE_STRING "-cddb-port") );
321
322     /* Set the location of the local CDDB cache directory.
323        The default location of this directory is */
324
325     if (!config_GetInt( p_access, MODULE_STRING "-cddb-enable-cache" ))
326       cddb_cache_disable(conn);
327
328     cddb_cache_set_dir(conn,
329                        config_GetPsz( p_access,
330                                       MODULE_STRING "-cddb-cachedir") );
331
332     cddb_set_timeout(conn,
333                      config_GetInt( p_access, MODULE_STRING "-cddb-timeout") );
334
335
336     if (config_GetInt( p_access, MODULE_STRING "-cddb-httpd" )) {
337       cddb_http_enable(conn);
338     } else
339       cddb_http_disable(conn);
340
341     p_cdda->cddb.disc = cddb_disc_new();
342     if (!p_cdda->cddb.disc) {
343       msg_Err( p_access, _("Unable to create CDDB disc structure.") );
344       goto cddb_end;
345     }
346
347     p_cdda->psz_mcn = cdio_get_mcn(p_cdio);
348
349     for(i = 1; i <= p_cdda->i_tracks; i++) {
350       cddb_track_t *t = cddb_track_new();
351       t->frame_offset = cdio_get_track_lba(p_cdio, i);
352       cddb_disc_add_track(p_cdda->cddb.disc, t);
353     }
354
355     p_cdda->cddb.disc->length =
356       cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK)
357       / CDIO_CD_FRAMES_PER_SEC;
358
359     if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
360       msg_Err( p_access, _("CDDB disc ID calculation failed") );
361       goto cddb_destroy;
362     }
363
364     i_matches = cddb_query(conn, p_cdda->cddb.disc);
365     if (i_matches > 0) {
366       if (i_matches > 1)
367         msg_Warn( p_access, _("Found %d matches in CDDB. Using first one."),
368                   i_matches);
369       cddb_read(conn, p_cdda->cddb.disc);
370
371       if (p_cdda->i_debug & INPUT_DBG_CDDB)
372         cddb_disc_print(p_cdda->cddb.disc);
373
374     } else {
375       msg_Warn( p_access, _("CDDB error: %s"), cddb_error_str(errno));
376     }
377
378   cddb_destroy:
379     cddb_destroy(conn);
380   }
381   cddb_end: ;
382 }
383 #endif /*HAVE_LIBCDDB*/
384
385 #define add_meta_val(FIELD, VLC_META, VAL)                              \
386   if ( p_cdda->p_meta && VAL) {                                         \
387     vlc_meta_Add( p_cdda->p_meta, VLC_META, VAL );                      \
388     dbg_print( INPUT_DBG_META, "field %s: %s\n", VLC_META, VAL );       \
389   }                                                                     \
390     
391 #define add_cddb_meta(FIELD, VLC_META)                                  \
392   add_meta_val(FIELD, VLC_META, p_cdda->cddb.disc->FIELD);
393     
394 #define add_cddb_meta_fmt(FIELD, FORMAT_SPEC, VLC_META)                 \
395   {                                                                     \
396     char psz_buf[100];                                                  \
397     snprintf( psz_buf, sizeof(psz_buf)-1, FORMAT_SPEC,                  \
398               p_cdda->cddb.disc->FIELD );                               \
399     psz_buf[sizeof(psz_buf)-1] = '\0';                                  \
400     add_meta_val(FIELD, VLC_META, psz_buf);                             \
401   }    
402
403 /*
404  Gets and saves CDDA Meta Information. In the Control routine, 
405  we handle Meta Information requests and basically copy what we've
406  saved here. 
407  */    
408 static void CDDAMetaInfo( access_t *p_access  )
409 {
410   cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
411
412 #ifdef HAVE_LIBCDDB
413   if ( p_cdda && p_cdda->i_cddb_enabled ) {
414
415     GetCDDBInfo(p_access, p_cdda);
416
417     if ( p_cdda->cddb.disc ) {
418
419       p_cdda->p_meta = vlc_meta_New();
420
421       add_cddb_meta(title,    VLC_META_CDDB_TITLE);
422       add_cddb_meta(artist,   VLC_META_CDDB_ARTIST);
423       add_cddb_meta(genre,    VLC_META_CDDB_GENRE);
424       add_cddb_meta(ext_data, VLC_META_CDDB_EXT_DATA);
425
426       add_cddb_meta_fmt(year,   "%d", VLC_META_CDDB_YEAR);
427       add_cddb_meta_fmt(discid, "%x", VLC_META_CDDB_DISCID);
428     }
429   }
430
431 #endif /*HAVE_LIBCDDB*/
432 #define TITLE_MAX 30
433
434 #if UPDATE_TRACK_INFORMATION_FINISHED
435   {
436     track_t i_track = p_cdda->i_tracks;
437     char psz_buffer[MSTRTIME_MAX_SIZE];
438     mtime_t i_duration =
439       (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[0])
440       / CDIO_CD_FRAMES_PER_SEC;
441
442     dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
443     input_Control( p_access, INPUT_ADD_INFO, _("General"), _("Duration"), "%s",
444                    secstotimestr( psz_buffer, i_duration ) );
445
446     for( i_track = 0 ; i_track < p_cdda->i_tracks ; i_track++ ) {
447       char track_str[TITLE_MAX];
448       mtime_t i_duration =
449         (p_cdda->p_lsns[i_track+1] - p_cdda->p_lsns[i_track])
450         / CDIO_CD_FRAMES_PER_SEC;
451       snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
452       input_Control( p_access, INPUT_ADD_INFO, track_str, _("Duration"), "%s",
453                      secstotimestr( psz_buffer, i_duration ) );
454
455 #ifdef HAVE_LIBCDDB
456       if (p_cdda->i_cddb_enabled) {
457         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
458                                             i_track);
459         if (t != NULL) {
460           if ( t->artist != NULL && strlen(t->artist) ) {
461             input_Control( p_access, INPUT_ADD_INFO, track_str,
462                            _("Artist"), "%s", t->artist );
463           }
464           if ( t->title != NULL && strlen(t->title) ) {
465             input_Control( p_access, INPUT_ADD_INFO, track_str,
466                            _("Title"), "%s",  t->title );
467           }
468           if ( t->ext_data != NULL && strlen(t->ext_data) ) {
469             input_Control( p_access, INPUT_ADD_INFO, track_str,
470                            _("Extended Data"), "%s",  t->ext_data );
471           }
472         }
473       }
474 #endif /*HAVE_LIBCDDB*/
475     }
476   }
477 #endif /* UPDATE_TRACK_INFORMATION_FINISHED */
478 }
479
480 #define add_format_str_info(val)                         \
481   {                                                      \
482     const char *str = val;                               \
483     unsigned int len;                                    \
484     if (val != NULL) {                                   \
485       len=strlen(str);                                   \
486       if (len != 0) {                                    \
487         strncat(tp, str, TEMP_STR_LEN-(tp-temp_str));    \
488         tp += len;                                       \
489       }                                                  \
490       saw_control_prefix = false;                        \
491     }                                                    \
492   }
493
494 #define add_format_num_info(val, fmt)                    \
495   {                                                      \
496     char num_str[10];                                    \
497     unsigned int len;                                    \
498     sprintf(num_str, fmt, val);                          \
499     len=strlen(num_str);                                 \
500     if (len != 0) {                                      \
501       strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str));  \
502       tp += len;                                         \
503     }                                                    \
504     saw_control_prefix = false;                          \
505   }
506
507 /*!
508    Take a format string and expand escape sequences, that is sequences that
509    begin with %, with information from the current CD.
510    The expanded string is returned. Here is a list of escape sequences:
511
512    %a : The album artist **
513    %A : The album information **
514    %C : Category **
515    %I : CDDB disk ID **
516    %G : Genre **
517    %M : The current MRL
518    %m : The CD-DA Media Catalog Number (MCN)
519    %n : The number of tracks on the CD
520    %p : The artist/performer/composer in the track **
521    %T : The track number **
522    %s : Number of seconds in this track
523    %t : The name **
524    %Y : The year 19xx or 20xx **
525    %% : a %
526 */
527 static char *
528 CDDAFormatStr( const access_t *p_access, cdda_data_t *p_cdda,
529                const char format_str[], const char *mrl, int i_track)
530 {
531 #define TEMP_STR_SIZE 256
532 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
533   static char    temp_str[TEMP_STR_SIZE];
534   size_t i;
535   char * tp = temp_str;
536   vlc_bool_t saw_control_prefix = false;
537   size_t format_len = strlen(format_str);
538
539   memset(temp_str, 0, TEMP_STR_SIZE);
540
541   for (i=0; i<format_len; i++) {
542
543     if (!saw_control_prefix && format_str[i] != '%') {
544       *tp++ = format_str[i];
545       saw_control_prefix = false;
546       continue;
547     }
548
549     switch(format_str[i]) {
550     case '%':
551       if (saw_control_prefix) {
552         *tp++ = '%';
553       }
554       saw_control_prefix = !saw_control_prefix;
555       break;
556 #ifdef HAVE_LIBCDDB
557     case 'a':
558       if (!p_cdda->i_cddb_enabled) goto not_special;
559       if (p_cdda->cddb.disc)
560         add_format_str_info(p_cdda->cddb.disc->artist);
561       break;
562     case 'A':
563       if (!p_cdda->i_cddb_enabled) goto not_special;
564       if (p_cdda->cddb.disc)
565         add_format_str_info(p_cdda->cddb.disc->title);
566       break;
567     case 'C':
568       if (!p_cdda->i_cddb_enabled) goto not_special;
569       if (p_cdda->cddb.disc)
570         add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
571       break;
572     case 'G':
573       if (!p_cdda->i_cddb_enabled) goto not_special;
574       if (p_cdda->cddb.disc)
575         add_format_str_info(p_cdda->cddb.disc->genre);
576       break;
577     case 'I':
578       if (!p_cdda->i_cddb_enabled) goto not_special;
579       if (p_cdda->cddb.disc)
580         add_format_num_info(p_cdda->cddb.disc->discid, "%x");
581       break;
582     case 'Y':
583       if (!p_cdda->i_cddb_enabled) goto not_special;
584       if (p_cdda->cddb.disc)
585         add_format_num_info(p_cdda->cddb.disc->year, "%5d");
586       break;
587     case 't':
588       if (p_cdda && p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
589         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
590                                             i_track-1);
591         if (t != NULL && t->title != NULL)
592           add_format_str_info(t->title);
593       } else goto not_special;
594       break;
595     case 'p':
596       if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
597         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
598                                             i_track-1);
599         if (t != NULL && t->artist != NULL)
600           add_format_str_info(t->artist);
601       } else goto not_special;
602       break;
603     case 'e':
604       if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
605         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
606                                             i_track-1);
607         if (t != NULL && t->ext_data != NULL)
608           add_format_str_info(t->ext_data);
609       } else goto not_special;
610       break;
611 #endif
612
613     case 'M':
614       add_format_str_info(mrl);
615       break;
616
617     case 'm':
618       add_format_str_info(p_cdda->psz_mcn);
619       break;
620
621     case 'n':
622       add_format_num_info(p_cdda->i_tracks, "%d");
623       break;
624
625     case 's':
626       if (p_cdda->i_cddb_enabled) {
627         char psz_buffer[MSTRTIME_MAX_SIZE];
628         mtime_t i_duration =
629           (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[i_track-1])
630           / CDIO_CD_FRAMES_PER_SEC;
631         add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
632       } else goto not_special;
633       break;
634
635     case 'T':
636       add_format_num_info(i_track, "%02d");
637       break;
638 #ifdef HAVE_LIBCDDB
639     not_special:
640 #endif
641     default:
642       *tp++ = '%';
643       *tp++ = format_str[i];
644       saw_control_prefix = false;
645     }
646   }
647   return strdup(temp_str);
648 }
649
650 static void
651 CDDACreatePlaylistItem(const access_t *p_access, cdda_data_t *p_cdda,
652                        playlist_t *p_playlist, track_t i_track,
653                        char *psz_mrl, int psz_mrl_max,
654                        const char *psz_source, int playlist_operation,
655                        int i_pos)
656 {
657   mtime_t i_duration =
658     (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[i_track-1])
659     * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
660   char *p_author;
661   char *p_title;
662   char *config_varname = MODULE_STRING "-title-format";
663   playlist_item_t *p_item;
664
665 #ifdef HAVE_LIBCDDB
666   if (p_cdda->i_cddb_enabled) {
667     config_varname = MODULE_STRING "-cddb-title-format";
668   }
669 #endif /*HAVE_LIBCDDB*/
670
671   snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
672            CDDA_MRL_PREFIX, psz_source, i_track);
673
674   p_title = CDDAFormatStr(p_access, p_cdda,
675                           config_GetPsz( p_access, config_varname ),
676                           psz_mrl, i_track);
677
678   dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
679              psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
680   playlist_AddExt( p_playlist, psz_mrl, p_title, playlist_operation,
681                          i_pos, i_duration , NULL, 0);
682
683   if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
684
685   vlc_mutex_lock( &p_playlist->object_lock );
686   p_item = playlist_ItemGetByPos( p_playlist, i_pos );
687   vlc_mutex_unlock( &p_playlist->object_lock );
688   if( !p_item )
689       return;
690
691   vlc_mutex_lock( &p_item->input.lock );
692
693   p_author =
694     CDDAFormatStr( p_access, p_cdda,
695                    config_GetPsz( p_access, MODULE_STRING "-author-format" ),
696                    psz_mrl, i_track );
697
698   playlist_ItemAddInfo( p_item ,  _("General"),_("Author"), p_author);
699
700 #ifdef HAVE_LIBCDDB
701   if (p_cdda->i_cddb_enabled) {
702     const char *psz_general_cat = _("General");
703
704     playlist_ItemAddInfo( p_item, psz_general_cat, _("Album"),
705                       "%s", p_cdda->cddb.disc->title);
706     playlist_ItemAddInfo( p_item, psz_general_cat, _("Disc Artist(s)"),
707                       "%s", p_cdda->cddb.disc->artist);
708     playlist_ItemAddInfo( p_item, psz_general_cat,
709                         _("CDDB Disc Category"),
710                       "%s", CDDB_CATEGORY[p_cdda->cddb.disc->category]);
711     playlist_ItemAddInfo( p_item, psz_general_cat, _("Genre"),
712                       "%s", p_cdda->cddb.disc->genre);
713     if ( p_cdda->cddb.disc->discid ) {
714       playlist_ItemAddInfo( p_item, psz_general_cat, _("CDDB Disc ID"),
715                         "%x", p_cdda->cddb.disc->discid );
716     }
717     if (p_cdda->cddb.disc->year != 0) {
718       playlist_ItemAddInfo( p_item, psz_general_cat,
719                         _("Year"), "%5d", p_cdda->cddb.disc->year );
720     }
721
722     if (p_cdda->i_cddb_enabled) {
723       cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
724                                           i_track-1);
725       if (t != NULL && t->artist != NULL) {
726         playlist_ItemAddInfo( p_item, psz_general_cat,
727                           _("Track Artist"), "%s", t->artist );
728         playlist_ItemAddInfo( p_item , psz_general_cat,
729                           _("Track Title"), "%s",  t->title );
730       }
731     }
732
733   }
734 #endif /*HAVE_LIBCDDB*/
735
736   vlc_mutex_unlock( &p_item->input.lock );
737 }
738
739 static int
740 CDDAFixupPlaylist( access_t *p_access, cdda_data_t *p_cdda, 
741                    const char *psz_source, vlc_bool_t b_single_track )
742 {
743   int i;
744   playlist_t * p_playlist;
745   char       * psz_mrl;
746   unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
747     strlen("@T") + strlen("100") + 1;
748
749 #ifdef HAVE_LIBCDDB
750   p_cdda->i_cddb_enabled =
751     config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
752   if( b_single_track && !p_cdda->i_cddb_enabled ) return 0;
753 #else
754   if( b_single_track ) return VLC_SUCCESS;
755 #endif
756
757   psz_mrl = malloc( psz_mrl_max );
758
759   if( psz_mrl == NULL )
760     {
761       msg_Warn( p_access, "out of memory" );
762       return VLC_ENOMEM;
763     }
764
765   p_playlist = (playlist_t *) vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
766                                                FIND_ANYWHERE );
767   if( !p_playlist )
768     {
769       msg_Warn( p_access, "can't find playlist" );
770       free(psz_mrl);
771       return VLC_EGENERIC;
772     }
773
774   CDDAMetaInfo(p_access);
775
776   if (b_single_track) {
777     /* May fill out more information when the playlist user interface becomes
778        more mature.
779      */
780     CDDACreatePlaylistItem(p_access, p_cdda, p_playlist, p_cdda->i_track,
781                            psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
782                            p_playlist->i_index);
783   } else {
784
785     for( i = 1 ; i <= p_cdda->i_tracks ; i++ )
786       {
787         input_title_t *t = p_cdda->p_title[i-1] = vlc_input_title_New();
788         
789         asprintf( &t->psz_name, _("Track %i"), i );
790         t->i_size = ( p_cdda->p_lsns[i] - p_cdda->p_lsns[i-1] ) *
791           (int64_t)CDIO_CD_FRAMESIZE_RAW;
792         
793         t->i_length = I64C(1000000) * t->i_size / CDDA_FREQUENCY_SAMPLE / 4;
794         CDDACreatePlaylistItem(p_access, p_cdda, p_playlist, i, psz_mrl,
795                                psz_mrl_max, psz_source, PLAYLIST_APPEND,
796                                PLAYLIST_END);
797       }
798   }
799
800   return VLC_SUCCESS;
801 }
802
803 /****************************************************************************
804  * Public functions
805  ****************************************************************************/
806
807 /*****************************************************************************
808  * Open: open cdda device or image file and initialize structures 
809  * for subsequent operations.
810  *****************************************************************************/
811 int
812 E_(CDDAOpen)( vlc_object_t *p_this )
813 {
814     access_t    *p_access = (access_t*)p_this;
815     char *      psz_source = NULL;
816     cdda_data_t *p_cdda;
817     CdIo        *p_cdio;
818     track_t     i_track = 1;
819     vlc_bool_t  b_single_track = false;
820     int         i_rc = VLC_EGENERIC;
821
822     /* Set where to log errors messages from libcdio. */
823     p_cdda_input = p_access;
824
825     /* parse the options passed in command line : */
826
827     if( p_access->psz_path && *p_access->psz_path )
828     {
829       char *psz_parser = psz_source = strdup( p_access->psz_path );
830
831       while( *psz_parser && *psz_parser != '@' )
832         {
833           psz_parser++;
834         }
835       
836       if( *psz_parser == '@' )
837         {
838           /* Found options */
839           *psz_parser = '\0';
840           ++psz_parser;
841           
842           if ('T' == *psz_parser || 't' == *psz_parser )
843             ++psz_parser;
844           
845           i_track = (int)strtol( psz_parser, NULL, 10 );
846           i_track = i_track ? i_track : 1;
847           b_single_track = true;
848         }
849     } 
850
851     if (!psz_source || !*psz_source)
852       {
853         /* No device/track given. Continue only when this plugin was 
854            selected */
855         if( !p_this->b_force ) return VLC_EGENERIC;
856
857         psz_source = var_CreateGetString( p_this, "cd-audio" );
858
859         if( !psz_source || !*psz_source ) {
860         /* Scan for a CD-ROM drive with a CD-DA in it. */
861         char **cd_drives =
862           cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
863
864         if (NULL == cd_drives || NULL == cd_drives[0] ) {
865           msg_Err( p_access, 
866                    _("libcdio couldn't find something with a CD-DA in it") );
867           if (cd_drives) cdio_free_device_list(cd_drives);
868           return VLC_EGENERIC;
869         }
870         
871         psz_source = strdup(cd_drives[0]);
872         cdio_free_device_list(cd_drives);
873       }
874     }
875
876     cdio_log_set_handler ( cdio_log_handler );
877
878     /* Open CDDA */
879     if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
880     {
881         msg_Warn( p_access, "could not open %s", psz_source );
882         goto error2;
883     }
884
885     p_cdda = malloc( sizeof(cdda_data_t) );
886     if( p_cdda == NULL )
887     {
888         msg_Err( p_access, "out of memory" );
889         free( psz_source );
890         return VLC_ENOMEM;
891     }
892     memset( p_cdda, 0, sizeof(cdda_data_t) );
893
894 #ifdef HAVE_LIBCDDB
895     cddb_log_set_handler ( cddb_log_handler );
896     p_cdda->cddb.disc = NULL;
897     p_cdda->i_cddb_enabled =
898       config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
899 #endif
900
901     p_cdda->b_header = VLC_FALSE;
902     p_cdda->p_cdio   = p_cdio;
903     p_cdda->i_track  = i_track;
904     p_cdda->i_debug  = config_GetInt(p_this, MODULE_STRING "-debug");
905     p_cdda->i_blocks_per_read
906                      = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
907
908     if (0 == p_cdda->i_blocks_per_read)
909       p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
910     
911     if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ 
912          || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ ) {
913       msg_Warn( p_cdda_input, 
914                 "Number of blocks (%d) has to be between %d and %d. "
915                 "Using %d.", 
916                 p_cdda->i_blocks_per_read, 
917                 MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
918                 DEFAULT_BLOCKS_PER_READ );
919       p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
920     }
921
922
923     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
924
925     /* Set up p_access */
926     p_access->pf_read    = NULL;
927     p_access->pf_block   = CDDAReadBlocks;
928     p_access->pf_control = CDDAControl;
929     p_access->pf_seek    = CDDASeek;
930
931     p_access->info.i_update    = 0;
932     p_access->info.i_size      = 0;
933     p_access->info.i_pos       = 0;
934     p_access->info.b_eof       = VLC_FALSE;
935     p_access->info.i_title     = 0;
936     p_access->info.i_seekpoint = 0;
937
938     p_access->p_sys     = (access_sys_t *) p_cdda;
939
940     /* We read the Table Of Content information */
941     i_rc = GetCDInfo( p_access, p_cdda );
942     if ( VLC_SUCCESS != i_rc ) goto error;
943
944     CDDAFixupPlaylist( p_access, p_cdda, psz_source, b_single_track );
945     
946     /* Build a WAV header to put in front of the output data. 
947        This gets sent back in the Block (read) routine.
948      */
949     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
950     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
951     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
952     p_cdda->waveheader.MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
953     p_cdda->waveheader.Length = 0;                     /* we just don't know */
954     p_cdda->waveheader.ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
955     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
956     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
957     SetWLE( &p_cdda->waveheader.Modus, 2);
958     SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
959     SetWLE( &p_cdda->waveheader.BytesPerSample,
960             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
961     SetDWLE( &p_cdda->waveheader.BytesPerSec,
962              2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
963     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
964     p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
965
966     /* PTS delay */
967     var_Create( p_access, MODULE_STRING "-caching", 
968                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
969     return VLC_SUCCESS;
970
971  error:
972     cdio_destroy( p_cdda->p_cdio );
973     free( p_cdda );
974  error2:
975     free( psz_source );
976     return i_rc;
977
978 }
979
980 /*****************************************************************************
981  * CDDAClose: closes cdda and frees any resources associded with it.
982  *****************************************************************************/
983 void
984 E_(CDDAClose)( vlc_object_t *p_this )
985 {
986     access_t    *p_access = (access_t *) p_this;
987     cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
988     track_t      i;
989
990     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
991
992     /* Remove playlist titles */
993     for( i = 0; i < p_cdda->i_tracks; i++ )
994     {
995         vlc_input_title_Delete( p_cdda->p_title[i] );
996     }
997
998     cdio_destroy( p_cdda->p_cdio );
999
1000     cdio_log_set_handler (uninit_log_handler);
1001
1002 #ifdef HAVE_LIBCDDB
1003     cddb_log_set_handler (uninit_log_handler);
1004     if (p_cdda->i_cddb_enabled)
1005       cddb_disc_destroy(p_cdda->cddb.disc);
1006 #endif
1007
1008     free( p_cdda->p_lsns );
1009     if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
1010     free( p_cdda );
1011     p_cdda_input = NULL;
1012 }
1013
1014 /*****************************************************************************
1015  * Control: The front-end or vlc engine calls here to ether get
1016  * information such as meta information or plugin capabilities or to
1017  * issue miscellaneous "set" requests.
1018  *****************************************************************************/
1019 static int CDDAControl( access_t *p_access, int i_query, va_list args )
1020 {
1021     cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
1022     int          *pi_int;
1023     int i;
1024
1025     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
1026                "query %d", i_query );
1027
1028     switch( i_query )
1029     {
1030         /* Pass back a copy of meta information that was gathered when we
1031            during the Open/Initialize call.
1032          */
1033         case ACCESS_GET_META:
1034           { 
1035             vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1036             if ( p_cdda->p_meta ) {
1037               *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
1038               dbg_print( INPUT_DBG_META, "%s", _("Meta copied") );
1039             } else 
1040               msg_Warn( p_access, _("Tried to copy NULL meta info") );
1041             
1042             return VLC_SUCCESS;
1043           }
1044           return VLC_EGENERIC;
1045
1046         case ACCESS_CAN_SEEK:
1047         case ACCESS_CAN_FASTSEEK:
1048         case ACCESS_CAN_PAUSE:
1049         case ACCESS_CAN_CONTROL_PACE: 
1050           {
1051             vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1052             *pb_bool = VLC_TRUE;
1053             break;
1054           }
1055
1056         /* */
1057         case ACCESS_GET_MTU:
1058             pi_int = (int*)va_arg( args, int * );
1059             *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
1060             break;
1061
1062         case ACCESS_GET_PTS_DELAY:
1063           { 
1064             int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
1065             *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
1066               * MILLISECONDS_PER_SEC;
1067             break;
1068           }
1069
1070         /* */
1071         case ACCESS_SET_PAUSE_STATE:
1072             break;
1073
1074         case ACCESS_GET_TITLE_INFO:
1075           { input_title_t ***ppp_title;
1076             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1077             pi_int    = (int*)va_arg( args, int* );
1078             *((int*)va_arg( args, int* )) = 1; /* Title offset */
1079
1080             /* Duplicate track info */
1081             *pi_int = p_cdda->i_tracks;
1082             *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_tracks );
1083
1084             if (!*ppp_title) return VLC_ENOMEM;
1085
1086             for( i = 0; i < p_cdda->i_tracks; i++ )
1087             {
1088               if ( p_cdda->p_title[i] )
1089                 (*ppp_title)[i] = 
1090                   vlc_input_title_Duplicate( p_cdda->p_title[i] );
1091             }
1092           }
1093           break;
1094
1095         case ACCESS_SET_TITLE:
1096             i = (int)va_arg( args, int );
1097             if( i != p_access->info.i_title )
1098             {
1099                 /* Update info */
1100                 p_access->info.i_update |=
1101                     INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
1102                 p_access->info.i_title = i;
1103                 p_access->info.i_size = p_cdda->p_title[i]->i_size;
1104                 p_access->info.i_pos = 0;
1105
1106                 /* Next sector to read */
1107                 p_cdda->i_lsn = p_cdda->p_lsns[i];
1108             }
1109             break;
1110
1111         case ACCESS_SET_SEEKPOINT:
1112         case ACCESS_SET_PRIVATE_ID_STATE:
1113             return VLC_EGENERIC;
1114         default:
1115           msg_Warn( p_access, _("unimplemented query in control") );
1116             return VLC_EGENERIC;
1117
1118     }
1119     return VLC_SUCCESS;
1120 }
1121
1122 /*****************************************************************************
1123   GetCDInfo: 
1124
1125  Initialize information pertaining to the CD: the number of tracks,
1126  first track number, LSNs for each track and the leadout. The leadout
1127  information is stored after the last track. The LSN array is
1128  0-origin, same as p_access->info.  Add first_track to get what track
1129  number this is on the CD. Note: libcdio uses the real track number.
1130
1131  On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
1132
1133  We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
1134  *****************************************************************************/
1135 static int
1136 GetCDInfo( access_t *p_access, cdda_data_t *p_cdda ) 
1137 {
1138     track_t i;
1139     discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
1140
1141     p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
1142     p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
1143
1144     discmode = cdio_get_discmode(p_cdda->p_cdio);
1145     switch(discmode) {
1146     case CDIO_DISC_MODE_CD_DA:
1147     case CDIO_DISC_MODE_CD_MIXED:
1148       /* These are possible for CD-DA */
1149       break;
1150     default:
1151       /* These are not possible for CD-DA */
1152       msg_Err( p_access, 
1153                _("Disc seems not to be CD-DA. libcdio reports it is %s"),
1154                discmode2str[discmode]
1155                );
1156       return VLC_EGENERIC;
1157     }
1158     
1159     p_cdda->p_lsns = malloc( (p_cdda->i_tracks + 1) * sizeof(lsn_t) );
1160
1161     if( p_cdda->p_lsns == NULL )
1162       {
1163         msg_Err( p_access, "out of memory" );
1164         return VLC_ENOMEM;
1165       }
1166
1167
1168     /* Fill the p_lsns structure with the track/sector matches.
1169        Note cdio_get_track_lsn when given num_tracks + 1 will return
1170        the leadout LSN.
1171      */
1172     for( i = 0 ; i <= p_cdda->i_tracks ; i++ )
1173       {
1174         (p_cdda->p_lsns)[ i ] = 
1175           cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
1176       }
1177
1178     /* Set reading start LSN. */
1179     p_cdda->i_lsn = p_cdda->p_lsns[p_cdda->i_track - p_cdda->i_first_track];
1180
1181     return VLC_SUCCESS;
1182 }