2002-06-21 06:15:36 +00:00
|
|
|
/*
|
2009-07-06 23:26:13 +00:00
|
|
|
* CDDB HTTP protocol
|
2010-01-30 22:26:47 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2002 Bertrand Baudet <bertrand_baudet@yahoo.com>
|
2002-06-21 06:15:36 +00:00
|
|
|
*
|
|
|
|
* Implementation follow the freedb.howto1.06.txt specification
|
|
|
|
* from http://freedb.freedb.org
|
2009-07-06 23:26:13 +00:00
|
|
|
*
|
2002-06-21 06:15:36 +00:00
|
|
|
* discid computation by Jeremy D. Zawodny
|
2010-02-20 19:57:47 +00:00
|
|
|
* Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
|
2002-06-21 06:15:36 +00:00
|
|
|
*
|
2010-01-30 22:26:47 +00:00
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-06-21 06:15:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2008-01-20 16:13:51 +00:00
|
|
|
#include <limits.h>
|
2012-02-03 07:05:11 +00:00
|
|
|
#include "osdep/io.h"
|
2008-10-13 16:23:55 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
2005-11-06 17:42:20 +00:00
|
|
|
#include <windows.h>
|
2009-02-01 13:42:27 +00:00
|
|
|
#if HAVE_WINSOCK2_H
|
2005-11-06 17:42:20 +00:00
|
|
|
#include <winsock2.h>
|
2006-01-20 22:44:12 +00:00
|
|
|
#endif
|
2005-11-06 17:42:20 +00:00
|
|
|
#else
|
|
|
|
#include <netdb.h>
|
2002-06-21 06:15:36 +00:00
|
|
|
#include <sys/ioctl.h>
|
2005-11-06 17:42:20 +00:00
|
|
|
#endif
|
2002-06-21 06:15:36 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.h"
|
|
|
|
#include "core/path.h"
|
2005-11-10 21:33:12 +00:00
|
|
|
|
2002-06-21 06:15:36 +00:00
|
|
|
#if defined(__linux__)
|
2010-02-20 19:57:47 +00:00
|
|
|
#include <linux/cdrom.h>
|
2007-01-10 14:56:23 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
2010-02-20 19:57:47 +00:00
|
|
|
#include <sys/cdio.h>
|
2008-10-13 16:23:55 +00:00
|
|
|
#elif defined(__MINGW32__) || defined(__CYGWIN__)
|
2013-04-05 03:56:35 +00:00
|
|
|
#include <ntddcdrm.h>
|
2010-02-19 13:40:04 +00:00
|
|
|
#elif defined(__bsdi__)
|
2010-02-20 19:57:47 +00:00
|
|
|
#include <dvd.h>
|
2007-12-16 11:31:52 +00:00
|
|
|
#elif defined(__APPLE__) || defined(__DARWIN__)
|
2010-02-20 19:57:47 +00:00
|
|
|
#include <IOKit/storage/IOCDTypes.h>
|
|
|
|
#include <IOKit/storage/IOCDMediaBSDClient.h>
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "compat/mpbswap.h"
|
2002-06-21 06:15:36 +00:00
|
|
|
#endif
|
|
|
|
|
2002-07-10 08:51:11 +00:00
|
|
|
#include "cdd.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_common.h"
|
2002-06-21 06:15:36 +00:00
|
|
|
#include "stream.h"
|
|
|
|
#include "network.h"
|
2008-02-11 09:05:31 +00:00
|
|
|
#include "libavutil/common.h"
|
2002-06-21 06:15:36 +00:00
|
|
|
|
2010-02-20 19:57:47 +00:00
|
|
|
#define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
|
|
|
|
#define DEFAULT_CACHE_DIR "/.cddb/"
|
2002-06-21 06:15:36 +00:00
|
|
|
|
2010-06-21 11:03:03 +00:00
|
|
|
typedef struct {
|
|
|
|
char cddb_hello[1024];
|
|
|
|
unsigned long disc_id;
|
|
|
|
unsigned int tracks;
|
|
|
|
char *cache_dir;
|
|
|
|
char *freedb_server;
|
|
|
|
int freedb_proto_level;
|
|
|
|
int anonymous;
|
|
|
|
char category[100];
|
|
|
|
char *xmcd_file;
|
|
|
|
size_t xmcd_file_size;
|
|
|
|
void *user_data;
|
|
|
|
} cddb_data_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
unsigned int min, sec, frame;
|
|
|
|
} cd_toc_t;
|
|
|
|
|
2002-07-10 08:51:11 +00:00
|
|
|
static cd_toc_t cdtoc[100];
|
2005-09-19 18:22:09 +00:00
|
|
|
static int cdtoc_last_track;
|
2002-06-21 06:15:36 +00:00
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int read_toc(const char *dev)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
int first = 0, last = -1;
|
|
|
|
int i;
|
2008-10-13 16:23:55 +00:00
|
|
|
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
2010-02-20 19:57:47 +00:00
|
|
|
HANDLE drive;
|
|
|
|
DWORD r;
|
|
|
|
CDROM_TOC toc;
|
|
|
|
char device[10];
|
|
|
|
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(device, sizeof(device), "\\\\.\\%s", dev);
|
2010-02-20 19:57:47 +00:00
|
|
|
drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, 0, 0);
|
|
|
|
|
|
|
|
if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
|
|
|
|
sizeof(CDROM_TOC), &r, 0)) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = toc.FirstTrack - 1; last = toc.LastTrack;
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
cdtoc[i].min = toc.TrackData[i].Address[1];
|
|
|
|
cdtoc[i].sec = toc.TrackData[i].Address[2];
|
|
|
|
cdtoc[i].frame = toc.TrackData[i].Address[3];
|
|
|
|
}
|
|
|
|
CloseHandle(drive);
|
2006-11-01 19:57:44 +00:00
|
|
|
|
|
|
|
#else
|
2010-02-20 19:57:47 +00:00
|
|
|
int drive;
|
|
|
|
drive = open(dev, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (drive < 0) {
|
|
|
|
return drive;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-11-01 20:03:32 +00:00
|
|
|
#if defined(__linux__) || defined(__bsdi__)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
struct cdrom_tochdr tochdr;
|
|
|
|
ioctl(drive, CDROMREADTOCHDR, &tochdr);
|
|
|
|
first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
|
|
|
|
}
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
struct cdrom_tocentry tocentry;
|
|
|
|
tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
|
|
|
|
tocentry.cdte_format = CDROM_MSF;
|
|
|
|
ioctl(drive, CDROMREADTOCENTRY, &tocentry);
|
|
|
|
cdtoc[i].min = tocentry.cdte_addr.msf.minute;
|
|
|
|
cdtoc[i].sec = tocentry.cdte_addr.msf.second;
|
|
|
|
cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
|
|
|
|
}
|
2007-01-10 14:56:23 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
struct ioc_toc_header tochdr;
|
|
|
|
ioctl(drive, CDIOREADTOCHEADER, &tochdr);
|
|
|
|
first = tochdr.starting_track - 1; last = tochdr.ending_track;
|
|
|
|
}
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
struct ioc_read_toc_single_entry tocentry;
|
|
|
|
tocentry.track = (i == last) ? 0xAA : i + 1;
|
|
|
|
tocentry.address_format = CD_MSF_FORMAT;
|
|
|
|
ioctl(drive, CDIOREADTOCENTRY, &tocentry);
|
|
|
|
cdtoc[i].min = tocentry.entry.addr.msf.minute;
|
|
|
|
cdtoc[i].sec = tocentry.entry.addr.msf.second;
|
|
|
|
cdtoc[i].frame = tocentry.entry.addr.msf.frame;
|
|
|
|
}
|
2003-01-15 22:44:35 +00:00
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
struct ioc_toc_header tochdr;
|
|
|
|
ioctl(drive, CDIOREADTOCHEADER, &tochdr);
|
|
|
|
first = tochdr.starting_track - 1; last = tochdr.ending_track;
|
|
|
|
}
|
|
|
|
for (i = first; i <= last; i++) {
|
|
|
|
struct ioc_read_toc_entry tocentry;
|
|
|
|
struct cd_toc_entry toc_buffer;
|
|
|
|
tocentry.starting_track = (i == last) ? 0xAA : i + 1;
|
|
|
|
tocentry.address_format = CD_MSF_FORMAT;
|
|
|
|
tocentry.data = &toc_buffer;
|
|
|
|
tocentry.data_len = sizeof(toc_buffer);
|
|
|
|
ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
|
|
|
|
cdtoc[i].min = toc_buffer.addr.msf.minute;
|
|
|
|
cdtoc[i].sec = toc_buffer.addr.msf.second;
|
|
|
|
cdtoc[i].frame = toc_buffer.addr.msf.frame;
|
|
|
|
}
|
2007-12-16 11:31:52 +00:00
|
|
|
#elif defined(__APPLE__) || defined(__DARWIN__)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
dk_cd_read_toc_t tochdr;
|
|
|
|
uint8_t buf[4];
|
|
|
|
uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
|
|
|
|
memset(&tochdr, 0, sizeof(tochdr));
|
|
|
|
tochdr.bufferLength = sizeof(buf);
|
|
|
|
tochdr.buffer = &buf;
|
|
|
|
if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
|
|
|
|
&& tochdr.bufferLength == sizeof(buf)) {
|
|
|
|
first = buf[2] - 1;
|
|
|
|
last = buf[3];
|
|
|
|
}
|
|
|
|
if (last >= 0) {
|
|
|
|
memset(&tochdr, 0, sizeof(tochdr));
|
|
|
|
tochdr.bufferLength = sizeof(buf2);
|
|
|
|
tochdr.buffer = &buf2;
|
|
|
|
tochdr.format = kCDTOCFormatTOC;
|
|
|
|
if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
|
|
|
|
|| tochdr.bufferLength < sizeof(CDTOC))
|
|
|
|
last = -1;
|
|
|
|
}
|
|
|
|
if (last >= 0) {
|
|
|
|
CDTOC *cdToc = (CDTOC *)buf2;
|
|
|
|
CDTrackInfo lastTrack;
|
|
|
|
dk_cd_read_track_info_t trackInfoParams;
|
|
|
|
for (i = first; i < last; ++i) {
|
|
|
|
CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
|
|
|
|
cdtoc[i].min = msf.minute;
|
|
|
|
cdtoc[i].sec = msf.second;
|
|
|
|
cdtoc[i].frame = msf.frame;
|
|
|
|
}
|
|
|
|
memset(&trackInfoParams, 0, sizeof(trackInfoParams));
|
|
|
|
trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
|
|
|
|
trackInfoParams.bufferLength = sizeof(lastTrack);
|
|
|
|
trackInfoParams.address = last;
|
|
|
|
trackInfoParams.buffer = &lastTrack;
|
|
|
|
if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
|
|
|
|
CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
|
|
|
|
+ be2me_32(lastTrack.trackSize));
|
|
|
|
cdtoc[last].min = msf.minute;
|
|
|
|
cdtoc[last].sec = msf.second;
|
|
|
|
cdtoc[last].frame = msf.frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-01 19:57:44 +00:00
|
|
|
#endif
|
2010-02-20 19:57:47 +00:00
|
|
|
close(drive);
|
2002-06-21 06:15:36 +00:00
|
|
|
#endif
|
2010-02-20 19:57:47 +00:00
|
|
|
for (i = first; i <= last; i++)
|
|
|
|
cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
|
|
|
|
return last;
|
2006-11-01 19:14:28 +00:00
|
|
|
}
|
2002-06-21 06:15:36 +00:00
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
/**
|
2005-09-19 18:22:09 +00:00
|
|
|
\brief Reads TOC from CD in the given device and prints the number of tracks
|
|
|
|
and the length of each track in minute:second:frame format.
|
|
|
|
\param *dev the device to analyse
|
|
|
|
\return if the command line -identify is given, returns the last track of
|
|
|
|
the TOC or -1 if the TOC can't be read,
|
|
|
|
otherwise just returns 0 and let cddb_resolve the TOC
|
|
|
|
*/
|
|
|
|
int cdd_identify(const char *dev)
|
|
|
|
{
|
2010-02-20 19:57:47 +00:00
|
|
|
cdtoc_last_track = 0;
|
|
|
|
if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
|
|
|
|
int i, min, sec, frame;
|
|
|
|
cdtoc_last_track = read_toc(dev);
|
|
|
|
if (cdtoc_last_track < 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
|
|
|
|
for (i = 1; i <= cdtoc_last_track; i++) {
|
|
|
|
frame = cdtoc[i].frame - cdtoc[i-1].frame;
|
|
|
|
sec = frame / 75;
|
|
|
|
frame -= sec * 75;
|
|
|
|
min = sec / 60;
|
|
|
|
sec -= min * 60;
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO,
|
|
|
|
"ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cdtoc_last_track;
|
2005-09-19 18:22:09 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static unsigned int cddb_sum(int n)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
unsigned int ret;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (n > 0) {
|
|
|
|
ret += (n % 10);
|
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
return ret;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static unsigned long cddb_discid(int tot_trks)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
unsigned int i, t = 0, n = 0;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < (unsigned int)tot_trks) {
|
|
|
|
n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
|
|
|
|
((cdtoc[0].min * 60) + cdtoc[0].sec);
|
|
|
|
return (n % 0xff) << 24 | t << 8 | tot_trks;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_http_request(char *command,
|
2010-02-20 19:57:47 +00:00
|
|
|
int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
|
|
|
|
cddb_data_t *cddb_data)
|
|
|
|
{
|
|
|
|
char request[4096];
|
|
|
|
int fd, ret = 0;
|
|
|
|
URL_t *url;
|
|
|
|
HTTP_header_t *http_hdr;
|
|
|
|
|
|
|
|
if (reply_parser == NULL || command == NULL || cddb_data == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(request, sizeof(request), "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
|
2010-02-20 19:57:47 +00:00
|
|
|
cddb_data->freedb_server, command, cddb_data->cddb_hello,
|
|
|
|
cddb_data->freedb_proto_level);
|
|
|
|
mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
|
|
|
|
|
|
|
|
url = url_new(request);
|
|
|
|
if (url == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = http_send_request(url,0);
|
|
|
|
if (fd < 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_hdr = http_read_response(fd);
|
|
|
|
if (http_hdr == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_debug_hdr(http_hdr);
|
|
|
|
mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
|
|
|
|
|
|
|
|
switch (http_hdr->status_code) {
|
|
|
|
case 200:
|
|
|
|
ret = reply_parser(http_hdr, cddb_data);
|
|
|
|
break;
|
|
|
|
case 400:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
http_free(http_hdr);
|
|
|
|
url_free(url);
|
|
|
|
|
|
|
|
return ret;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_read_cache(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char file_name[100];
|
|
|
|
struct stat stats;
|
|
|
|
int file_fd, ret;
|
|
|
|
size_t file_size;
|
2002-06-21 06:15:36 +00:00
|
|
|
|
2010-02-20 19:57:47 +00:00
|
|
|
if (cddb_data == NULL || cddb_data->cache_dir == NULL)
|
|
|
|
return -1;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(file_name, sizeof(file_name), "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2010-02-25 09:09:57 +00:00
|
|
|
file_fd = open(file_name, O_RDONLY | O_BINARY);
|
2010-02-20 19:57:47 +00:00
|
|
|
if (file_fd < 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fstat(file_fd, &stats);
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("fstat");
|
|
|
|
file_size = 4096;
|
|
|
|
} else {
|
|
|
|
file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cddb_data->xmcd_file = malloc(file_size + 1);
|
|
|
|
if (cddb_data->xmcd_file == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
close(file_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
|
|
|
|
if (cddb_data->xmcd_file_size != file_size) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
close(file_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
|
|
|
|
|
|
|
|
close(file_fd);
|
|
|
|
|
|
|
|
return 0;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_write_cache(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
// We have the file, save it for cache.
|
|
|
|
char file_name[100];
|
|
|
|
int file_fd, ret;
|
|
|
|
int wrote = 0;
|
|
|
|
|
|
|
|
if (cddb_data == NULL || cddb_data->cache_dir == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Check if the CDDB cache dir exist
|
2012-02-03 07:05:11 +00:00
|
|
|
if (!mp_path_exists(cddb_data->cache_dir)) {
|
2010-02-20 19:57:47 +00:00
|
|
|
// Directory not present, create it.
|
|
|
|
ret = mkdir(cddb_data->cache_dir, 0755);
|
2005-11-06 17:42:20 +00:00
|
|
|
#ifdef __MINGW32__
|
2010-02-20 19:57:47 +00:00
|
|
|
if (ret < 0 && errno != EEXIST) {
|
2005-11-06 17:42:20 +00:00
|
|
|
#else
|
2010-02-20 19:57:47 +00:00
|
|
|
if (ret < 0) {
|
2005-11-06 17:42:20 +00:00
|
|
|
#endif
|
2010-02-20 19:57:47 +00:00
|
|
|
perror("mkdir");
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
|
|
|
|
cddb_data->cache_dir);
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(file_name, sizeof(file_name), "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
|
2010-02-20 19:57:47 +00:00
|
|
|
|
|
|
|
file_fd = creat(file_name, S_IRUSR | S_IWUSR);
|
|
|
|
if (file_fd < 0) {
|
|
|
|
perror("create");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
|
|
|
|
if (wrote < 0) {
|
|
|
|
perror("write");
|
|
|
|
close(file_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
close(file_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(file_fd);
|
|
|
|
|
|
|
|
return 0;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
unsigned long disc_id;
|
|
|
|
char category[100];
|
|
|
|
char *ptr = NULL, *ptr2 = NULL;
|
|
|
|
int ret, status;
|
|
|
|
|
|
|
|
if (http_hdr == NULL || cddb_data == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = sscanf(http_hdr->body, "%d ", &status);
|
|
|
|
if (ret != 1) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 210:
|
|
|
|
ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
|
|
|
|
category, &disc_id);
|
|
|
|
if (ret != 3) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// Check if it's a xmcd database file
|
|
|
|
ptr = strstr(http_hdr->body, "# xmcd");
|
|
|
|
if (ptr == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR,
|
|
|
|
"Invalid xmcd database file returned.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = strdup(ptr);
|
|
|
|
// Ok found the beginning of the file
|
|
|
|
// look for the end
|
|
|
|
ptr2 = strstr(ptr, "\n.\r\n");
|
|
|
|
if (!ptr2)
|
|
|
|
ptr2 = strstr(ptr, "\n.\n");
|
|
|
|
if (ptr2) {
|
|
|
|
ptr2++;
|
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
|
|
|
|
ptr2 = ptr + strlen(ptr); //return -1;
|
|
|
|
}
|
|
|
|
// Ok found the end
|
|
|
|
// do a sanity check
|
|
|
|
if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cddb_data->xmcd_file = ptr;
|
|
|
|
cddb_data->xmcd_file_size = ptr2 - ptr;
|
|
|
|
cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
|
|
|
|
return cddb_write_cache(cddb_data);
|
|
|
|
default:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_request_titles(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char command[1024];
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(command, sizeof(command), "cddb+read+%s+%08lx",
|
2010-02-20 19:57:47 +00:00
|
|
|
cddb_data->category, cddb_data->disc_id);
|
|
|
|
return cddb_http_request(command, cddb_read_parse, cddb_data);
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char album_title[100];
|
|
|
|
char *ptr = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ptr = strstr(http_hdr->body, "\n");
|
|
|
|
if (ptr == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
// We have a list of exact/inexact matches, so which one do we use?
|
|
|
|
// So let's take the first one.
|
|
|
|
ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
|
|
|
|
&(cddb_data->disc_id), album_title);
|
|
|
|
if (ret != 3) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = strstr(http_hdr->body, album_title);
|
|
|
|
if (ptr != NULL) {
|
|
|
|
char *ptr2;
|
|
|
|
int len;
|
|
|
|
ptr2 = strstr(ptr, "\n");
|
|
|
|
if (ptr2 == NULL) {
|
|
|
|
len = (http_hdr->body_size)-(ptr-(http_hdr->body));
|
|
|
|
} else {
|
|
|
|
len = ptr2-ptr+1;
|
|
|
|
}
|
|
|
|
len = FFMIN(sizeof(album_title) - 1, len);
|
|
|
|
strncpy(album_title, ptr, len);
|
|
|
|
album_title[len]='\0';
|
|
|
|
}
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
|
2010-02-20 19:57:47 +00:00
|
|
|
return 0;
|
2003-01-03 20:50:26 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char album_title[100];
|
|
|
|
char *ptr = NULL;
|
|
|
|
int ret, status;
|
|
|
|
|
|
|
|
ret = sscanf(http_hdr->body, "%d ", &status);
|
|
|
|
if (ret != 1) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 200:
|
|
|
|
// Found exact match
|
|
|
|
ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
|
|
|
|
cddb_data->category, &(cddb_data->disc_id), album_title);
|
|
|
|
if (ret != 4) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr = strstr(http_hdr->body, album_title);
|
|
|
|
if (ptr != NULL) {
|
|
|
|
char *ptr2;
|
|
|
|
int len;
|
|
|
|
ptr2 = strstr(ptr, "\n");
|
|
|
|
if (ptr2 == NULL) {
|
|
|
|
len = (http_hdr->body_size)-(ptr-(http_hdr->body));
|
|
|
|
} else {
|
|
|
|
len = ptr2-ptr+1;
|
|
|
|
}
|
|
|
|
len = FFMIN(sizeof(album_title) - 1, len);
|
|
|
|
strncpy(album_title, ptr, len);
|
|
|
|
album_title[len]='\0';
|
|
|
|
}
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
|
2010-02-20 19:57:47 +00:00
|
|
|
return cddb_request_titles(cddb_data);
|
|
|
|
case 202:
|
|
|
|
// No match found
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
break;
|
|
|
|
case 210:
|
|
|
|
// Found exact matches, list follows
|
|
|
|
cddb_parse_matches_list(http_hdr, cddb_data);
|
|
|
|
return cddb_request_titles(cddb_data);
|
2002-06-21 06:15:36 +00:00
|
|
|
/*
|
|
|
|
body=[210 Found exact matches, list follows (until terminating `.')
|
|
|
|
misc c711930d Santana / Supernatural
|
|
|
|
rock c711930d Santana / Supernatural
|
|
|
|
blues c711930d Santana / Supernatural
|
|
|
|
.]
|
2009-07-06 23:26:13 +00:00
|
|
|
*/
|
2010-02-20 19:57:47 +00:00
|
|
|
case 211:
|
|
|
|
// Found inexact matches, list follows
|
|
|
|
cddb_parse_matches_list(http_hdr, cddb_data);
|
|
|
|
return cddb_request_titles(cddb_data);
|
|
|
|
case 500:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
|
|
|
|
"Server returns: Command syntax error\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
int max;
|
|
|
|
int ret, status;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ret = sscanf(http_hdr->body, "%d ", &status);
|
|
|
|
if (ret != 1) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case 210:
|
|
|
|
ptr = strstr(http_hdr->body, "max proto:");
|
|
|
|
if (ptr == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = sscanf(ptr, "max proto: %d", &max);
|
|
|
|
if (ret != 1) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cddb_data->freedb_proto_level = max;
|
|
|
|
return 0;
|
|
|
|
default:
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_get_proto_level(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static void cddb_create_hello(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char host_name[51];
|
|
|
|
char *user_name;
|
|
|
|
|
|
|
|
if (cddb_data->anonymous) { // Default is anonymous
|
|
|
|
/* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
|
|
|
|
* We don't send current user/host name in hello to prevent spam.
|
|
|
|
* Software that sends this is considered spyware
|
|
|
|
* that most people don't like.
|
|
|
|
*/
|
|
|
|
user_name = "anonymous";
|
|
|
|
strcpy(host_name, "localhost");
|
|
|
|
} else {
|
|
|
|
if (gethostname(host_name, 50) < 0) {
|
|
|
|
strcpy(host_name, "localhost");
|
|
|
|
}
|
|
|
|
user_name = getenv("LOGNAME");
|
|
|
|
}
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(cddb_data->cddb_hello, sizeof(cddb_data->cddb_hello),
|
|
|
|
"&hello=%s+%s+%s",
|
2010-06-28 08:26:14 +00:00
|
|
|
user_name, host_name, mplayer_version);
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static int cddb_retrieve(cddb_data_t *cddb_data)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char offsets[1024], command[1024];
|
|
|
|
char *ptr;
|
|
|
|
unsigned int i, time_len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ptr = offsets;
|
|
|
|
for (i = 0; i < cddb_data->tracks ; i++) {
|
2012-10-31 18:55:33 +00:00
|
|
|
unsigned space = sizeof(offsets) - (ptr - offsets);
|
|
|
|
if (space < 40) break;
|
|
|
|
ptr += snprintf(ptr, space, "%d+", cdtoc[i].frame);
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
ptr[0] = 0;
|
|
|
|
time_len = (cdtoc[cddb_data->tracks].frame)/75;
|
|
|
|
|
|
|
|
cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
|
|
|
|
cddb_data->freedb_proto_level = 1;
|
|
|
|
cddb_data->xmcd_file = NULL;
|
|
|
|
|
|
|
|
cddb_create_hello(cddb_data);
|
|
|
|
if (cddb_get_proto_level(cddb_data) < 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(command, sizeof(command), "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
|
2010-02-20 19:57:47 +00:00
|
|
|
cddb_data->tracks, offsets, time_len);
|
|
|
|
ret = cddb_http_request(command, cddb_query_parse, cddb_data);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
2010-11-07 12:47:40 +00:00
|
|
|
free(cddb_data->cache_dir);
|
2010-02-20 19:57:47 +00:00
|
|
|
return 0;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 19:57:47 +00:00
|
|
|
int cddb_resolve(const char *dev, char **xmcd_file)
|
|
|
|
{
|
|
|
|
char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
|
|
|
|
char *home_dir = NULL;
|
|
|
|
cddb_data_t cddb_data;
|
2012-12-09 14:05:21 +00:00
|
|
|
void *talloc_ctx = talloc_new(NULL);
|
2010-02-20 19:57:47 +00:00
|
|
|
|
|
|
|
if (cdtoc_last_track <= 0) {
|
|
|
|
cdtoc_last_track = read_toc(dev);
|
|
|
|
if (cdtoc_last_track < 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cddb_data.tracks = cdtoc_last_track;
|
|
|
|
cddb_data.disc_id = cddb_discid(cddb_data.tracks);
|
|
|
|
cddb_data.anonymous = 1; // Don't send user info by default
|
|
|
|
|
|
|
|
mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
|
|
|
|
cddb_data.disc_id);
|
|
|
|
|
|
|
|
// Check if there is a CD in the drive
|
|
|
|
// FIXME: That's not really a good way to check
|
|
|
|
if (cddb_data.disc_id == 0) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
home_dir = getenv("HOME");
|
2005-11-06 17:42:20 +00:00
|
|
|
#ifdef __MINGW32__
|
2010-02-20 19:57:47 +00:00
|
|
|
if (home_dir == NULL)
|
|
|
|
home_dir = getenv("USERPROFILE");
|
|
|
|
if (home_dir == NULL)
|
|
|
|
home_dir = getenv("HOMEPATH");
|
|
|
|
// Last resort, store the cddb cache in the mplayer directory
|
|
|
|
if (home_dir == NULL)
|
2012-12-09 14:05:21 +00:00
|
|
|
home_dir = (char *)talloc_steal(talloc_ctx,
|
|
|
|
mp_find_user_config_file(""));
|
2005-11-06 17:42:20 +00:00
|
|
|
#endif
|
2010-02-20 19:57:47 +00:00
|
|
|
if (home_dir == NULL) {
|
|
|
|
cddb_data.cache_dir = NULL;
|
|
|
|
} else {
|
2012-10-31 18:55:33 +00:00
|
|
|
unsigned len = strlen(home_dir) + strlen(cddb_cache_dir) + 1;
|
|
|
|
cddb_data.cache_dir = malloc(len);
|
2010-02-20 19:57:47 +00:00
|
|
|
if (cddb_data.cache_dir == NULL) {
|
2010-03-09 22:34:16 +00:00
|
|
|
mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
|
2012-12-09 14:05:21 +00:00
|
|
|
talloc_free(talloc_ctx);
|
2010-02-20 19:57:47 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-31 18:55:33 +00:00
|
|
|
snprintf(cddb_data.cache_dir, len, "%s%s", home_dir, cddb_cache_dir);
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
2012-12-09 14:05:21 +00:00
|
|
|
talloc_free(talloc_ctx);
|
2010-02-20 19:57:47 +00:00
|
|
|
|
|
|
|
// Check for a cached file
|
|
|
|
if (cddb_read_cache(&cddb_data) < 0) {
|
|
|
|
// No Cache found
|
|
|
|
if (cddb_retrieve(&cddb_data) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cddb_data.xmcd_file != NULL) {
|
|
|
|
// printf("%s\n", cddb_data.xmcd_file);
|
|
|
|
*xmcd_file = cddb_data.xmcd_file;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 19:57:47 +00:00
|
|
|
/***************
|
|
|
|
* xmcd parser *
|
|
|
|
***************/
|
2010-02-25 22:27:53 +00:00
|
|
|
static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char *ptr, *album;
|
|
|
|
ptr = strstr(line, "DTITLE=");
|
|
|
|
if (ptr != NULL) {
|
|
|
|
ptr += 7;
|
|
|
|
album = strstr(ptr, "/");
|
|
|
|
if (album == NULL)
|
|
|
|
return NULL;
|
|
|
|
cd_info->album = malloc(strlen(album + 2) + 1);
|
|
|
|
if (cd_info->album == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy(cd_info->album, album + 2);
|
|
|
|
album--;
|
|
|
|
album[0] = '\0';
|
|
|
|
cd_info->artist = malloc(strlen(ptr) + 1);
|
|
|
|
if (cd_info->artist == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy(cd_info->artist, ptr);
|
|
|
|
}
|
|
|
|
return ptr;
|
2002-07-10 08:51:11 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
ptr = strstr(line, "DGENRE=");
|
|
|
|
if (ptr != NULL) {
|
|
|
|
ptr += 7;
|
|
|
|
cd_info->genre = malloc(strlen(ptr)+1);
|
|
|
|
if (cd_info->genre == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
strcpy(cd_info->genre, ptr);
|
|
|
|
}
|
|
|
|
return ptr;
|
2002-06-21 06:15:36 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:27:53 +00:00
|
|
|
static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
|
2010-02-20 19:57:47 +00:00
|
|
|
{
|
|
|
|
unsigned int track_nb;
|
|
|
|
unsigned long sec, off;
|
|
|
|
char *ptr;
|
|
|
|
ptr = strstr(line, "TTITLE");
|
|
|
|
if (ptr != NULL) {
|
|
|
|
ptr += 6;
|
|
|
|
// Here we point to the track number
|
|
|
|
track_nb = atoi(ptr);
|
|
|
|
ptr = strstr(ptr, "=");
|
|
|
|
if (ptr == NULL)
|
|
|
|
return NULL;
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
sec = cdtoc[track_nb].frame;
|
|
|
|
off = cdtoc[track_nb + 1].frame - sec + 1;
|
|
|
|
|
|
|
|
cd_info_add_track(cd_info, ptr, track_nb + 1,
|
|
|
|
(unsigned int) (off / (60 * 75)),
|
|
|
|
(unsigned int) ((off / 75) % 60),
|
|
|
|
(unsigned int) (off % 75),
|
|
|
|
sec, off);
|
|
|
|
}
|
|
|
|
return ptr;
|
2002-07-10 08:51:11 +00:00
|
|
|
}
|
|
|
|
|
2010-02-20 19:57:47 +00:00
|
|
|
cd_info_t *cddb_parse_xmcd(char *xmcd_file)
|
|
|
|
{
|
|
|
|
cd_info_t *cd_info = NULL;
|
|
|
|
int length, pos = 0;
|
|
|
|
char *ptr, *ptr2;
|
|
|
|
if (xmcd_file == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cd_info = cd_info_new();
|
|
|
|
if (cd_info == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
length = strlen(xmcd_file);
|
|
|
|
ptr = xmcd_file;
|
|
|
|
while (ptr != NULL && pos < length) {
|
|
|
|
// Read a line
|
|
|
|
ptr2 = ptr;
|
|
|
|
while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
|
|
|
|
ptr2++;
|
|
|
|
if (ptr2[0] == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr2[0] = '\0';
|
|
|
|
// Ignore comments
|
|
|
|
if (ptr[0] != '#') {
|
|
|
|
// Search for the album title
|
|
|
|
if (xmcd_parse_dtitle(cd_info, ptr))
|
|
|
|
;
|
|
|
|
// Search for the genre
|
|
|
|
else if (xmcd_parse_dgenre(cd_info, ptr))
|
|
|
|
;
|
|
|
|
// Search for a track title
|
|
|
|
else if (xmcd_parse_ttitle(cd_info, ptr))
|
2013-01-15 21:15:55 +00:00
|
|
|
;
|
2010-02-20 19:57:47 +00:00
|
|
|
}
|
|
|
|
if (ptr2[1] == '\n')
|
|
|
|
ptr2++;
|
|
|
|
pos = (ptr2 + 1) - ptr;
|
|
|
|
ptr = ptr2 + 1;
|
|
|
|
}
|
|
|
|
|
2013-01-15 21:15:55 +00:00
|
|
|
unsigned int audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
|
2010-02-20 19:57:47 +00:00
|
|
|
cd_info->min = (unsigned int) (audiolen / (60 * 75));
|
|
|
|
cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
|
|
|
|
cd_info->msec = (unsigned int) (audiolen % 75);
|
|
|
|
|
|
|
|
return cd_info;
|
2002-07-10 08:51:11 +00:00
|
|
|
}
|