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__)
42 #define mkdir(a,b) mkdir(a)
50 #include <sys/ioctl.h>
52 #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>
71 #include "osdep/osdep.h"
77 #include "libavutil/common.h"
79 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
80 #define DEFAULT_CACHE_DIR "/.cddb/"
83 char cddb_hello
[1024];
84 unsigned long disc_id
;
88 int freedb_proto_level
;
92 size_t xmcd_file_size
;
97 unsigned int min
, sec
, frame
;
100 static cd_toc_t cdtoc
[100];
101 static int cdtoc_last_track
;
103 static int read_toc(const char *dev
)
105 int first
= 0, last
= -1;
107 #if defined(__MINGW32__) || defined(__CYGWIN__)
113 sprintf(device
, "\\\\.\\%s", dev
);
114 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
115 OPEN_EXISTING
, 0, 0);
117 if (!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
,
118 sizeof(CDROM_TOC
), &r
, 0)) {
119 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
123 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
124 for (i
= first
; i
<= last
; i
++) {
125 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
126 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
127 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
131 #elif defined(__OS2__)
132 UCHAR auchParamDisk
[4] = {'C', 'D', '0', '1'};
140 BYTE bLeadOutReserved
;
141 } __attribute__((packed
)) sDataDisk
;
146 } __attribute__((packed
)) sParamTrack
= {{'C', 'D', '0', '1'},};
154 } __attribute__((packed
)) sDataTrack
;
162 rc
= DosOpen(dev
, &hcd
, &ulAction
, 0, FILE_NORMAL
,
163 OPEN_ACTION_OPEN_IF_EXISTS
| OPEN_ACTION_FAIL_IF_NEW
,
164 OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
| OPEN_FLAGS_DASD
,
167 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
171 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIODISK
,
172 auchParamDisk
, sizeof(auchParamDisk
), &ulParamLen
,
173 &sDataDisk
, sizeof(sDataDisk
), &ulDataLen
);
175 first
= sDataDisk
.bFirstTrack
- 1;
176 last
= sDataDisk
.bLastTrack
;
177 for (i
= first
; i
<= last
; i
++) {
179 sDataTrack
.bStartM
= sDataDisk
.bLeadOutM
;
180 sDataTrack
.bStartS
= sDataDisk
.bLeadOutS
;
181 sDataTrack
.bStartF
= sDataDisk
.bLeadOutF
;
183 sParamTrack
.bTrack
= i
+ 1;
184 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIOTRACK
,
185 &sParamTrack
, sizeof(sParamTrack
), &ulParamLen
,
186 &sDataTrack
, sizeof(sDataTrack
), &ulDataLen
);
191 cdtoc
[i
].min
= sDataTrack
.bStartM
;
192 cdtoc
[i
].sec
= sDataTrack
.bStartS
;
193 cdtoc
[i
].frame
= sDataTrack
.bStartF
;
200 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
205 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
210 #if defined(__linux__) || defined(__bsdi__)
212 struct cdrom_tochdr tochdr
;
213 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
214 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
216 for (i
= first
; i
<= last
; i
++) {
217 struct cdrom_tocentry tocentry
;
218 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
219 tocentry
.cdte_format
= CDROM_MSF
;
220 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
221 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
222 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
223 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
225 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
227 struct ioc_toc_header tochdr
;
228 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
229 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
231 for (i
= first
; i
<= last
; i
++) {
232 struct ioc_read_toc_single_entry tocentry
;
233 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
234 tocentry
.address_format
= CD_MSF_FORMAT
;
235 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
236 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
237 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
238 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
240 #elif defined(__NetBSD__) || defined(__OpenBSD__)
242 struct ioc_toc_header tochdr
;
243 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
244 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
246 for (i
= first
; i
<= last
; i
++) {
247 struct ioc_read_toc_entry tocentry
;
248 struct cd_toc_entry toc_buffer
;
249 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
250 tocentry
.address_format
= CD_MSF_FORMAT
;
251 tocentry
.data
= &toc_buffer
;
252 tocentry
.data_len
= sizeof(toc_buffer
);
253 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
254 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
255 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
256 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
258 #elif defined(__APPLE__) || defined(__DARWIN__)
260 dk_cd_read_toc_t tochdr
;
262 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
263 memset(&tochdr
, 0, sizeof(tochdr
));
264 tochdr
.bufferLength
= sizeof(buf
);
265 tochdr
.buffer
= &buf
;
266 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
267 && tochdr
.bufferLength
== sizeof(buf
)) {
272 memset(&tochdr
, 0, sizeof(tochdr
));
273 tochdr
.bufferLength
= sizeof(buf2
);
274 tochdr
.buffer
= &buf2
;
275 tochdr
.format
= kCDTOCFormatTOC
;
276 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
277 || tochdr
.bufferLength
< sizeof(CDTOC
))
281 CDTOC
*cdToc
= (CDTOC
*)buf2
;
282 CDTrackInfo lastTrack
;
283 dk_cd_read_track_info_t trackInfoParams
;
284 for (i
= first
; i
< last
; ++i
) {
285 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
286 cdtoc
[i
].min
= msf
.minute
;
287 cdtoc
[i
].sec
= msf
.second
;
288 cdtoc
[i
].frame
= msf
.frame
;
290 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
291 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
292 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
293 trackInfoParams
.address
= last
;
294 trackInfoParams
.buffer
= &lastTrack
;
295 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
296 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
297 + be2me_32(lastTrack
.trackSize
));
298 cdtoc
[last
].min
= msf
.minute
;
299 cdtoc
[last
].sec
= msf
.second
;
300 cdtoc
[last
].frame
= msf
.frame
;
307 for (i
= first
; i
<= last
; i
++)
308 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
313 \brief Reads TOC from CD in the given device and prints the number of tracks
314 and the length of each track in minute:second:frame format.
315 \param *dev the device to analyse
316 \return if the command line -identify is given, returns the last track of
317 the TOC or -1 if the TOC can't be read,
318 otherwise just returns 0 and let cddb_resolve the TOC
320 int cdd_identify(const char *dev
)
322 cdtoc_last_track
= 0;
323 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
324 int i
, min
, sec
, frame
;
325 cdtoc_last_track
= read_toc(dev
);
326 if (cdtoc_last_track
< 0) {
327 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
330 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
331 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
332 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
337 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
338 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
341 return cdtoc_last_track
;
344 static unsigned int cddb_sum(int n
)
356 static unsigned long cddb_discid(int tot_trks
)
358 unsigned int i
, t
= 0, n
= 0;
361 while (i
< (unsigned int)tot_trks
) {
362 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
365 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
366 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
367 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
372 static int cddb_http_request(char *command
,
373 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
374 cddb_data_t
*cddb_data
)
379 HTTP_header_t
*http_hdr
;
381 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
384 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
385 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
386 cddb_data
->freedb_proto_level
);
387 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
389 url
= url_new(request
);
391 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "not a valid URL\n");
395 fd
= http_send_request(url
,0);
397 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to send the HTTP request.\n");
401 http_hdr
= http_read_response(fd
);
402 if (http_hdr
== NULL
) {
403 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to read the HTTP response.\n");
407 http_debug_hdr(http_hdr
);
408 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
410 switch (http_hdr
->status_code
) {
412 ret
= reply_parser(http_hdr
, cddb_data
);
415 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Not Found.\n");
418 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unknown error code\n");
427 static int cddb_read_cache(cddb_data_t
*cddb_data
)
434 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
437 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
439 file_fd
= open(file_name
, O_RDONLY
| O_BINARY
);
441 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No cache found.\n");
445 ret
= fstat(file_fd
, &stats
);
450 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
453 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
454 if (cddb_data
->xmcd_file
== NULL
) {
455 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
459 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
460 if (cddb_data
->xmcd_file_size
!= file_size
) {
461 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all the xmcd file has been read.\n");
465 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
472 static int cddb_write_cache(cddb_data_t
*cddb_data
)
474 // We have the file, save it for cache.
475 struct stat file_stat
;
480 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
483 // Check if the CDDB cache dir exist
484 ret
= stat(cddb_data
->cache_dir
, &file_stat
);
486 // Directory not present, create it.
487 ret
= mkdir(cddb_data
->cache_dir
, 0755);
489 if (ret
< 0 && errno
!= EEXIST
) {
494 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to create directory %s.\n",
495 cddb_data
->cache_dir
);
500 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
502 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
508 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
514 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
515 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all of the xmcd file has been written.\n");
525 static int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
527 unsigned long disc_id
;
529 char *ptr
= NULL
, *ptr2
= NULL
;
532 if (http_hdr
== NULL
|| cddb_data
== NULL
)
535 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
537 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
543 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
546 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
549 // Check if it's a xmcd database file
550 ptr
= strstr(http_hdr
->body
, "# xmcd");
552 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
553 "Invalid xmcd database file returned.\n");
557 // Ok found the beginning of the file
559 ptr2
= strstr(ptr
, "\n.\r\n");
561 ptr2
= strstr(ptr
, "\n.\n");
565 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
566 ptr2
= ptr
+ strlen(ptr
); //return -1;
570 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
571 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unexpected FIXME\n");
574 cddb_data
->xmcd_file
= ptr
;
575 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
576 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
577 return cddb_write_cache(cddb_data
);
579 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
584 static int cddb_request_titles(cddb_data_t
*cddb_data
)
587 sprintf(command
, "cddb+read+%s+%08lx",
588 cddb_data
->category
, cddb_data
->disc_id
);
589 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
592 static int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
594 char album_title
[100];
598 ptr
= strstr(http_hdr
->body
, "\n");
600 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Unable to find end of line.\n");
604 // We have a list of exact/inexact matches, so which one do we use?
605 // So let's take the first one.
606 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
607 &(cddb_data
->disc_id
), album_title
);
609 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
612 ptr
= strstr(http_hdr
->body
, album_title
);
616 ptr2
= strstr(ptr
, "\n");
618 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
622 len
= FFMIN(sizeof(album_title
) - 1, len
);
623 strncpy(album_title
, ptr
, len
);
624 album_title
[len
]='\0';
626 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
630 static int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
632 char album_title
[100];
636 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
638 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
645 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
646 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
648 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
651 ptr
= strstr(http_hdr
->body
, album_title
);
655 ptr2
= strstr(ptr
, "\n");
657 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
661 len
= FFMIN(sizeof(album_title
) - 1, len
);
662 strncpy(album_title
, ptr
, len
);
663 album_title
[len
]='\0';
665 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
666 return cddb_request_titles(cddb_data
);
669 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Album not found.\n");
672 // Found exact matches, list follows
673 cddb_parse_matches_list(http_hdr
, cddb_data
);
674 return cddb_request_titles(cddb_data
);
676 body=[210 Found exact matches, list follows (until terminating `.')
677 misc c711930d Santana / Supernatural
678 rock c711930d Santana / Supernatural
679 blues c711930d Santana / Supernatural
683 // Found inexact matches, list follows
684 cddb_parse_matches_list(http_hdr
, cddb_data
);
685 return cddb_request_titles(cddb_data
);
687 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
,
688 "Server returns: Command syntax error\n");
691 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
696 static int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
702 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
704 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
710 ptr
= strstr(http_hdr
->body
, "max proto:");
712 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
715 ret
= sscanf(ptr
, "max proto: %d", &max
);
717 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
720 cddb_data
->freedb_proto_level
= max
;
723 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
728 static int cddb_get_proto_level(cddb_data_t
*cddb_data
)
730 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
733 static void cddb_create_hello(cddb_data_t
*cddb_data
)
738 if (cddb_data
->anonymous
) { // Default is anonymous
739 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
740 * We don't send current user/host name in hello to prevent spam.
741 * Software that sends this is considered spyware
742 * that most people don't like.
744 user_name
= "anonymous";
745 strcpy(host_name
, "localhost");
747 if (gethostname(host_name
, 50) < 0) {
748 strcpy(host_name
, "localhost");
750 user_name
= getenv("LOGNAME");
752 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s",
753 user_name
, host_name
, mplayer_version
);
756 static int cddb_retrieve(cddb_data_t
*cddb_data
)
758 char offsets
[1024], command
[1024];
760 unsigned int i
, time_len
;
764 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
765 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
766 if (ptr
-offsets
> sizeof offsets
- 40) break;
769 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
771 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
772 cddb_data
->freedb_proto_level
= 1;
773 cddb_data
->xmcd_file
= NULL
;
775 cddb_create_hello(cddb_data
);
776 if (cddb_get_proto_level(cddb_data
) < 0) {
777 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to get the protocol level.\n");
781 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
782 cddb_data
->tracks
, offsets
, time_len
);
783 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
787 free(cddb_data
->cache_dir
);
791 int cddb_resolve(const char *dev
, char **xmcd_file
)
793 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
794 char *home_dir
= NULL
;
795 cddb_data_t cddb_data
;
797 if (cdtoc_last_track
<= 0) {
798 cdtoc_last_track
= read_toc(dev
);
799 if (cdtoc_last_track
< 0) {
800 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
804 cddb_data
.tracks
= cdtoc_last_track
;
805 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
806 cddb_data
.anonymous
= 1; // Don't send user info by default
808 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
811 // Check if there is a CD in the drive
812 // FIXME: That's not really a good way to check
813 if (cddb_data
.disc_id
== 0) {
814 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No CD in the drive.\n");
818 home_dir
= getenv("HOME");
820 if (home_dir
== NULL
)
821 home_dir
= getenv("USERPROFILE");
822 if (home_dir
== NULL
)
823 home_dir
= getenv("HOMEPATH");
824 // Last resort, store the cddb cache in the mplayer directory
825 if (home_dir
== NULL
)
826 home_dir
= (char *)get_path("");
828 if (home_dir
== NULL
) {
829 cddb_data
.cache_dir
= NULL
;
831 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
832 + strlen(cddb_cache_dir
) + 1);
833 if (cddb_data
.cache_dir
== NULL
) {
834 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
837 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
840 // Check for a cached file
841 if (cddb_read_cache(&cddb_data
) < 0) {
843 if (cddb_retrieve(&cddb_data
) < 0) {
848 if (cddb_data
.xmcd_file
!= NULL
) {
849 // printf("%s\n", cddb_data.xmcd_file);
850 *xmcd_file
= cddb_data
.xmcd_file
;
860 static char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
863 ptr
= strstr(line
, "DTITLE=");
866 album
= strstr(ptr
, "/");
869 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
870 if (cd_info
->album
== NULL
) {
873 strcpy(cd_info
->album
, album
+ 2);
876 cd_info
->artist
= malloc(strlen(ptr
) + 1);
877 if (cd_info
->artist
== NULL
) {
880 strcpy(cd_info
->artist
, ptr
);
885 static char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
888 ptr
= strstr(line
, "DGENRE=");
891 cd_info
->genre
= malloc(strlen(ptr
)+1);
892 if (cd_info
->genre
== NULL
) {
895 strcpy(cd_info
->genre
, ptr
);
900 static char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
902 unsigned int track_nb
;
903 unsigned long sec
, off
;
905 ptr
= strstr(line
, "TTITLE");
908 // Here we point to the track number
909 track_nb
= atoi(ptr
);
910 ptr
= strstr(ptr
, "=");
915 sec
= cdtoc
[track_nb
].frame
;
916 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
918 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
919 (unsigned int) (off
/ (60 * 75)),
920 (unsigned int) ((off
/ 75) % 60),
921 (unsigned int) (off
% 75),
927 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
929 cd_info_t
*cd_info
= NULL
;
932 unsigned int audiolen
;
933 if (xmcd_file
== NULL
)
936 cd_info
= cd_info_new();
937 if (cd_info
== NULL
) {
941 length
= strlen(xmcd_file
);
943 while (ptr
!= NULL
&& pos
< length
) {
946 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
948 if (ptr2
[0] == '\0') {
954 // Search for the album title
955 if (xmcd_parse_dtitle(cd_info
, ptr
))
957 // Search for the genre
958 else if (xmcd_parse_dgenre(cd_info
, ptr
))
960 // Search for a track title
961 else if (xmcd_parse_ttitle(cd_info
, ptr
))
962 audiolen
++; // <-- audiolen++ to shut up gcc warning
966 pos
= (ptr2
+ 1) - ptr
;
970 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
971 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
972 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
973 cd_info
->msec
= (unsigned int) (audiolen
% 75);