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
use bytesio::bytes_writer::BytesWriter;
use {
super::{
define,
errors::{FlvDemuxerError, FlvMuxerError},
},
super::{Marshal, Unmarshal},
bytes::BytesMut,
bytesio::bytes_reader::BytesReader,
};
#[derive(Clone, Debug)]
pub struct AudioTagHeader {
//1010 11 1 1
/*
SoundFormat: UB[4]
0 = Linear PCM, platform endian
1 = ADPCM
2 = MP3
3 = Linear PCM, little endian
4 = Nellymoser 16-kHz mono
5 = Nellymoser 8-kHz mono
6 = Nellymoser
7 = G.711 A-law logarithmic PCM
8 = G.711 mu-law logarithmic PCM
9 = reserved
10 = AAC
11 = Speex
14 = MP3 8-Khz
15 = Device-specific sound
Formats 7, 8, 14, and 15 are reserved for internal use
AAC is supported in Flash Player 9,0,115,0 and higher.
Speex is supported in Flash Player 10 and higher.
*/
pub sound_format: u8,
/*
SoundRate: UB[2]
Sampling rate
0 = 5.5-kHz For AAC: always 3
1 = 11-kHz
2 = 22-kHz
3 = 44-kHz
*/
pub sound_rate: u8,
/*
SoundSize: UB[1]
0 = snd8Bit
1 = snd16Bit
Size of each sample.
This parameter only pertains to uncompressed formats.
Compressed formats always decode to 16 bits internally
*/
pub sound_size: u8,
/*
SoundType: UB[1]
0 = sndMono
1 = sndStereo
Mono or stereo sound For Nellymoser: always 0
For AAC: always 1
*/
pub sound_type: u8,
/*
0: AAC sequence header
1: AAC raw
*/
pub aac_packet_type: u8,
}
impl AudioTagHeader {
pub fn defalut() -> Self {
AudioTagHeader {
sound_format: 0,
sound_rate: 0,
sound_size: 0,
sound_type: 0,
aac_packet_type: 0,
}
}
}
impl Unmarshal<&mut BytesReader, Result<Self, FlvDemuxerError>> for AudioTagHeader {
fn unmarshal(reader: &mut BytesReader) -> Result<Self, FlvDemuxerError>
where
Self: Sized,
{
let mut tag_header = AudioTagHeader::defalut();
let flags = reader.read_u8()?;
tag_header.sound_format = flags >> 4;
tag_header.sound_rate = (flags >> 2) & 0x03;
tag_header.sound_size = (flags >> 1) & 0x01;
tag_header.sound_type = flags & 0x01;
if tag_header.sound_format == define::SoundFormat::AAC as u8 {
tag_header.aac_packet_type = reader.read_u8()?;
}
Ok(tag_header)
}
}
impl Marshal<Result<BytesMut, FlvMuxerError>> for AudioTagHeader {
fn marshal(&self) -> Result<BytesMut, FlvMuxerError> {
let mut writer = BytesWriter::default();
let byte_1st =
self.sound_format << 4 | self.sound_rate << 2 | self.sound_size << 1 | self.sound_type;
writer.write_u8(byte_1st)?;
if self.sound_format == define::SoundFormat::AAC as u8 {
writer.write_u8(self.aac_packet_type)?;
}
Ok(writer.extract_current_bytes())
}
}
#[derive(Clone)]
pub struct VideoTagHeader {
/*
1: keyframe (for AVC, a seekable frame)
2: inter frame (for AVC, a non- seekable frame)
3: disposable inter frame (H.263 only)
4: generated keyframe (reserved for server use only)
5: video info/command frame
*/
pub frame_type: u8,
/*
1: JPEG (currently unused)
2: Sorenson H.263
3: Screen video
4: On2 VP6
5: On2 VP6 with alpha channel
6: Screen video version 2
7: AVC
12: HEVC
*/
pub codec_id: u8,
/*
0: AVC sequence header
1: AVC NALU
2: AVC end of sequence (lower level NALU sequence ender is not required or supported)
*/
pub avc_packet_type: u8,
pub composition_time: i32,
}
impl VideoTagHeader {
pub fn defalut() -> Self {
VideoTagHeader {
frame_type: 0,
codec_id: 0,
avc_packet_type: 0,
composition_time: 0,
}
}
}
impl Unmarshal<&mut BytesReader, Result<Self, FlvDemuxerError>> for VideoTagHeader {
fn unmarshal(reader: &mut BytesReader) -> Result<Self, FlvDemuxerError>
where
Self: Sized,
{
let mut tag_header = VideoTagHeader::defalut();
let flags = reader.read_u8()?;
tag_header.frame_type = flags >> 4;
tag_header.codec_id = flags & 0x0f;
if tag_header.codec_id == define::AvcCodecId::H264 as u8
|| tag_header.codec_id == define::AvcCodecId::HEVC as u8
{
tag_header.avc_packet_type = reader.read_u8()?;
tag_header.composition_time = 0;
//bigend 3bytes
for _ in 0..3 {
let time = reader.read_u8()?;
//print!("==time0=={}\n", time);
//print!("==time1=={}\n", self.tag.composition_time);
tag_header.composition_time = (tag_header.composition_time << 8) + time as i32;
}
//transfer to signed i24
if tag_header.composition_time & (1 << 23) != 0 {
let sign_extend_mask = 0xff_ff << 23;
// Sign extend the value
tag_header.composition_time |= sign_extend_mask
}
}
Ok(tag_header)
}
}
impl Marshal<Result<BytesMut, FlvMuxerError>> for VideoTagHeader {
fn marshal(&self) -> Result<BytesMut, FlvMuxerError> {
let mut writer = BytesWriter::default();
let byte_1st = self.frame_type << 4 | self.codec_id;
writer.write_u8(byte_1st)?;
if self.codec_id == define::AvcCodecId::H264 as u8
|| self.codec_id == define::AvcCodecId::HEVC as u8
{
writer.write_u8(self.avc_packet_type)?;
let mut cts = self.composition_time;
for _ in 0..3 {
writer.write_u8((cts & 0xFF) as u8)?;
cts >>= 8;
}
}
Ok(writer.extract_current_bytes())
}
}