aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_flv/StreamProcessor.cpp
blob: d5331cdcdf034f5a2c195443c06246acf41c596b (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
#include "StreamProcessor.h"
#include "FLVHeader.h"

StreamProcessor::StreamProcessor()
{
	buffer.reserve(1024*1024);
	bytesWritten=0;
	readHeader=false;
}

int StreamProcessor::Write(void *data, size_t datalen, size_t *written)
{
	*written = buffer.write(data, datalen);
	bytesWritten += *written;
	if (*written != datalen)
		return FLVPROCESSOR_WRITE_WAIT; // tell the FLVReader we need a break :)
	return FLVPROCESSOR_WRITE_OK;
}

int StreamProcessor::Process()
{
	// we're actually not going to parse anything until the GetFrame() call.
	// since we can't seek anyway
	// but we do need to validate that this is an FLV stream

	if (!readHeader) 
	{
		if (buffer.size() >= 9) // see if we have enough data
		{
			uint8_t data[9] = {0};
			buffer.read(data, 9);

			if (header.Read(data, 9))
			{
				readHeader=true;
				return FLV_OK;
			}
			else // not an FLV header
			{
				return FLV_ERROR;
			}
		}
	}

	return FLV_NEED_MORE_DATA;
}

uint64_t StreamProcessor::GetProcessedPosition()
{
	// since we parse the bitstream on-demand, we'll just return how many bytes we've buffered so far
	if (readHeader) // make sure we've at least found the main FLV header
		return bytesWritten;
	else
		return 0;
}

uint32_t StreamProcessor::GetMaxTimestamp()
{
	return -1000; // it's a stream! no length!
}

uint64_t StreamProcessor::Seek(uint64_t position)
{
	// we can't really seek in a traditional sense, but since Seek gets called to simply advance the read pointer, 
	// we'll advance within our buffer
	// we always set FrameData::location to 0, so can just call like this
	return buffer.advance((size_t)position);
}

size_t StreamProcessor::Read(void *data, size_t bytes)
{
	// easy :)
	return buffer.read(data, bytes);
}

// the fun happens here
bool StreamProcessor::GetFrame(size_t frameIndex, FrameData &frameData)
{
	// since this is a stream, we're going to ignore frameIndex and just give them the next frame

	if (buffer.size() >= 15)
	{
		uint8_t data[15] = {0};
		buffer.peek(data, 15);
		if (frameData.header.Read(data, 15))
		{
			// first, make sure we have enough data buffered to read the whole thing
			// because the next thing to get called after this function is Read()
			if (frameData.header.dataSize + 15 <= buffer.size()) 
			{
				 // since we're streaming and only processing one frame at a time
				// we're going to set the returned frame's location to 0 (start of the ring buffer)
				frameData.location = 0;
				return true;
			}
		}
	}
	return false; // not ready for a frame yet
}

bool StreamProcessor::GetPosition(int time_in_ms, size_t *frameIndex, bool needVideoKeyFrame)
{
	return false; // can't seek!
}

FLVHeader *StreamProcessor::GetHeader()
{
if (readHeader)
return &header;
else
return 0;
}