Merge svn changes up to r30672
[mplayer/kovensky.git] / stream / stream_cddb.c
blob8bbe9d33b28c08c5e63b6eab77c667b949656673
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"
55 #include "help_mp.h"
57 #if defined(__linux__)
58 #include <linux/cdrom.h>
59 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 #include <sys/cdio.h>
61 #elif defined(__MINGW32__) || defined(__CYGWIN__)
62 #include <ddk/ntddcdrm.h>
63 #elif defined(__bsdi__)
64 #include <dvd.h>
65 #elif defined(__APPLE__) || defined(__DARWIN__)
66 #include <IOKit/storage/IOCDTypes.h>
67 #include <IOKit/storage/IOCDMediaBSDClient.h>
68 #include "mpbswap.h"
69 #endif
71 #include "cdd.h"
72 #include "version.h"
73 #include "stream.h"
74 #include "network.h"
75 #include "libavutil/common.h"
77 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
78 #define DEFAULT_CACHE_DIR "/.cddb/"
80 stream_t* open_cdda(char *dev, char *track);
82 static cd_toc_t cdtoc[100];
83 static int cdtoc_last_track;
85 int read_toc(const char *dev)
87 int first = 0, last = -1;
88 int i;
89 #if defined(__MINGW32__) || defined(__CYGWIN__)
90 HANDLE drive;
91 DWORD r;
92 CDROM_TOC toc;
93 char device[10];
95 sprintf(device, "\\\\.\\%s", dev);
96 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
97 OPEN_EXISTING, 0, 0);
99 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
100 sizeof(CDROM_TOC), &r, 0)) {
101 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
102 return 0;
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];
111 CloseHandle(drive);
113 #else
114 int drive;
115 drive = open(dev, O_RDONLY | O_NONBLOCK);
116 if (drive < 0) {
117 return drive;
120 #if defined(__linux__) || defined(__bsdi__)
122 struct cdrom_tochdr tochdr;
123 ioctl(drive, CDROMREADTOCHDR, &tochdr);
124 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
126 for (i = first; i <= last; i++) {
127 struct cdrom_tocentry tocentry;
128 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
129 tocentry.cdte_format = CDROM_MSF;
130 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
131 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
132 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
133 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
135 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
137 struct ioc_toc_header tochdr;
138 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
139 first = tochdr.starting_track - 1; last = tochdr.ending_track;
141 for (i = first; i <= last; i++) {
142 struct ioc_read_toc_single_entry tocentry;
143 tocentry.track = (i == last) ? 0xAA : i + 1;
144 tocentry.address_format = CD_MSF_FORMAT;
145 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
146 cdtoc[i].min = tocentry.entry.addr.msf.minute;
147 cdtoc[i].sec = tocentry.entry.addr.msf.second;
148 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
150 #elif defined(__NetBSD__) || defined(__OpenBSD__)
152 struct ioc_toc_header tochdr;
153 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
154 first = tochdr.starting_track - 1; last = tochdr.ending_track;
156 for (i = first; i <= last; i++) {
157 struct ioc_read_toc_entry tocentry;
158 struct cd_toc_entry toc_buffer;
159 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
160 tocentry.address_format = CD_MSF_FORMAT;
161 tocentry.data = &toc_buffer;
162 tocentry.data_len = sizeof(toc_buffer);
163 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
164 cdtoc[i].min = toc_buffer.addr.msf.minute;
165 cdtoc[i].sec = toc_buffer.addr.msf.second;
166 cdtoc[i].frame = toc_buffer.addr.msf.frame;
168 #elif defined(__APPLE__) || defined(__DARWIN__)
170 dk_cd_read_toc_t tochdr;
171 uint8_t buf[4];
172 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
173 memset(&tochdr, 0, sizeof(tochdr));
174 tochdr.bufferLength = sizeof(buf);
175 tochdr.buffer = &buf;
176 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
177 && tochdr.bufferLength == sizeof(buf)) {
178 first = buf[2] - 1;
179 last = buf[3];
181 if (last >= 0) {
182 memset(&tochdr, 0, sizeof(tochdr));
183 tochdr.bufferLength = sizeof(buf2);
184 tochdr.buffer = &buf2;
185 tochdr.format = kCDTOCFormatTOC;
186 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
187 || tochdr.bufferLength < sizeof(CDTOC))
188 last = -1;
190 if (last >= 0) {
191 CDTOC *cdToc = (CDTOC *)buf2;
192 CDTrackInfo lastTrack;
193 dk_cd_read_track_info_t trackInfoParams;
194 for (i = first; i < last; ++i) {
195 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
196 cdtoc[i].min = msf.minute;
197 cdtoc[i].sec = msf.second;
198 cdtoc[i].frame = msf.frame;
200 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
201 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
202 trackInfoParams.bufferLength = sizeof(lastTrack);
203 trackInfoParams.address = last;
204 trackInfoParams.buffer = &lastTrack;
205 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
206 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
207 + be2me_32(lastTrack.trackSize));
208 cdtoc[last].min = msf.minute;
209 cdtoc[last].sec = msf.second;
210 cdtoc[last].frame = msf.frame;
214 #endif
215 close(drive);
216 #endif
217 for (i = first; i <= last; i++)
218 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
219 return last;
223 \brief Reads TOC from CD in the given device and prints the number of tracks
224 and the length of each track in minute:second:frame format.
225 \param *dev the device to analyse
226 \return if the command line -identify is given, returns the last track of
227 the TOC or -1 if the TOC can't be read,
228 otherwise just returns 0 and let cddb_resolve the TOC
230 int cdd_identify(const char *dev)
232 cdtoc_last_track = 0;
233 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
234 int i, min, sec, frame;
235 cdtoc_last_track = read_toc(dev);
236 if (cdtoc_last_track < 0) {
237 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
238 return -1;
240 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
241 for (i = 1; i <= cdtoc_last_track; i++) {
242 frame = cdtoc[i].frame - cdtoc[i-1].frame;
243 sec = frame / 75;
244 frame -= sec * 75;
245 min = sec / 60;
246 sec -= min * 60;
247 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
248 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
251 return cdtoc_last_track;
254 unsigned int cddb_sum(int n)
256 unsigned int ret;
258 ret = 0;
259 while (n > 0) {
260 ret += (n % 10);
261 n /= 10;
263 return ret;
266 unsigned long cddb_discid(int tot_trks)
268 unsigned int i, t = 0, n = 0;
270 i = 0;
271 while (i < (unsigned int)tot_trks) {
272 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
273 i++;
275 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
276 ((cdtoc[0].min * 60) + cdtoc[0].sec);
277 return (n % 0xff) << 24 | t << 8 | tot_trks;
282 int cddb_http_request(char *command,
283 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
284 cddb_data_t *cddb_data)
286 char request[4096];
287 int fd, ret = 0;
288 URL_t *url;
289 HTTP_header_t *http_hdr;
291 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
292 return -1;
294 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
295 cddb_data->freedb_server, command, cddb_data->cddb_hello,
296 cddb_data->freedb_proto_level);
297 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
299 url = url_new(request);
300 if (url == NULL) {
301 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
302 return -1;
305 fd = http_send_request(url,0);
306 if (fd < 0) {
307 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
308 return -1;
311 http_hdr = http_read_response(fd);
312 if (http_hdr == NULL) {
313 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
314 return -1;
317 http_debug_hdr(http_hdr);
318 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
320 switch (http_hdr->status_code) {
321 case 200:
322 ret = reply_parser(http_hdr, cddb_data);
323 break;
324 case 400:
325 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
326 break;
327 default:
328 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
331 http_free(http_hdr);
332 url_free(url);
334 return ret;
337 int cddb_read_cache(cddb_data_t *cddb_data)
339 char file_name[100];
340 struct stat stats;
341 int file_fd, ret;
342 size_t file_size;
344 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
345 return -1;
347 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
349 file_fd = open(file_name, O_RDONLY
350 #if defined(__MINGW32__) || defined(__CYGWIN__)
351 | O_BINARY
352 #endif
354 if (file_fd < 0) {
355 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
356 return -1;
359 ret = fstat(file_fd, &stats);
360 if (ret < 0) {
361 perror("fstat");
362 file_size = 4096;
363 } else {
364 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
367 cddb_data->xmcd_file = malloc(file_size + 1);
368 if (cddb_data->xmcd_file == NULL) {
369 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
370 close(file_fd);
371 return -1;
373 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
374 if (cddb_data->xmcd_file_size != file_size) {
375 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
376 close(file_fd);
377 return -1;
379 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
381 close(file_fd);
383 return 0;
386 int cddb_write_cache(cddb_data_t *cddb_data)
388 // We have the file, save it for cache.
389 struct stat file_stat;
390 char file_name[100];
391 int file_fd, ret;
392 int wrote = 0;
394 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
395 return -1;
397 // Check if the CDDB cache dir exist
398 ret = stat(cddb_data->cache_dir, &file_stat);
399 if (ret < 0) {
400 // Directory not present, create it.
401 ret = mkdir(cddb_data->cache_dir, 0755);
402 #ifdef __MINGW32__
403 if (ret < 0 && errno != EEXIST) {
404 #else
405 if (ret < 0) {
406 #endif
407 perror("mkdir");
408 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
409 cddb_data->cache_dir);
410 return -1;
414 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
416 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
417 if (file_fd < 0) {
418 perror("create");
419 return -1;
422 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
423 if (wrote < 0) {
424 perror("write");
425 close(file_fd);
426 return -1;
428 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
429 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
430 close(file_fd);
431 return -1;
434 close(file_fd);
436 return 0;
439 int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
441 unsigned long disc_id;
442 char category[100];
443 char *ptr = NULL, *ptr2 = NULL;
444 int ret, status;
446 if (http_hdr == NULL || cddb_data == NULL)
447 return -1;
449 ret = sscanf(http_hdr->body, "%d ", &status);
450 if (ret != 1) {
451 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
452 return -1;
455 switch (status) {
456 case 210:
457 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
458 category, &disc_id);
459 if (ret != 3) {
460 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
461 return -1;
463 // Check if it's a xmcd database file
464 ptr = strstr(http_hdr->body, "# xmcd");
465 if (ptr == NULL) {
466 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
467 "Invalid xmcd database file returned.\n");
468 return -1;
470 ptr = strdup(ptr);
471 // Ok found the beginning of the file
472 // look for the end
473 ptr2 = strstr(ptr, "\n.\r\n");
474 if (!ptr2)
475 ptr2 = strstr(ptr, "\n.\n");
476 if (ptr2) {
477 ptr2++;
478 } else {
479 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
480 ptr2 = ptr + strlen(ptr); //return -1;
482 // Ok found the end
483 // do a sanity check
484 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
485 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
486 return -1;
488 cddb_data->xmcd_file = ptr;
489 cddb_data->xmcd_file_size = ptr2 - ptr;
490 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
491 return cddb_write_cache(cddb_data);
492 default:
493 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
495 return 0;
498 int cddb_request_titles(cddb_data_t *cddb_data)
500 char command[1024];
501 sprintf(command, "cddb+read+%s+%08lx",
502 cddb_data->category, cddb_data->disc_id);
503 return cddb_http_request(command, cddb_read_parse, cddb_data);
506 int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
508 char album_title[100];
509 char *ptr = NULL;
510 int ret;
512 ptr = strstr(http_hdr->body, "\n");
513 if (ptr == NULL) {
514 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
515 return -1;
517 ptr++;
518 // We have a list of exact/inexact matches, so which one do we use?
519 // So let's take the first one.
520 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
521 &(cddb_data->disc_id), album_title);
522 if (ret != 3) {
523 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
524 return -1;
526 ptr = strstr(http_hdr->body, album_title);
527 if (ptr != NULL) {
528 char *ptr2;
529 int len;
530 ptr2 = strstr(ptr, "\n");
531 if (ptr2 == NULL) {
532 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
533 } else {
534 len = ptr2-ptr+1;
536 len = FFMIN(sizeof(album_title) - 1, len);
537 strncpy(album_title, ptr, len);
538 album_title[len]='\0';
540 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
541 return 0;
544 int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
546 char album_title[100];
547 char *ptr = NULL;
548 int ret, status;
550 ret = sscanf(http_hdr->body, "%d ", &status);
551 if (ret != 1) {
552 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
553 return -1;
556 switch (status) {
557 case 200:
558 // Found exact match
559 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
560 cddb_data->category, &(cddb_data->disc_id), album_title);
561 if (ret != 4) {
562 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
563 return -1;
565 ptr = strstr(http_hdr->body, album_title);
566 if (ptr != NULL) {
567 char *ptr2;
568 int len;
569 ptr2 = strstr(ptr, "\n");
570 if (ptr2 == NULL) {
571 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
572 } else {
573 len = ptr2-ptr+1;
575 len = FFMIN(sizeof(album_title) - 1, len);
576 strncpy(album_title, ptr, len);
577 album_title[len]='\0';
579 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
580 return cddb_request_titles(cddb_data);
581 case 202:
582 // No match found
583 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
584 break;
585 case 210:
586 // Found exact matches, list follows
587 cddb_parse_matches_list(http_hdr, cddb_data);
588 return cddb_request_titles(cddb_data);
590 body=[210 Found exact matches, list follows (until terminating `.')
591 misc c711930d Santana / Supernatural
592 rock c711930d Santana / Supernatural
593 blues c711930d Santana / Supernatural
596 case 211:
597 // Found inexact matches, list follows
598 cddb_parse_matches_list(http_hdr, cddb_data);
599 return cddb_request_titles(cddb_data);
600 case 500:
601 mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
602 "Server returns: Command syntax error\n");
603 break;
604 default:
605 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
607 return -1;
610 int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
612 int max;
613 int ret, status;
614 char *ptr;
616 ret = sscanf(http_hdr->body, "%d ", &status);
617 if (ret != 1) {
618 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
619 return -1;
622 switch (status) {
623 case 210:
624 ptr = strstr(http_hdr->body, "max proto:");
625 if (ptr == NULL) {
626 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
627 return -1;
629 ret = sscanf(ptr, "max proto: %d", &max);
630 if (ret != 1) {
631 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
632 return -1;
634 cddb_data->freedb_proto_level = max;
635 return 0;
636 default:
637 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
639 return -1;
642 int cddb_get_proto_level(cddb_data_t *cddb_data)
644 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
647 int cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
649 int ret, status;
651 ret = sscanf(http_hdr->body, "%d ", &status);
652 if (ret != 1) {
653 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
654 return -1;
657 switch (status) {
658 case 210:
659 // TODO: Parse the sites
660 ret = cddb_data->anonymous; // For gcc complaining about unused parameter.
661 return 0;
662 case 401:
663 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "No sites information available.\n");
664 break;
665 default:
666 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
668 return -1;
671 int cddb_get_freedb_sites(cddb_data_t *cddb_data)
673 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);
676 void cddb_create_hello(cddb_data_t *cddb_data)
678 char host_name[51];
679 char *user_name;
681 if (cddb_data->anonymous) { // Default is anonymous
682 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
683 * We don't send current user/host name in hello to prevent spam.
684 * Software that sends this is considered spyware
685 * that most people don't like.
687 user_name = "anonymous";
688 strcpy(host_name, "localhost");
689 } else {
690 if (gethostname(host_name, 50) < 0) {
691 strcpy(host_name, "localhost");
693 user_name = getenv("LOGNAME");
695 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s+%s",
696 user_name, host_name, "MPlayer", VERSION);
699 int cddb_retrieve(cddb_data_t *cddb_data)
701 char offsets[1024], command[1024];
702 char *ptr;
703 unsigned int i, time_len;
704 int ret;
706 ptr = offsets;
707 for (i = 0; i < cddb_data->tracks ; i++) {
708 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
709 if (ptr-offsets > sizeof offsets - 40) break;
711 ptr[0] = 0;
712 time_len = (cdtoc[cddb_data->tracks].frame)/75;
714 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
715 cddb_data->freedb_proto_level = 1;
716 cddb_data->xmcd_file = NULL;
718 cddb_create_hello(cddb_data);
719 if (cddb_get_proto_level(cddb_data) < 0) {
720 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
721 return -1;
724 //cddb_get_freedb_sites(&cddb_data);
726 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
727 cddb_data->tracks, offsets, time_len);
728 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
729 if (ret < 0)
730 return -1;
732 if (cddb_data->cache_dir != NULL) {
733 free(cddb_data->cache_dir);
735 return 0;
738 int cddb_resolve(const char *dev, char **xmcd_file)
740 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
741 char *home_dir = NULL;
742 cddb_data_t cddb_data;
744 if (cdtoc_last_track <= 0) {
745 cdtoc_last_track = read_toc(dev);
746 if (cdtoc_last_track < 0) {
747 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
748 return -1;
751 cddb_data.tracks = cdtoc_last_track;
752 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
753 cddb_data.anonymous = 1; // Don't send user info by default
755 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
756 cddb_data.disc_id);
758 // Check if there is a CD in the drive
759 // FIXME: That's not really a good way to check
760 if (cddb_data.disc_id == 0) {
761 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
762 return -1;
765 home_dir = getenv("HOME");
766 #ifdef __MINGW32__
767 if (home_dir == NULL)
768 home_dir = getenv("USERPROFILE");
769 if (home_dir == NULL)
770 home_dir = getenv("HOMEPATH");
771 // Last resort, store the cddb cache in the mplayer directory
772 if (home_dir == NULL)
773 home_dir = (char *)get_path("");
774 #endif
775 if (home_dir == NULL) {
776 cddb_data.cache_dir = NULL;
777 } else {
778 cddb_data.cache_dir = malloc(strlen(home_dir)
779 + strlen(cddb_cache_dir) + 1);
780 if (cddb_data.cache_dir == NULL) {
781 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
782 return -1;
784 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
787 // Check for a cached file
788 if (cddb_read_cache(&cddb_data) < 0) {
789 // No Cache found
790 if (cddb_retrieve(&cddb_data) < 0) {
791 return -1;
795 if (cddb_data.xmcd_file != NULL) {
796 // printf("%s\n", cddb_data.xmcd_file);
797 *xmcd_file = cddb_data.xmcd_file;
798 return 0;
801 return -1;
804 /***************
805 * xmcd parser *
806 ***************/
807 char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
809 char *ptr, *album;
810 ptr = strstr(line, "DTITLE=");
811 if (ptr != NULL) {
812 ptr += 7;
813 album = strstr(ptr, "/");
814 if (album == NULL)
815 return NULL;
816 cd_info->album = malloc(strlen(album + 2) + 1);
817 if (cd_info->album == NULL) {
818 return NULL;
820 strcpy(cd_info->album, album + 2);
821 album--;
822 album[0] = '\0';
823 cd_info->artist = malloc(strlen(ptr) + 1);
824 if (cd_info->artist == NULL) {
825 return NULL;
827 strcpy(cd_info->artist, ptr);
829 return ptr;
832 char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
834 char *ptr;
835 ptr = strstr(line, "DGENRE=");
836 if (ptr != NULL) {
837 ptr += 7;
838 cd_info->genre = malloc(strlen(ptr)+1);
839 if (cd_info->genre == NULL) {
840 return NULL;
842 strcpy(cd_info->genre, ptr);
844 return ptr;
847 char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
849 unsigned int track_nb;
850 unsigned long sec, off;
851 char *ptr;
852 ptr = strstr(line, "TTITLE");
853 if (ptr != NULL) {
854 ptr += 6;
855 // Here we point to the track number
856 track_nb = atoi(ptr);
857 ptr = strstr(ptr, "=");
858 if (ptr == NULL)
859 return NULL;
860 ptr++;
862 sec = cdtoc[track_nb].frame;
863 off = cdtoc[track_nb + 1].frame - sec + 1;
865 cd_info_add_track(cd_info, ptr, track_nb + 1,
866 (unsigned int) (off / (60 * 75)),
867 (unsigned int) ((off / 75) % 60),
868 (unsigned int) (off % 75),
869 sec, off);
871 return ptr;
874 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
876 cd_info_t *cd_info = NULL;
877 int length, pos = 0;
878 char *ptr, *ptr2;
879 unsigned int audiolen;
880 if (xmcd_file == NULL)
881 return NULL;
883 cd_info = cd_info_new();
884 if (cd_info == NULL) {
885 return NULL;
888 length = strlen(xmcd_file);
889 ptr = xmcd_file;
890 while (ptr != NULL && pos < length) {
891 // Read a line
892 ptr2 = ptr;
893 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
894 ptr2++;
895 if (ptr2[0] == '\0') {
896 break;
898 ptr2[0] = '\0';
899 // Ignore comments
900 if (ptr[0] != '#') {
901 // Search for the album title
902 if (xmcd_parse_dtitle(cd_info, ptr))
904 // Search for the genre
905 else if (xmcd_parse_dgenre(cd_info, ptr))
907 // Search for a track title
908 else if (xmcd_parse_ttitle(cd_info, ptr))
909 audiolen++; // <-- audiolen++ to shut up gcc warning
911 if (ptr2[1] == '\n')
912 ptr2++;
913 pos = (ptr2 + 1) - ptr;
914 ptr = ptr2 + 1;
917 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
918 cd_info->min = (unsigned int) (audiolen / (60 * 75));
919 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
920 cd_info->msec = (unsigned int) (audiolen % 75);
922 return cd_info;