3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
4 * (C) 2002, MPlayer team.
6 * Implementation follow the freedb.howto1.06.txt specification
7 * from http://freedb.freedb.org
9 * discid computation by Jeremy D. Zawodny
10 * Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
11 * Code release under GPL
26 #define mkdir(a,b) mkdir(a)
34 #include <sys/ioctl.h>
36 #include <sys/types.h>
42 #if defined(__linux__)
43 #include <linux/cdrom.h>
44 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
47 #include <ddk/ntddcdrm.h>
50 #elif defined(__APPLE__) || defined(__DARWIN__)
51 #include <IOKit/storage/IOCDTypes.h>
52 #include <IOKit/storage/IOCDMediaBSDClient.h>
61 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
62 #define DEFAULT_CACHE_DIR "/.cddb/"
64 stream_t
* open_cdda(char *dev
, char *track
);
66 static cd_toc_t cdtoc
[100];
67 static int cdtoc_last_track
;
70 read_toc(const char *dev
) {
71 int first
= 0, last
= -1;
79 sprintf(device
, "\\\\.\\%s", dev
);
80 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
82 if(!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
, sizeof(CDROM_TOC
), &r
, 0)) {
83 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToReadTOC
);
87 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
88 for (i
= first
; i
<= last
; i
++) {
89 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
90 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
91 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
97 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
102 #if defined(__linux__) || defined(__bsdi__)
104 struct cdrom_tochdr tochdr
;
105 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
106 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
108 for (i
= first
; i
<= last
; i
++) {
109 struct cdrom_tocentry tocentry
;
110 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
111 tocentry
.cdte_format
= CDROM_MSF
;
112 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
113 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
114 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
115 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
117 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
119 struct ioc_toc_header tochdr
;
120 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
121 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
123 for (i
= first
; i
<= last
; i
++) {
124 struct ioc_read_toc_single_entry tocentry
;
125 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
126 tocentry
.address_format
= CD_MSF_FORMAT
;
127 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
128 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
129 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
130 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
132 #elif defined(__NetBSD__) || defined(__OpenBSD__)
134 struct ioc_toc_header tochdr
;
135 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
136 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
138 for (i
= first
; i
<= last
; i
++) {
139 struct ioc_read_toc_entry tocentry
;
140 struct cd_toc_entry toc_buffer
;
141 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
142 tocentry
.address_format
= CD_MSF_FORMAT
;
143 tocentry
.data
= &toc_buffer
;
144 tocentry
.data_len
= sizeof(toc_buffer
);
145 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
146 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
147 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
148 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
150 #elif defined(__APPLE__) || defined(__DARWIN__)
152 dk_cd_read_toc_t tochdr
;
154 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
155 memset(&tochdr
, 0, sizeof(tochdr
));
156 tochdr
.bufferLength
= sizeof(buf
);
157 tochdr
.buffer
= &buf
;
158 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
159 && tochdr
.bufferLength
== sizeof(buf
)) {
164 memset(&tochdr
, 0, sizeof(tochdr
));
165 tochdr
.bufferLength
= sizeof(buf2
);
166 tochdr
.buffer
= &buf2
;
167 tochdr
.format
= kCDTOCFormatTOC
;
168 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
169 || tochdr
.bufferLength
< sizeof(CDTOC
))
173 CDTOC
*cdToc
= (CDTOC
*)buf2
;
174 CDTrackInfo lastTrack
;
175 dk_cd_read_track_info_t trackInfoParams
;
176 for (i
= first
; i
< last
; ++i
) {
177 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
178 cdtoc
[i
].min
= msf
.minute
;
179 cdtoc
[i
].sec
= msf
.second
;
180 cdtoc
[i
].frame
= msf
.frame
;
182 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
183 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
184 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
185 trackInfoParams
.address
= last
;
186 trackInfoParams
.buffer
= &lastTrack
;
187 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
188 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
189 + be2me_32(lastTrack
.trackSize
));
190 cdtoc
[last
].min
= msf
.minute
;
191 cdtoc
[last
].sec
= msf
.second
;
192 cdtoc
[last
].frame
= msf
.frame
;
199 for (i
= first
; i
<= last
; i
++)
200 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
205 \brief Reads TOC from CD in the given device and prints the number of tracks
206 and the length of each track in minute:second:frame format.
207 \param *dev the device to analyse
208 \return if the command line -identify is given, returns the last track of
209 the TOC or -1 if the TOC can't be read,
210 otherwise just returns 0 and let cddb_resolve the TOC
212 int cdd_identify(const char *dev
)
214 cdtoc_last_track
= 0;
215 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
))
217 int i
, min
, sec
, frame
;
218 cdtoc_last_track
= read_toc(dev
);
219 if (cdtoc_last_track
< 0) {
220 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice
, dev
);
223 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
224 for (i
= 1; i
<= cdtoc_last_track
; i
++)
226 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
231 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
234 return cdtoc_last_track
;
250 cddb_discid(int tot_trks
) {
251 unsigned int i
, t
= 0, n
= 0;
254 while (i
< (unsigned int)tot_trks
) {
255 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
258 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
259 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
260 return ((n
% 0xff) << 24 | t
<< 8 | tot_trks
);
266 cddb_http_request(char *command
, int (*reply_parser
)(HTTP_header_t
*,cddb_data_t
*), cddb_data_t
*cddb_data
) {
270 HTTP_header_t
*http_hdr
;
272 if( reply_parser
==NULL
|| command
==NULL
|| cddb_data
==NULL
) return -1;
274 sprintf( request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d", cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
, cddb_data
->freedb_proto_level
);
275 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
277 url
= url_new(request
);
279 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NotAValidURL
);
283 fd
= http_send_request(url
,0);
285 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToSendHTTPRequest
);
289 http_hdr
= http_read_response( fd
);
290 if( http_hdr
==NULL
) {
291 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToReadHTTPResponse
);
295 http_debug_hdr(http_hdr
);
296 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
298 switch(http_hdr
->status_code
) {
300 ret
= reply_parser(http_hdr
, cddb_data
);
303 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_HTTPErrorNOTFOUND
);
306 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_HTTPErrorUnknown
);
309 http_free( http_hdr
);
316 cddb_read_cache(cddb_data_t
*cddb_data
) {
322 if( cddb_data
==NULL
|| cddb_data
->cache_dir
==NULL
) return -1;
324 sprintf( file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
326 file_fd
= open(file_name
, O_RDONLY
332 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NoCacheFound
);
336 ret
= fstat( file_fd
, &stats
);
341 file_size
= stats
.st_size
;
344 cddb_data
->xmcd_file
= malloc(file_size
);
345 if( cddb_data
->xmcd_file
==NULL
) {
346 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MemAllocFailed
);
350 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
351 if( cddb_data
->xmcd_file_size
!=file_size
) {
352 mp_msg(MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenRead
);
363 cddb_write_cache(cddb_data_t
*cddb_data
) {
364 // We have the file, save it for cache.
365 struct stat file_stat
;
370 if( cddb_data
==NULL
|| cddb_data
->cache_dir
==NULL
) return -1;
372 // Check if the CDDB cache dir exist
373 ret
= stat( cddb_data
->cache_dir
, &file_stat
);
375 // Directory not present, create it.
376 ret
= mkdir( cddb_data
->cache_dir
, 0755 );
378 if( ret
<0 && errno
!= EEXIST
) {
383 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToCreateDirectory
, cddb_data
->cache_dir
);
388 sprintf( file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
390 file_fd
= creat(file_name
, S_IREAD
|S_IWRITE
);
396 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
402 if( (unsigned int)wrote
!=cddb_data
->xmcd_file_size
) {
403 mp_msg(MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenWritten
);
414 cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
) {
415 unsigned long disc_id
;
417 char *ptr
=NULL
, *ptr2
=NULL
;
420 if( http_hdr
==NULL
|| cddb_data
==NULL
) return -1;
422 ret
= sscanf( http_hdr
->body
, "%d ", &status
);
424 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
430 ret
= sscanf( http_hdr
->body
, "%d %99s %08lx", &status
, category
, &disc_id
);
432 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
435 // Check if it's a xmcd database file
436 ptr
= strstr(http_hdr
->body
, "# xmcd");
438 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_InvalidXMCDDatabaseReturned
);
441 // Ok found the beginning of the file
443 ptr2
= strstr(ptr
, "\r\n.\r\n");
445 ptr2
= strstr(ptr
, "\n.\n");
447 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
448 ptr2
=ptr
+strlen(ptr
); //return -1;
453 if( http_hdr
->body_size
<(unsigned int)(ptr2
-ptr
) ) {
454 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_UnexpectedFIXME
);
457 cddb_data
->xmcd_file
= ptr
;
458 cddb_data
->xmcd_file_size
= ptr2
-ptr
+2;
459 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
460 // Avoid the http_free function to free the xmcd file...save a mempcy...
461 http_hdr
->body
= NULL
;
462 http_hdr
->body_size
= 0;
463 return cddb_write_cache(cddb_data
);
465 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
471 cddb_request_titles(cddb_data_t
*cddb_data
) {
473 sprintf( command
, "cddb+read+%s+%08lx", cddb_data
->category
, cddb_data
->disc_id
);
474 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
478 cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
) {
479 char album_title
[100];
483 ptr
= strstr(http_hdr
->body
, "\n");
485 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_UnableToFindEOL
);
489 // We have a list of exact/inexact matches, so which one do we use?
490 // So let's take the first one.
491 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
493 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
496 ptr
= strstr(http_hdr
->body
, album_title
);
500 ptr2
= strstr(ptr
, "\n");
502 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
506 strncpy(album_title
, ptr
, len
);
507 album_title
[len
-2]='\0';
509 mp_msg(MSGT_DEMUX
, MSGL_STATUS
, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle
, album_title
);
514 cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
) {
515 char album_title
[100];
519 ret
= sscanf( http_hdr
->body
, "%d ", &status
);
521 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
528 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
, cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
530 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
533 ptr
= strstr(http_hdr
->body
, album_title
);
537 ptr2
= strstr(ptr
, "\n");
539 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
543 strncpy(album_title
, ptr
, len
);
544 album_title
[len
-2]='\0';
546 mp_msg(MSGT_DEMUX
, MSGL_STATUS
, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle
, album_title
);
547 return cddb_request_titles(cddb_data
);
550 mp_msg(MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_CDDB_AlbumNotFound
);
553 // Found exact matches, list follows
554 cddb_parse_matches_list(http_hdr
, cddb_data
);
555 return cddb_request_titles(cddb_data
);
557 body=[210 Found exact matches, list follows (until terminating `.')
558 misc c711930d Santana / Supernatural
559 rock c711930d Santana / Supernatural
560 blues c711930d Santana / Supernatural
564 // Found inexact matches, list follows
565 cddb_parse_matches_list(http_hdr
, cddb_data
);
566 return cddb_request_titles(cddb_data
);
568 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_ServerReturnsCommandSyntaxErr
);
571 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
577 cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
) {
582 ret
= sscanf( http_hdr
->body
, "%d ", &status
);
584 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
590 ptr
= strstr(http_hdr
->body
, "max proto:");
592 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
595 ret
= sscanf(ptr
, "max proto: %d", &max
);
597 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
600 cddb_data
->freedb_proto_level
= max
;
603 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
609 cddb_get_proto_level(cddb_data_t
*cddb_data
) {
610 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
614 cddb_freedb_sites_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
) {
617 ret
= sscanf( http_hdr
->body
, "%d ", &status
);
619 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
625 // TODO: Parse the sites
626 ret
= cddb_data
->anonymous
; // For gcc complaining about unused parameter.
629 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_NoSitesInfoAvailable
);
632 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
638 cddb_get_freedb_sites(cddb_data_t
*cddb_data
) {
639 return cddb_http_request("sites", cddb_freedb_sites_parse
, cddb_data
);
643 cddb_create_hello(cddb_data_t
*cddb_data
) {
647 if( cddb_data
->anonymous
) { // Default is anonymous
648 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
649 * We don't send current user/host name in hello to prevent spam.
650 * Software that sends this is considered spyware
651 * that most people don't like.
653 user_name
= "anonymous";
654 strcpy(host_name
, "localhost");
656 if( gethostname(host_name
, 50)<0 ) {
657 strcpy(host_name
, "localhost");
659 user_name
= getenv("LOGNAME");
661 sprintf( cddb_data
->cddb_hello
, "&hello=%s+%s+%s+%s", user_name
, host_name
, "MPlayer", VERSION
);
665 cddb_retrieve(cddb_data_t
*cddb_data
) {
666 char offsets
[1024], command
[1024];
668 unsigned int i
, time_len
;
672 for( i
=0; i
<cddb_data
->tracks
; i
++ ) {
673 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
674 if (ptr
-offsets
> sizeof offsets
- 40) break;
677 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
679 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
680 cddb_data
->freedb_proto_level
= 1;
681 cddb_data
->xmcd_file
= NULL
;
683 cddb_create_hello(cddb_data
);
684 if( cddb_get_proto_level(cddb_data
)<0 ) {
685 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToGetProtocolLevel
);
689 //cddb_get_freedb_sites(&cddb_data);
691 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
, cddb_data
->tracks
, offsets
, time_len
);
692 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
693 if( ret
<0 ) return -1;
695 if( cddb_data
->cache_dir
!=NULL
) {
696 free(cddb_data
->cache_dir
);
702 cddb_resolve(const char *dev
, char **xmcd_file
) {
703 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
704 char *home_dir
= NULL
;
705 cddb_data_t cddb_data
;
707 if (cdtoc_last_track
<= 0)
709 cdtoc_last_track
= read_toc(dev
);
710 if (cdtoc_last_track
< 0) {
711 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice
, dev
);
715 cddb_data
.tracks
= cdtoc_last_track
;
716 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
717 cddb_data
.anonymous
= 1; // Don't send user info by default
719 // Check if there is a CD in the drive
720 // FIXME: That's not really a good way to check
721 if( cddb_data
.disc_id
==0 ) {
722 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NoCDInDrive
);
726 home_dir
= getenv("HOME");
728 if( home_dir
==NULL
) home_dir
= getenv("USERPROFILE");
729 if( home_dir
==NULL
) home_dir
= getenv("HOMEPATH");
730 // Last resort, store the cddb cache in the mplayer directory
731 if( home_dir
==NULL
) home_dir
= (char *)get_path("");
733 if( home_dir
==NULL
) {
734 cddb_data
.cache_dir
= NULL
;
736 cddb_data
.cache_dir
= malloc(strlen(home_dir
)+strlen(cddb_cache_dir
)+1);
737 if( cddb_data
.cache_dir
==NULL
) {
738 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MemAllocFailed
);
741 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
744 // Check for a cached file
745 if( cddb_read_cache(&cddb_data
)<0 ) {
747 if( cddb_retrieve(&cddb_data
)<0 ) {
752 if( cddb_data
.xmcd_file
!=NULL
) {
753 // printf("%s\n", cddb_data.xmcd_file );
754 *xmcd_file
= cddb_data
.xmcd_file
;
761 /*******************************************************************************************************************
765 *******************************************************************************************************************/
767 xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
) {
769 ptr
= strstr(line
, "DTITLE=");
772 album
= strstr(ptr
, "/");
773 if( album
==NULL
) return NULL
;
774 cd_info
->album
= malloc(strlen(album
+2)+1);
775 if( cd_info
->album
==NULL
) {
778 strcpy( cd_info
->album
, album
+2 );
781 cd_info
->artist
= malloc(strlen(ptr
)+1);
782 if( cd_info
->artist
==NULL
) {
785 strcpy( cd_info
->artist
, ptr
);
791 xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
) {
793 ptr
= strstr(line
, "DGENRE=");
796 cd_info
->genre
= malloc(strlen(ptr
)+1);
797 if( cd_info
->genre
==NULL
) {
800 strcpy( cd_info
->genre
, ptr
);
806 xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
) {
807 unsigned int track_nb
;
808 unsigned long sec
, off
;
810 ptr
= strstr(line
, "TTITLE");
813 // Here we point to the track number
814 track_nb
= atoi(ptr
);
815 ptr
= strstr(ptr
, "=");
816 if( ptr
==NULL
) return NULL
;
819 sec
= cdtoc
[track_nb
].frame
;
820 off
= cdtoc
[track_nb
+1].frame
-sec
+1;
822 cd_info_add_track( cd_info
, ptr
, track_nb
+1, (unsigned int)(off
/(60*75)), (unsigned int)((off
/75)%60), (unsigned int)(off
%75), sec
, off
);
828 cddb_parse_xmcd(char *xmcd_file
) {
829 cd_info_t
*cd_info
= NULL
;
832 unsigned int audiolen
;
833 if( xmcd_file
==NULL
) return NULL
;
835 cd_info
= cd_info_new();
836 if( cd_info
==NULL
) {
840 length
= strlen(xmcd_file
);
842 while( ptr
!=NULL
&& pos
<length
) {
845 while( ptr2
[0]!='\0' && ptr2
[0]!='\r' && ptr2
[0]!='\n' ) ptr2
++;
846 if( ptr2
[0]=='\0' ) {
852 // Search for the album title
853 if( xmcd_parse_dtitle(cd_info
, ptr
) );
854 // Search for the genre
855 else if( xmcd_parse_dgenre(cd_info
, ptr
) );
856 // Search for a track title
857 else if( xmcd_parse_ttitle(cd_info
, ptr
) ) audiolen
++; // <-- audiolen++ to shut up gcc warning
859 if( ptr2
[1]=='\n' ) ptr2
++;
864 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
865 cd_info
->min
= (unsigned int)(audiolen
/(60*75));
866 cd_info
->sec
= (unsigned int)((audiolen
/75)%60);
867 cd_info
->msec
= (unsigned int)(audiolen
%75);