ad_ffmpeg: remove incorrect request_sample_fmt setting
[mplayer.git] / stream / stream_cddb.c
blob339f8a75f1e98b688daee35664ff607a13e3f456
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 #include "osdep/io.h"
40 #if defined(__MINGW32__) || defined(__CYGWIN__)
41 #include <windows.h>
42 #if HAVE_WINSOCK2_H
43 #include <winsock2.h>
44 #endif
45 #else
46 #include <netdb.h>
47 #include <sys/ioctl.h>
48 #endif
49 #include <sys/types.h>
50 #include <sys/stat.h>
52 #include "mp_msg.h"
53 #include "path.h"
55 #if defined(__linux__)
56 #include <linux/cdrom.h>
57 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
58 #include <sys/cdio.h>
59 #elif defined(__MINGW32__) || defined(__CYGWIN__)
60 #include <ddk/ntddcdrm.h>
61 #elif defined(__bsdi__)
62 #include <dvd.h>
63 #elif defined(__APPLE__) || defined(__DARWIN__)
64 #include <IOKit/storage/IOCDTypes.h>
65 #include <IOKit/storage/IOCDMediaBSDClient.h>
66 #include "mpbswap.h"
67 #endif
69 #include "cdd.h"
70 #include "mpcommon.h"
71 #include "stream.h"
72 #include "network.h"
73 #include "libavutil/common.h"
75 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
76 #define DEFAULT_CACHE_DIR "/.cddb/"
78 typedef struct {
79 char cddb_hello[1024];
80 unsigned long disc_id;
81 unsigned int tracks;
82 char *cache_dir;
83 char *freedb_server;
84 int freedb_proto_level;
85 int anonymous;
86 char category[100];
87 char *xmcd_file;
88 size_t xmcd_file_size;
89 void *user_data;
90 } cddb_data_t;
92 typedef struct {
93 unsigned int min, sec, frame;
94 } cd_toc_t;
96 static cd_toc_t cdtoc[100];
97 static int cdtoc_last_track;
99 static int read_toc(const char *dev)
101 int first = 0, last = -1;
102 int i;
103 #if defined(__MINGW32__) || defined(__CYGWIN__)
104 HANDLE drive;
105 DWORD r;
106 CDROM_TOC toc;
107 char device[10];
109 sprintf(device, "\\\\.\\%s", dev);
110 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
111 OPEN_EXISTING, 0, 0);
113 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
114 sizeof(CDROM_TOC), &r, 0)) {
115 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
116 return 0;
119 first = toc.FirstTrack - 1; last = toc.LastTrack;
120 for (i = first; i <= last; i++) {
121 cdtoc[i].min = toc.TrackData[i].Address[1];
122 cdtoc[i].sec = toc.TrackData[i].Address[2];
123 cdtoc[i].frame = toc.TrackData[i].Address[3];
125 CloseHandle(drive);
127 #else
128 int drive;
129 drive = open(dev, O_RDONLY | O_NONBLOCK);
130 if (drive < 0) {
131 return drive;
134 #if defined(__linux__) || defined(__bsdi__)
136 struct cdrom_tochdr tochdr;
137 ioctl(drive, CDROMREADTOCHDR, &tochdr);
138 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
140 for (i = first; i <= last; i++) {
141 struct cdrom_tocentry tocentry;
142 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
143 tocentry.cdte_format = CDROM_MSF;
144 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
145 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
146 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
147 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
149 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
151 struct ioc_toc_header tochdr;
152 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
153 first = tochdr.starting_track - 1; last = tochdr.ending_track;
155 for (i = first; i <= last; i++) {
156 struct ioc_read_toc_single_entry tocentry;
157 tocentry.track = (i == last) ? 0xAA : i + 1;
158 tocentry.address_format = CD_MSF_FORMAT;
159 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
160 cdtoc[i].min = tocentry.entry.addr.msf.minute;
161 cdtoc[i].sec = tocentry.entry.addr.msf.second;
162 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
164 #elif defined(__NetBSD__) || defined(__OpenBSD__)
166 struct ioc_toc_header tochdr;
167 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
168 first = tochdr.starting_track - 1; last = tochdr.ending_track;
170 for (i = first; i <= last; i++) {
171 struct ioc_read_toc_entry tocentry;
172 struct cd_toc_entry toc_buffer;
173 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
174 tocentry.address_format = CD_MSF_FORMAT;
175 tocentry.data = &toc_buffer;
176 tocentry.data_len = sizeof(toc_buffer);
177 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
178 cdtoc[i].min = toc_buffer.addr.msf.minute;
179 cdtoc[i].sec = toc_buffer.addr.msf.second;
180 cdtoc[i].frame = toc_buffer.addr.msf.frame;
182 #elif defined(__APPLE__) || defined(__DARWIN__)
184 dk_cd_read_toc_t tochdr;
185 uint8_t buf[4];
186 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
187 memset(&tochdr, 0, sizeof(tochdr));
188 tochdr.bufferLength = sizeof(buf);
189 tochdr.buffer = &buf;
190 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
191 && tochdr.bufferLength == sizeof(buf)) {
192 first = buf[2] - 1;
193 last = buf[3];
195 if (last >= 0) {
196 memset(&tochdr, 0, sizeof(tochdr));
197 tochdr.bufferLength = sizeof(buf2);
198 tochdr.buffer = &buf2;
199 tochdr.format = kCDTOCFormatTOC;
200 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
201 || tochdr.bufferLength < sizeof(CDTOC))
202 last = -1;
204 if (last >= 0) {
205 CDTOC *cdToc = (CDTOC *)buf2;
206 CDTrackInfo lastTrack;
207 dk_cd_read_track_info_t trackInfoParams;
208 for (i = first; i < last; ++i) {
209 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
210 cdtoc[i].min = msf.minute;
211 cdtoc[i].sec = msf.second;
212 cdtoc[i].frame = msf.frame;
214 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
215 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
216 trackInfoParams.bufferLength = sizeof(lastTrack);
217 trackInfoParams.address = last;
218 trackInfoParams.buffer = &lastTrack;
219 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
220 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
221 + be2me_32(lastTrack.trackSize));
222 cdtoc[last].min = msf.minute;
223 cdtoc[last].sec = msf.second;
224 cdtoc[last].frame = msf.frame;
228 #endif
229 close(drive);
230 #endif
231 for (i = first; i <= last; i++)
232 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
233 return last;
237 \brief Reads TOC from CD in the given device and prints the number of tracks
238 and the length of each track in minute:second:frame format.
239 \param *dev the device to analyse
240 \return if the command line -identify is given, returns the last track of
241 the TOC or -1 if the TOC can't be read,
242 otherwise just returns 0 and let cddb_resolve the TOC
244 int cdd_identify(const char *dev)
246 cdtoc_last_track = 0;
247 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
248 int i, min, sec, frame;
249 cdtoc_last_track = read_toc(dev);
250 if (cdtoc_last_track < 0) {
251 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
252 return -1;
254 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
255 for (i = 1; i <= cdtoc_last_track; i++) {
256 frame = cdtoc[i].frame - cdtoc[i-1].frame;
257 sec = frame / 75;
258 frame -= sec * 75;
259 min = sec / 60;
260 sec -= min * 60;
261 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
262 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
265 return cdtoc_last_track;
268 static unsigned int cddb_sum(int n)
270 unsigned int ret;
272 ret = 0;
273 while (n > 0) {
274 ret += (n % 10);
275 n /= 10;
277 return ret;
280 static unsigned long cddb_discid(int tot_trks)
282 unsigned int i, t = 0, n = 0;
284 i = 0;
285 while (i < (unsigned int)tot_trks) {
286 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
287 i++;
289 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
290 ((cdtoc[0].min * 60) + cdtoc[0].sec);
291 return (n % 0xff) << 24 | t << 8 | tot_trks;
296 static int cddb_http_request(char *command,
297 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
298 cddb_data_t *cddb_data)
300 char request[4096];
301 int fd, ret = 0;
302 URL_t *url;
303 HTTP_header_t *http_hdr;
305 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
306 return -1;
308 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
309 cddb_data->freedb_server, command, cddb_data->cddb_hello,
310 cddb_data->freedb_proto_level);
311 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
313 url = url_new(request);
314 if (url == NULL) {
315 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
316 return -1;
319 fd = http_send_request(url,0);
320 if (fd < 0) {
321 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
322 return -1;
325 http_hdr = http_read_response(fd);
326 if (http_hdr == NULL) {
327 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
328 return -1;
331 http_debug_hdr(http_hdr);
332 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
334 switch (http_hdr->status_code) {
335 case 200:
336 ret = reply_parser(http_hdr, cddb_data);
337 break;
338 case 400:
339 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
340 break;
341 default:
342 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
345 http_free(http_hdr);
346 url_free(url);
348 return ret;
351 static int cddb_read_cache(cddb_data_t *cddb_data)
353 char file_name[100];
354 struct stat stats;
355 int file_fd, ret;
356 size_t file_size;
358 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
359 return -1;
361 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
363 file_fd = open(file_name, O_RDONLY | O_BINARY);
364 if (file_fd < 0) {
365 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
366 return -1;
369 ret = fstat(file_fd, &stats);
370 if (ret < 0) {
371 perror("fstat");
372 file_size = 4096;
373 } else {
374 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
377 cddb_data->xmcd_file = malloc(file_size + 1);
378 if (cddb_data->xmcd_file == NULL) {
379 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
380 close(file_fd);
381 return -1;
383 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
384 if (cddb_data->xmcd_file_size != file_size) {
385 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
386 close(file_fd);
387 return -1;
389 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
391 close(file_fd);
393 return 0;
396 static int cddb_write_cache(cddb_data_t *cddb_data)
398 // We have the file, save it for cache.
399 char file_name[100];
400 int file_fd, ret;
401 int wrote = 0;
403 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
404 return -1;
406 // Check if the CDDB cache dir exist
407 if (!mp_path_exists(cddb_data->cache_dir)) {
408 // Directory not present, create it.
409 ret = mkdir(cddb_data->cache_dir, 0755);
410 #ifdef __MINGW32__
411 if (ret < 0 && errno != EEXIST) {
412 #else
413 if (ret < 0) {
414 #endif
415 perror("mkdir");
416 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
417 cddb_data->cache_dir);
418 return -1;
422 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
424 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
425 if (file_fd < 0) {
426 perror("create");
427 return -1;
430 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
431 if (wrote < 0) {
432 perror("write");
433 close(file_fd);
434 return -1;
436 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
437 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
438 close(file_fd);
439 return -1;
442 close(file_fd);
444 return 0;
447 static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
449 unsigned long disc_id;
450 char category[100];
451 char *ptr = NULL, *ptr2 = NULL;
452 int ret, status;
454 if (http_hdr == NULL || cddb_data == NULL)
455 return -1;
457 ret = sscanf(http_hdr->body, "%d ", &status);
458 if (ret != 1) {
459 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
460 return -1;
463 switch (status) {
464 case 210:
465 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
466 category, &disc_id);
467 if (ret != 3) {
468 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
469 return -1;
471 // Check if it's a xmcd database file
472 ptr = strstr(http_hdr->body, "# xmcd");
473 if (ptr == NULL) {
474 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
475 "Invalid xmcd database file returned.\n");
476 return -1;
478 ptr = strdup(ptr);
479 // Ok found the beginning of the file
480 // look for the end
481 ptr2 = strstr(ptr, "\n.\r\n");
482 if (!ptr2)
483 ptr2 = strstr(ptr, "\n.\n");
484 if (ptr2) {
485 ptr2++;
486 } else {
487 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
488 ptr2 = ptr + strlen(ptr); //return -1;
490 // Ok found the end
491 // do a sanity check
492 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
493 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
494 return -1;
496 cddb_data->xmcd_file = ptr;
497 cddb_data->xmcd_file_size = ptr2 - ptr;
498 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
499 return cddb_write_cache(cddb_data);
500 default:
501 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
503 return 0;
506 static int cddb_request_titles(cddb_data_t *cddb_data)
508 char command[1024];
509 sprintf(command, "cddb+read+%s+%08lx",
510 cddb_data->category, cddb_data->disc_id);
511 return cddb_http_request(command, cddb_read_parse, cddb_data);
514 static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
516 char album_title[100];
517 char *ptr = NULL;
518 int ret;
520 ptr = strstr(http_hdr->body, "\n");
521 if (ptr == NULL) {
522 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
523 return -1;
525 ptr++;
526 // We have a list of exact/inexact matches, so which one do we use?
527 // So let's take the first one.
528 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
529 &(cddb_data->disc_id), album_title);
530 if (ret != 3) {
531 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
532 return -1;
534 ptr = strstr(http_hdr->body, album_title);
535 if (ptr != NULL) {
536 char *ptr2;
537 int len;
538 ptr2 = strstr(ptr, "\n");
539 if (ptr2 == NULL) {
540 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
541 } else {
542 len = ptr2-ptr+1;
544 len = FFMIN(sizeof(album_title) - 1, len);
545 strncpy(album_title, ptr, len);
546 album_title[len]='\0';
548 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
549 return 0;
552 static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
554 char album_title[100];
555 char *ptr = NULL;
556 int ret, status;
558 ret = sscanf(http_hdr->body, "%d ", &status);
559 if (ret != 1) {
560 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
561 return -1;
564 switch (status) {
565 case 200:
566 // Found exact match
567 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
568 cddb_data->category, &(cddb_data->disc_id), album_title);
569 if (ret != 4) {
570 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
571 return -1;
573 ptr = strstr(http_hdr->body, album_title);
574 if (ptr != NULL) {
575 char *ptr2;
576 int len;
577 ptr2 = strstr(ptr, "\n");
578 if (ptr2 == NULL) {
579 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
580 } else {
581 len = ptr2-ptr+1;
583 len = FFMIN(sizeof(album_title) - 1, len);
584 strncpy(album_title, ptr, len);
585 album_title[len]='\0';
587 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
588 return cddb_request_titles(cddb_data);
589 case 202:
590 // No match found
591 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
592 break;
593 case 210:
594 // Found exact matches, list follows
595 cddb_parse_matches_list(http_hdr, cddb_data);
596 return cddb_request_titles(cddb_data);
598 body=[210 Found exact matches, list follows (until terminating `.')
599 misc c711930d Santana / Supernatural
600 rock c711930d Santana / Supernatural
601 blues c711930d Santana / Supernatural
604 case 211:
605 // Found inexact matches, list follows
606 cddb_parse_matches_list(http_hdr, cddb_data);
607 return cddb_request_titles(cddb_data);
608 case 500:
609 mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
610 "Server returns: Command syntax error\n");
611 break;
612 default:
613 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
615 return -1;
618 static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
620 int max;
621 int ret, status;
622 char *ptr;
624 ret = sscanf(http_hdr->body, "%d ", &status);
625 if (ret != 1) {
626 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
627 return -1;
630 switch (status) {
631 case 210:
632 ptr = strstr(http_hdr->body, "max proto:");
633 if (ptr == NULL) {
634 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
635 return -1;
637 ret = sscanf(ptr, "max proto: %d", &max);
638 if (ret != 1) {
639 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
640 return -1;
642 cddb_data->freedb_proto_level = max;
643 return 0;
644 default:
645 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
647 return -1;
650 static int cddb_get_proto_level(cddb_data_t *cddb_data)
652 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
655 static void cddb_create_hello(cddb_data_t *cddb_data)
657 char host_name[51];
658 char *user_name;
660 if (cddb_data->anonymous) { // Default is anonymous
661 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
662 * We don't send current user/host name in hello to prevent spam.
663 * Software that sends this is considered spyware
664 * that most people don't like.
666 user_name = "anonymous";
667 strcpy(host_name, "localhost");
668 } else {
669 if (gethostname(host_name, 50) < 0) {
670 strcpy(host_name, "localhost");
672 user_name = getenv("LOGNAME");
674 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s",
675 user_name, host_name, mplayer_version);
678 static int cddb_retrieve(cddb_data_t *cddb_data)
680 char offsets[1024], command[1024];
681 char *ptr;
682 unsigned int i, time_len;
683 int ret;
685 ptr = offsets;
686 for (i = 0; i < cddb_data->tracks ; i++) {
687 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
688 if (ptr-offsets > sizeof offsets - 40) break;
690 ptr[0] = 0;
691 time_len = (cdtoc[cddb_data->tracks].frame)/75;
693 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
694 cddb_data->freedb_proto_level = 1;
695 cddb_data->xmcd_file = NULL;
697 cddb_create_hello(cddb_data);
698 if (cddb_get_proto_level(cddb_data) < 0) {
699 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
700 return -1;
703 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
704 cddb_data->tracks, offsets, time_len);
705 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
706 if (ret < 0)
707 return -1;
709 free(cddb_data->cache_dir);
710 return 0;
713 int cddb_resolve(const char *dev, char **xmcd_file)
715 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
716 char *home_dir = NULL;
717 cddb_data_t cddb_data;
719 if (cdtoc_last_track <= 0) {
720 cdtoc_last_track = read_toc(dev);
721 if (cdtoc_last_track < 0) {
722 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
723 return -1;
726 cddb_data.tracks = cdtoc_last_track;
727 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
728 cddb_data.anonymous = 1; // Don't send user info by default
730 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
731 cddb_data.disc_id);
733 // Check if there is a CD in the drive
734 // FIXME: That's not really a good way to check
735 if (cddb_data.disc_id == 0) {
736 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
737 return -1;
740 home_dir = getenv("HOME");
741 #ifdef __MINGW32__
742 if (home_dir == NULL)
743 home_dir = getenv("USERPROFILE");
744 if (home_dir == NULL)
745 home_dir = getenv("HOMEPATH");
746 // Last resort, store the cddb cache in the mplayer directory
747 if (home_dir == NULL)
748 home_dir = (char *)get_path("");
749 #endif
750 if (home_dir == NULL) {
751 cddb_data.cache_dir = NULL;
752 } else {
753 cddb_data.cache_dir = malloc(strlen(home_dir)
754 + strlen(cddb_cache_dir) + 1);
755 if (cddb_data.cache_dir == NULL) {
756 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
757 return -1;
759 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
762 // Check for a cached file
763 if (cddb_read_cache(&cddb_data) < 0) {
764 // No Cache found
765 if (cddb_retrieve(&cddb_data) < 0) {
766 return -1;
770 if (cddb_data.xmcd_file != NULL) {
771 // printf("%s\n", cddb_data.xmcd_file);
772 *xmcd_file = cddb_data.xmcd_file;
773 return 0;
776 return -1;
779 /***************
780 * xmcd parser *
781 ***************/
782 static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
784 char *ptr, *album;
785 ptr = strstr(line, "DTITLE=");
786 if (ptr != NULL) {
787 ptr += 7;
788 album = strstr(ptr, "/");
789 if (album == NULL)
790 return NULL;
791 cd_info->album = malloc(strlen(album + 2) + 1);
792 if (cd_info->album == NULL) {
793 return NULL;
795 strcpy(cd_info->album, album + 2);
796 album--;
797 album[0] = '\0';
798 cd_info->artist = malloc(strlen(ptr) + 1);
799 if (cd_info->artist == NULL) {
800 return NULL;
802 strcpy(cd_info->artist, ptr);
804 return ptr;
807 static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
809 char *ptr;
810 ptr = strstr(line, "DGENRE=");
811 if (ptr != NULL) {
812 ptr += 7;
813 cd_info->genre = malloc(strlen(ptr)+1);
814 if (cd_info->genre == NULL) {
815 return NULL;
817 strcpy(cd_info->genre, ptr);
819 return ptr;
822 static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
824 unsigned int track_nb;
825 unsigned long sec, off;
826 char *ptr;
827 ptr = strstr(line, "TTITLE");
828 if (ptr != NULL) {
829 ptr += 6;
830 // Here we point to the track number
831 track_nb = atoi(ptr);
832 ptr = strstr(ptr, "=");
833 if (ptr == NULL)
834 return NULL;
835 ptr++;
837 sec = cdtoc[track_nb].frame;
838 off = cdtoc[track_nb + 1].frame - sec + 1;
840 cd_info_add_track(cd_info, ptr, track_nb + 1,
841 (unsigned int) (off / (60 * 75)),
842 (unsigned int) ((off / 75) % 60),
843 (unsigned int) (off % 75),
844 sec, off);
846 return ptr;
849 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
851 cd_info_t *cd_info = NULL;
852 int length, pos = 0;
853 char *ptr, *ptr2;
854 unsigned int audiolen;
855 if (xmcd_file == NULL)
856 return NULL;
858 cd_info = cd_info_new();
859 if (cd_info == NULL) {
860 return NULL;
863 length = strlen(xmcd_file);
864 ptr = xmcd_file;
865 while (ptr != NULL && pos < length) {
866 // Read a line
867 ptr2 = ptr;
868 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
869 ptr2++;
870 if (ptr2[0] == '\0') {
871 break;
873 ptr2[0] = '\0';
874 // Ignore comments
875 if (ptr[0] != '#') {
876 // Search for the album title
877 if (xmcd_parse_dtitle(cd_info, ptr))
879 // Search for the genre
880 else if (xmcd_parse_dgenre(cd_info, ptr))
882 // Search for a track title
883 else if (xmcd_parse_ttitle(cd_info, ptr))
884 audiolen++; // <-- audiolen++ to shut up gcc warning
886 if (ptr2[1] == '\n')
887 ptr2++;
888 pos = (ptr2 + 1) - ptr;
889 ptr = ptr2 + 1;
892 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
893 cd_info->min = (unsigned int) (audiolen / (60 * 75));
894 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
895 cd_info->msec = (unsigned int) (audiolen % 75);
897 return cd_info;