winlin

add assert to flv.

@@ -53,6 +53,8 @@ int SrsFlvEncoder::initialize(SrsFileWriter* fs) @@ -53,6 +53,8 @@ int SrsFlvEncoder::initialize(SrsFileWriter* fs)
53 { 53 {
54 int ret = ERROR_SUCCESS; 54 int ret = ERROR_SUCCESS;
55 55
  56 + srs_assert(fs);
  57 +
56 if (!fs->is_open()) { 58 if (!fs->is_open()) {
57 ret = ERROR_KERNEL_FLV_STREAM_CLOSED; 59 ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
58 srs_warn("stream is not open for decoder. ret=%d", ret); 60 srs_warn("stream is not open for decoder. ret=%d", ret);
@@ -110,6 +112,8 @@ int SrsFlvEncoder::write_metadata(char* data, int size) @@ -110,6 +112,8 @@ int SrsFlvEncoder::write_metadata(char* data, int size)
110 { 112 {
111 int ret = ERROR_SUCCESS; 113 int ret = ERROR_SUCCESS;
112 114
  115 + srs_assert(data);
  116 +
113 // 11 bytes tag header 117 // 11 bytes tag header
114 static char tag_header[] = { 118 static char tag_header[] = {
115 (char)18, // TagType UB [5], 18 = script data 119 (char)18, // TagType UB [5], 18 = script data
@@ -137,6 +141,8 @@ int SrsFlvEncoder::write_audio(int64_t timestamp, char* data, int size) @@ -137,6 +141,8 @@ int SrsFlvEncoder::write_audio(int64_t timestamp, char* data, int size)
137 { 141 {
138 int ret = ERROR_SUCCESS; 142 int ret = ERROR_SUCCESS;
139 143
  144 + srs_assert(data);
  145 +
140 timestamp &= 0x7fffffff; 146 timestamp &= 0x7fffffff;
141 147
142 // 11bytes tag header 148 // 11bytes tag header
@@ -169,6 +175,8 @@ int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size) @@ -169,6 +175,8 @@ int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size)
169 { 175 {
170 int ret = ERROR_SUCCESS; 176 int ret = ERROR_SUCCESS;
171 177
  178 + srs_assert(data);
  179 +
172 timestamp &= 0x7fffffff; 180 timestamp &= 0x7fffffff;
173 181
174 // 11bytes tag header 182 // 11bytes tag header
@@ -199,6 +207,7 @@ int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size) @@ -199,6 +207,7 @@ int SrsFlvEncoder::write_video(int64_t timestamp, char* data, int size)
199 207
200 int SrsFlvEncoder::size_tag(int data_size) 208 int SrsFlvEncoder::size_tag(int data_size)
201 { 209 {
  210 + srs_assert(data_size >= 0);
202 return SRS_FLV_TAG_HEADER_SIZE + data_size + SRS_FLV_PREVIOUS_TAG_SIZE; 211 return SRS_FLV_TAG_HEADER_SIZE + data_size + SRS_FLV_PREVIOUS_TAG_SIZE;
203 } 212 }
204 213
@@ -247,6 +256,8 @@ int SrsFlvDecoder::initialize(SrsFileReader* fs) @@ -247,6 +256,8 @@ int SrsFlvDecoder::initialize(SrsFileReader* fs)
247 { 256 {
248 int ret = ERROR_SUCCESS; 257 int ret = ERROR_SUCCESS;
249 258
  259 + srs_assert(fs);
  260 +
250 if (!fs->is_open()) { 261 if (!fs->is_open()) {
251 ret = ERROR_KERNEL_FLV_STREAM_CLOSED; 262 ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
252 srs_warn("stream is not open for decoder. ret=%d", ret); 263 srs_warn("stream is not open for decoder. ret=%d", ret);
@@ -262,6 +273,8 @@ int SrsFlvDecoder::read_header(char header[9]) @@ -262,6 +273,8 @@ int SrsFlvDecoder::read_header(char header[9])
262 { 273 {
263 int ret = ERROR_SUCCESS; 274 int ret = ERROR_SUCCESS;
264 275
  276 + srs_assert(header);
  277 +
265 if ((ret = _fs->read(header, 9, NULL)) != ERROR_SUCCESS) { 278 if ((ret = _fs->read(header, 9, NULL)) != ERROR_SUCCESS) {
266 return ret; 279 return ret;
267 } 280 }
@@ -280,6 +293,10 @@ int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* @@ -280,6 +293,10 @@ int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t*
280 { 293 {
281 int ret = ERROR_SUCCESS; 294 int ret = ERROR_SUCCESS;
282 295
  296 + srs_assert(ptype);
  297 + srs_assert(pdata_size);
  298 + srs_assert(ptime);
  299 +
283 char th[11]; // tag header 300 char th[11]; // tag header
284 301
285 // read tag header 302 // read tag header
@@ -317,6 +334,8 @@ int SrsFlvDecoder::read_tag_data(char* data, int32_t size) @@ -317,6 +334,8 @@ int SrsFlvDecoder::read_tag_data(char* data, int32_t size)
317 { 334 {
318 int ret = ERROR_SUCCESS; 335 int ret = ERROR_SUCCESS;
319 336
  337 + srs_assert(data);
  338 +
320 if ((ret = _fs->read(data, size, NULL)) != ERROR_SUCCESS) { 339 if ((ret = _fs->read(data, size, NULL)) != ERROR_SUCCESS) {
321 if (ret != ERROR_SYSTEM_FILE_EOF) { 340 if (ret != ERROR_SYSTEM_FILE_EOF) {
322 srs_error("read flv tag header failed. ret=%d", ret); 341 srs_error("read flv tag header failed. ret=%d", ret);
@@ -328,12 +347,14 @@ int SrsFlvDecoder::read_tag_data(char* data, int32_t size) @@ -328,12 +347,14 @@ int SrsFlvDecoder::read_tag_data(char* data, int32_t size)
328 347
329 } 348 }
330 349
331 -int SrsFlvDecoder::read_previous_tag_size(char ts[4]) 350 +int SrsFlvDecoder::read_previous_tag_size(char previous_tag_size[4])
332 { 351 {
333 int ret = ERROR_SUCCESS; 352 int ret = ERROR_SUCCESS;
334 353
  354 + srs_assert(previous_tag_size);
  355 +
335 // ignore 4bytes tag size. 356 // ignore 4bytes tag size.
336 - if ((ret = _fs->read(ts, 4, NULL)) != ERROR_SUCCESS) { 357 + if ((ret = _fs->read(previous_tag_size, 4, NULL)) != ERROR_SUCCESS) {
337 if (ret != ERROR_SYSTEM_FILE_EOF) { 358 if (ret != ERROR_SYSTEM_FILE_EOF) {
338 srs_error("read flv previous tag size failed. ret=%d", ret); 359 srs_error("read flv previous tag size failed. ret=%d", ret);
339 } 360 }
@@ -358,6 +379,8 @@ int SrsFlvVodStreamDecoder::initialize(SrsFileReader* fs) @@ -358,6 +379,8 @@ int SrsFlvVodStreamDecoder::initialize(SrsFileReader* fs)
358 { 379 {
359 int ret = ERROR_SUCCESS; 380 int ret = ERROR_SUCCESS;
360 381
  382 + srs_assert(fs);
  383 +
361 if (!fs->is_open()) { 384 if (!fs->is_open()) {
362 ret = ERROR_KERNEL_FLV_STREAM_CLOSED; 385 ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
363 srs_warn("stream is not open for decoder. ret=%d", ret); 386 srs_warn("stream is not open for decoder. ret=%d", ret);
@@ -373,7 +396,7 @@ int SrsFlvVodStreamDecoder::read_header_ext(char header[13]) @@ -373,7 +396,7 @@ int SrsFlvVodStreamDecoder::read_header_ext(char header[13])
373 { 396 {
374 int ret = ERROR_SUCCESS; 397 int ret = ERROR_SUCCESS;
375 398
376 - srs_assert(_fs); 399 + srs_assert(header);
377 400
378 // @remark, always false, for sizeof(char[13]) equals to sizeof(char*) 401 // @remark, always false, for sizeof(char[13]) equals to sizeof(char*)
379 //srs_assert(13 == sizeof(header)); 402 //srs_assert(13 == sizeof(header));
@@ -390,12 +413,10 @@ int SrsFlvVodStreamDecoder::read_header_ext(char header[13]) @@ -390,12 +413,10 @@ int SrsFlvVodStreamDecoder::read_header_ext(char header[13])
390 413
391 int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* psize) 414 int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* psize)
392 { 415 {
393 - *pstart = 0;  
394 - *psize = 0;  
395 -  
396 int ret = ERROR_SUCCESS; 416 int ret = ERROR_SUCCESS;
397 417
398 - srs_assert(_fs); 418 + srs_assert(pstart);
  419 + srs_assert(psize);
399 420
400 // simply, the first video/audio must be the sequence header. 421 // simply, the first video/audio must be the sequence header.
401 // and must be a sequence video and audio. 422 // and must be a sequence video and audio.
@@ -485,8 +506,6 @@ int SrsFlvVodStreamDecoder::lseek(int64_t offset) @@ -485,8 +506,6 @@ int SrsFlvVodStreamDecoder::lseek(int64_t offset)
485 { 506 {
486 int ret = ERROR_SUCCESS; 507 int ret = ERROR_SUCCESS;
487 508
488 - srs_assert(_fs);  
489 -  
490 if (offset >= _fs->filesize()) { 509 if (offset >= _fs->filesize()) {
491 ret = ERROR_SYSTEM_FILE_EOF; 510 ret = ERROR_SYSTEM_FILE_EOF;
492 srs_warn("flv fast decoder seek overflow file, " 511 srs_warn("flv fast decoder seek overflow file, "
@@ -69,10 +69,12 @@ public: @@ -69,10 +69,12 @@ public:
69 * @param data, the amf0 metadata which serialize from: 69 * @param data, the amf0 metadata which serialize from:
70 * AMF0 string: onMetaData, 70 * AMF0 string: onMetaData,
71 * AMF0 object: the metadata object. 71 * AMF0 object: the metadata object.
  72 + * @remark assert data is not NULL.
72 */ 73 */
73 virtual int write_metadata(char* data, int size); 74 virtual int write_metadata(char* data, int size);
74 /** 75 /**
75 * write audio/video packet. 76 * write audio/video packet.
  77 + * @remark assert data is not NULL.
76 */ 78 */
77 virtual int write_audio(int64_t timestamp, char* data, int size); 79 virtual int write_audio(int64_t timestamp, char* data, int size);
78 virtual int write_video(int64_t timestamp, char* data, int size); 80 virtual int write_video(int64_t timestamp, char* data, int size);
@@ -80,6 +82,7 @@ public: @@ -80,6 +82,7 @@ public:
80 /** 82 /**
81 * get the tag size, 83 * get the tag size,
82 * including the tag header, body, and 4bytes previous tag size. 84 * including the tag header, body, and 4bytes previous tag size.
  85 + * @remark assert data_size is not negative.
83 */ 86 */
84 static int size_tag(int data_size); 87 static int size_tag(int data_size);
85 private: 88 private:
@@ -106,10 +109,26 @@ public: @@ -106,10 +109,26 @@ public:
106 */ 109 */
107 virtual int initialize(SrsFileReader* fs); 110 virtual int initialize(SrsFileReader* fs);
108 public: 111 public:
  112 + /**
  113 + * read the flv header, donot including the 4bytes previous tag size.
  114 + * @remark assert header not NULL.
  115 + */
109 virtual int read_header(char header[9]); 116 virtual int read_header(char header[9]);
  117 + /**
  118 + * read the tag header infos.
  119 + * @remark assert ptype/pdata_size/ptime not NULL.
  120 + */
110 virtual int read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime); 121 virtual int read_tag_header(char* ptype, int32_t* pdata_size, u_int32_t* ptime);
  122 + /**
  123 + * read the tag data.
  124 + * @remark assert data not NULL.
  125 + */
111 virtual int read_tag_data(char* data, int32_t size); 126 virtual int read_tag_data(char* data, int32_t size);
112 - virtual int read_previous_tag_size(char ts[4]); 127 + /**
  128 + * read the 4bytes previous tag size.
  129 + * @remark assert previous_tag_size not NULL.
  130 + */
  131 + virtual int read_previous_tag_size(char previous_tag_size[4]);
113 }; 132 };
114 133
115 /** 134 /**
@@ -137,6 +156,7 @@ public: @@ -137,6 +156,7 @@ public:
137 /** 156 /**
138 * read the flv header and its size. 157 * read the flv header and its size.
139 * @param header, fill it 13bytes(9bytes header, 4bytes previous tag size). 158 * @param header, fill it 13bytes(9bytes header, 4bytes previous tag size).
  159 + * @remark assert header not NULL.
140 */ 160 */
141 virtual int read_header_ext(char header[13]); 161 virtual int read_header_ext(char header[13]);
142 /** 162 /**
@@ -144,6 +164,7 @@ public: @@ -144,6 +164,7 @@ public:
144 * @param pstart, the start offset of sequence header. 164 * @param pstart, the start offset of sequence header.
145 * @param psize, output the size, (tag header)+(tag body)+(4bytes previous tag size). 165 * @param psize, output the size, (tag header)+(tag body)+(4bytes previous tag size).
146 * @remark we think the first audio/video is sequence header. 166 * @remark we think the first audio/video is sequence header.
  167 + * @remark assert pstart/psize not NULL.
147 */ 168 */
148 virtual int read_sequence_header_summary(int64_t* pstart, int* psize); 169 virtual int read_sequence_header_summary(int64_t* pstart, int* psize);
149 public: 170 public:
@@ -176,6 +176,10 @@ void MockSrsFileReader::mock_reset_offset() @@ -176,6 +176,10 @@ void MockSrsFileReader::mock_reset_offset()
176 offset = 0; 176 offset = 0;
177 } 177 }
178 178
  179 +/**
  180 +* test the codec,
  181 +* whether H.264 keyframe
  182 +*/
179 VOID TEST(KernelCodecTest, IsKeyFrame) 183 VOID TEST(KernelCodecTest, IsKeyFrame)
180 { 184 {
181 int8_t data; 185 int8_t data;
@@ -188,6 +192,10 @@ VOID TEST(KernelCodecTest, IsKeyFrame) @@ -188,6 +192,10 @@ VOID TEST(KernelCodecTest, IsKeyFrame)
188 EXPECT_FALSE(SrsFlvCodec::video_is_keyframe(&data, 1)); 192 EXPECT_FALSE(SrsFlvCodec::video_is_keyframe(&data, 1));
189 } 193 }
190 194
  195 +/**
  196 +* test the codec,
  197 +* check whether H.264 video
  198 +*/
191 VOID TEST(KernelCodecTest, IsH264) 199 VOID TEST(KernelCodecTest, IsH264)
192 { 200 {
193 int8_t data; 201 int8_t data;
@@ -204,6 +212,10 @@ VOID TEST(KernelCodecTest, IsH264) @@ -204,6 +212,10 @@ VOID TEST(KernelCodecTest, IsH264)
204 EXPECT_FALSE(SrsFlvCodec::video_is_h264(&data, 1)); 212 EXPECT_FALSE(SrsFlvCodec::video_is_h264(&data, 1));
205 } 213 }
206 214
  215 +/**
  216 +* test the codec,
  217 +* whether H.264 video sequence header
  218 +*/
207 VOID TEST(KernelCodecTest, IsSequenceHeader) 219 VOID TEST(KernelCodecTest, IsSequenceHeader)
208 { 220 {
209 int16_t data; 221 int16_t data;
@@ -224,6 +236,10 @@ VOID TEST(KernelCodecTest, IsSequenceHeader) @@ -224,6 +236,10 @@ VOID TEST(KernelCodecTest, IsSequenceHeader)
224 EXPECT_FALSE(SrsFlvCodec::video_is_sequence_header((int8_t*)pp, 2)); 236 EXPECT_FALSE(SrsFlvCodec::video_is_sequence_header((int8_t*)pp, 2));
225 } 237 }
226 238
  239 +/**
  240 +* test the codec,
  241 +* check whether AAC codec
  242 +*/
227 VOID TEST(KernelCodecTest, IsAAC) 243 VOID TEST(KernelCodecTest, IsAAC)
228 { 244 {
229 int8_t data; 245 int8_t data;
@@ -240,6 +256,10 @@ VOID TEST(KernelCodecTest, IsAAC) @@ -240,6 +256,10 @@ VOID TEST(KernelCodecTest, IsAAC)
240 EXPECT_FALSE(SrsFlvCodec::audio_is_aac(&data, 1)); 256 EXPECT_FALSE(SrsFlvCodec::audio_is_aac(&data, 1));
241 } 257 }
242 258
  259 +/**
  260 +* test the codec,
  261 +* AAC audio sequence header
  262 +*/
243 VOID TEST(KernelCodecTest, IsAudioSequenceHeader) 263 VOID TEST(KernelCodecTest, IsAudioSequenceHeader)
244 { 264 {
245 int16_t data; 265 int16_t data;
@@ -258,6 +278,10 @@ VOID TEST(KernelCodecTest, IsAudioSequenceHeader) @@ -258,6 +278,10 @@ VOID TEST(KernelCodecTest, IsAudioSequenceHeader)
258 EXPECT_FALSE(SrsFlvCodec::video_is_sequence_header((int8_t*)pp, 2)); 278 EXPECT_FALSE(SrsFlvCodec::video_is_sequence_header((int8_t*)pp, 2));
259 } 279 }
260 280
  281 +/**
  282 +* test the flv encoder,
  283 +* exception: file stream not open
  284 +*/
261 VOID TEST(KernelFlvTest, FlvEncoderStreamClosed) 285 VOID TEST(KernelFlvTest, FlvEncoderStreamClosed)
262 { 286 {
263 MockSrsFileWriter fs; 287 MockSrsFileWriter fs;
@@ -265,6 +289,10 @@ VOID TEST(KernelFlvTest, FlvEncoderStreamClosed) @@ -265,6 +289,10 @@ VOID TEST(KernelFlvTest, FlvEncoderStreamClosed)
265 ASSERT_TRUE(ERROR_SUCCESS != enc.initialize(&fs)); 289 ASSERT_TRUE(ERROR_SUCCESS != enc.initialize(&fs));
266 } 290 }
267 291
  292 +/**
  293 +* test the flv encoder,
  294 +* write flv header
  295 +*/
268 VOID TEST(KernelFlvTest, FlvEncoderWriteHeader) 296 VOID TEST(KernelFlvTest, FlvEncoderWriteHeader)
269 { 297 {
270 MockSrsFileWriter fs; 298 MockSrsFileWriter fs;
@@ -301,6 +329,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteHeader) @@ -301,6 +329,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteHeader)
301 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 9, 4)); 329 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 9, 4));
302 } 330 }
303 331
  332 +/**
  333 +* test the flv encoder,
  334 +* write metadata tag
  335 +*/
304 VOID TEST(KernelFlvTest, FlvEncoderWriteMetadata) 336 VOID TEST(KernelFlvTest, FlvEncoderWriteMetadata)
305 { 337 {
306 MockSrsFileWriter fs; 338 MockSrsFileWriter fs;
@@ -331,6 +363,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteMetadata) @@ -331,6 +363,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteMetadata)
331 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 19, 4)); 363 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 19, 4));
332 } 364 }
333 365
  366 +/**
  367 +* test the flv encoder,
  368 +* write audio tag
  369 +*/
334 VOID TEST(KernelFlvTest, FlvEncoderWriteAudio) 370 VOID TEST(KernelFlvTest, FlvEncoderWriteAudio)
335 { 371 {
336 MockSrsFileWriter fs; 372 MockSrsFileWriter fs;
@@ -361,6 +397,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteAudio) @@ -361,6 +397,10 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteAudio)
361 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 11 + 8, 4)); 397 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 11 + 8, 4));
362 } 398 }
363 399
  400 +/**
  401 +* test the flv encoder,
  402 +* write video tag
  403 +*/
364 VOID TEST(KernelFlvTest, FlvEncoderWriteVideo) 404 VOID TEST(KernelFlvTest, FlvEncoderWriteVideo)
365 { 405 {
366 MockSrsFileWriter fs; 406 MockSrsFileWriter fs;
@@ -391,12 +431,20 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteVideo) @@ -391,12 +431,20 @@ VOID TEST(KernelFlvTest, FlvEncoderWriteVideo)
391 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 11 + 8, 4)); 431 EXPECT_TRUE(srs_bytes_equals(pts, fs.data + 11 + 8, 4));
392 } 432 }
393 433
  434 +/**
  435 +* test the flv encoder,
  436 +* calc the tag size.
  437 +*/
394 VOID TEST(KernelFlvTest, FlvEncoderSizeTag) 438 VOID TEST(KernelFlvTest, FlvEncoderSizeTag)
395 { 439 {
396 EXPECT_EQ(11+4+10, SrsFlvEncoder::size_tag(10)); 440 EXPECT_EQ(11+4+10, SrsFlvEncoder::size_tag(10));
397 EXPECT_EQ(11+4+0, SrsFlvEncoder::size_tag(0)); 441 EXPECT_EQ(11+4+0, SrsFlvEncoder::size_tag(0));
398 } 442 }
399 443
  444 +/**
  445 +* test the flv decoder,
  446 +* exception: file stream not open.
  447 +*/
400 VOID TEST(KernelFlvTest, FlvDecoderStreamClosed) 448 VOID TEST(KernelFlvTest, FlvDecoderStreamClosed)
401 { 449 {
402 MockSrsFileReader fs; 450 MockSrsFileReader fs;
@@ -404,6 +452,10 @@ VOID TEST(KernelFlvTest, FlvDecoderStreamClosed) @@ -404,6 +452,10 @@ VOID TEST(KernelFlvTest, FlvDecoderStreamClosed)
404 ASSERT_TRUE(ERROR_SUCCESS != dec.initialize(&fs)); 452 ASSERT_TRUE(ERROR_SUCCESS != dec.initialize(&fs));
405 } 453 }
406 454
  455 +/**
  456 +* test the flv decoder,
  457 +* decode flv header
  458 +*/
407 VOID TEST(KernelFlvTest, FlvDecoderHeader) 459 VOID TEST(KernelFlvTest, FlvDecoderHeader)
408 { 460 {
409 MockSrsFileReader fs; 461 MockSrsFileReader fs;
@@ -432,6 +484,10 @@ VOID TEST(KernelFlvTest, FlvDecoderHeader) @@ -432,6 +484,10 @@ VOID TEST(KernelFlvTest, FlvDecoderHeader)
432 EXPECT_TRUE(srs_bytes_equals(pts, data, 4)); 484 EXPECT_TRUE(srs_bytes_equals(pts, data, 4));
433 } 485 }
434 486
  487 +/**
  488 +* test the flv decoder,
  489 +* decode metadata tag
  490 +*/
435 VOID TEST(KernelFlvTest, FlvDecoderMetadata) 491 VOID TEST(KernelFlvTest, FlvDecoderMetadata)
436 { 492 {
437 MockSrsFileReader fs; 493 MockSrsFileReader fs;
@@ -474,6 +530,10 @@ VOID TEST(KernelFlvTest, FlvDecoderMetadata) @@ -474,6 +530,10 @@ VOID TEST(KernelFlvTest, FlvDecoderMetadata)
474 EXPECT_TRUE(srs_bytes_equals(pts, data, 4)); 530 EXPECT_TRUE(srs_bytes_equals(pts, data, 4));
475 } 531 }
476 532
  533 +/**
  534 +* test the flv decoder,
  535 +* decode audio tag
  536 +*/
477 VOID TEST(KernelFlvTest, FlvDecoderAudio) 537 VOID TEST(KernelFlvTest, FlvDecoderAudio)
478 { 538 {
479 MockSrsFileReader fs; 539 MockSrsFileReader fs;
@@ -516,6 +576,10 @@ VOID TEST(KernelFlvTest, FlvDecoderAudio) @@ -516,6 +576,10 @@ VOID TEST(KernelFlvTest, FlvDecoderAudio)
516 EXPECT_TRUE(srs_bytes_equals(pts, data, 4)); 576 EXPECT_TRUE(srs_bytes_equals(pts, data, 4));
517 } 577 }
518 578
  579 +/**
  580 +* test the flv decoder,
  581 +* decode video tag
  582 +*/
519 VOID TEST(KernelFlvTest, FlvDecoderVideo) 583 VOID TEST(KernelFlvTest, FlvDecoderVideo)
520 { 584 {
521 MockSrsFileReader fs; 585 MockSrsFileReader fs;
@@ -558,6 +622,10 @@ VOID TEST(KernelFlvTest, FlvDecoderVideo) @@ -558,6 +622,10 @@ VOID TEST(KernelFlvTest, FlvDecoderVideo)
558 EXPECT_TRUE(srs_bytes_equals(pts, data, 4)); 622 EXPECT_TRUE(srs_bytes_equals(pts, data, 4));
559 } 623 }
560 624
  625 +/**
  626 +* test the flv vod stream decoder,
  627 +* exception: file stream not open.
  628 +*/
561 VOID TEST(KernelFlvTest, FlvVSDecoderStreamClosed) 629 VOID TEST(KernelFlvTest, FlvVSDecoderStreamClosed)
562 { 630 {
563 MockSrsFileReader fs; 631 MockSrsFileReader fs;
@@ -565,6 +633,10 @@ VOID TEST(KernelFlvTest, FlvVSDecoderStreamClosed) @@ -565,6 +633,10 @@ VOID TEST(KernelFlvTest, FlvVSDecoderStreamClosed)
565 ASSERT_TRUE(ERROR_SUCCESS != dec.initialize(&fs)); 633 ASSERT_TRUE(ERROR_SUCCESS != dec.initialize(&fs));
566 } 634 }
567 635
  636 +/**
  637 +* test the flv vod stream decoder,
  638 +* decode the flv header
  639 +*/
568 VOID TEST(KernelFlvTest, FlvVSDecoderHeader) 640 VOID TEST(KernelFlvTest, FlvVSDecoderHeader)
569 { 641 {
570 MockSrsFileReader fs; 642 MockSrsFileReader fs;
@@ -591,6 +663,11 @@ VOID TEST(KernelFlvTest, FlvVSDecoderHeader) @@ -591,6 +663,11 @@ VOID TEST(KernelFlvTest, FlvVSDecoderHeader)
591 EXPECT_TRUE(srs_bytes_equals(flv_header, data, 9)); 663 EXPECT_TRUE(srs_bytes_equals(flv_header, data, 9));
592 } 664 }
593 665
  666 +/**
  667 +* test the flv vod stream decoder,
  668 +* get the start offset and size of sequence header
  669 +* mock data: metadata-audio-video
  670 +*/
594 VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader) 671 VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader)
595 { 672 {
596 MockSrsFileReader fs; 673 MockSrsFileReader fs;
@@ -598,6 +675,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader) @@ -598,6 +675,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader)
598 ASSERT_TRUE(ERROR_SUCCESS == fs.open("")); 675 ASSERT_TRUE(ERROR_SUCCESS == fs.open(""));
599 ASSERT_TRUE(ERROR_SUCCESS == dec.initialize(&fs)); 676 ASSERT_TRUE(ERROR_SUCCESS == dec.initialize(&fs));
600 677
  678 + // push metadata tag
601 if (true) { 679 if (true) {
602 // 11 bytes tag header 680 // 11 bytes tag header
603 char tag_header[] = { 681 char tag_header[] = {
@@ -616,6 +694,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader) @@ -616,6 +694,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader)
616 fs.mock_append_data(md, 8); 694 fs.mock_append_data(md, 8);
617 fs.mock_append_data(pts, 4); 695 fs.mock_append_data(pts, 4);
618 } 696 }
  697 + // push audio tag
619 if (true) { 698 if (true) {
620 // 11bytes tag header 699 // 11bytes tag header
621 char tag_header[] = { 700 char tag_header[] = {
@@ -634,6 +713,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader) @@ -634,6 +713,7 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader)
634 fs.mock_append_data(audio, 8); 713 fs.mock_append_data(audio, 8);
635 fs.mock_append_data(pts, 4); 714 fs.mock_append_data(pts, 4);
636 } 715 }
  716 + // push video tag
637 if (true) { 717 if (true) {
638 // 11bytes tag header 718 // 11bytes tag header
639 char tag_header[] = { 719 char tag_header[] = {
@@ -662,6 +742,90 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader) @@ -662,6 +742,90 @@ VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader)
662 EXPECT_TRUE(46 == size); 742 EXPECT_TRUE(46 == size);
663 } 743 }
664 744
  745 +/**
  746 +* test the flv vod stream decoder,
  747 +* get the start offset and size of sequence header
  748 +* mock data: metadata-video-audio
  749 +*/
  750 +VOID TEST(KernelFlvTest, FlvVSDecoderSequenceHeader2)
  751 +{
  752 + MockSrsFileReader fs;
  753 + SrsFlvVodStreamDecoder dec;
  754 + ASSERT_TRUE(ERROR_SUCCESS == fs.open(""));
  755 + ASSERT_TRUE(ERROR_SUCCESS == dec.initialize(&fs));
  756 +
  757 + // push metadata tag
  758 + if (true) {
  759 + // 11 bytes tag header
  760 + char tag_header[] = {
  761 + (char)18, // TagType UB [5], 18 = script data
  762 + (char)0x00, (char)0x00, (char)0x08, // DataSize UI24 Length of the message.
  763 + (char)0x00, (char)0x00, (char)0x00, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
  764 + (char)0x00, // TimestampExtended UI8
  765 + (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
  766 + };
  767 + char md[] = {
  768 + (char)0x01, (char)0x02, (char)0x03, (char)0x04,
  769 + (char)0x04, (char)0x03, (char)0x02, (char)0x01
  770 + };
  771 + char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)19 };
  772 + fs.mock_append_data(tag_header, 11);
  773 + fs.mock_append_data(md, 8);
  774 + fs.mock_append_data(pts, 4);
  775 + }
  776 + // push video tag
  777 + if (true) {
  778 + // 11bytes tag header
  779 + char tag_header[] = {
  780 + (char)9, // TagType UB [5], 9 = video
  781 + (char)0x00, (char)0x00, (char)0x08, // DataSize UI24 Length of the message.
  782 + (char)0x00, (char)0x00, (char)0x30, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
  783 + (char)0x00, // TimestampExtended UI8
  784 + (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
  785 + };
  786 + char video[] = {
  787 + (char)0x01, (char)0x02, (char)0x03, (char)0x04,
  788 + (char)0x04, (char)0x03, (char)0x02, (char)0x01
  789 + };
  790 + char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)19 };
  791 + fs.mock_append_data(tag_header, 11);
  792 + fs.mock_append_data(video, 8);
  793 + fs.mock_append_data(pts, 4);
  794 + }
  795 + // push audio tag
  796 + if (true) {
  797 + // 11bytes tag header
  798 + char tag_header[] = {
  799 + (char)8, // TagType UB [5], 8 = audio
  800 + (char)0x00, (char)0x00, (char)0x08, // DataSize UI24 Length of the message.
  801 + (char)0x00, (char)0x00, (char)0x30, // Timestamp UI24 Time in milliseconds at which the data in this tag applies.
  802 + (char)0x00, // TimestampExtended UI8
  803 + (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
  804 + };
  805 + char audio[] = {
  806 + (char)0x01, (char)0x02, (char)0x03, (char)0x04,
  807 + (char)0x04, (char)0x03, (char)0x02, (char)0x01
  808 + };
  809 + char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)19 };
  810 + fs.mock_append_data(tag_header, 11);
  811 + fs.mock_append_data(audio, 8);
  812 + fs.mock_append_data(pts, 4);
  813 + }
  814 +
  815 + fs.mock_reset_offset();
  816 +
  817 + int64_t start = 0;
  818 + int size = 0;
  819 + EXPECT_TRUE(ERROR_SUCCESS == dec.read_sequence_header_summary(&start, &size));
  820 + EXPECT_TRUE(23 == start);
  821 + EXPECT_TRUE(46 == size);
  822 +}
  823 +
  824 +/**
  825 +* test the flv vod stream decoder,
  826 +* seek stream after got the offset and start of flv sequence header,
  827 +* to directly response flv data by seek to the offset of file.
  828 +*/
665 VOID TEST(KernelFlvTest, FlvVSDecoderSeek) 829 VOID TEST(KernelFlvTest, FlvVSDecoderSeek)
666 { 830 {
667 MockSrsFileReader fs; 831 MockSrsFileReader fs;