aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_wmvdrm/BufferLayer.cpp
blob: d41a7cfd709a18f7b898c2147fb8ebe628c8413a (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
#include "BufferLayer.h"
#include "Main.h"
#include "resource.h"
#define killEvent events[0]
#define startEvent events[1]

enum
{
    KILL_EVENT = 0,
    START_EVENT = 1,
};

DWORD WINAPI BufferLayer::BufThread_stub(void *ptr)
{
	((BufferLayer *)ptr)->BufThread();
	return 0;
}

BufferLayer::BufferLayer(IWMReader *reader) : reader2(0), buffering(false)
{
	if (FAILED(reader->QueryInterface(&reader2)))
		reader2 = 0;

	startEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	killEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	DWORD id;
	thread = CreateThread(NULL, 128*1024, BufThread_stub, (void *)this, NULL, &id);
}

BufferLayer::~BufferLayer()
{
	SetEvent(killEvent);
	ResetEvent(startEvent);
	WaitForSingleObject(thread, INFINITE);
	if (reader2) reader2->Release(); reader2 = 0;
}


void BufferLayer::BufferingStarted()
{
	winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_BUFFERING));
	buffering=true;
	SetEvent(startEvent);
	WMHandler::BufferingStarted();
}

void BufferLayer::BufferingStopped()
{
	winamp.SetStatus(L"");
	buffering=false;
	ResetEvent(startEvent);
	WMHandler::BufferingStopped();
}

int BufferLayer::Wait()
{
	if (WaitForSingleObject(killEvent, 0) == WAIT_OBJECT_0)
		return KILL_EVENT;

	return WaitForMultipleObjects(2, events, FALSE, INFINITE) - WAIT_OBJECT_0;

}

void BufferLayer::BufThread()
{
	do
	{
		switch (Wait())
		{
		case KILL_EVENT:
			return ;
		case START_EVENT:
			{
				if (reader2)
				{
					DWORD percent;
					QWORD throwAway;
					if (SUCCEEDED(reader2->GetBufferProgress(&percent, &throwAway)))
						winamp.Buffering(percent, WASABI_API_LNGSTRINGW(IDS_BUFFERING));

				}
				Sleep(10);
			}
			continue;
		}
	}
	while (true);
}

void BufferLayer::OpenFailed()
{
	ResetEvent(startEvent);
	WMHandler::OpenFailed();
}