aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_flv/FLVReader.cpp
blob: c756cc703119a8625707ff587b95c10de760248d (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
#include "FLVReader.h"
#include "FLVHeader.h"
#include "FLVStreamheader.h"
#include "BackgroundDownloader.h"
#include "../nu/AutoChar.h"
#include "FileProcessor.h"
#include "ProgressiveProcessor.h"
#include "StreamProcessor.h"
#include "../../..\Components\wac_network\wac_network_http_receiver_api.h"
#include <shlwapi.h>

FLVReader::FLVReader(const wchar_t *_url)
{
	processor = 0;
	url = _wcsdup(_url);
	end_of_stream=false;

	killswitch=false;
	DWORD threadId;
	flvThread=CreateThread(NULL, NULL, ParserThreadStub, this, 0, &threadId);
	SetThreadPriority(flvThread, THREAD_PRIORITY_BELOW_NORMAL);
}

FLVReader::~FLVReader()
{
	free(url);
	delete processor;
}

uint64_t FLVReader::Seek(uint64_t position)
{
	if (processor)
		return processor->Seek(position);
	else
		return -1;
}

size_t FLVReader::Read(void *data, size_t bytes)
{
	if (processor)
		return processor->Read(data, bytes);
	else
		return 0;
}

uint64_t FLVReader::GetProcessedPosition()
{
	if (processor)
		return processor->GetProcessedPosition();
	else
		return 0;
}

void FLVReader::ProcessFile()
{
	if (!processor)
		return;

	while (processor->Process() == FLV_OK)
	{
		if (killswitch) 
			return ;
	}
}

int FLVReader::OnConnect(api_httpreceiver *http)
{
	if (http->content_length())
		processor = new ProgressiveProcessor;
	else
		processor = new StreamProcessor;
	return 0;
}

int FLVReader::OnData(void *data, size_t datalen)
{
	if (!processor)
		return 1;

	bool needSleep=false;
	while (datalen)
	{
		if (killswitch)	
			return 1;

		if (needSleep)
		{
			Sleep(10);
			needSleep=false;
		}

		size_t written=0;
		switch (processor->Write(data, datalen, &written))
		{
		case FLVPROCESSOR_WRITE_ERROR:
			return 1;
		case FLVPROCESSOR_WRITE_WAIT:
			needSleep=true;
			break;
		}
			
		datalen -= written;

		if (written)
		{
			while (1)
			{
				if (killswitch) 
					return 1;
	
				int ret = processor->Process();
				if (ret == FLV_OK)
					continue;
				if (ret == FLV_NEED_MORE_DATA)
					break;
				if (ret == FLV_ERROR)
					return 1;
			}
		}
	}
	return !!killswitch;
}

bool FLVReader::GetFrame(size_t frameIndex, FrameData &frameData)
{
	if (processor)
		return processor->GetFrame(frameIndex, frameData);
	else
	return false;
}

/*
Test URLs (valid of as oct 23 2007)
http://pdl.stream.aol.com/aol/us/aolmusic/artists/astralwerks/2220s/2220s_shootyourgun_hakjfh_700_dl.flv
http://pdl.stream.aol.com/aol/us/aolmusic/sessions/2007/amberpacific/suc_amberpacific_fallbackintomylife_700_dl.flv
http://pdl.stream.aol.com/aol/us/aolmusic/sessions/2007/amberpacific/suc_amberpacific_gonesoyoung_700_dl.flv
http://pdl.stream.aol.com/aol/us/aolmusic/sessions/2007/amberpacific/suc_amberpacific_soyesterday_700_dl.flv
http://pdl.stream.aol.com/aol/us/aolmusic/sessions/2007/amberpacific/suc_amberpacific_watchingoverme_700_dl.flv

a potential crasher:
http://pdl.stream.aol.com/aol/us/aolcomvideo/TVGuide/johncmcginley_6346/johncmcginley_6346_460_700_dl.flv

FLV streams:
http://208.80.52.96/CBS_R20_452P_F128?ext=.flv
http://208.80.54.37/KROQFM?ext=.flv
http://208.80.52.96/CBS_R20_568P_F128?ext=.flv
*/
DWORD FLVReader::ParserThread()
{
	if (PathIsURL(url))
	{
		Downloader downloader;
		downloader.Download(AutoChar(url), this);
	}
	else
	{		
		processor = new FileProcessor(url);
		ProcessFile();
	}
	end_of_stream=true;
	return 0;
}

void FLVReader::Kill()
{
	killswitch=true;
	WaitForSingleObject(flvThread, INFINITE);
	CloseHandle(flvThread);
}

void FLVReader::SignalKill()
{
	killswitch=true;
}

bool FLVReader::IsEOF()
{
	return end_of_stream;
}

bool FLVReader::GetPosition(int time_in_ms, size_t *frameIndex, bool needVideoKeyFrame)
{
	if (processor)
		return processor->GetPosition(time_in_ms, frameIndex, needVideoKeyFrame);
	else
		return false;
}

uint32_t FLVReader::GetMaxTimestamp()
{
	if (processor)
		return processor->GetMaxTimestamp();
	else
		return 0;
}

bool FLVReader::IsStreaming()
{
		if (processor)
		return processor->IsStreaming();
	else
		return true;
}

FLVHeader *FLVReader::GetHeader()
{
	if (processor)
		return processor->GetHeader();
	else
		return 0;
}