aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/OutputPluginAudioStream.cpp
blob: f724aad187cf14fbe50ac5c1555988d0cf2ae74b (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/** (c) Nullsoft, Inc.         C O N F I D E N T I A L
 ** Filename: 
 ** Project:
 ** Description:
 ** Author: Ben Allison benski@nullsoft.com
 ** Created:
 **/
#include "main.h"
#include "OutputPluginAudioStream.h"
#include "out.h"
#include "api.h"
#include "WinampAttributes.h"

int m_converting = 0;
static volatile int streamsInUse = 0;
static void * volatile streamBuffer = 0;
static volatile size_t streamCanWrite = 0;
static volatile HANDLE streamWait = 0;
static volatile HANDLE streamGo = 0;
static volatile HANDLE streamKill = 0;

static volatile bool streamPlaying = false;
static volatile AudioParameters *streamParameters = 0;
static In_Module * volatile streamIn = 0;
static volatile int opens = 0;

void ConvertEOF()
{
	streamPlaying = false;
	//if (--opens==0)
	SetEvent(streamWait);
}

static int StreamOpen(int samplerate, int numchannels, int bitspersamp, int bufferlenms, int prebufferms)
{
	streamParameters->bitsPerSample = bitspersamp;
	streamParameters->channels = numchannels;
	streamParameters->sampleRate = samplerate;
	streamParameters->sizeBytes = (size_t) - 1;

	// we will try to use GetFileInfo to get a length
	int lengthMS;
	InW_GetFileInfo(streamIn, 0, 0, &lengthMS);
	if (lengthMS > 0)
	{
		streamParameters->sizeBytes = MulDiv(lengthMS, numchannels * bitspersamp * samplerate, 8000);
	}

	streamPlaying = true;

	return 0;
}

static void StreamClose()
{
	streamPlaying = false;
	// SetEvent(streamWait);
}

static int StreamWrite(char *buf, int len)
{

again:
	// null buffer means EOF
	if (buf == NULL)
	{
		streamPlaying = false;
		//SetEvent(streamWait);
		return 0;
	}

	if (streamCanWrite == 0)
	{
		//Sleep(10); // input plugin isn't checking StreamCanWrite() properly, so we'll sleep for them
		//return 1;
	}

	// copy into user-supplied buffer
	int toCopy = min((int)streamCanWrite, len);
	memcpy(streamBuffer, buf, toCopy);
	streamCanWrite -= toCopy;
	streamBuffer = ((char *)streamBuffer) + toCopy;

	// the input plugin may have given us too much data, so we'll have to check that
	// increment the user's stuff
	len -= toCopy;
	buf += toCopy;
	if (len) // len>0 implies streamCanWrite == 0
	{
		ResetEvent(streamGo);
		SetEvent(streamWait);

		/* benski> this Sleep() code causes a major slowdown,
		             probably because of high thread priorities in some plugins
		while (streamCanWrite == 0) 
			Sleep(100);
			*/
		HANDLE events[2] = {streamKill, streamGo};
		switch (WaitForMultipleObjects(2, events, FALSE, INFINITE))
		{
		case WAIT_OBJECT_0 + 1:
			goto again;
		default:
			return 0;
		}
	}

	// signal event to let ReadAudio() return, if the buffer is full
	if (streamCanWrite == 0)
		SetEvent(streamWait);
	return 0;
}


static int StreamCanWrite()
{
	if (streamCanWrite)
		return 65536;
	else
		return 0;
}

static int StreamIsPlaying()
{
	return 0;
}

static void StreamSet(int nothing)
{}

static int StreamGetOutputTime()
{
	return 0;
}

static int StreamGetWrittenTime()
{
	return 0;
}

static Out_Module streamOut =
    {
        OUT_VER,
        "dummy output",
        14000,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        StreamOpen,
        StreamClose,
        StreamWrite,
        StreamCanWrite,
        StreamIsPlaying,
        NULL,    //pause
        StreamSet,    //setvolume
        StreamSet,    //setpan
        StreamSet,    //flush
        StreamGetOutputTime,
        StreamGetWrittenTime,
    };

OutputPluginAudioStream::OutputPluginAudioStream()
{
	oldBits=config_audio_bits;
	oldSurround=config_audio_surround;
	oldMono=config_audio_mono;
	oldRG=config_replaygain;
}

bool OutputPluginAudioStream::Open(In_Module *in, const wchar_t *filename, AudioParameters *parameters)
{
	// set some globals, since winamp output plugins don't have user data/context pointers
	opens++;
	m_converting = 1;

	// this will ask the input plugin to produce our output format (not a guarantee, though)
	config_audio_bits = parameters->bitsPerSample;
	config_audio_surround = (parameters->channels > 2);
	config_audio_mono = (parameters->channels == 1);
	config_replaygain=false;

	streamWait = CreateEvent(NULL, FALSE, FALSE, NULL);
	streamGo = CreateEvent(NULL, FALSE, FALSE, NULL);
	streamKill = CreateEvent(NULL, TRUE, FALSE, NULL);
	streamCanWrite = 0;
	streamBuffer = 0;
	streamPlaying = false;
	streamParameters = parameters;
	streamIn = in;

	in->outMod = &streamOut;

	int ret = InW_Play(in, filename);
	if (ret)
	{
		parameters->errorCode = API_DECODEFILE_FAILURE;
		opens--;
		return false;
	}

	if (in->UsesOutputPlug&IN_MODULE_FLAG_USES_OUTPUT_PLUGIN)
	{
		int cnt = 5000;
		while (!streamPlaying && cnt > 0)
		{
			MSG msg;
			if (PeekMessage(&msg, NULL, 0, 0, FALSE))
				WASABI_API_APP->app_messageLoopStep();
			else
			{
				Sleep(1);
				cnt--;
			}
		}
	}
	else
	{
		parameters->errorCode = API_DECODEFILE_NO_INTERFACE;
		opens--;
		return false;
	}

	if (!streamPlaying)
	{
		parameters->errorCode = API_DECODEFILE_FAILURE;
		opens--;
		return false;
	}

	return true;
}

size_t OutputPluginAudioStream::ReadAudio(void *buffer, size_t sizeBytes)
{
	streamBuffer = buffer;
	streamCanWrite = sizeBytes;
	SetEvent(streamGo);
	HANDLE events[2] = {streamKill, streamWait};
	switch (WaitForMultipleObjects(2, events, FALSE, INFINITE))
	{
	case WAIT_OBJECT_0 + 1:  // streamWait, which gets triggered when buffer is full or output is done
		return sizeBytes - streamCanWrite; // streamCanWrite will be >0 if there was a partial write, e.g. on EOF
	default:
		return 0; // no point
	}

	return sizeBytes - streamCanWrite;
}

OutputPluginAudioStream::~OutputPluginAudioStream()
{
	SetEvent(streamKill);
	streamIn->Stop();
	DeleteObject(streamWait);
	DeleteObject(streamGo);
	DeleteObject(streamKill);
	streamWait = 0;
	config_audio_bits = oldBits;
	config_audio_surround = oldSurround;
	config_audio_mono=oldMono;
	config_replaygain=oldRG;
}

#define CBCLASS OutputPluginAudioStream
START_DISPATCH;
CB(IFC_AUDIOSTREAM_READAUDIO, ReadAudio)
END_DISPATCH;