-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflv_reader.c
82 lines (60 loc) · 2.2 KB
/
flv_reader.c
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
#include "flv_reader.h"
void FlvReaderOpen(FlvReader* flvReader, const char* filename)
{
FlvFileHeader flvheader;
unsigned int headersize = 0;
flvReader->fp_ = fopen(filename, "rb");
fseek(flvReader->fp_, 0, SEEK_END);
flvReader->filesize_ = ftell(flvReader->fp_);
fseek(flvReader->fp_, 0, SEEK_SET);
// 读取flv文件头
fread(&flvheader, sizeof(FlvFileHeader), 1, flvReader->fp_);
// 跳过flv头剩余的大小
headersize = FLV_UI32(flvheader.headersize);
fseek(flvReader->fp_, headersize-sizeof(FlvFileHeader), SEEK_CUR);
// 跳过第一个PreviousTagSize
fseek(flvReader->fp_, 4, SEEK_CUR);
flvReader->read_pos_ = headersize + 4;
}
void FlvReaderClose(FlvReader* flvReader)
{
fclose(flvReader->fp_);
}
void FlvReaderRewind(FlvReader* flvReader)
{
FlvFileHeader flvheader;
unsigned int headersize = 0;
fseek(flvReader->fp_, 0, SEEK_SET);
// 读取flv文件头
fread(&flvheader, sizeof(FlvFileHeader), 1, flvReader->fp_);
// 跳过flv头剩余的大小
headersize = FLV_UI32(flvheader.headersize);
fseek(flvReader->fp_, headersize-sizeof(FlvFileHeader), SEEK_CUR);
// 跳过第一个PreviousTagSize
fseek(flvReader->fp_, 4, SEEK_CUR);
flvReader->read_pos_ = headersize + 4;
}
int FlvReaderReadNextTagHeader(FlvReader* flvReader, int* tagType, int* timestamp)
{
FlvTag flvtag;
if (flvReader->read_pos_ + sizeof(FlvTag) > flvReader->filesize_)
{
return -1;
}
fread(&flvtag, sizeof(FlvTag), 1, flvReader->fp_);
flvReader->read_pos_ += sizeof(FlvTag);
*timestamp = (flvtag.timestamp_ex << 24) +
(flvtag.timestamp[0] << 16) +
(flvtag.timestamp[1] << 8) +
(flvtag.timestamp[2]);
*tagType = flvtag.type;
flvReader->reading_tag_data_size_ = FLV_UI24(flvtag.datasize);
return flvReader->reading_tag_data_size_;
}
void FlvReaderReadNextTagData(FlvReader* flvReader, char* outBuf)
{
int readsize = fread(outBuf, 1, flvReader->reading_tag_data_size_, flvReader->fp_);
flvReader->read_pos_ += flvReader->reading_tag_data_size_;
fseek(flvReader->fp_, 4, SEEK_CUR);
flvReader->read_pos_ += 4;
}