4 * Copyright (C) 2002 Bertrand Baudet <bertrand_baudet@yahoo.com>
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>
12 * This file is part of MPlayer.
14 * MPlayer is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * MPlayer is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
39 #if defined(__MINGW32__) || defined(__CYGWIN__)
41 #define mkdir(a,b) mkdir(a)
49 #include <sys/ioctl.h>
51 #include <sys/types.h>
57 #if defined(__linux__)
58 #include <linux/cdrom.h>
59 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
61 #elif defined(__MINGW32__) || defined(__CYGWIN__)
62 #include <ddk/ntddcdrm.h>
63 #elif defined(__bsdi__)
65 #elif defined(__APPLE__) || defined(__DARWIN__)
66 #include <IOKit/storage/IOCDTypes.h>
67 #include <IOKit/storage/IOCDMediaBSDClient.h>
75 #include "libavutil/common.h"
77 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
78 #define DEFAULT_CACHE_DIR "/.cddb/"
80 stream_t
* open_cdda(char *dev
, char *track
);
82 static cd_toc_t cdtoc
[100];
83 static int cdtoc_last_track
;
85 int read_toc(const char *dev
)
87 int first
= 0, last
= -1;
89 #if defined(__MINGW32__) || defined(__CYGWIN__)
95 sprintf(device
, "\\\\.\\%s", dev
);
96 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
99 if (!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
,
100 sizeof(CDROM_TOC
), &r
, 0)) {
101 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
105 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
106 for (i
= first
; i
<= last
; i
++) {
107 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
108 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
109 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
115 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
120 #if defined(__linux__) || defined(__bsdi__)
122 struct cdrom_tochdr tochdr
;
123 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
124 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
126 for (i
= first
; i
<= last
; i
++) {
127 struct cdrom_tocentry tocentry
;
128 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
129 tocentry
.cdte_format
= CDROM_MSF
;
130 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
131 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
132 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
133 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
135 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
137 struct ioc_toc_header tochdr
;
138 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
139 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
141 for (i
= first
; i
<= last
; i
++) {
142 struct ioc_read_toc_single_entry tocentry
;
143 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
144 tocentry
.address_format
= CD_MSF_FORMAT
;
145 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
146 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
147 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
148 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
150 #elif defined(__NetBSD__) || defined(__OpenBSD__)
152 struct ioc_toc_header tochdr
;
153 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
154 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
156 for (i
= first
; i
<= last
; i
++) {
157 struct ioc_read_toc_entry tocentry
;
158 struct cd_toc_entry toc_buffer
;
159 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
160 tocentry
.address_format
= CD_MSF_FORMAT
;
161 tocentry
.data
= &toc_buffer
;
162 tocentry
.data_len
= sizeof(toc_buffer
);
163 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
164 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
165 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
166 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
168 #elif defined(__APPLE__) || defined(__DARWIN__)
170 dk_cd_read_toc_t tochdr
;
172 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
173 memset(&tochdr
, 0, sizeof(tochdr
));
174 tochdr
.bufferLength
= sizeof(buf
);
175 tochdr
.buffer
= &buf
;
176 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
177 && tochdr
.bufferLength
== sizeof(buf
)) {
182 memset(&tochdr
, 0, sizeof(tochdr
));
183 tochdr
.bufferLength
= sizeof(buf2
);
184 tochdr
.buffer
= &buf2
;
185 tochdr
.format
= kCDTOCFormatTOC
;
186 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
187 || tochdr
.bufferLength
< sizeof(CDTOC
))
191 CDTOC
*cdToc
= (CDTOC
*)buf2
;
192 CDTrackInfo lastTrack
;
193 dk_cd_read_track_info_t trackInfoParams
;
194 for (i
= first
; i
< last
; ++i
) {
195 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
196 cdtoc
[i
].min
= msf
.minute
;
197 cdtoc
[i
].sec
= msf
.second
;
198 cdtoc
[i
].frame
= msf
.frame
;
200 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
201 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
202 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
203 trackInfoParams
.address
= last
;
204 trackInfoParams
.buffer
= &lastTrack
;
205 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
206 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
207 + be2me_32(lastTrack
.trackSize
));
208 cdtoc
[last
].min
= msf
.minute
;
209 cdtoc
[last
].sec
= msf
.second
;
210 cdtoc
[last
].frame
= msf
.frame
;
217 for (i
= first
; i
<= last
; i
++)
218 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
223 \brief Reads TOC from CD in the given device and prints the number of tracks
224 and the length of each track in minute:second:frame format.
225 \param *dev the device to analyse
226 \return if the command line -identify is given, returns the last track of
227 the TOC or -1 if the TOC can't be read,
228 otherwise just returns 0 and let cddb_resolve the TOC
230 int cdd_identify(const char *dev
)
232 cdtoc_last_track
= 0;
233 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
234 int i
, min
, sec
, frame
;
235 cdtoc_last_track
= read_toc(dev
);
236 if (cdtoc_last_track
< 0) {
237 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
240 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
241 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
242 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
247 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
248 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
251 return cdtoc_last_track
;
254 unsigned int cddb_sum(int n
)
266 unsigned long cddb_discid(int tot_trks
)
268 unsigned int i
, t
= 0, n
= 0;
271 while (i
< (unsigned int)tot_trks
) {
272 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
275 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
276 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
277 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
282 int cddb_http_request(char *command
,
283 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
284 cddb_data_t
*cddb_data
)
289 HTTP_header_t
*http_hdr
;
291 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
294 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
295 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
296 cddb_data
->freedb_proto_level
);
297 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
299 url
= url_new(request
);
301 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "not a valid URL\n");
305 fd
= http_send_request(url
,0);
307 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to send the HTTP request.\n");
311 http_hdr
= http_read_response(fd
);
312 if (http_hdr
== NULL
) {
313 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to read the HTTP response.\n");
317 http_debug_hdr(http_hdr
);
318 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
320 switch (http_hdr
->status_code
) {
322 ret
= reply_parser(http_hdr
, cddb_data
);
325 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Not Found.\n");
328 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unknown error code\n");
337 int cddb_read_cache(cddb_data_t
*cddb_data
)
344 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
347 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
349 file_fd
= open(file_name
, O_RDONLY
350 #if defined(__MINGW32__) || defined(__CYGWIN__)
355 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No cache found.\n");
359 ret
= fstat(file_fd
, &stats
);
364 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
367 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
368 if (cddb_data
->xmcd_file
== NULL
) {
369 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
373 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
374 if (cddb_data
->xmcd_file_size
!= file_size
) {
375 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all the xmcd file has been read.\n");
379 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
386 int cddb_write_cache(cddb_data_t
*cddb_data
)
388 // We have the file, save it for cache.
389 struct stat file_stat
;
394 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
397 // Check if the CDDB cache dir exist
398 ret
= stat(cddb_data
->cache_dir
, &file_stat
);
400 // Directory not present, create it.
401 ret
= mkdir(cddb_data
->cache_dir
, 0755);
403 if (ret
< 0 && errno
!= EEXIST
) {
408 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to create directory %s.\n",
409 cddb_data
->cache_dir
);
414 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
416 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
422 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
428 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
429 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all of the xmcd file has been written.\n");
439 int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
441 unsigned long disc_id
;
443 char *ptr
= NULL
, *ptr2
= NULL
;
446 if (http_hdr
== NULL
|| cddb_data
== NULL
)
449 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
451 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
457 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
460 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
463 // Check if it's a xmcd database file
464 ptr
= strstr(http_hdr
->body
, "# xmcd");
466 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
467 "Invalid xmcd database file returned.\n");
471 // Ok found the beginning of the file
473 ptr2
= strstr(ptr
, "\n.\r\n");
475 ptr2
= strstr(ptr
, "\n.\n");
479 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
480 ptr2
= ptr
+ strlen(ptr
); //return -1;
484 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
485 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unexpected FIXME\n");
488 cddb_data
->xmcd_file
= ptr
;
489 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
490 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
491 return cddb_write_cache(cddb_data
);
493 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
498 int cddb_request_titles(cddb_data_t
*cddb_data
)
501 sprintf(command
, "cddb+read+%s+%08lx",
502 cddb_data
->category
, cddb_data
->disc_id
);
503 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
506 int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
508 char album_title
[100];
512 ptr
= strstr(http_hdr
->body
, "\n");
514 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Unable to find end of line.\n");
518 // We have a list of exact/inexact matches, so which one do we use?
519 // So let's take the first one.
520 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
521 &(cddb_data
->disc_id
), album_title
);
523 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
526 ptr
= strstr(http_hdr
->body
, album_title
);
530 ptr2
= strstr(ptr
, "\n");
532 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
536 len
= FFMIN(sizeof(album_title
) - 1, len
);
537 strncpy(album_title
, ptr
, len
);
538 album_title
[len
]='\0';
540 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
544 int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
546 char album_title
[100];
550 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
552 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
559 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
560 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
562 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
565 ptr
= strstr(http_hdr
->body
, album_title
);
569 ptr2
= strstr(ptr
, "\n");
571 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
575 len
= FFMIN(sizeof(album_title
) - 1, len
);
576 strncpy(album_title
, ptr
, len
);
577 album_title
[len
]='\0';
579 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
580 return cddb_request_titles(cddb_data
);
583 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Album not found.\n");
586 // Found exact matches, list follows
587 cddb_parse_matches_list(http_hdr
, cddb_data
);
588 return cddb_request_titles(cddb_data
);
590 body=[210 Found exact matches, list follows (until terminating `.')
591 misc c711930d Santana / Supernatural
592 rock c711930d Santana / Supernatural
593 blues c711930d Santana / Supernatural
597 // Found inexact matches, list follows
598 cddb_parse_matches_list(http_hdr
, cddb_data
);
599 return cddb_request_titles(cddb_data
);
601 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
,
602 "Server returns: Command syntax error\n");
605 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
610 int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
616 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
618 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
624 ptr
= strstr(http_hdr
->body
, "max proto:");
626 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
629 ret
= sscanf(ptr
, "max proto: %d", &max
);
631 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
634 cddb_data
->freedb_proto_level
= max
;
637 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
642 int cddb_get_proto_level(cddb_data_t
*cddb_data
)
644 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
647 int cddb_freedb_sites_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
651 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
653 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
659 // TODO: Parse the sites
660 ret
= cddb_data
->anonymous
; // For gcc complaining about unused parameter.
663 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "No sites information available.\n");
666 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
671 int cddb_get_freedb_sites(cddb_data_t
*cddb_data
)
673 return cddb_http_request("sites", cddb_freedb_sites_parse
, cddb_data
);
676 void cddb_create_hello(cddb_data_t
*cddb_data
)
681 if (cddb_data
->anonymous
) { // Default is anonymous
682 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
683 * We don't send current user/host name in hello to prevent spam.
684 * Software that sends this is considered spyware
685 * that most people don't like.
687 user_name
= "anonymous";
688 strcpy(host_name
, "localhost");
690 if (gethostname(host_name
, 50) < 0) {
691 strcpy(host_name
, "localhost");
693 user_name
= getenv("LOGNAME");
695 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s+%s",
696 user_name
, host_name
, "MPlayer", VERSION
);
699 int cddb_retrieve(cddb_data_t
*cddb_data
)
701 char offsets
[1024], command
[1024];
703 unsigned int i
, time_len
;
707 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
708 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
709 if (ptr
-offsets
> sizeof offsets
- 40) break;
712 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
714 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
715 cddb_data
->freedb_proto_level
= 1;
716 cddb_data
->xmcd_file
= NULL
;
718 cddb_create_hello(cddb_data
);
719 if (cddb_get_proto_level(cddb_data
) < 0) {
720 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to get the protocol level.\n");
724 //cddb_get_freedb_sites(&cddb_data);
726 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
727 cddb_data
->tracks
, offsets
, time_len
);
728 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
732 if (cddb_data
->cache_dir
!= NULL
) {
733 free(cddb_data
->cache_dir
);
738 int cddb_resolve(const char *dev
, char **xmcd_file
)
740 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
741 char *home_dir
= NULL
;
742 cddb_data_t cddb_data
;
744 if (cdtoc_last_track
<= 0) {
745 cdtoc_last_track
= read_toc(dev
);
746 if (cdtoc_last_track
< 0) {
747 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
751 cddb_data
.tracks
= cdtoc_last_track
;
752 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
753 cddb_data
.anonymous
= 1; // Don't send user info by default
755 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
758 // Check if there is a CD in the drive
759 // FIXME: That's not really a good way to check
760 if (cddb_data
.disc_id
== 0) {
761 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No CD in the drive.\n");
765 home_dir
= getenv("HOME");
767 if (home_dir
== NULL
)
768 home_dir
= getenv("USERPROFILE");
769 if (home_dir
== NULL
)
770 home_dir
= getenv("HOMEPATH");
771 // Last resort, store the cddb cache in the mplayer directory
772 if (home_dir
== NULL
)
773 home_dir
= (char *)get_path("");
775 if (home_dir
== NULL
) {
776 cddb_data
.cache_dir
= NULL
;
778 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
779 + strlen(cddb_cache_dir
) + 1);
780 if (cddb_data
.cache_dir
== NULL
) {
781 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
784 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
787 // Check for a cached file
788 if (cddb_read_cache(&cddb_data
) < 0) {
790 if (cddb_retrieve(&cddb_data
) < 0) {
795 if (cddb_data
.xmcd_file
!= NULL
) {
796 // printf("%s\n", cddb_data.xmcd_file);
797 *xmcd_file
= cddb_data
.xmcd_file
;
807 char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
810 ptr
= strstr(line
, "DTITLE=");
813 album
= strstr(ptr
, "/");
816 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
817 if (cd_info
->album
== NULL
) {
820 strcpy(cd_info
->album
, album
+ 2);
823 cd_info
->artist
= malloc(strlen(ptr
) + 1);
824 if (cd_info
->artist
== NULL
) {
827 strcpy(cd_info
->artist
, ptr
);
832 char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
835 ptr
= strstr(line
, "DGENRE=");
838 cd_info
->genre
= malloc(strlen(ptr
)+1);
839 if (cd_info
->genre
== NULL
) {
842 strcpy(cd_info
->genre
, ptr
);
847 char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
849 unsigned int track_nb
;
850 unsigned long sec
, off
;
852 ptr
= strstr(line
, "TTITLE");
855 // Here we point to the track number
856 track_nb
= atoi(ptr
);
857 ptr
= strstr(ptr
, "=");
862 sec
= cdtoc
[track_nb
].frame
;
863 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
865 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
866 (unsigned int) (off
/ (60 * 75)),
867 (unsigned int) ((off
/ 75) % 60),
868 (unsigned int) (off
% 75),
874 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
876 cd_info_t
*cd_info
= NULL
;
879 unsigned int audiolen
;
880 if (xmcd_file
== NULL
)
883 cd_info
= cd_info_new();
884 if (cd_info
== NULL
) {
888 length
= strlen(xmcd_file
);
890 while (ptr
!= NULL
&& pos
< length
) {
893 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
895 if (ptr2
[0] == '\0') {
901 // Search for the album title
902 if (xmcd_parse_dtitle(cd_info
, ptr
))
904 // Search for the genre
905 else if (xmcd_parse_dgenre(cd_info
, ptr
))
907 // Search for a track title
908 else if (xmcd_parse_ttitle(cd_info
, ptr
))
909 audiolen
++; // <-- audiolen++ to shut up gcc warning
913 pos
= (ptr2
+ 1) - ptr
;
917 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
918 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
919 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
920 cd_info
->msec
= (unsigned int) (audiolen
% 75);