Fix vf_tcdump's compilation
[mplayer/kovensky.git] / stream / stream_cddb.c
blob719eb05754e08b82d19a43fb3ea8c87f5b47418e
1 /*
2 * CDDB HTTP protocol
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.
29 #include "config.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <limits.h>
39 #if defined(__MINGW32__) || defined(__CYGWIN__)
40 #ifdef __MINGW32__
41 #define mkdir(a,b) mkdir(a)
42 #endif
43 #include <windows.h>
44 #if HAVE_WINSOCK2_H
45 #include <winsock2.h>
46 #endif
47 #else
48 #include <netdb.h>
49 #include <sys/ioctl.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #include "mp_msg.h"
56 #if defined(__linux__)
57 #include <linux/cdrom.h>
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
59 #include <sys/cdio.h>
60 #elif defined(__MINGW32__) || defined(__CYGWIN__)
61 #include <ddk/ntddcdrm.h>
62 #elif defined(__bsdi__)
63 #include <dvd.h>
64 #elif defined(__APPLE__) || defined(__DARWIN__)
65 #include <IOKit/storage/IOCDTypes.h>
66 #include <IOKit/storage/IOCDMediaBSDClient.h>
67 #include "mpbswap.h"
68 #endif
70 #include "osdep/osdep.h"
72 #include "cdd.h"
73 #include "version.h"
74 #include "stream.h"
75 #include "network.h"
76 #include "libavutil/common.h"
78 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
79 #define DEFAULT_CACHE_DIR "/.cddb/"
81 static cd_toc_t cdtoc[100];
82 static int cdtoc_last_track;
84 static int read_toc(const char *dev)
86 int first = 0, last = -1;
87 int i;
88 #if defined(__MINGW32__) || defined(__CYGWIN__)
89 HANDLE drive;
90 DWORD r;
91 CDROM_TOC toc;
92 char device[10];
94 sprintf(device, "\\\\.\\%s", dev);
95 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
96 OPEN_EXISTING, 0, 0);
98 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
99 sizeof(CDROM_TOC), &r, 0)) {
100 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
101 return 0;
104 first = toc.FirstTrack - 1; last = toc.LastTrack;
105 for (i = first; i <= last; i++) {
106 cdtoc[i].min = toc.TrackData[i].Address[1];
107 cdtoc[i].sec = toc.TrackData[i].Address[2];
108 cdtoc[i].frame = toc.TrackData[i].Address[3];
110 CloseHandle(drive);
112 #elif defined(__OS2__)
113 UCHAR auchParamDisk[4] = {'C', 'D', '0', '1'};
115 struct {
116 BYTE bFirstTrack;
117 BYTE bLastTrack;
118 BYTE bLeadOutF;
119 BYTE bLeadOutS;
120 BYTE bLeadOutM;
121 BYTE bLeadOutReserved;
122 } __attribute__((packed)) sDataDisk;
124 struct {
125 UCHAR auchSign[4];
126 BYTE bTrack;
127 } __attribute__((packed)) sParamTrack = {{'C', 'D', '0', '1'},};
129 struct {
130 BYTE bStartF;
131 BYTE bStartS;
132 BYTE bStartM;
133 BYTE bStartReserved;
134 BYTE bControlInfo;
135 } __attribute__((packed)) sDataTrack;
137 HFILE hcd;
138 ULONG ulAction;
139 ULONG ulParamLen;
140 ULONG ulDataLen;
141 ULONG rc;
143 rc = DosOpen(dev, &hcd, &ulAction, 0, FILE_NORMAL,
144 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
145 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
146 NULL);
147 if (rc) {
148 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
149 return -1;
152 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIODISK,
153 auchParamDisk, sizeof(auchParamDisk), &ulParamLen,
154 &sDataDisk, sizeof(sDataDisk), &ulDataLen);
155 if (!rc) {
156 first = sDataDisk.bFirstTrack - 1;
157 last = sDataDisk.bLastTrack;
158 for (i = first; i <= last; i++) {
159 if (i == last) {
160 sDataTrack.bStartM = sDataDisk.bLeadOutM;
161 sDataTrack.bStartS = sDataDisk.bLeadOutS;
162 sDataTrack.bStartF = sDataDisk.bLeadOutF;
163 } else {
164 sParamTrack.bTrack = i + 1;
165 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
166 &sParamTrack, sizeof(sParamTrack), &ulParamLen,
167 &sDataTrack, sizeof(sDataTrack), &ulDataLen);
168 if (rc)
169 break;
172 cdtoc[i].min = sDataTrack.bStartM;
173 cdtoc[i].sec = sDataTrack.bStartS;
174 cdtoc[i].frame = sDataTrack.bStartF;
178 DosClose(hcd);
180 if (rc) {
181 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
182 return -1;
184 #else
185 int drive;
186 drive = open(dev, O_RDONLY | O_NONBLOCK);
187 if (drive < 0) {
188 return drive;
191 #if defined(__linux__) || defined(__bsdi__)
193 struct cdrom_tochdr tochdr;
194 ioctl(drive, CDROMREADTOCHDR, &tochdr);
195 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
197 for (i = first; i <= last; i++) {
198 struct cdrom_tocentry tocentry;
199 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
200 tocentry.cdte_format = CDROM_MSF;
201 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
202 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
203 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
204 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
206 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
208 struct ioc_toc_header tochdr;
209 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
210 first = tochdr.starting_track - 1; last = tochdr.ending_track;
212 for (i = first; i <= last; i++) {
213 struct ioc_read_toc_single_entry tocentry;
214 tocentry.track = (i == last) ? 0xAA : i + 1;
215 tocentry.address_format = CD_MSF_FORMAT;
216 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
217 cdtoc[i].min = tocentry.entry.addr.msf.minute;
218 cdtoc[i].sec = tocentry.entry.addr.msf.second;
219 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
221 #elif defined(__NetBSD__) || defined(__OpenBSD__)
223 struct ioc_toc_header tochdr;
224 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
225 first = tochdr.starting_track - 1; last = tochdr.ending_track;
227 for (i = first; i <= last; i++) {
228 struct ioc_read_toc_entry tocentry;
229 struct cd_toc_entry toc_buffer;
230 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
231 tocentry.address_format = CD_MSF_FORMAT;
232 tocentry.data = &toc_buffer;
233 tocentry.data_len = sizeof(toc_buffer);
234 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
235 cdtoc[i].min = toc_buffer.addr.msf.minute;
236 cdtoc[i].sec = toc_buffer.addr.msf.second;
237 cdtoc[i].frame = toc_buffer.addr.msf.frame;
239 #elif defined(__APPLE__) || defined(__DARWIN__)
241 dk_cd_read_toc_t tochdr;
242 uint8_t buf[4];
243 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
244 memset(&tochdr, 0, sizeof(tochdr));
245 tochdr.bufferLength = sizeof(buf);
246 tochdr.buffer = &buf;
247 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
248 && tochdr.bufferLength == sizeof(buf)) {
249 first = buf[2] - 1;
250 last = buf[3];
252 if (last >= 0) {
253 memset(&tochdr, 0, sizeof(tochdr));
254 tochdr.bufferLength = sizeof(buf2);
255 tochdr.buffer = &buf2;
256 tochdr.format = kCDTOCFormatTOC;
257 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
258 || tochdr.bufferLength < sizeof(CDTOC))
259 last = -1;
261 if (last >= 0) {
262 CDTOC *cdToc = (CDTOC *)buf2;
263 CDTrackInfo lastTrack;
264 dk_cd_read_track_info_t trackInfoParams;
265 for (i = first; i < last; ++i) {
266 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
267 cdtoc[i].min = msf.minute;
268 cdtoc[i].sec = msf.second;
269 cdtoc[i].frame = msf.frame;
271 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
272 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
273 trackInfoParams.bufferLength = sizeof(lastTrack);
274 trackInfoParams.address = last;
275 trackInfoParams.buffer = &lastTrack;
276 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
277 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
278 + be2me_32(lastTrack.trackSize));
279 cdtoc[last].min = msf.minute;
280 cdtoc[last].sec = msf.second;
281 cdtoc[last].frame = msf.frame;
285 #endif
286 close(drive);
287 #endif
288 for (i = first; i <= last; i++)
289 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
290 return last;
294 \brief Reads TOC from CD in the given device and prints the number of tracks
295 and the length of each track in minute:second:frame format.
296 \param *dev the device to analyse
297 \return if the command line -identify is given, returns the last track of
298 the TOC or -1 if the TOC can't be read,
299 otherwise just returns 0 and let cddb_resolve the TOC
301 int cdd_identify(const char *dev)
303 cdtoc_last_track = 0;
304 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
305 int i, min, sec, frame;
306 cdtoc_last_track = read_toc(dev);
307 if (cdtoc_last_track < 0) {
308 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
309 return -1;
311 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
312 for (i = 1; i <= cdtoc_last_track; i++) {
313 frame = cdtoc[i].frame - cdtoc[i-1].frame;
314 sec = frame / 75;
315 frame -= sec * 75;
316 min = sec / 60;
317 sec -= min * 60;
318 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
319 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
322 return cdtoc_last_track;
325 static unsigned int cddb_sum(int n)
327 unsigned int ret;
329 ret = 0;
330 while (n > 0) {
331 ret += (n % 10);
332 n /= 10;
334 return ret;
337 static unsigned long cddb_discid(int tot_trks)
339 unsigned int i, t = 0, n = 0;
341 i = 0;
342 while (i < (unsigned int)tot_trks) {
343 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
344 i++;
346 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
347 ((cdtoc[0].min * 60) + cdtoc[0].sec);
348 return (n % 0xff) << 24 | t << 8 | tot_trks;
353 static int cddb_http_request(char *command,
354 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
355 cddb_data_t *cddb_data)
357 char request[4096];
358 int fd, ret = 0;
359 URL_t *url;
360 HTTP_header_t *http_hdr;
362 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
363 return -1;
365 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
366 cddb_data->freedb_server, command, cddb_data->cddb_hello,
367 cddb_data->freedb_proto_level);
368 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
370 url = url_new(request);
371 if (url == NULL) {
372 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
373 return -1;
376 fd = http_send_request(url,0);
377 if (fd < 0) {
378 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
379 return -1;
382 http_hdr = http_read_response(fd);
383 if (http_hdr == NULL) {
384 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
385 return -1;
388 http_debug_hdr(http_hdr);
389 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
391 switch (http_hdr->status_code) {
392 case 200:
393 ret = reply_parser(http_hdr, cddb_data);
394 break;
395 case 400:
396 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
397 break;
398 default:
399 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
402 http_free(http_hdr);
403 url_free(url);
405 return ret;
408 static int cddb_read_cache(cddb_data_t *cddb_data)
410 char file_name[100];
411 struct stat stats;
412 int file_fd, ret;
413 size_t file_size;
415 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
416 return -1;
418 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
420 file_fd = open(file_name, O_RDONLY | O_BINARY);
421 if (file_fd < 0) {
422 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
423 return -1;
426 ret = fstat(file_fd, &stats);
427 if (ret < 0) {
428 perror("fstat");
429 file_size = 4096;
430 } else {
431 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
434 cddb_data->xmcd_file = malloc(file_size + 1);
435 if (cddb_data->xmcd_file == NULL) {
436 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
437 close(file_fd);
438 return -1;
440 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
441 if (cddb_data->xmcd_file_size != file_size) {
442 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
443 close(file_fd);
444 return -1;
446 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
448 close(file_fd);
450 return 0;
453 static int cddb_write_cache(cddb_data_t *cddb_data)
455 // We have the file, save it for cache.
456 struct stat file_stat;
457 char file_name[100];
458 int file_fd, ret;
459 int wrote = 0;
461 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
462 return -1;
464 // Check if the CDDB cache dir exist
465 ret = stat(cddb_data->cache_dir, &file_stat);
466 if (ret < 0) {
467 // Directory not present, create it.
468 ret = mkdir(cddb_data->cache_dir, 0755);
469 #ifdef __MINGW32__
470 if (ret < 0 && errno != EEXIST) {
471 #else
472 if (ret < 0) {
473 #endif
474 perror("mkdir");
475 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
476 cddb_data->cache_dir);
477 return -1;
481 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
483 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
484 if (file_fd < 0) {
485 perror("create");
486 return -1;
489 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
490 if (wrote < 0) {
491 perror("write");
492 close(file_fd);
493 return -1;
495 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
496 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
497 close(file_fd);
498 return -1;
501 close(file_fd);
503 return 0;
506 static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
508 unsigned long disc_id;
509 char category[100];
510 char *ptr = NULL, *ptr2 = NULL;
511 int ret, status;
513 if (http_hdr == NULL || cddb_data == NULL)
514 return -1;
516 ret = sscanf(http_hdr->body, "%d ", &status);
517 if (ret != 1) {
518 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
519 return -1;
522 switch (status) {
523 case 210:
524 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
525 category, &disc_id);
526 if (ret != 3) {
527 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
528 return -1;
530 // Check if it's a xmcd database file
531 ptr = strstr(http_hdr->body, "# xmcd");
532 if (ptr == NULL) {
533 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
534 "Invalid xmcd database file returned.\n");
535 return -1;
537 ptr = strdup(ptr);
538 // Ok found the beginning of the file
539 // look for the end
540 ptr2 = strstr(ptr, "\n.\r\n");
541 if (!ptr2)
542 ptr2 = strstr(ptr, "\n.\n");
543 if (ptr2) {
544 ptr2++;
545 } else {
546 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
547 ptr2 = ptr + strlen(ptr); //return -1;
549 // Ok found the end
550 // do a sanity check
551 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
552 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
553 return -1;
555 cddb_data->xmcd_file = ptr;
556 cddb_data->xmcd_file_size = ptr2 - ptr;
557 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
558 return cddb_write_cache(cddb_data);
559 default:
560 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
562 return 0;
565 static int cddb_request_titles(cddb_data_t *cddb_data)
567 char command[1024];
568 sprintf(command, "cddb+read+%s+%08lx",
569 cddb_data->category, cddb_data->disc_id);
570 return cddb_http_request(command, cddb_read_parse, cddb_data);
573 static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
575 char album_title[100];
576 char *ptr = NULL;
577 int ret;
579 ptr = strstr(http_hdr->body, "\n");
580 if (ptr == NULL) {
581 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
582 return -1;
584 ptr++;
585 // We have a list of exact/inexact matches, so which one do we use?
586 // So let's take the first one.
587 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
588 &(cddb_data->disc_id), album_title);
589 if (ret != 3) {
590 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
591 return -1;
593 ptr = strstr(http_hdr->body, album_title);
594 if (ptr != NULL) {
595 char *ptr2;
596 int len;
597 ptr2 = strstr(ptr, "\n");
598 if (ptr2 == NULL) {
599 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
600 } else {
601 len = ptr2-ptr+1;
603 len = FFMIN(sizeof(album_title) - 1, len);
604 strncpy(album_title, ptr, len);
605 album_title[len]='\0';
607 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
608 return 0;
611 static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
613 char album_title[100];
614 char *ptr = NULL;
615 int ret, status;
617 ret = sscanf(http_hdr->body, "%d ", &status);
618 if (ret != 1) {
619 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
620 return -1;
623 switch (status) {
624 case 200:
625 // Found exact match
626 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
627 cddb_data->category, &(cddb_data->disc_id), album_title);
628 if (ret != 4) {
629 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
630 return -1;
632 ptr = strstr(http_hdr->body, album_title);
633 if (ptr != NULL) {
634 char *ptr2;
635 int len;
636 ptr2 = strstr(ptr, "\n");
637 if (ptr2 == NULL) {
638 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
639 } else {
640 len = ptr2-ptr+1;
642 len = FFMIN(sizeof(album_title) - 1, len);
643 strncpy(album_title, ptr, len);
644 album_title[len]='\0';
646 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
647 return cddb_request_titles(cddb_data);
648 case 202:
649 // No match found
650 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
651 break;
652 case 210:
653 // Found exact matches, list follows
654 cddb_parse_matches_list(http_hdr, cddb_data);
655 return cddb_request_titles(cddb_data);
657 body=[210 Found exact matches, list follows (until terminating `.')
658 misc c711930d Santana / Supernatural
659 rock c711930d Santana / Supernatural
660 blues c711930d Santana / Supernatural
663 case 211:
664 // Found inexact matches, list follows
665 cddb_parse_matches_list(http_hdr, cddb_data);
666 return cddb_request_titles(cddb_data);
667 case 500:
668 mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
669 "Server returns: Command syntax error\n");
670 break;
671 default:
672 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
674 return -1;
677 static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
679 int max;
680 int ret, status;
681 char *ptr;
683 ret = sscanf(http_hdr->body, "%d ", &status);
684 if (ret != 1) {
685 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
686 return -1;
689 switch (status) {
690 case 210:
691 ptr = strstr(http_hdr->body, "max proto:");
692 if (ptr == NULL) {
693 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
694 return -1;
696 ret = sscanf(ptr, "max proto: %d", &max);
697 if (ret != 1) {
698 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
699 return -1;
701 cddb_data->freedb_proto_level = max;
702 return 0;
703 default:
704 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
706 return -1;
709 static int cddb_get_proto_level(cddb_data_t *cddb_data)
711 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
714 static int cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
716 int ret, status;
718 ret = sscanf(http_hdr->body, "%d ", &status);
719 if (ret != 1) {
720 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
721 return -1;
724 switch (status) {
725 case 210:
726 // TODO: Parse the sites
727 ret = cddb_data->anonymous; // For gcc complaining about unused parameter.
728 return 0;
729 case 401:
730 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "No sites information available.\n");
731 break;
732 default:
733 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
735 return -1;
738 static int cddb_get_freedb_sites(cddb_data_t *cddb_data)
740 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);
743 static void cddb_create_hello(cddb_data_t *cddb_data)
745 char host_name[51];
746 char *user_name;
748 if (cddb_data->anonymous) { // Default is anonymous
749 /* Note from Eduardo PĂ©rez Ureta <eperez@it.uc3m.es> :
750 * We don't send current user/host name in hello to prevent spam.
751 * Software that sends this is considered spyware
752 * that most people don't like.
754 user_name = "anonymous";
755 strcpy(host_name, "localhost");
756 } else {
757 if (gethostname(host_name, 50) < 0) {
758 strcpy(host_name, "localhost");
760 user_name = getenv("LOGNAME");
762 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s+%s",
763 user_name, host_name, "MPlayer", VERSION);
766 static int cddb_retrieve(cddb_data_t *cddb_data)
768 char offsets[1024], command[1024];
769 char *ptr;
770 unsigned int i, time_len;
771 int ret;
773 ptr = offsets;
774 for (i = 0; i < cddb_data->tracks ; i++) {
775 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
776 if (ptr-offsets > sizeof offsets - 40) break;
778 ptr[0] = 0;
779 time_len = (cdtoc[cddb_data->tracks].frame)/75;
781 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
782 cddb_data->freedb_proto_level = 1;
783 cddb_data->xmcd_file = NULL;
785 cddb_create_hello(cddb_data);
786 if (cddb_get_proto_level(cddb_data) < 0) {
787 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
788 return -1;
791 //cddb_get_freedb_sites(&cddb_data);
793 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
794 cddb_data->tracks, offsets, time_len);
795 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
796 if (ret < 0)
797 return -1;
799 if (cddb_data->cache_dir != NULL) {
800 free(cddb_data->cache_dir);
802 return 0;
805 int cddb_resolve(const char *dev, char **xmcd_file)
807 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
808 char *home_dir = NULL;
809 cddb_data_t cddb_data;
811 if (cdtoc_last_track <= 0) {
812 cdtoc_last_track = read_toc(dev);
813 if (cdtoc_last_track < 0) {
814 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
815 return -1;
818 cddb_data.tracks = cdtoc_last_track;
819 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
820 cddb_data.anonymous = 1; // Don't send user info by default
822 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
823 cddb_data.disc_id);
825 // Check if there is a CD in the drive
826 // FIXME: That's not really a good way to check
827 if (cddb_data.disc_id == 0) {
828 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
829 return -1;
832 home_dir = getenv("HOME");
833 #ifdef __MINGW32__
834 if (home_dir == NULL)
835 home_dir = getenv("USERPROFILE");
836 if (home_dir == NULL)
837 home_dir = getenv("HOMEPATH");
838 // Last resort, store the cddb cache in the mplayer directory
839 if (home_dir == NULL)
840 home_dir = (char *)get_path("");
841 #endif
842 if (home_dir == NULL) {
843 cddb_data.cache_dir = NULL;
844 } else {
845 cddb_data.cache_dir = malloc(strlen(home_dir)
846 + strlen(cddb_cache_dir) + 1);
847 if (cddb_data.cache_dir == NULL) {
848 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
849 return -1;
851 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
854 // Check for a cached file
855 if (cddb_read_cache(&cddb_data) < 0) {
856 // No Cache found
857 if (cddb_retrieve(&cddb_data) < 0) {
858 return -1;
862 if (cddb_data.xmcd_file != NULL) {
863 // printf("%s\n", cddb_data.xmcd_file);
864 *xmcd_file = cddb_data.xmcd_file;
865 return 0;
868 return -1;
871 /***************
872 * xmcd parser *
873 ***************/
874 static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
876 char *ptr, *album;
877 ptr = strstr(line, "DTITLE=");
878 if (ptr != NULL) {
879 ptr += 7;
880 album = strstr(ptr, "/");
881 if (album == NULL)
882 return NULL;
883 cd_info->album = malloc(strlen(album + 2) + 1);
884 if (cd_info->album == NULL) {
885 return NULL;
887 strcpy(cd_info->album, album + 2);
888 album--;
889 album[0] = '\0';
890 cd_info->artist = malloc(strlen(ptr) + 1);
891 if (cd_info->artist == NULL) {
892 return NULL;
894 strcpy(cd_info->artist, ptr);
896 return ptr;
899 static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
901 char *ptr;
902 ptr = strstr(line, "DGENRE=");
903 if (ptr != NULL) {
904 ptr += 7;
905 cd_info->genre = malloc(strlen(ptr)+1);
906 if (cd_info->genre == NULL) {
907 return NULL;
909 strcpy(cd_info->genre, ptr);
911 return ptr;
914 static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
916 unsigned int track_nb;
917 unsigned long sec, off;
918 char *ptr;
919 ptr = strstr(line, "TTITLE");
920 if (ptr != NULL) {
921 ptr += 6;
922 // Here we point to the track number
923 track_nb = atoi(ptr);
924 ptr = strstr(ptr, "=");
925 if (ptr == NULL)
926 return NULL;
927 ptr++;
929 sec = cdtoc[track_nb].frame;
930 off = cdtoc[track_nb + 1].frame - sec + 1;
932 cd_info_add_track(cd_info, ptr, track_nb + 1,
933 (unsigned int) (off / (60 * 75)),
934 (unsigned int) ((off / 75) % 60),
935 (unsigned int) (off % 75),
936 sec, off);
938 return ptr;
941 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
943 cd_info_t *cd_info = NULL;
944 int length, pos = 0;
945 char *ptr, *ptr2;
946 unsigned int audiolen;
947 if (xmcd_file == NULL)
948 return NULL;
950 cd_info = cd_info_new();
951 if (cd_info == NULL) {
952 return NULL;
955 length = strlen(xmcd_file);
956 ptr = xmcd_file;
957 while (ptr != NULL && pos < length) {
958 // Read a line
959 ptr2 = ptr;
960 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
961 ptr2++;
962 if (ptr2[0] == '\0') {
963 break;
965 ptr2[0] = '\0';
966 // Ignore comments
967 if (ptr[0] != '#') {
968 // Search for the album title
969 if (xmcd_parse_dtitle(cd_info, ptr))
971 // Search for the genre
972 else if (xmcd_parse_dgenre(cd_info, ptr))
974 // Search for a track title
975 else if (xmcd_parse_ttitle(cd_info, ptr))
976 audiolen++; // <-- audiolen++ to shut up gcc warning
978 if (ptr2[1] == '\n')
979 ptr2++;
980 pos = (ptr2 + 1) - ptr;
981 ptr = ptr2 + 1;
984 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
985 cd_info->min = (unsigned int) (audiolen / (60 * 75));
986 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
987 cd_info->msec = (unsigned int) (audiolen % 75);
989 return cd_info;