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.
40 #if defined(__MINGW32__) || defined(__CYGWIN__)
47 #include <sys/ioctl.h>
49 #include <sys/types.h>
55 #if defined(__linux__)
56 #include <linux/cdrom.h>
57 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
59 #elif defined(__MINGW32__) || defined(__CYGWIN__)
60 #include <ddk/ntddcdrm.h>
61 #elif defined(__bsdi__)
63 #elif defined(__APPLE__) || defined(__DARWIN__)
64 #include <IOKit/storage/IOCDTypes.h>
65 #include <IOKit/storage/IOCDMediaBSDClient.h>
69 #include "osdep/osdep.h"
75 #include "libavutil/common.h"
77 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
78 #define DEFAULT_CACHE_DIR "/.cddb/"
81 char cddb_hello
[1024];
82 unsigned long disc_id
;
86 int freedb_proto_level
;
90 size_t xmcd_file_size
;
95 unsigned int min
, sec
, frame
;
98 static cd_toc_t cdtoc
[100];
99 static int cdtoc_last_track
;
101 static int read_toc(const char *dev
)
103 int first
= 0, last
= -1;
105 #if defined(__MINGW32__) || defined(__CYGWIN__)
111 sprintf(device
, "\\\\.\\%s", dev
);
112 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
113 OPEN_EXISTING
, 0, 0);
115 if (!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
,
116 sizeof(CDROM_TOC
), &r
, 0)) {
117 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
121 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
122 for (i
= first
; i
<= last
; i
++) {
123 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
124 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
125 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
129 #elif defined(__OS2__)
130 UCHAR auchParamDisk
[4] = {'C', 'D', '0', '1'};
138 BYTE bLeadOutReserved
;
139 } __attribute__((packed
)) sDataDisk
;
144 } __attribute__((packed
)) sParamTrack
= {{'C', 'D', '0', '1'},};
152 } __attribute__((packed
)) sDataTrack
;
160 rc
= DosOpen(dev
, &hcd
, &ulAction
, 0, FILE_NORMAL
,
161 OPEN_ACTION_OPEN_IF_EXISTS
| OPEN_ACTION_FAIL_IF_NEW
,
162 OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
| OPEN_FLAGS_DASD
,
165 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
169 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIODISK
,
170 auchParamDisk
, sizeof(auchParamDisk
), &ulParamLen
,
171 &sDataDisk
, sizeof(sDataDisk
), &ulDataLen
);
173 first
= sDataDisk
.bFirstTrack
- 1;
174 last
= sDataDisk
.bLastTrack
;
175 for (i
= first
; i
<= last
; i
++) {
177 sDataTrack
.bStartM
= sDataDisk
.bLeadOutM
;
178 sDataTrack
.bStartS
= sDataDisk
.bLeadOutS
;
179 sDataTrack
.bStartF
= sDataDisk
.bLeadOutF
;
181 sParamTrack
.bTrack
= i
+ 1;
182 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIOTRACK
,
183 &sParamTrack
, sizeof(sParamTrack
), &ulParamLen
,
184 &sDataTrack
, sizeof(sDataTrack
), &ulDataLen
);
189 cdtoc
[i
].min
= sDataTrack
.bStartM
;
190 cdtoc
[i
].sec
= sDataTrack
.bStartS
;
191 cdtoc
[i
].frame
= sDataTrack
.bStartF
;
198 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
203 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
208 #if defined(__linux__) || defined(__bsdi__)
210 struct cdrom_tochdr tochdr
;
211 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
212 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
214 for (i
= first
; i
<= last
; i
++) {
215 struct cdrom_tocentry tocentry
;
216 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
217 tocentry
.cdte_format
= CDROM_MSF
;
218 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
219 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
220 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
221 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
223 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
225 struct ioc_toc_header tochdr
;
226 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
227 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
229 for (i
= first
; i
<= last
; i
++) {
230 struct ioc_read_toc_single_entry tocentry
;
231 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
232 tocentry
.address_format
= CD_MSF_FORMAT
;
233 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
234 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
235 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
236 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
238 #elif defined(__NetBSD__) || defined(__OpenBSD__)
240 struct ioc_toc_header tochdr
;
241 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
242 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
244 for (i
= first
; i
<= last
; i
++) {
245 struct ioc_read_toc_entry tocentry
;
246 struct cd_toc_entry toc_buffer
;
247 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
248 tocentry
.address_format
= CD_MSF_FORMAT
;
249 tocentry
.data
= &toc_buffer
;
250 tocentry
.data_len
= sizeof(toc_buffer
);
251 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
252 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
253 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
254 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
256 #elif defined(__APPLE__) || defined(__DARWIN__)
258 dk_cd_read_toc_t tochdr
;
260 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
261 memset(&tochdr
, 0, sizeof(tochdr
));
262 tochdr
.bufferLength
= sizeof(buf
);
263 tochdr
.buffer
= &buf
;
264 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
265 && tochdr
.bufferLength
== sizeof(buf
)) {
270 memset(&tochdr
, 0, sizeof(tochdr
));
271 tochdr
.bufferLength
= sizeof(buf2
);
272 tochdr
.buffer
= &buf2
;
273 tochdr
.format
= kCDTOCFormatTOC
;
274 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
275 || tochdr
.bufferLength
< sizeof(CDTOC
))
279 CDTOC
*cdToc
= (CDTOC
*)buf2
;
280 CDTrackInfo lastTrack
;
281 dk_cd_read_track_info_t trackInfoParams
;
282 for (i
= first
; i
< last
; ++i
) {
283 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
284 cdtoc
[i
].min
= msf
.minute
;
285 cdtoc
[i
].sec
= msf
.second
;
286 cdtoc
[i
].frame
= msf
.frame
;
288 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
289 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
290 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
291 trackInfoParams
.address
= last
;
292 trackInfoParams
.buffer
= &lastTrack
;
293 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
294 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
295 + be2me_32(lastTrack
.trackSize
));
296 cdtoc
[last
].min
= msf
.minute
;
297 cdtoc
[last
].sec
= msf
.second
;
298 cdtoc
[last
].frame
= msf
.frame
;
305 for (i
= first
; i
<= last
; i
++)
306 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
311 \brief Reads TOC from CD in the given device and prints the number of tracks
312 and the length of each track in minute:second:frame format.
313 \param *dev the device to analyse
314 \return if the command line -identify is given, returns the last track of
315 the TOC or -1 if the TOC can't be read,
316 otherwise just returns 0 and let cddb_resolve the TOC
318 int cdd_identify(const char *dev
)
320 cdtoc_last_track
= 0;
321 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
322 int i
, min
, sec
, frame
;
323 cdtoc_last_track
= read_toc(dev
);
324 if (cdtoc_last_track
< 0) {
325 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
328 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
329 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
330 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
335 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
336 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
339 return cdtoc_last_track
;
342 static unsigned int cddb_sum(int n
)
354 static unsigned long cddb_discid(int tot_trks
)
356 unsigned int i
, t
= 0, n
= 0;
359 while (i
< (unsigned int)tot_trks
) {
360 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
363 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
364 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
365 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
370 static int cddb_http_request(char *command
,
371 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
372 cddb_data_t
*cddb_data
)
377 HTTP_header_t
*http_hdr
;
379 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
382 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
383 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
384 cddb_data
->freedb_proto_level
);
385 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
387 url
= url_new(request
);
389 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "not a valid URL\n");
393 fd
= http_send_request(url
,0);
395 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to send the HTTP request.\n");
399 http_hdr
= http_read_response(fd
);
400 if (http_hdr
== NULL
) {
401 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to read the HTTP response.\n");
405 http_debug_hdr(http_hdr
);
406 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
408 switch (http_hdr
->status_code
) {
410 ret
= reply_parser(http_hdr
, cddb_data
);
413 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Not Found.\n");
416 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unknown error code\n");
425 static int cddb_read_cache(cddb_data_t
*cddb_data
)
432 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
435 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
437 file_fd
= open(file_name
, O_RDONLY
| O_BINARY
);
439 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No cache found.\n");
443 ret
= fstat(file_fd
, &stats
);
448 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
451 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
452 if (cddb_data
->xmcd_file
== NULL
) {
453 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
457 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
458 if (cddb_data
->xmcd_file_size
!= file_size
) {
459 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all the xmcd file has been read.\n");
463 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
470 static int cddb_write_cache(cddb_data_t
*cddb_data
)
472 // We have the file, save it for cache.
477 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
480 // Check if the CDDB cache dir exist
481 if (!mp_path_exists(cddb_data
->cache_dir
)) {
482 // Directory not present, create it.
483 ret
= mkdir(cddb_data
->cache_dir
, 0755);
485 if (ret
< 0 && errno
!= EEXIST
) {
490 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to create directory %s.\n",
491 cddb_data
->cache_dir
);
496 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
498 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
504 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
510 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
511 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all of the xmcd file has been written.\n");
521 static int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
523 unsigned long disc_id
;
525 char *ptr
= NULL
, *ptr2
= NULL
;
528 if (http_hdr
== NULL
|| cddb_data
== NULL
)
531 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
533 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
539 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
542 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
545 // Check if it's a xmcd database file
546 ptr
= strstr(http_hdr
->body
, "# xmcd");
548 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
549 "Invalid xmcd database file returned.\n");
553 // Ok found the beginning of the file
555 ptr2
= strstr(ptr
, "\n.\r\n");
557 ptr2
= strstr(ptr
, "\n.\n");
561 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
562 ptr2
= ptr
+ strlen(ptr
); //return -1;
566 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
567 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unexpected FIXME\n");
570 cddb_data
->xmcd_file
= ptr
;
571 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
572 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
573 return cddb_write_cache(cddb_data
);
575 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
580 static int cddb_request_titles(cddb_data_t
*cddb_data
)
583 sprintf(command
, "cddb+read+%s+%08lx",
584 cddb_data
->category
, cddb_data
->disc_id
);
585 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
588 static int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
590 char album_title
[100];
594 ptr
= strstr(http_hdr
->body
, "\n");
596 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Unable to find end of line.\n");
600 // We have a list of exact/inexact matches, so which one do we use?
601 // So let's take the first one.
602 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
603 &(cddb_data
->disc_id
), album_title
);
605 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
608 ptr
= strstr(http_hdr
->body
, album_title
);
612 ptr2
= strstr(ptr
, "\n");
614 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
618 len
= FFMIN(sizeof(album_title
) - 1, len
);
619 strncpy(album_title
, ptr
, len
);
620 album_title
[len
]='\0';
622 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
626 static int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
628 char album_title
[100];
632 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
634 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
641 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
642 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
644 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
647 ptr
= strstr(http_hdr
->body
, album_title
);
651 ptr2
= strstr(ptr
, "\n");
653 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
657 len
= FFMIN(sizeof(album_title
) - 1, len
);
658 strncpy(album_title
, ptr
, len
);
659 album_title
[len
]='\0';
661 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
662 return cddb_request_titles(cddb_data
);
665 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Album not found.\n");
668 // Found exact matches, list follows
669 cddb_parse_matches_list(http_hdr
, cddb_data
);
670 return cddb_request_titles(cddb_data
);
672 body=[210 Found exact matches, list follows (until terminating `.')
673 misc c711930d Santana / Supernatural
674 rock c711930d Santana / Supernatural
675 blues c711930d Santana / Supernatural
679 // Found inexact matches, list follows
680 cddb_parse_matches_list(http_hdr
, cddb_data
);
681 return cddb_request_titles(cddb_data
);
683 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
,
684 "Server returns: Command syntax error\n");
687 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
692 static int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
698 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
700 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
706 ptr
= strstr(http_hdr
->body
, "max proto:");
708 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
711 ret
= sscanf(ptr
, "max proto: %d", &max
);
713 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
716 cddb_data
->freedb_proto_level
= max
;
719 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
724 static int cddb_get_proto_level(cddb_data_t
*cddb_data
)
726 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
729 static void cddb_create_hello(cddb_data_t
*cddb_data
)
734 if (cddb_data
->anonymous
) { // Default is anonymous
735 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
736 * We don't send current user/host name in hello to prevent spam.
737 * Software that sends this is considered spyware
738 * that most people don't like.
740 user_name
= "anonymous";
741 strcpy(host_name
, "localhost");
743 if (gethostname(host_name
, 50) < 0) {
744 strcpy(host_name
, "localhost");
746 user_name
= getenv("LOGNAME");
748 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s",
749 user_name
, host_name
, mplayer_version
);
752 static int cddb_retrieve(cddb_data_t
*cddb_data
)
754 char offsets
[1024], command
[1024];
756 unsigned int i
, time_len
;
760 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
761 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
762 if (ptr
-offsets
> sizeof offsets
- 40) break;
765 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
767 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
768 cddb_data
->freedb_proto_level
= 1;
769 cddb_data
->xmcd_file
= NULL
;
771 cddb_create_hello(cddb_data
);
772 if (cddb_get_proto_level(cddb_data
) < 0) {
773 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to get the protocol level.\n");
777 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
778 cddb_data
->tracks
, offsets
, time_len
);
779 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
783 free(cddb_data
->cache_dir
);
787 int cddb_resolve(const char *dev
, char **xmcd_file
)
789 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
790 char *home_dir
= NULL
;
791 cddb_data_t cddb_data
;
793 if (cdtoc_last_track
<= 0) {
794 cdtoc_last_track
= read_toc(dev
);
795 if (cdtoc_last_track
< 0) {
796 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
800 cddb_data
.tracks
= cdtoc_last_track
;
801 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
802 cddb_data
.anonymous
= 1; // Don't send user info by default
804 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
807 // Check if there is a CD in the drive
808 // FIXME: That's not really a good way to check
809 if (cddb_data
.disc_id
== 0) {
810 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No CD in the drive.\n");
814 home_dir
= getenv("HOME");
816 if (home_dir
== NULL
)
817 home_dir
= getenv("USERPROFILE");
818 if (home_dir
== NULL
)
819 home_dir
= getenv("HOMEPATH");
820 // Last resort, store the cddb cache in the mplayer directory
821 if (home_dir
== NULL
)
822 home_dir
= (char *)get_path("");
824 if (home_dir
== NULL
) {
825 cddb_data
.cache_dir
= NULL
;
827 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
828 + strlen(cddb_cache_dir
) + 1);
829 if (cddb_data
.cache_dir
== NULL
) {
830 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
833 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
836 // Check for a cached file
837 if (cddb_read_cache(&cddb_data
) < 0) {
839 if (cddb_retrieve(&cddb_data
) < 0) {
844 if (cddb_data
.xmcd_file
!= NULL
) {
845 // printf("%s\n", cddb_data.xmcd_file);
846 *xmcd_file
= cddb_data
.xmcd_file
;
856 static char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
859 ptr
= strstr(line
, "DTITLE=");
862 album
= strstr(ptr
, "/");
865 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
866 if (cd_info
->album
== NULL
) {
869 strcpy(cd_info
->album
, album
+ 2);
872 cd_info
->artist
= malloc(strlen(ptr
) + 1);
873 if (cd_info
->artist
== NULL
) {
876 strcpy(cd_info
->artist
, ptr
);
881 static char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
884 ptr
= strstr(line
, "DGENRE=");
887 cd_info
->genre
= malloc(strlen(ptr
)+1);
888 if (cd_info
->genre
== NULL
) {
891 strcpy(cd_info
->genre
, ptr
);
896 static char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
898 unsigned int track_nb
;
899 unsigned long sec
, off
;
901 ptr
= strstr(line
, "TTITLE");
904 // Here we point to the track number
905 track_nb
= atoi(ptr
);
906 ptr
= strstr(ptr
, "=");
911 sec
= cdtoc
[track_nb
].frame
;
912 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
914 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
915 (unsigned int) (off
/ (60 * 75)),
916 (unsigned int) ((off
/ 75) % 60),
917 (unsigned int) (off
% 75),
923 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
925 cd_info_t
*cd_info
= NULL
;
928 unsigned int audiolen
;
929 if (xmcd_file
== NULL
)
932 cd_info
= cd_info_new();
933 if (cd_info
== NULL
) {
937 length
= strlen(xmcd_file
);
939 while (ptr
!= NULL
&& pos
< length
) {
942 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
944 if (ptr2
[0] == '\0') {
950 // Search for the album title
951 if (xmcd_parse_dtitle(cd_info
, ptr
))
953 // Search for the genre
954 else if (xmcd_parse_dgenre(cd_info
, ptr
))
956 // Search for a track title
957 else if (xmcd_parse_ttitle(cd_info
, ptr
))
958 audiolen
++; // <-- audiolen++ to shut up gcc warning
962 pos
= (ptr2
+ 1) - ptr
;
966 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
967 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
968 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
969 cd_info
->msec
= (unsigned int) (audiolen
% 75);