aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_cdda/ExtendedRead.cpp
blob: 007b6a6532f4e02c4e682f89aa426047f7a727a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "main.h"

#include "CDPlay.h"
#include "DAEPlay.h"
#include "MCIPlay.h"
#include "WindacPlay.h"

#include "api__in_cdda.h"
#include "workorder.h"
#include "CDDB.h"

//returns handle!=0 if successful, 0 if error
//size will return the final nb of bytes written to the output, -1 if unknown
//TODO> add output format stuff (srate, nch, bps)
extern "C" __declspec(dllexport) intptr_t winampGetExtendedRead_openW(const wchar_t *fn, int *size, int *bps, int *nch, int *srate)
{
	s_last_error = NULL;
	C_CDPlay *wp = NULL;
	int ret = 1;

	wchar_t device = 0;
	int track = -1;
	if (!ParseName(fn, device, track))
		return 0;

	if (playStatus[device].IsRipping() || (g_cdplay && g_cdplay->IsPlaying(device)))
	{
		wchar_t title[32] = {0};
		MessageBoxW(NULL, WASABI_API_LNGSTRINGW(IDS_CD_CURRENTLY_IN_USE),
		            WASABI_API_LNGSTRINGW_BUF(IDS_DRIVE_IN_USE,title,32),
					MB_ICONWARNING | MB_OK);
		return -1;
	}

	// Get a ICddbDisc object for MusicID
	#ifndef IGNORE_API_GRACENOTE
	ICddbDiscPtr pDisc = NULL;
	OpenMusicIDWorkOrder();
	if (workorder)
	{
		MCIDEVICEID submitDev = 0;
		if (!CDOpen(&submitDev, device, L"MusicID")) submitDev = 0;
		if (submitDev)
		{
			DINFO info;
			ret = GetDiscID(submitDev, &info);
			CDClose(&submitDev);
			if (ret == 0)
			{
				wchar_t szTOC[2048] = {0};
				if (Cddb_CalculateTOC(&info, szTOC, sizeof(szTOC)/sizeof(wchar_t)))
					Cddb_GetDiscFromCache(szTOC, &pDisc);
			}
		}
	}

	if (config_rip_veritas)
	{
		VeritasPlay *vp = new VeritasPlay(true);
		wp = vp;
		ret = wp->open(device, track);
		if (ret)
		{
			delete(wp);
			wp = NULL;
		}
		else if (workorder && pDisc != 0) // see if MusicID is interested
		{
			void *handle = 0;
			workorder->GetSigHandle(&handle, pDisc, track);
			vp->submitHandle = handle;
		}
	}
	#endif

	if (ret)
	{
		DAEPlay *dae = new DAEPlay;
		if (dae)
		{
			wp = dae;
			ret = wp->open(device, track);
			if (ret)
			{
				delete(wp);
				wp = NULL;
			}
		}
		else
		{
			wp = NULL;
		}
	}

	if (ret)
	{
		wp = new WindacPlay;
		if (wp->open(device, track))
		{
			delete(wp);
			return 0;
		}
	}

	if (size && wp)
	{
		double s = (double)wp->getlength() / 1000;
		s *= (44100 * 4);
		*size = (int)s;
	}
	if (srate) *srate = 44100;
	if (nch) *nch = 2;
	if (bps) *bps = 16;
	playStatus[device].RippingStarted();
	return (intptr_t)wp;
}

//returns nb of bytes read. -1 if read error (like CD ejected). if (ret<len), EOF is assumed
extern "C" __declspec(dllexport) int winampGetExtendedRead_getData(intptr_t handle, char *dest, int len, int *killswitch)
{
	s_last_error = NULL;
	C_CDPlay *wp = (C_CDPlay*)handle;
	return (wp ? wp->read(dest, len, killswitch) : -1);
}

extern "C" __declspec(dllexport) void winampGetExtendedRead_close(intptr_t handle)
{
	s_last_error = NULL;
	C_CDPlay *wp = (C_CDPlay*)handle;
	if (wp)
	{
		playStatus[wp->g_drive].RippingStopped();
		delete wp;
		wp = 0;
	}
}

// extended info writing (used by gen_ml to update the local CDDB database after a burn)

extern "C" __declspec(dllexport) char * winampGetExtendedRead_lasterror()
{
	char * retval = s_last_error;
	s_last_error = NULL;
	return retval;
}