]> git.sesse.net Git - vlc/blob - modules/access/cdda/access.c
Better operator association in computing duration and should be
[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 VideoLAN
5  * $Id: access.c,v 1.22 2004/01/07 07:21:31 rocky Exp $
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 <stdio.h>
30 #include <stdlib.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/intf.h>
34
35 #include <sys/types.h>
36 #include <cdio/cdio.h>
37 #include <cdio/cd_types.h>
38
39 #include "codecs.h"
40 #include "vlc_keys.h"
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #ifdef HAVE_ERRNO_H
47 #   include <errno.h>
48 #endif
49
50 #include <string.h>
51
52 #include "cdda.h"
53
54 /* how many blocks Open will read in each loop */
55 #define CDDA_BLOCKS_ONCE 1
56 #define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
57
58 #define CDDA_MRL_PREFIX "cddax://"
59
60 /* FIXME: This variable is a hack. Would be nice to eliminate. */
61 static input_thread_t *p_cdda_input = NULL;
62
63 /*****************************************************************************
64  * Local prototypes
65  *****************************************************************************/
66 static int  CDDARead         ( input_thread_t *, byte_t *, size_t );
67 static void CDDASeek         ( input_thread_t *, off_t );
68 static int  CDDASetArea      ( input_thread_t *, input_area_t * );
69 static int  CDDASetProgram   ( input_thread_t *, pgrm_descriptor_t * );
70
71 static int  CDDAFixupPlayList( input_thread_t *p_input,
72                               cdda_data_t *p_cdda, const char *psz_source,
73                               bool play_single_track);
74
75 /****************************************************************************
76  * Private functions
77  ****************************************************************************/
78
79 /* process messages that originate from libcdio. */
80 static void
81 cdio_log_handler (cdio_log_level_t level, const char message[])
82 {
83   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
84   switch (level) {
85   case CDIO_LOG_DEBUG:
86   case CDIO_LOG_INFO:
87     if (p_cdda->i_debug & INPUT_DBG_CDIO)
88       msg_Dbg( p_cdda_input, message);
89     break;
90   case CDIO_LOG_WARN:
91     msg_Warn( p_cdda_input, message);
92     break;
93   case CDIO_LOG_ERROR:
94   case CDIO_LOG_ASSERT:
95     msg_Err( p_cdda_input, message);
96     break;
97   default:
98     msg_Warn( p_cdda_input, message,
99             _("The above message had unknown vcdimager log level"),
100             level);
101   }
102   return;
103 }
104
105
106 #ifdef HAVE_LIBCDDB
107 /*! This routine is called by libcddb routines on error.
108    Setup is done by init_input_plugin.
109 */
110 static void
111 cddb_log_handler (cddb_log_level_t level, const char message[])
112 {
113   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
114   switch (level) {
115   case CDDB_LOG_DEBUG:
116   case CDDB_LOG_INFO:
117     if (!(p_cdda->i_debug & INPUT_DBG_CDDB)) return;
118     /* Fall through if to warn case */
119   default:
120     cdio_log_handler (level, message);
121   }
122 }
123 #endif /*HAVE_LIBCDDB*/
124
125
126 /*! This routine is when xine is not fully set up (before full initialization)
127    or is not around (before finalization).
128 */
129 static void
130 uninit_log_handler (cdio_log_level_t level, const char message[])
131 {
132   cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
133   switch (level) {
134   case CDIO_LOG_DEBUG:
135   case CDIO_LOG_INFO:
136     if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
137       return;
138     /* Fall through if to warn case */
139   case CDIO_LOG_WARN:
140     fprintf(stderr, "WARN: %s\n", message);
141     break;
142   case CDIO_LOG_ERROR:
143     fprintf(stderr, "ERROR: %s\n", message);
144     break;
145   case CDIO_LOG_ASSERT:
146     fprintf(stderr, "ASSERT ERROR: %s\n", message);
147     break;
148   default:
149     fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
150             message,
151             _("The above message had unknown cdio log level"),
152             level);
153   }
154
155   /* gl_default_cdio_log_handler (level, message); */
156 }
157
158 /*****************************************************************************
159  * CDDAPlay: Arrange things so we play the specified track.
160  * VLC_TRUE is returned if there was no error.
161  *****************************************************************************/
162 vlc_bool_t
163 CDDAPlay( input_thread_t *p_input, int i_track )
164 {
165   cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
166
167   if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
168     return VLC_FALSE;
169
170   CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
171   return VLC_TRUE;
172 }
173
174 /*****************************************************************************
175  * CDDARead: reads from the CDDA into PES packets.
176  *****************************************************************************
177  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
178  * bytes.
179  *****************************************************************************/
180 static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
181                      size_t i_len )
182 {
183     cdda_data_t *           p_cdda;
184     int                     i_blocks;
185     int                     i_index;
186     int                     i_read;
187
188     p_cdda = (cdda_data_t *)p_input->p_access_data;
189
190     i_read = 0;
191
192     /* Compute the number of blocks we have to read */
193
194     i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
195
196     for ( i_index = 0; i_index < i_blocks; i_index++ )
197     {
198
199       if (cdio_read_audio_sector(p_cdda->p_cddev->cdio, p_buffer,
200                                  p_cdda->i_sector) != 0)
201         {
202           msg_Err( p_input, "could not read sector %d", p_cdda->i_sector );
203           return -1;
204         }
205
206         p_cdda->i_sector ++;
207         if ( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
208         {
209             input_area_t *p_area;
210
211             dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
212                        "end of track, cur: %u", p_cdda->i_sector );
213
214             /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
215                 return 0; /* EOF */
216
217             vlc_mutex_lock( &p_input->stream.stream_lock );
218             p_area = p_input->stream.pp_areas[
219                     p_input->stream.p_selected_area->i_id + 1 ];
220
221             p_area->i_part = 1;
222             CDDASetArea( p_input, p_area );
223             vlc_mutex_unlock( &p_input->stream.stream_lock );
224         }
225         i_read += CDIO_CD_FRAMESIZE_RAW;
226     }
227
228     if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
229     {
230         msg_Err( p_input, "must read full sectors" );
231     }
232
233     return i_read;
234 }
235
236 /*****************************************************************************
237  * CDDASetProgram: Does nothing since a CDDA is mono_program
238  *****************************************************************************/
239 static int CDDASetProgram( input_thread_t * p_input,
240                            pgrm_descriptor_t * p_program)
241 {
242     cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
243     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
244     return 0;
245 }
246
247 /*****************************************************************************
248  * CDDASetArea: initialize input data for title x.
249  * It should be called for each user navigation request.
250  ****************************************************************************/
251 static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
252 {
253     cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
254     vlc_value_t val;
255
256     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
257
258     /* we can't use the interface slider until initilization is complete */
259     p_input->stream.b_seekable = 0;
260
261     if( p_area != p_input->stream.p_selected_area )
262     {
263         /* Change the default area */
264         p_input->stream.p_selected_area = p_area;
265
266         /* Change the current track */
267         p_cdda->i_track = p_area->i_id - 1;
268         p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
269
270         /* Update the navigation variables without triggering a callback */
271         val.i_int = p_area->i_id;
272         var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
273     }
274
275     p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
276
277     p_input->stream.p_selected_area->i_tell =
278         (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
279          - p_input->stream.p_selected_area->i_start;
280
281     /* warn interface that something has changed */
282     p_input->stream.b_seekable = 1;
283     p_input->stream.b_changed = 1;
284
285     return 0;
286 }
287
288 /****************************************************************************
289  * CDDASeek
290  ****************************************************************************/
291 static void CDDASeek( input_thread_t * p_input, off_t i_off )
292 {
293     cdda_data_t * p_cdda;
294
295     p_cdda = (cdda_data_t *) p_input->p_access_data;
296
297     p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
298                        + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
299
300     vlc_mutex_lock( &p_input->stream.stream_lock );
301     p_input->stream.p_selected_area->i_tell =
302         (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
303          - p_input->stream.p_selected_area->i_start;
304
305     vlc_mutex_unlock( &p_input->stream.stream_lock );
306
307     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
308     "sector %ud, offset: %lld, i_tell: %lld",  p_cdda->i_sector, i_off,
309                p_input->stream.p_selected_area->i_tell );
310
311 }
312
313 #define meta_info_add_str(title, str)                          \
314   if ( str ) {                                                 \
315     dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str);  \
316     input_AddInfo( p_cat, _(title), "%s", str );               \
317     playlist_AddInfo( p_playlist, -1, p_cat->psz_name,         \
318                       _(title), "%s" , str );                  \
319   }
320
321
322 static void InformationCreate( input_thread_t *p_input  )
323 {
324   cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
325   playlist_t *p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
326                                             FIND_PARENT );
327   input_info_category_t *p_cat;
328
329   p_cat = input_InfoCategory( p_input, "General" );
330   
331
332 #ifdef HAVE_LIBCDDB
333   if (p_cdda->i_cddb_enabled) {
334
335     meta_info_add_str( "Title", p_cdda->cddb.disc->title );
336     meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
337     meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
338     meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
339     {
340       char year[5];
341       if (p_cdda->cddb.disc->year != 0) {
342         snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
343         meta_info_add_str( "Year", year );
344       }
345       if ( p_cdda->cddb.disc->discid ) {
346         input_AddInfo( p_cat, _("CDDB Disc ID"), "%x",
347                        p_cdda->cddb.disc->discid );
348       }
349
350       if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
351         input_AddInfo( p_cat, _("CDDB Disc Category"), "%s",
352                        CDDB_CATEGORY[p_cdda->cddb.disc->category] );
353       }
354
355     }
356   }
357
358 #endif /*HAVE_LIBCDDB*/
359 #define TITLE_MAX 30
360
361   {
362     track_t i_track = p_cdda->i_nb_tracks;
363     char psz_buffer[MSTRTIME_MAX_SIZE];
364     mtime_t i_duration =
365       (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
366       / CDIO_CD_FRAMES_PER_SEC;
367
368     dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
369     input_AddInfo( p_cat, _("Duration"), "%s",
370                    secstotimestr( psz_buffer, i_duration ) );
371
372     for( i_track = 0 ; i_track < p_cdda->i_nb_tracks ; i_track++ ) {
373       char track_str[TITLE_MAX];
374       mtime_t i_duration =
375         (p_cdda->p_sectors[i_track+1] - p_cdda->p_sectors[i_track])
376         / CDIO_CD_FRAMES_PER_SEC;
377       snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
378       p_cat = input_InfoCategory( p_input, track_str );
379       input_AddInfo( p_cat, _("Duration"), "%s",
380                      secstotimestr( psz_buffer, i_duration ) );
381
382 #ifdef HAVE_LIBCDDB
383       if (p_cdda->i_cddb_enabled) {
384         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
385                                             i_track);
386         if (t != NULL && t->artist != NULL) {
387           input_AddInfo( p_cat, _("Artist"), "%s", t->artist );
388           input_AddInfo( p_cat, _("Title"), "%s",  t->title );
389         }
390       }
391 #endif
392     }
393   }
394
395   if( p_playlist ) vlc_object_release( p_playlist );
396 }
397
398
399 #ifdef HAVE_LIBCDDB
400
401 #define free_and_dup(var, val) \
402   if (var) free(var);          \
403   if (val) var=strdup(val);
404
405
406 static void
407 GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
408 {
409
410   dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
411
412   if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
413     int i, i_matches;
414     cddb_conn_t  *conn = cddb_new();
415     const CdIo *cdio = p_cdda->p_cddev->cdio;
416
417
418     cddb_log_set_handler (uninit_log_handler);
419
420     if (!conn) {
421       msg_Warn( p_input, "unable to initialize libcddb" );
422       goto cddb_destroy;
423     }
424
425     cddb_set_email_address( conn,
426                             config_GetPsz( p_input,
427                                            MODULE_STRING "-cddb-email") );
428
429     cddb_set_server_name( conn,
430                           config_GetPsz( p_input,
431                                          MODULE_STRING "-cddb-server") );
432
433     cddb_set_server_port(conn,
434                           config_GetInt( p_input,
435                                          MODULE_STRING "-cddb-port") );
436
437     /* Set the location of the local CDDB cache directory.
438        The default location of this directory is */
439
440     if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" ))
441       cddb_cache_disable(conn);
442
443     cddb_cache_set_dir(conn,
444                        config_GetPsz( p_input,
445                                       MODULE_STRING "-cddb-cachedir") );
446
447     cddb_set_timeout(conn,
448                      config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
449
450
451     if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
452       cddb_http_enable(conn);
453     } else
454       cddb_http_disable(conn);
455
456     p_cdda->cddb.disc = cddb_disc_new();
457     if (!p_cdda->cddb.disc) {
458       msg_Err( p_input, "Unable to create CDDB disc structure." );
459       goto cddb_end;
460     }
461
462     for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
463       cddb_track_t *t = cddb_track_new();
464       t->frame_offset = cdio_get_track_lba(cdio, i);
465       cddb_disc_add_track(p_cdda->cddb.disc, t);
466     }
467
468     p_cdda->cddb.disc->length =
469       cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK)
470       / CDIO_CD_FRAMES_PER_SEC;
471
472
473     if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
474       msg_Err( p_input, "CDDB disc calc failed" );
475       goto cddb_destroy;
476     }
477
478     i_matches = cddb_query(conn, p_cdda->cddb.disc);
479     if (i_matches > 0) {
480       if (i_matches > 1)
481         msg_Warn( p_input, "Found %d matches in CDDB. Using first one.",
482                   i_matches);
483       cddb_read(conn, p_cdda->cddb.disc);
484
485       if (p_cdda->i_debug & INPUT_DBG_CDDB)
486         cddb_disc_print(p_cdda->cddb.disc);
487
488     } else {
489       msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
490     }
491
492   cddb_destroy:
493     cddb_destroy(conn);
494   }
495   cddb_end: ;
496 }
497 #endif /*HAVE_LIBCDDB*/
498
499 #define add_format_str_info(val)                         \
500   {                                                      \
501     const char *str = val;                               \
502     unsigned int len;                                    \
503     if (val != NULL) {                                   \
504       len=strlen(str);                                   \
505       if (len != 0) {                                    \
506         strncat(tp, str, TEMP_STR_LEN-(tp-temp_str));    \
507         tp += len;                                       \
508       }                                                  \
509       saw_control_prefix = false;                        \
510     }                                                    \
511   }
512
513 #define add_format_num_info(val, fmt)                    \
514   {                                                      \
515     char num_str[10];                                    \
516     unsigned int len;                                    \
517     sprintf(num_str, fmt, val);                          \
518     len=strlen(num_str);                                 \
519     if (len != 0) {                                      \
520       strncat(tp, num_str, TEMP_STR_LEN-(tp-temp_str));  \
521       tp += len;                                         \
522     }                                                    \
523     saw_control_prefix = false;                          \
524   }
525
526 /*!
527    Take a format string and expand escape sequences, that is sequences that
528    begin with %, with information from the current CD.
529    The expanded string is returned. Here is a list of escape sequences:
530
531    %a : The album artist **
532    %A : The album information **
533    %C : Category **
534    %I : CDDB disk ID **
535    %G : Genre **
536    %M : The current MRL
537    %m : The CD-DA Media Catalog Number (MCN)
538    %n : The number of tracks on the CD
539    %p : The artist/performer/composer in the track **
540    %T : The track number **
541    %s : Number of seconds in this track
542    %t : The name **
543    %Y : The year 19xx or 20xx **
544    %% : a %
545 */
546 static char *
547 CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
548               const char format_str[], const char *mrl, int i_track)
549 {
550 #define TEMP_STR_SIZE 256
551 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
552   static char    temp_str[TEMP_STR_SIZE];
553   size_t i;
554   char * tp = temp_str;
555   bool saw_control_prefix = false;
556   size_t format_len = strlen(format_str);
557
558   bzero(temp_str, TEMP_STR_SIZE);
559
560   for (i=0; i<format_len; i++) {
561
562     if (!saw_control_prefix && format_str[i] != '%') {
563       *tp++ = format_str[i];
564       saw_control_prefix = false;
565       continue;
566     }
567
568     switch(format_str[i]) {
569     case '%':
570       if (saw_control_prefix) {
571         *tp++ = '%';
572       }
573       saw_control_prefix = !saw_control_prefix;
574       break;
575 #ifdef HAVE_LIBCDDB
576     case 'a':
577       if (!p_cdda->i_cddb_enabled) goto not_special;
578       add_format_str_info(p_cdda->cddb.disc->artist);
579       break;
580     case 'A':
581       if (!p_cdda->i_cddb_enabled) goto not_special;
582       add_format_str_info(p_cdda->cddb.disc->title);
583       break;
584     case 'C':
585       if (!p_cdda->i_cddb_enabled) goto not_special;
586       add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
587       break;
588     case 'G':
589       if (!p_cdda->i_cddb_enabled) goto not_special;
590       add_format_str_info(p_cdda->cddb.disc->genre);
591       break;
592     case 'I':
593       if (!p_cdda->i_cddb_enabled) goto not_special;
594       add_format_num_info(p_cdda->cddb.disc->discid, "%x");
595       break;
596     case 'Y':
597       if (!p_cdda->i_cddb_enabled) goto not_special;
598       add_format_num_info(p_cdda->cddb.disc->year, "%5d");
599       break;
600     case 't':
601       if (p_cdda->i_cddb_enabled) {
602         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
603                                             i_track-1);
604         if (t != NULL && t->title != NULL)
605           add_format_str_info(t->title);
606       } else goto not_special;
607       break;
608     case 'p':
609       if (p_cdda->i_cddb_enabled) {
610         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
611                                             i_track-1);
612         if (t != NULL && t->artist != NULL)
613           add_format_str_info(t->artist);
614       } else goto not_special;
615       break;
616 #endif
617
618     case 'M':
619       add_format_str_info(mrl);
620       break;
621
622 #if FINISHED
623     case 'm':
624       add_format_str_info(p_cdda->mcn);
625       break;
626 #endif
627
628     case 'n':
629       add_format_num_info(p_cdda->i_nb_tracks, "%d");
630       break;
631
632 #ifdef HAVE_LIBCDDB
633     case 's':
634       if (p_cdda->i_cddb_enabled) {
635         char psz_buffer[MSTRTIME_MAX_SIZE];
636         mtime_t i_duration =
637           (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
638           / CDIO_CD_FRAMES_PER_SEC;
639         add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
640       } else goto not_special;
641       break;
642 #endif
643
644     case 'T':
645       add_format_num_info(i_track, "%02d");
646       break;
647 #ifdef HAVE_LIBCDDB
648     not_special:
649 #endif
650     default:
651       *tp++ = '%';
652       *tp++ = format_str[i];
653       saw_control_prefix = false;
654     }
655   }
656   return strdup(temp_str);
657 }
658
659 static void
660 CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
661                        playlist_t *p_playlist, unsigned int i_track,
662                        char *psz_mrl, int psz_mrl_max,
663                        const char *psz_source, int playlist_operation,
664                        int i_pos)
665 {
666   mtime_t i_duration =
667     (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
668     * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
669   char *p_author;
670   char *p_title;
671   char *config_varname = MODULE_STRING "-title-format";
672
673 #ifdef HAVE_LIBCDDB
674   if (p_cdda->i_cddb_enabled) {
675     config_varname = MODULE_STRING "-cddb-title-format";
676   }
677 #endif /*HAVE_LIBCDDB*/
678
679   snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
680            CDDA_MRL_PREFIX, psz_source, i_track);
681
682   p_title = CDDAFormatStr(p_input, p_cdda,
683                           config_GetPsz( p_input, config_varname ),
684                           psz_mrl, i_track);
685
686   dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
687              psz_mrl, p_title, (long int) i_duration / 1000000 , i_pos );
688   playlist_AddWDuration( p_playlist, psz_mrl, p_title, playlist_operation, 
689                          i_pos, i_duration );
690
691   if( i_pos == PLAYLIST_END ) i_pos = p_playlist->i_size - 1;
692
693   p_author =
694     CDDAFormatStr( p_input, p_cdda,
695                    config_GetPsz( p_input, MODULE_STRING "-author-format" ),
696                    psz_mrl, i_track );
697
698   playlist_AddInfo( p_playlist, i_pos, _("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_AddInfo( p_playlist, i_pos, psz_general_cat, _("Album"),
705                       "%s", p_cdda->cddb.disc->title);
706     playlist_AddInfo( p_playlist, i_pos, psz_general_cat, _("Disc Artist(s)"),
707                       "%s", p_cdda->cddb.disc->artist);
708     playlist_AddInfo( p_playlist, i_pos, psz_general_cat,
709                       _("CDDB Disc Category"),
710                       "%s", CDDB_CATEGORY[p_cdda->cddb.disc->category]);
711     playlist_AddInfo( p_playlist, i_pos, psz_general_cat, _("Genre"),
712                       "%s", p_cdda->cddb.disc->genre);
713     if ( p_cdda->cddb.disc->discid ) {
714       playlist_AddInfo( p_playlist, i_pos, psz_general_cat, _("CDDB Disc ID"),
715                         "%x", p_cdda->cddb.disc->discid );
716     }
717     if (p_cdda->cddb.disc->year != 0) {
718       playlist_AddInfo( p_playlist, i_pos, 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_AddInfo( p_playlist, i_pos, psz_general_cat,
727                           _("Track Artist"), "%s", t->artist );
728         playlist_AddInfo( p_playlist, i_pos, psz_general_cat,
729                           _("Track Title"), "%s",  t->title );
730       }
731     }
732
733   }
734 #endif /*HAVE_LIBCDDB*/
735
736 }
737
738 static int
739 CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
740                    const char *psz_source, bool play_single_track)
741 {
742   int i;
743   playlist_t * p_playlist;
744   char       * psz_mrl;
745   unsigned int psz_mrl_max = strlen(CDDA_MRL_PREFIX) + strlen(psz_source) +
746     strlen("@T") + strlen("100") + 1;
747
748 #ifdef HAVE_LIBCDDB
749   p_cdda->i_cddb_enabled =
750     config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
751   if( play_single_track && !p_cdda->i_cddb_enabled ) return 0;
752 #else
753   if( play_single_track ) return 0;
754 #endif
755
756
757   psz_mrl = malloc( psz_mrl_max );
758
759   if( psz_mrl == NULL )
760     {
761       msg_Warn( p_input, "out of memory" );
762       return -1;
763     }
764
765   p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
766                                                FIND_ANYWHERE );
767   if( !p_playlist )
768     {
769       msg_Warn( p_input, "can't find playlist" );
770       free(psz_mrl);
771       return -1;
772     }
773
774 #ifdef HAVE_LIBCDDB
775   if (p_cdda->i_cddb_enabled)
776     GetCDDBInfo(p_input, p_cdda);
777   else
778     p_cdda->cddb.disc = NULL;
779 #endif
780
781   InformationCreate(p_input);
782
783   if (play_single_track) {
784     /* May fill out more information when the playlist user interface becomes
785        more mature.
786      */
787     CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1,
788                            psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
789                            p_playlist->i_index);
790   } else {
791
792     playlist_Delete( p_playlist, p_playlist->i_index);
793
794     for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
795       {
796         CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl,
797                                psz_mrl_max, psz_source, PLAYLIST_APPEND,
798                                PLAYLIST_END);
799
800       }
801
802     playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
803
804   }
805
806   vlc_object_release( p_playlist );
807   free(psz_mrl);
808   return 0;
809 }
810
811 /****************************************************************************
812  * Public functions
813  ****************************************************************************/
814 int
815 E_(DebugCB)   ( vlc_object_t *p_this, const char *psz_name,
816                 vlc_value_t oldval, vlc_value_t val, void *p_data )
817 {
818   cdda_data_t *p_cdda;
819
820   if (NULL == p_cdda_input) return VLC_EGENERIC;
821
822   p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
823
824   if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
825     msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
826              p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
827   }
828   p_cdda->i_debug = val.i_int;
829   return VLC_SUCCESS;
830 }
831
832 int
833 E_(CDDBEnabledCB)   ( vlc_object_t *p_this, const char *psz_name,
834                       vlc_value_t oldval, vlc_value_t val, void *p_data )
835 {
836   cdda_data_t *p_cdda;
837
838   if (NULL == p_cdda_input) return VLC_EGENERIC;
839
840   p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
841
842 #ifdef HAVE_LIBCDDB
843   if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
844     msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d",
845              p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled,
846              val.i_int, val.i_int);
847   }
848   p_cdda->i_cddb_enabled = val.i_int;
849 #endif
850   return VLC_SUCCESS;
851 }
852
853 /*FIXME*/
854 #if PLAYLIST_INTERFACE_IS_FIXED
855 int
856 E_(TitleFormatCB)   ( vlc_object_t *p_this, const char *psz_name,
857                       vlc_value_t oldval, vlc_value_t val, void *p_data )
858 {
859   cdda_data_t *p_cdda;
860
861   if (NULL == p_cdda_input) return VLC_EGENERIC;
862
863   p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
864
865   if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
866     msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)",
867              oldval.psz_string, val.psz_string);
868   }
869   ????
870   return VLC_SUCCESS;
871 }
872 #endif
873
874 /*****************************************************************************
875  * Open: open cdda
876  *****************************************************************************/
877 int
878 E_(Open)( vlc_object_t *p_this )
879 {
880     input_thread_t *        p_input = (input_thread_t *)p_this;
881     char *                  psz_orig;
882     char *                  psz_parser;
883     char *                  psz_source;
884     cdda_data_t *           p_cdda;
885     int                     i;
886     int                     i_track = 1;
887     cddev_t                 *p_cddev;
888     bool                    play_single_track = false;
889
890     /* Set where to log errors messages from libcdio. */
891     p_cdda_input = (input_thread_t *)p_this;
892
893     /* parse the options passed in command line : */
894     psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
895
896     if( !psz_orig )
897     {
898         return( -1 );
899     }
900
901     while( *psz_parser && *psz_parser != '@' )
902     {
903         psz_parser++;
904     }
905
906     if( *psz_parser == '@' )
907     {
908         /* Found options */
909         *psz_parser = '\0';
910         ++psz_parser;
911
912         if ('T' == *psz_parser || 't' == *psz_parser )
913             ++psz_parser;
914
915         i_track = (int)strtol( psz_parser, NULL, 10 );
916         i_track = i_track ? i_track : 1;
917         play_single_track = true;
918     }
919
920     if( !*psz_source ) {
921       /* No source specified, so figure it out. */
922       if( !p_input->psz_access ) {
923         free( psz_orig );
924         return -1;
925       }
926       psz_source = config_GetPsz( p_input, "cd-audio" );
927
928       if( !psz_source || 0==strlen(psz_source) ) {
929         /* Scan for a CD-ROM drive with a CD-DA in it. */
930         char **cd_drives =
931           cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
932         if (NULL == cd_drives) return -1;
933         if (cd_drives[0] == NULL) {
934           cdio_free_device_list(cd_drives);
935           return -1;
936         }
937         psz_source = strdup(cd_drives[0]);
938         cdio_free_device_list(cd_drives);
939       }
940     }
941
942     /* Open CDDA */
943     cdio_log_set_handler ( cdio_log_handler );
944 #ifdef HAVE_LIBCDDB
945     cddb_log_set_handler ( cddb_log_handler );
946 #endif
947
948     if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
949     {
950         msg_Warn( p_input, "could not open %s", psz_source );
951         free( psz_source );
952         return -1;
953     }
954
955     p_cdda = malloc( sizeof(cdda_data_t) );
956     if( p_cdda == NULL )
957     {
958         msg_Err( p_input, "out of memory" );
959         free( psz_source );
960         return -1;
961     }
962
963     p_cdda->p_cddev        = p_cddev;
964     p_cdda->i_debug        = config_GetInt( p_this, MODULE_STRING "-debug" );
965     p_input->p_access_data = (void *)p_cdda;
966
967     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
968
969     p_input->i_mtu = CDDA_DATA_ONCE;
970
971     /* We read the Table Of Content information */
972     p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
973                               p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
974     if( p_cdda->i_nb_tracks < 0 )
975         msg_Err( p_input, "unable to count tracks" );
976     else if( p_cdda->i_nb_tracks <= 0 )
977         msg_Err( p_input, "no audio tracks found" );
978
979     if( p_cdda->i_nb_tracks <= 1)
980     {
981         ioctl_Close( p_cdda->p_cddev );
982         free( p_cdda );
983         free( psz_source );
984         return -1;
985     }
986
987     if( i_track >= p_cdda->i_nb_tracks || i_track < 1 )
988         i_track = 1;
989
990     /* Set stream and area data */
991     vlc_mutex_lock( &p_input->stream.stream_lock );
992
993     /* Initialize ES structures */
994     input_InitStream( p_input, 0 );
995
996     /* cdda input method */
997     p_input->stream.i_method = INPUT_METHOD_CDDA;
998
999     p_input->stream.b_pace_control = 1;
1000     p_input->stream.b_seekable = 1;
1001     p_input->stream.i_mux_rate = 44100 * 4 / 50;
1002
1003 #define area p_input->stream.pp_areas
1004     for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
1005     {
1006         input_AddArea( p_input, i, 1 );
1007
1008         /* Absolute start offset and size */
1009         area[i]->i_start =
1010             (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
1011         area[i]->i_size =
1012             (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
1013             * (off_t)CDIO_CD_FRAMESIZE_RAW;
1014     }
1015 #undef area
1016
1017     CDDAPlay( p_input, i_track);
1018
1019     CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
1020
1021     vlc_mutex_unlock( &p_input->stream.stream_lock );
1022
1023     if( !p_input->psz_demux || !*p_input->psz_demux )
1024     {
1025         p_input->psz_demux = "cdda";
1026     }
1027
1028     p_input->pf_read = CDDARead;
1029     p_input->pf_seek = CDDASeek;
1030     p_input->pf_set_area = CDDASetArea;
1031     p_input->pf_set_program = CDDASetProgram;
1032
1033     /* Update default_pts to a suitable value for cdda access */
1034     p_input->i_pts_delay = config_GetInt( p_input,
1035                                           MODULE_STRING "-caching" ) * 1000;
1036
1037     free( psz_source );
1038
1039     return 0;
1040 }
1041
1042 /*****************************************************************************
1043  * CDDAClose: closes cdda
1044  *****************************************************************************/
1045 void
1046 E_(Close)( vlc_object_t *p_this )
1047 {
1048     input_thread_t *   p_input = (input_thread_t *)p_this;
1049     cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
1050
1051     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
1052     ioctl_Close( p_cdda->p_cddev );
1053
1054     cdio_log_set_handler (uninit_log_handler);
1055
1056 #ifdef HAVE_LIBCDDB
1057     cddb_log_set_handler (uninit_log_handler);
1058     if (p_cdda->i_cddb_enabled)
1059       cddb_disc_destroy(p_cdda->cddb.disc);
1060 #endif
1061
1062     free( p_cdda );
1063     p_cdda_input = NULL;
1064 }