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>
56 #if defined(__linux__)
57 #include <linux/cdrom.h>
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 #elif defined(__MINGW32__) || defined(__CYGWIN__)
61 #include <ddk/ntddcdrm.h>
62 #elif defined(__bsdi__)
64 #elif defined(__APPLE__) || defined(__DARWIN__)
65 #include <IOKit/storage/IOCDTypes.h>
66 #include <IOKit/storage/IOCDMediaBSDClient.h>
70 #include "osdep/osdep.h"
76 #include "libavutil/common.h"
78 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
79 #define DEFAULT_CACHE_DIR "/.cddb/"
82 char cddb_hello
[1024];
83 unsigned long disc_id
;
87 int freedb_proto_level
;
91 size_t xmcd_file_size
;
96 unsigned int min
, sec
, frame
;
99 static cd_toc_t cdtoc
[100];
100 static int cdtoc_last_track
;
102 static int read_toc(const char *dev
)
104 int first
= 0, last
= -1;
106 #if defined(__MINGW32__) || defined(__CYGWIN__)
112 sprintf(device
, "\\\\.\\%s", dev
);
113 drive
= CreateFile(device
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
114 OPEN_EXISTING
, 0, 0);
116 if (!DeviceIoControl(drive
, IOCTL_CDROM_READ_TOC
, NULL
, 0, &toc
,
117 sizeof(CDROM_TOC
), &r
, 0)) {
118 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
122 first
= toc
.FirstTrack
- 1; last
= toc
.LastTrack
;
123 for (i
= first
; i
<= last
; i
++) {
124 cdtoc
[i
].min
= toc
.TrackData
[i
].Address
[1];
125 cdtoc
[i
].sec
= toc
.TrackData
[i
].Address
[2];
126 cdtoc
[i
].frame
= toc
.TrackData
[i
].Address
[3];
130 #elif defined(__OS2__)
131 UCHAR auchParamDisk
[4] = {'C', 'D', '0', '1'};
139 BYTE bLeadOutReserved
;
140 } __attribute__((packed
)) sDataDisk
;
145 } __attribute__((packed
)) sParamTrack
= {{'C', 'D', '0', '1'},};
153 } __attribute__((packed
)) sDataTrack
;
161 rc
= DosOpen(dev
, &hcd
, &ulAction
, 0, FILE_NORMAL
,
162 OPEN_ACTION_OPEN_IF_EXISTS
| OPEN_ACTION_FAIL_IF_NEW
,
163 OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
| OPEN_FLAGS_DASD
,
166 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
170 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIODISK
,
171 auchParamDisk
, sizeof(auchParamDisk
), &ulParamLen
,
172 &sDataDisk
, sizeof(sDataDisk
), &ulDataLen
);
174 first
= sDataDisk
.bFirstTrack
- 1;
175 last
= sDataDisk
.bLastTrack
;
176 for (i
= first
; i
<= last
; i
++) {
178 sDataTrack
.bStartM
= sDataDisk
.bLeadOutM
;
179 sDataTrack
.bStartS
= sDataDisk
.bLeadOutS
;
180 sDataTrack
.bStartF
= sDataDisk
.bLeadOutF
;
182 sParamTrack
.bTrack
= i
+ 1;
183 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIOTRACK
,
184 &sParamTrack
, sizeof(sParamTrack
), &ulParamLen
,
185 &sDataTrack
, sizeof(sDataTrack
), &ulDataLen
);
190 cdtoc
[i
].min
= sDataTrack
.bStartM
;
191 cdtoc
[i
].sec
= sDataTrack
.bStartS
;
192 cdtoc
[i
].frame
= sDataTrack
.bStartF
;
199 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to read TOC.\n");
204 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
209 #if defined(__linux__) || defined(__bsdi__)
211 struct cdrom_tochdr tochdr
;
212 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
213 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
215 for (i
= first
; i
<= last
; i
++) {
216 struct cdrom_tocentry tocentry
;
217 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
218 tocentry
.cdte_format
= CDROM_MSF
;
219 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
220 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
221 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
222 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
224 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
226 struct ioc_toc_header tochdr
;
227 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
228 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
230 for (i
= first
; i
<= last
; i
++) {
231 struct ioc_read_toc_single_entry tocentry
;
232 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
233 tocentry
.address_format
= CD_MSF_FORMAT
;
234 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
235 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
236 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
237 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
239 #elif defined(__NetBSD__) || defined(__OpenBSD__)
241 struct ioc_toc_header tochdr
;
242 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
243 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
245 for (i
= first
; i
<= last
; i
++) {
246 struct ioc_read_toc_entry tocentry
;
247 struct cd_toc_entry toc_buffer
;
248 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
249 tocentry
.address_format
= CD_MSF_FORMAT
;
250 tocentry
.data
= &toc_buffer
;
251 tocentry
.data_len
= sizeof(toc_buffer
);
252 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
253 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
254 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
255 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
257 #elif defined(__APPLE__) || defined(__DARWIN__)
259 dk_cd_read_toc_t tochdr
;
261 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
262 memset(&tochdr
, 0, sizeof(tochdr
));
263 tochdr
.bufferLength
= sizeof(buf
);
264 tochdr
.buffer
= &buf
;
265 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
266 && tochdr
.bufferLength
== sizeof(buf
)) {
271 memset(&tochdr
, 0, sizeof(tochdr
));
272 tochdr
.bufferLength
= sizeof(buf2
);
273 tochdr
.buffer
= &buf2
;
274 tochdr
.format
= kCDTOCFormatTOC
;
275 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
276 || tochdr
.bufferLength
< sizeof(CDTOC
))
280 CDTOC
*cdToc
= (CDTOC
*)buf2
;
281 CDTrackInfo lastTrack
;
282 dk_cd_read_track_info_t trackInfoParams
;
283 for (i
= first
; i
< last
; ++i
) {
284 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
285 cdtoc
[i
].min
= msf
.minute
;
286 cdtoc
[i
].sec
= msf
.second
;
287 cdtoc
[i
].frame
= msf
.frame
;
289 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
290 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
291 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
292 trackInfoParams
.address
= last
;
293 trackInfoParams
.buffer
= &lastTrack
;
294 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
295 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
296 + be2me_32(lastTrack
.trackSize
));
297 cdtoc
[last
].min
= msf
.minute
;
298 cdtoc
[last
].sec
= msf
.second
;
299 cdtoc
[last
].frame
= msf
.frame
;
306 for (i
= first
; i
<= last
; i
++)
307 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
312 \brief Reads TOC from CD in the given device and prints the number of tracks
313 and the length of each track in minute:second:frame format.
314 \param *dev the device to analyse
315 \return if the command line -identify is given, returns the last track of
316 the TOC or -1 if the TOC can't be read,
317 otherwise just returns 0 and let cddb_resolve the TOC
319 int cdd_identify(const char *dev
)
321 cdtoc_last_track
= 0;
322 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
323 int i
, min
, sec
, frame
;
324 cdtoc_last_track
= read_toc(dev
);
325 if (cdtoc_last_track
< 0) {
326 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
329 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
330 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
331 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
336 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
337 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
340 return cdtoc_last_track
;
343 static unsigned int cddb_sum(int n
)
355 static unsigned long cddb_discid(int tot_trks
)
357 unsigned int i
, t
= 0, n
= 0;
360 while (i
< (unsigned int)tot_trks
) {
361 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
364 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
365 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
366 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
371 static int cddb_http_request(char *command
,
372 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
373 cddb_data_t
*cddb_data
)
378 HTTP_header_t
*http_hdr
;
380 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
383 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
384 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
385 cddb_data
->freedb_proto_level
);
386 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
388 url
= url_new(request
);
390 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "not a valid URL\n");
394 fd
= http_send_request(url
,0);
396 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to send the HTTP request.\n");
400 http_hdr
= http_read_response(fd
);
401 if (http_hdr
== NULL
) {
402 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to read the HTTP response.\n");
406 http_debug_hdr(http_hdr
);
407 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
409 switch (http_hdr
->status_code
) {
411 ret
= reply_parser(http_hdr
, cddb_data
);
414 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Not Found.\n");
417 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unknown error code\n");
426 static int cddb_read_cache(cddb_data_t
*cddb_data
)
433 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
436 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
438 file_fd
= open(file_name
, O_RDONLY
| O_BINARY
);
440 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No cache found.\n");
444 ret
= fstat(file_fd
, &stats
);
449 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
452 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
453 if (cddb_data
->xmcd_file
== NULL
) {
454 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
458 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
459 if (cddb_data
->xmcd_file_size
!= file_size
) {
460 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all the xmcd file has been read.\n");
464 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
471 static int cddb_write_cache(cddb_data_t
*cddb_data
)
473 // We have the file, save it for cache.
474 struct stat file_stat
;
479 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
482 // Check if the CDDB cache dir exist
483 ret
= stat(cddb_data
->cache_dir
, &file_stat
);
485 // Directory not present, create it.
486 ret
= mkdir(cddb_data
->cache_dir
, 0755);
488 if (ret
< 0 && errno
!= EEXIST
) {
493 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to create directory %s.\n",
494 cddb_data
->cache_dir
);
499 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
501 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
507 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
513 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
514 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Not all of the xmcd file has been written.\n");
524 static int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
526 unsigned long disc_id
;
528 char *ptr
= NULL
, *ptr2
= NULL
;
531 if (http_hdr
== NULL
|| cddb_data
== NULL
)
534 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
536 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
542 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
545 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
548 // Check if it's a xmcd database file
549 ptr
= strstr(http_hdr
->body
, "# xmcd");
551 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
,
552 "Invalid xmcd database file returned.\n");
556 // Ok found the beginning of the file
558 ptr2
= strstr(ptr
, "\n.\r\n");
560 ptr2
= strstr(ptr
, "\n.\n");
564 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
565 ptr2
= ptr
+ strlen(ptr
); //return -1;
569 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
570 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "unexpected FIXME\n");
573 cddb_data
->xmcd_file
= ptr
;
574 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
575 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
576 return cddb_write_cache(cddb_data
);
578 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
583 static int cddb_request_titles(cddb_data_t
*cddb_data
)
586 sprintf(command
, "cddb+read+%s+%08lx",
587 cddb_data
->category
, cddb_data
->disc_id
);
588 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
591 static int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
593 char album_title
[100];
597 ptr
= strstr(http_hdr
->body
, "\n");
599 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Unable to find end of line.\n");
603 // We have a list of exact/inexact matches, so which one do we use?
604 // So let's take the first one.
605 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
606 &(cddb_data
->disc_id
), album_title
);
608 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
611 ptr
= strstr(http_hdr
->body
, album_title
);
615 ptr2
= strstr(ptr
, "\n");
617 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
621 len
= FFMIN(sizeof(album_title
) - 1, len
);
622 strncpy(album_title
, ptr
, len
);
623 album_title
[len
]='\0';
625 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
629 static int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
631 char album_title
[100];
635 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
637 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
644 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
645 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
647 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
650 ptr
= strstr(http_hdr
->body
, album_title
);
654 ptr2
= strstr(ptr
, "\n");
656 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
660 len
= FFMIN(sizeof(album_title
) - 1, len
);
661 strncpy(album_title
, ptr
, len
);
662 album_title
[len
]='\0';
664 mp_tmsg(MSGT_DEMUX
, MSGL_STATUS
, "Parse OK, found: %s\n", album_title
);
665 return cddb_request_titles(cddb_data
);
668 mp_tmsg(MSGT_DEMUX
, MSGL_WARN
, "Album not found.\n");
671 // Found exact matches, list follows
672 cddb_parse_matches_list(http_hdr
, cddb_data
);
673 return cddb_request_titles(cddb_data
);
675 body=[210 Found exact matches, list follows (until terminating `.')
676 misc c711930d Santana / Supernatural
677 rock c711930d Santana / Supernatural
678 blues c711930d Santana / Supernatural
682 // Found inexact matches, list follows
683 cddb_parse_matches_list(http_hdr
, cddb_data
);
684 return cddb_request_titles(cddb_data
);
686 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
,
687 "Server returns: Command syntax error\n");
690 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
695 static int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
701 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
703 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
709 ptr
= strstr(http_hdr
->body
, "max proto:");
711 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
714 ret
= sscanf(ptr
, "max proto: %d", &max
);
716 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "parse error");
719 cddb_data
->freedb_proto_level
= max
;
722 mp_tmsg(MSGT_DEMUX
, MSGL_FIXME
, "unhandled code\n");
727 static int cddb_get_proto_level(cddb_data_t
*cddb_data
)
729 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
732 static void cddb_create_hello(cddb_data_t
*cddb_data
)
737 if (cddb_data
->anonymous
) { // Default is anonymous
738 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
739 * We don't send current user/host name in hello to prevent spam.
740 * Software that sends this is considered spyware
741 * that most people don't like.
743 user_name
= "anonymous";
744 strcpy(host_name
, "localhost");
746 if (gethostname(host_name
, 50) < 0) {
747 strcpy(host_name
, "localhost");
749 user_name
= getenv("LOGNAME");
751 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s",
752 user_name
, host_name
, mplayer_version
);
755 static int cddb_retrieve(cddb_data_t
*cddb_data
)
757 char offsets
[1024], command
[1024];
759 unsigned int i
, time_len
;
763 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
764 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
765 if (ptr
-offsets
> sizeof offsets
- 40) break;
768 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
770 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
771 cddb_data
->freedb_proto_level
= 1;
772 cddb_data
->xmcd_file
= NULL
;
774 cddb_create_hello(cddb_data
);
775 if (cddb_get_proto_level(cddb_data
) < 0) {
776 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Failed to get the protocol level.\n");
780 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
781 cddb_data
->tracks
, offsets
, time_len
);
782 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
786 if (cddb_data
->cache_dir
!= NULL
) {
787 free(cddb_data
->cache_dir
);
792 int cddb_resolve(const char *dev
, char **xmcd_file
)
794 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
795 char *home_dir
= NULL
;
796 cddb_data_t cddb_data
;
798 if (cdtoc_last_track
<= 0) {
799 cdtoc_last_track
= read_toc(dev
);
800 if (cdtoc_last_track
< 0) {
801 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed to open %s device.\n", dev
);
805 cddb_data
.tracks
= cdtoc_last_track
;
806 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
807 cddb_data
.anonymous
= 1; // Don't send user info by default
809 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
812 // Check if there is a CD in the drive
813 // FIXME: That's not really a good way to check
814 if (cddb_data
.disc_id
== 0) {
815 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "No CD in the drive.\n");
819 home_dir
= getenv("HOME");
821 if (home_dir
== NULL
)
822 home_dir
= getenv("USERPROFILE");
823 if (home_dir
== NULL
)
824 home_dir
= getenv("HOMEPATH");
825 // Last resort, store the cddb cache in the mplayer directory
826 if (home_dir
== NULL
)
827 home_dir
= (char *)get_path("");
829 if (home_dir
== NULL
) {
830 cddb_data
.cache_dir
= NULL
;
832 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
833 + strlen(cddb_cache_dir
) + 1);
834 if (cddb_data
.cache_dir
== NULL
) {
835 mp_tmsg(MSGT_DEMUX
, MSGL_ERR
, "Memory allocation failed.\n");
838 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
841 // Check for a cached file
842 if (cddb_read_cache(&cddb_data
) < 0) {
844 if (cddb_retrieve(&cddb_data
) < 0) {
849 if (cddb_data
.xmcd_file
!= NULL
) {
850 // printf("%s\n", cddb_data.xmcd_file);
851 *xmcd_file
= cddb_data
.xmcd_file
;
861 static char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
864 ptr
= strstr(line
, "DTITLE=");
867 album
= strstr(ptr
, "/");
870 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
871 if (cd_info
->album
== NULL
) {
874 strcpy(cd_info
->album
, album
+ 2);
877 cd_info
->artist
= malloc(strlen(ptr
) + 1);
878 if (cd_info
->artist
== NULL
) {
881 strcpy(cd_info
->artist
, ptr
);
886 static char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
889 ptr
= strstr(line
, "DGENRE=");
892 cd_info
->genre
= malloc(strlen(ptr
)+1);
893 if (cd_info
->genre
== NULL
) {
896 strcpy(cd_info
->genre
, ptr
);
901 static char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
903 unsigned int track_nb
;
904 unsigned long sec
, off
;
906 ptr
= strstr(line
, "TTITLE");
909 // Here we point to the track number
910 track_nb
= atoi(ptr
);
911 ptr
= strstr(ptr
, "=");
916 sec
= cdtoc
[track_nb
].frame
;
917 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
919 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
920 (unsigned int) (off
/ (60 * 75)),
921 (unsigned int) ((off
/ 75) % 60),
922 (unsigned int) (off
% 75),
928 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
930 cd_info_t
*cd_info
= NULL
;
933 unsigned int audiolen
;
934 if (xmcd_file
== NULL
)
937 cd_info
= cd_info_new();
938 if (cd_info
== NULL
) {
942 length
= strlen(xmcd_file
);
944 while (ptr
!= NULL
&& pos
< length
) {
947 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
949 if (ptr2
[0] == '\0') {
955 // Search for the album title
956 if (xmcd_parse_dtitle(cd_info
, ptr
))
958 // Search for the genre
959 else if (xmcd_parse_dgenre(cd_info
, ptr
))
961 // Search for a track title
962 else if (xmcd_parse_ttitle(cd_info
, ptr
))
963 audiolen
++; // <-- audiolen++ to shut up gcc warning
967 pos
= (ptr2
+ 1) - ptr
;
971 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
972 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
973 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
974 cd_info
->msec
= (unsigned int) (audiolen
% 75);