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>
71 #include "osdep/osdep.h"
77 #include "libavutil/common.h"
79 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
80 #define DEFAULT_CACHE_DIR "/.cddb/"
82 static cd_toc_t cdtoc
[100];
83 static int cdtoc_last_track
;
85 static 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_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToReadTOC
);
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];
113 #elif defined(__OS2__)
114 UCHAR auchParamDisk
[4] = {'C', 'D', '0', '1'};
122 BYTE bLeadOutReserved
;
123 } __attribute__((packed
)) sDataDisk
;
128 } __attribute__((packed
)) sParamTrack
= {{'C', 'D', '0', '1'},};
136 } __attribute__((packed
)) sDataTrack
;
144 rc
= DosOpen(dev
, &hcd
, &ulAction
, 0, FILE_NORMAL
,
145 OPEN_ACTION_OPEN_IF_EXISTS
| OPEN_ACTION_FAIL_IF_NEW
,
146 OPEN_ACCESS_READONLY
| OPEN_SHARE_DENYNONE
| OPEN_FLAGS_DASD
,
149 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToReadTOC
);
153 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIODISK
,
154 auchParamDisk
, sizeof(auchParamDisk
), &ulParamLen
,
155 &sDataDisk
, sizeof(sDataDisk
), &ulDataLen
);
157 first
= sDataDisk
.bFirstTrack
- 1;
158 last
= sDataDisk
.bLastTrack
;
159 for (i
= first
; i
<= last
; i
++) {
161 sDataTrack
.bStartM
= sDataDisk
.bLeadOutM
;
162 sDataTrack
.bStartS
= sDataDisk
.bLeadOutS
;
163 sDataTrack
.bStartF
= sDataDisk
.bLeadOutF
;
165 sParamTrack
.bTrack
= i
+ 1;
166 rc
= DosDevIOCtl(hcd
, IOCTL_CDROMAUDIO
, CDROMAUDIO_GETAUDIOTRACK
,
167 &sParamTrack
, sizeof(sParamTrack
), &ulParamLen
,
168 &sDataTrack
, sizeof(sDataTrack
), &ulDataLen
);
173 cdtoc
[i
].min
= sDataTrack
.bStartM
;
174 cdtoc
[i
].sec
= sDataTrack
.bStartS
;
175 cdtoc
[i
].frame
= sDataTrack
.bStartF
;
182 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToReadTOC
);
187 drive
= open(dev
, O_RDONLY
| O_NONBLOCK
);
192 #if defined(__linux__) || defined(__bsdi__)
194 struct cdrom_tochdr tochdr
;
195 ioctl(drive
, CDROMREADTOCHDR
, &tochdr
);
196 first
= tochdr
.cdth_trk0
- 1; last
= tochdr
.cdth_trk1
;
198 for (i
= first
; i
<= last
; i
++) {
199 struct cdrom_tocentry tocentry
;
200 tocentry
.cdte_track
= (i
== last
) ? 0xAA : i
+ 1;
201 tocentry
.cdte_format
= CDROM_MSF
;
202 ioctl(drive
, CDROMREADTOCENTRY
, &tocentry
);
203 cdtoc
[i
].min
= tocentry
.cdte_addr
.msf
.minute
;
204 cdtoc
[i
].sec
= tocentry
.cdte_addr
.msf
.second
;
205 cdtoc
[i
].frame
= tocentry
.cdte_addr
.msf
.frame
;
207 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
209 struct ioc_toc_header tochdr
;
210 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
211 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
213 for (i
= first
; i
<= last
; i
++) {
214 struct ioc_read_toc_single_entry tocentry
;
215 tocentry
.track
= (i
== last
) ? 0xAA : i
+ 1;
216 tocentry
.address_format
= CD_MSF_FORMAT
;
217 ioctl(drive
, CDIOREADTOCENTRY
, &tocentry
);
218 cdtoc
[i
].min
= tocentry
.entry
.addr
.msf
.minute
;
219 cdtoc
[i
].sec
= tocentry
.entry
.addr
.msf
.second
;
220 cdtoc
[i
].frame
= tocentry
.entry
.addr
.msf
.frame
;
222 #elif defined(__NetBSD__) || defined(__OpenBSD__)
224 struct ioc_toc_header tochdr
;
225 ioctl(drive
, CDIOREADTOCHEADER
, &tochdr
);
226 first
= tochdr
.starting_track
- 1; last
= tochdr
.ending_track
;
228 for (i
= first
; i
<= last
; i
++) {
229 struct ioc_read_toc_entry tocentry
;
230 struct cd_toc_entry toc_buffer
;
231 tocentry
.starting_track
= (i
== last
) ? 0xAA : i
+ 1;
232 tocentry
.address_format
= CD_MSF_FORMAT
;
233 tocentry
.data
= &toc_buffer
;
234 tocentry
.data_len
= sizeof(toc_buffer
);
235 ioctl(drive
, CDIOREADTOCENTRYS
, &tocentry
);
236 cdtoc
[i
].min
= toc_buffer
.addr
.msf
.minute
;
237 cdtoc
[i
].sec
= toc_buffer
.addr
.msf
.second
;
238 cdtoc
[i
].frame
= toc_buffer
.addr
.msf
.frame
;
240 #elif defined(__APPLE__) || defined(__DARWIN__)
242 dk_cd_read_toc_t tochdr
;
244 uint8_t buf2
[100 * sizeof(CDTOCDescriptor
) + sizeof(CDTOC
)];
245 memset(&tochdr
, 0, sizeof(tochdr
));
246 tochdr
.bufferLength
= sizeof(buf
);
247 tochdr
.buffer
= &buf
;
248 if (!ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
249 && tochdr
.bufferLength
== sizeof(buf
)) {
254 memset(&tochdr
, 0, sizeof(tochdr
));
255 tochdr
.bufferLength
= sizeof(buf2
);
256 tochdr
.buffer
= &buf2
;
257 tochdr
.format
= kCDTOCFormatTOC
;
258 if (ioctl(drive
, DKIOCCDREADTOC
, &tochdr
)
259 || tochdr
.bufferLength
< sizeof(CDTOC
))
263 CDTOC
*cdToc
= (CDTOC
*)buf2
;
264 CDTrackInfo lastTrack
;
265 dk_cd_read_track_info_t trackInfoParams
;
266 for (i
= first
; i
< last
; ++i
) {
267 CDMSF msf
= CDConvertTrackNumberToMSF(i
+ 1, cdToc
);
268 cdtoc
[i
].min
= msf
.minute
;
269 cdtoc
[i
].sec
= msf
.second
;
270 cdtoc
[i
].frame
= msf
.frame
;
272 memset(&trackInfoParams
, 0, sizeof(trackInfoParams
));
273 trackInfoParams
.addressType
= kCDTrackInfoAddressTypeTrackNumber
;
274 trackInfoParams
.bufferLength
= sizeof(lastTrack
);
275 trackInfoParams
.address
= last
;
276 trackInfoParams
.buffer
= &lastTrack
;
277 if (!ioctl(drive
, DKIOCCDREADTRACKINFO
, &trackInfoParams
)) {
278 CDMSF msf
= CDConvertLBAToMSF(be2me_32(lastTrack
.trackStartAddress
)
279 + be2me_32(lastTrack
.trackSize
));
280 cdtoc
[last
].min
= msf
.minute
;
281 cdtoc
[last
].sec
= msf
.second
;
282 cdtoc
[last
].frame
= msf
.frame
;
289 for (i
= first
; i
<= last
; i
++)
290 cdtoc
[i
].frame
+= (cdtoc
[i
].min
* 60 + cdtoc
[i
].sec
) * 75;
295 \brief Reads TOC from CD in the given device and prints the number of tracks
296 and the length of each track in minute:second:frame format.
297 \param *dev the device to analyse
298 \return if the command line -identify is given, returns the last track of
299 the TOC or -1 if the TOC can't be read,
300 otherwise just returns 0 and let cddb_resolve the TOC
302 int cdd_identify(const char *dev
)
304 cdtoc_last_track
= 0;
305 if (mp_msg_test(MSGT_IDENTIFY
, MSGL_INFO
)) {
306 int i
, min
, sec
, frame
;
307 cdtoc_last_track
= read_toc(dev
);
308 if (cdtoc_last_track
< 0) {
309 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice
,
313 mp_msg(MSGT_GLOBAL
, MSGL_INFO
, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track
);
314 for (i
= 1; i
<= cdtoc_last_track
; i
++) {
315 frame
= cdtoc
[i
].frame
- cdtoc
[i
-1].frame
;
320 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
,
321 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i
, min
, sec
, frame
);
324 return cdtoc_last_track
;
327 static unsigned int cddb_sum(int n
)
339 static unsigned long cddb_discid(int tot_trks
)
341 unsigned int i
, t
= 0, n
= 0;
344 while (i
< (unsigned int)tot_trks
) {
345 n
= n
+ cddb_sum((cdtoc
[i
].min
* 60) + cdtoc
[i
].sec
);
348 t
= ((cdtoc
[tot_trks
].min
* 60) + cdtoc
[tot_trks
].sec
) -
349 ((cdtoc
[0].min
* 60) + cdtoc
[0].sec
);
350 return (n
% 0xff) << 24 | t
<< 8 | tot_trks
;
355 static int cddb_http_request(char *command
,
356 int (*reply_parser
)(HTTP_header_t
*, cddb_data_t
*),
357 cddb_data_t
*cddb_data
)
362 HTTP_header_t
*http_hdr
;
364 if (reply_parser
== NULL
|| command
== NULL
|| cddb_data
== NULL
)
367 sprintf(request
, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
368 cddb_data
->freedb_server
, command
, cddb_data
->cddb_hello
,
369 cddb_data
->freedb_proto_level
);
370 mp_msg(MSGT_OPEN
, MSGL_INFO
,"Request[%s]\n", request
);
372 url
= url_new(request
);
374 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NotAValidURL
);
378 fd
= http_send_request(url
,0);
380 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
381 MSGTR_MPDEMUX_CDDB_FailedToSendHTTPRequest
);
385 http_hdr
= http_read_response(fd
);
386 if (http_hdr
== NULL
) {
387 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
388 MSGTR_MPDEMUX_CDDB_FailedToReadHTTPResponse
);
392 http_debug_hdr(http_hdr
);
393 mp_msg(MSGT_OPEN
, MSGL_INFO
,"body=[%s]\n", http_hdr
->body
);
395 switch (http_hdr
->status_code
) {
397 ret
= reply_parser(http_hdr
, cddb_data
);
400 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_HTTPErrorNOTFOUND
);
403 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_HTTPErrorUnknown
);
412 static int cddb_read_cache(cddb_data_t
*cddb_data
)
419 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
422 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
424 file_fd
= open(file_name
, O_RDONLY
| O_BINARY
);
426 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NoCacheFound
);
430 ret
= fstat(file_fd
, &stats
);
435 file_size
= stats
.st_size
< UINT_MAX
? stats
.st_size
: UINT_MAX
- 1;
438 cddb_data
->xmcd_file
= malloc(file_size
+ 1);
439 if (cddb_data
->xmcd_file
== NULL
) {
440 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MemAllocFailed
);
444 cddb_data
->xmcd_file_size
= read(file_fd
, cddb_data
->xmcd_file
, file_size
);
445 if (cddb_data
->xmcd_file_size
!= file_size
) {
446 mp_msg(MSGT_DEMUX
, MSGL_WARN
,
447 MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenRead
);
451 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = 0;
458 static int cddb_write_cache(cddb_data_t
*cddb_data
)
460 // We have the file, save it for cache.
461 struct stat file_stat
;
466 if (cddb_data
== NULL
|| cddb_data
->cache_dir
== NULL
)
469 // Check if the CDDB cache dir exist
470 ret
= stat(cddb_data
->cache_dir
, &file_stat
);
472 // Directory not present, create it.
473 ret
= mkdir(cddb_data
->cache_dir
, 0755);
475 if (ret
< 0 && errno
!= EEXIST
) {
480 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
481 MSGTR_MPDEMUX_CDDB_FailedToCreateDirectory
,
482 cddb_data
->cache_dir
);
487 sprintf(file_name
, "%s%08lx", cddb_data
->cache_dir
, cddb_data
->disc_id
);
489 file_fd
= creat(file_name
, S_IRUSR
| S_IWUSR
);
495 wrote
= write(file_fd
, cddb_data
->xmcd_file
, cddb_data
->xmcd_file_size
);
501 if ((unsigned int) wrote
!= cddb_data
->xmcd_file_size
) {
502 mp_msg(MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenWritten
);
512 static int cddb_read_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
514 unsigned long disc_id
;
516 char *ptr
= NULL
, *ptr2
= NULL
;
519 if (http_hdr
== NULL
|| cddb_data
== NULL
)
522 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
524 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
530 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx", &status
,
533 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
536 // Check if it's a xmcd database file
537 ptr
= strstr(http_hdr
->body
, "# xmcd");
539 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
540 MSGTR_MPDEMUX_CDDB_InvalidXMCDDatabaseReturned
);
544 // Ok found the beginning of the file
546 ptr2
= strstr(ptr
, "\n.\r\n");
548 ptr2
= strstr(ptr
, "\n.\n");
552 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, "Unable to find '.'\n");
553 ptr2
= ptr
+ strlen(ptr
); //return -1;
557 if (http_hdr
->body_size
< (unsigned int)(ptr2
- ptr
)) {
558 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_UnexpectedFIXME
);
561 cddb_data
->xmcd_file
= ptr
;
562 cddb_data
->xmcd_file_size
= ptr2
- ptr
;
563 cddb_data
->xmcd_file
[cddb_data
->xmcd_file_size
] = '\0';
564 return cddb_write_cache(cddb_data
);
566 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
571 static int cddb_request_titles(cddb_data_t
*cddb_data
)
574 sprintf(command
, "cddb+read+%s+%08lx",
575 cddb_data
->category
, cddb_data
->disc_id
);
576 return cddb_http_request(command
, cddb_read_parse
, cddb_data
);
579 static int cddb_parse_matches_list(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
581 char album_title
[100];
585 ptr
= strstr(http_hdr
->body
, "\n");
587 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_UnableToFindEOL
);
591 // We have a list of exact/inexact matches, so which one do we use?
592 // So let's take the first one.
593 ret
= sscanf(ptr
, "%99s %08lx %99s", cddb_data
->category
,
594 &(cddb_data
->disc_id
), album_title
);
596 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
599 ptr
= strstr(http_hdr
->body
, album_title
);
603 ptr2
= strstr(ptr
, "\n");
605 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
609 len
= FFMIN(sizeof(album_title
) - 1, len
);
610 strncpy(album_title
, ptr
, len
);
611 album_title
[len
]='\0';
613 mp_msg(MSGT_DEMUX
, MSGL_STATUS
, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle
,
618 static int cddb_query_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
620 char album_title
[100];
624 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
626 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
633 ret
= sscanf(http_hdr
->body
, "%d %99s %08lx %99s", &status
,
634 cddb_data
->category
, &(cddb_data
->disc_id
), album_title
);
636 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
639 ptr
= strstr(http_hdr
->body
, album_title
);
643 ptr2
= strstr(ptr
, "\n");
645 len
= (http_hdr
->body_size
)-(ptr
-(http_hdr
->body
));
649 len
= FFMIN(sizeof(album_title
) - 1, len
);
650 strncpy(album_title
, ptr
, len
);
651 album_title
[len
]='\0';
653 mp_msg(MSGT_DEMUX
, MSGL_STATUS
,
654 MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle
, album_title
);
655 return cddb_request_titles(cddb_data
);
658 mp_msg(MSGT_DEMUX
, MSGL_WARN
, MSGTR_MPDEMUX_CDDB_AlbumNotFound
);
661 // Found exact matches, list follows
662 cddb_parse_matches_list(http_hdr
, cddb_data
);
663 return cddb_request_titles(cddb_data
);
665 body=[210 Found exact matches, list follows (until terminating `.')
666 misc c711930d Santana / Supernatural
667 rock c711930d Santana / Supernatural
668 blues c711930d Santana / Supernatural
672 // Found inexact matches, list follows
673 cddb_parse_matches_list(http_hdr
, cddb_data
);
674 return cddb_request_titles(cddb_data
);
676 mp_msg(MSGT_DEMUX
, MSGL_FIXME
,
677 MSGTR_MPDEMUX_CDDB_ServerReturnsCommandSyntaxErr
);
680 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
685 static int cddb_proto_level_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
691 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
693 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
699 ptr
= strstr(http_hdr
->body
, "max proto:");
701 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
704 ret
= sscanf(ptr
, "max proto: %d", &max
);
706 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
709 cddb_data
->freedb_proto_level
= max
;
712 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
717 static int cddb_get_proto_level(cddb_data_t
*cddb_data
)
719 return cddb_http_request("stat", cddb_proto_level_parse
, cddb_data
);
722 static int cddb_freedb_sites_parse(HTTP_header_t
*http_hdr
, cddb_data_t
*cddb_data
)
726 ret
= sscanf(http_hdr
->body
, "%d ", &status
);
728 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_ParseError
);
734 // TODO: Parse the sites
735 ret
= cddb_data
->anonymous
; // For gcc complaining about unused parameter.
738 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_NoSitesInfoAvailable
);
741 mp_msg(MSGT_DEMUX
, MSGL_FIXME
, MSGTR_MPDEMUX_CDDB_UnhandledCode
);
746 static int cddb_get_freedb_sites(cddb_data_t
*cddb_data
)
748 return cddb_http_request("sites", cddb_freedb_sites_parse
, cddb_data
);
751 static void cddb_create_hello(cddb_data_t
*cddb_data
)
756 if (cddb_data
->anonymous
) { // Default is anonymous
757 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
758 * We don't send current user/host name in hello to prevent spam.
759 * Software that sends this is considered spyware
760 * that most people don't like.
762 user_name
= "anonymous";
763 strcpy(host_name
, "localhost");
765 if (gethostname(host_name
, 50) < 0) {
766 strcpy(host_name
, "localhost");
768 user_name
= getenv("LOGNAME");
770 sprintf(cddb_data
->cddb_hello
, "&hello=%s+%s+%s+%s",
771 user_name
, host_name
, "MPlayer", VERSION
);
774 static int cddb_retrieve(cddb_data_t
*cddb_data
)
776 char offsets
[1024], command
[1024];
778 unsigned int i
, time_len
;
782 for (i
= 0; i
< cddb_data
->tracks
; i
++) {
783 ptr
+= sprintf(ptr
, "%d+", cdtoc
[i
].frame
);
784 if (ptr
-offsets
> sizeof offsets
- 40) break;
787 time_len
= (cdtoc
[cddb_data
->tracks
].frame
)/75;
789 cddb_data
->freedb_server
= DEFAULT_FREEDB_SERVER
;
790 cddb_data
->freedb_proto_level
= 1;
791 cddb_data
->xmcd_file
= NULL
;
793 cddb_create_hello(cddb_data
);
794 if (cddb_get_proto_level(cddb_data
) < 0) {
795 mp_msg(MSGT_DEMUX
, MSGL_ERR
,
796 MSGTR_MPDEMUX_CDDB_FailedToGetProtocolLevel
);
800 //cddb_get_freedb_sites(&cddb_data);
802 sprintf(command
, "cddb+query+%08lx+%d+%s%d", cddb_data
->disc_id
,
803 cddb_data
->tracks
, offsets
, time_len
);
804 ret
= cddb_http_request(command
, cddb_query_parse
, cddb_data
);
808 if (cddb_data
->cache_dir
!= NULL
) {
809 free(cddb_data
->cache_dir
);
814 int cddb_resolve(const char *dev
, char **xmcd_file
)
816 char cddb_cache_dir
[] = DEFAULT_CACHE_DIR
;
817 char *home_dir
= NULL
;
818 cddb_data_t cddb_data
;
820 if (cdtoc_last_track
<= 0) {
821 cdtoc_last_track
= read_toc(dev
);
822 if (cdtoc_last_track
< 0) {
823 mp_msg(MSGT_OPEN
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice
,
828 cddb_data
.tracks
= cdtoc_last_track
;
829 cddb_data
.disc_id
= cddb_discid(cddb_data
.tracks
);
830 cddb_data
.anonymous
= 1; // Don't send user info by default
832 mp_msg(MSGT_IDENTIFY
, MSGL_INFO
, "ID_CDDB_DISCID=%08lx\n",
835 // Check if there is a CD in the drive
836 // FIXME: That's not really a good way to check
837 if (cddb_data
.disc_id
== 0) {
838 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MPDEMUX_CDDB_NoCDInDrive
);
842 home_dir
= getenv("HOME");
844 if (home_dir
== NULL
)
845 home_dir
= getenv("USERPROFILE");
846 if (home_dir
== NULL
)
847 home_dir
= getenv("HOMEPATH");
848 // Last resort, store the cddb cache in the mplayer directory
849 if (home_dir
== NULL
)
850 home_dir
= (char *)get_path("");
852 if (home_dir
== NULL
) {
853 cddb_data
.cache_dir
= NULL
;
855 cddb_data
.cache_dir
= malloc(strlen(home_dir
)
856 + strlen(cddb_cache_dir
) + 1);
857 if (cddb_data
.cache_dir
== NULL
) {
858 mp_msg(MSGT_DEMUX
, MSGL_ERR
, MSGTR_MemAllocFailed
);
861 sprintf(cddb_data
.cache_dir
, "%s%s", home_dir
, cddb_cache_dir
);
864 // Check for a cached file
865 if (cddb_read_cache(&cddb_data
) < 0) {
867 if (cddb_retrieve(&cddb_data
) < 0) {
872 if (cddb_data
.xmcd_file
!= NULL
) {
873 // printf("%s\n", cddb_data.xmcd_file);
874 *xmcd_file
= cddb_data
.xmcd_file
;
884 static char *xmcd_parse_dtitle(cd_info_t
*cd_info
, char *line
)
887 ptr
= strstr(line
, "DTITLE=");
890 album
= strstr(ptr
, "/");
893 cd_info
->album
= malloc(strlen(album
+ 2) + 1);
894 if (cd_info
->album
== NULL
) {
897 strcpy(cd_info
->album
, album
+ 2);
900 cd_info
->artist
= malloc(strlen(ptr
) + 1);
901 if (cd_info
->artist
== NULL
) {
904 strcpy(cd_info
->artist
, ptr
);
909 static char *xmcd_parse_dgenre(cd_info_t
*cd_info
, char *line
)
912 ptr
= strstr(line
, "DGENRE=");
915 cd_info
->genre
= malloc(strlen(ptr
)+1);
916 if (cd_info
->genre
== NULL
) {
919 strcpy(cd_info
->genre
, ptr
);
924 static char *xmcd_parse_ttitle(cd_info_t
*cd_info
, char *line
)
926 unsigned int track_nb
;
927 unsigned long sec
, off
;
929 ptr
= strstr(line
, "TTITLE");
932 // Here we point to the track number
933 track_nb
= atoi(ptr
);
934 ptr
= strstr(ptr
, "=");
939 sec
= cdtoc
[track_nb
].frame
;
940 off
= cdtoc
[track_nb
+ 1].frame
- sec
+ 1;
942 cd_info_add_track(cd_info
, ptr
, track_nb
+ 1,
943 (unsigned int) (off
/ (60 * 75)),
944 (unsigned int) ((off
/ 75) % 60),
945 (unsigned int) (off
% 75),
951 cd_info_t
*cddb_parse_xmcd(char *xmcd_file
)
953 cd_info_t
*cd_info
= NULL
;
956 unsigned int audiolen
;
957 if (xmcd_file
== NULL
)
960 cd_info
= cd_info_new();
961 if (cd_info
== NULL
) {
965 length
= strlen(xmcd_file
);
967 while (ptr
!= NULL
&& pos
< length
) {
970 while(ptr2
[0] != '\0' && ptr2
[0] != '\r' && ptr2
[0] != '\n')
972 if (ptr2
[0] == '\0') {
978 // Search for the album title
979 if (xmcd_parse_dtitle(cd_info
, ptr
))
981 // Search for the genre
982 else if (xmcd_parse_dgenre(cd_info
, ptr
))
984 // Search for a track title
985 else if (xmcd_parse_ttitle(cd_info
, ptr
))
986 audiolen
++; // <-- audiolen++ to shut up gcc warning
990 pos
= (ptr2
+ 1) - ptr
;
994 audiolen
= cdtoc
[cd_info
->nb_tracks
].frame
-cdtoc
[0].frame
;
995 cd_info
->min
= (unsigned int) (audiolen
/ (60 * 75));
996 cd_info
->sec
= (unsigned int) ((audiolen
/ 75) % 60);
997 cd_info
->msec
= (unsigned int) (audiolen
% 75);