Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: media/formats/webm/webm_cluster_parser_unittest.cc

Issue 2254093002: Return buffers from StreamParsers in a single unified map (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added braces + pts/dts buffer logging in tests Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/formats/webm/webm_cluster_parser.h" 5 #include "media/formats/webm/webm_cluster_parser.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 std::unique_ptr<Cluster> CreateEncryptedCluster(int bytes_to_write) { 172 std::unique_ptr<Cluster> CreateEncryptedCluster(int bytes_to_write) {
173 CHECK_GT(bytes_to_write, 0); 173 CHECK_GT(bytes_to_write, 0);
174 CHECK_LE(bytes_to_write, static_cast<int>(sizeof(kEncryptedFrame))); 174 CHECK_LE(bytes_to_write, static_cast<int>(sizeof(kEncryptedFrame)));
175 175
176 ClusterBuilder cb; 176 ClusterBuilder cb;
177 cb.SetClusterTimecode(0); 177 cb.SetClusterTimecode(0);
178 cb.AddSimpleBlock(kVideoTrackNum, 0, 0, kEncryptedFrame, bytes_to_write); 178 cb.AddSimpleBlock(kVideoTrackNum, 0, 0, kEncryptedFrame, bytes_to_write);
179 return cb.Finish(); 179 return cb.Finish();
180 } 180 }
181 181
182 bool VerifyBuffers(const WebMClusterParser::BufferQueue& audio_buffers, 182 bool VerifyBuffers(const StreamParser::BufferQueueMap& buffer_queue_map,
183 const WebMClusterParser::BufferQueue& video_buffers,
184 const WebMClusterParser::BufferQueue& text_buffers,
185 const BlockInfo* block_info, 183 const BlockInfo* block_info,
186 int block_count) { 184 int block_count) {
187 int buffer_count = audio_buffers.size() + video_buffers.size() + 185 int buffer_count = 0;
188 text_buffers.size(); 186 for (const auto& it : buffer_queue_map)
187 buffer_count += it.second.size();
189 if (block_count != buffer_count) { 188 if (block_count != buffer_count) {
190 DVLOG(1) << __FUNCTION__ << " : block_count (" << block_count 189 DVLOG(1) << __FUNCTION__ << " : block_count (" << block_count
191 << ") mismatches buffer_count (" << buffer_count << ")"; 190 << ") mismatches buffer_count (" << buffer_count << ")";
192 return false; 191 return false;
193 } 192 }
194 193
195 size_t audio_offset = 0; 194 size_t audio_offset = 0;
196 size_t video_offset = 0; 195 size_t video_offset = 0;
197 size_t text_offset = 0; 196 size_t text_offset = 0;
198 for (int i = 0; i < block_count; i++) { 197 for (int i = 0; i < block_count; i++) {
199 const WebMClusterParser::BufferQueue* buffers = NULL; 198 const StreamParser::BufferQueue* buffers = NULL;
200 size_t* offset; 199 size_t* offset;
201 StreamParserBuffer::Type expected_type = DemuxerStream::UNKNOWN; 200 StreamParserBuffer::Type expected_type = DemuxerStream::UNKNOWN;
202 201
202 const auto& it = buffer_queue_map.find(block_info[i].track_num);
203 EXPECT_NE(buffer_queue_map.end(), it);
204 buffers = &it->second;
203 if (block_info[i].track_num == kAudioTrackNum) { 205 if (block_info[i].track_num == kAudioTrackNum) {
204 buffers = &audio_buffers;
205 offset = &audio_offset; 206 offset = &audio_offset;
206 expected_type = DemuxerStream::AUDIO; 207 expected_type = DemuxerStream::AUDIO;
207 } else if (block_info[i].track_num == kVideoTrackNum) { 208 } else if (block_info[i].track_num == kVideoTrackNum) {
208 buffers = &video_buffers;
209 offset = &video_offset; 209 offset = &video_offset;
210 expected_type = DemuxerStream::VIDEO; 210 expected_type = DemuxerStream::VIDEO;
211 } else if (block_info[i].track_num == kTextTrackNum) { 211 } else if (block_info[i].track_num == kTextTrackNum) {
212 buffers = &text_buffers;
213 offset = &text_offset; 212 offset = &text_offset;
214 expected_type = DemuxerStream::TEXT; 213 expected_type = DemuxerStream::TEXT;
215 } else { 214 } else {
216 LOG(ERROR) << "Unexpected track number " << block_info[i].track_num; 215 LOG(ERROR) << "Unexpected track number " << block_info[i].track_num;
217 return false; 216 return false;
218 } 217 }
219 218
220 if (*offset >= buffers->size()) { 219 if (*offset >= buffers->size()) {
221 DVLOG(1) << __FUNCTION__ << " : Too few buffers (" << buffers->size() 220 DVLOG(1) << __FUNCTION__ << " : Too few buffers (" << buffers->size()
222 << ") for track_num (" << block_info[i].track_num 221 << ") for track_num (" << block_info[i].track_num
(...skipping 10 matching lines...) Expand all
233 EXPECT_EQ(block_info[i].track_num, buffer->track_id()); 232 EXPECT_EQ(block_info[i].track_num, buffer->track_id());
234 EXPECT_EQ(block_info[i].is_key_frame, buffer->is_key_frame()); 233 EXPECT_EQ(block_info[i].is_key_frame, buffer->is_key_frame());
235 } 234 }
236 235
237 return true; 236 return true;
238 } 237 }
239 238
240 bool VerifyBuffers(const std::unique_ptr<WebMClusterParser>& parser, 239 bool VerifyBuffers(const std::unique_ptr<WebMClusterParser>& parser,
241 const BlockInfo* block_info, 240 const BlockInfo* block_info,
242 int block_count) { 241 int block_count) {
243 const WebMClusterParser::TextBufferQueueMap& text_map = 242 StreamParser::BufferQueueMap buffers;
244 parser->GetTextBuffers(); 243 parser->GetBuffers(&buffers);
245 const WebMClusterParser::BufferQueue* text_buffers; 244 return VerifyBuffers(buffers, block_info, block_count);
246 const WebMClusterParser::BufferQueue no_text_buffers;
247 if (!text_map.empty())
248 text_buffers = &(text_map.rbegin()->second);
249 else
250 text_buffers = &no_text_buffers;
251
252 return VerifyBuffers(parser->GetAudioBuffers(),
253 parser->GetVideoBuffers(),
254 *text_buffers,
255 block_info,
256 block_count);
257 } 245 }
258 246
259 bool VerifyTextBuffers(const std::unique_ptr<WebMClusterParser>& parser, 247 bool VerifyTextBuffers(const std::unique_ptr<WebMClusterParser>& parser,
260 const BlockInfo* block_info_ptr, 248 const BlockInfo* block_info_ptr,
261 int block_count, 249 int block_count,
262 int text_track_num, 250 int text_track_num,
263 const WebMClusterParser::BufferQueue& text_buffers) { 251 const WebMClusterParser::BufferQueue& text_buffers) {
264 const BlockInfo* const block_info_end = block_info_ptr + block_count; 252 const BlockInfo* const block_info_end = block_info_ptr + block_count;
265 253
266 typedef WebMClusterParser::BufferQueue::const_iterator TextBufferIter; 254 typedef WebMClusterParser::BufferQueue::const_iterator TextBufferIter;
(...skipping 20 matching lines...) Expand all
287 EXPECT_TRUE(buffer_iter == buffer_end); 275 EXPECT_TRUE(buffer_iter == buffer_end);
288 return true; 276 return true;
289 } 277 }
290 278
291 void VerifyEncryptedBuffer(scoped_refptr<StreamParserBuffer> buffer) { 279 void VerifyEncryptedBuffer(scoped_refptr<StreamParserBuffer> buffer) {
292 EXPECT_TRUE(buffer->decrypt_config()); 280 EXPECT_TRUE(buffer->decrypt_config());
293 EXPECT_EQ(static_cast<unsigned long>(DecryptConfig::kDecryptionKeySize), 281 EXPECT_EQ(static_cast<unsigned long>(DecryptConfig::kDecryptionKeySize),
294 buffer->decrypt_config()->iv().length()); 282 buffer->decrypt_config()->iv().length());
295 } 283 }
296 284
297 void AppendToEnd(const WebMClusterParser::BufferQueue& src,
298 WebMClusterParser::BufferQueue* dest) {
299 for (WebMClusterParser::BufferQueue::const_iterator itr = src.begin();
300 itr != src.end(); ++itr) {
301 dest->push_back(*itr);
302 }
303 }
304
305 } // namespace 285 } // namespace
306 286
307 class WebMClusterParserTest : public testing::Test { 287 class WebMClusterParserTest : public testing::Test {
308 public: 288 public:
309 WebMClusterParserTest() 289 WebMClusterParserTest()
310 : media_log_(new StrictMock<MockMediaLog>()), 290 : media_log_(new StrictMock<MockMediaLog>()),
311 parser_(CreateDefaultParser()) {} 291 parser_(CreateDefaultParser()) {}
312 292
313 protected: 293 protected:
314 void ResetParserToHaveDefaultDurations() { 294 void ResetParserToHaveDefaultDurations() {
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 int result = parser_->Parse(cluster->data(), cluster->size()); 488 int result = parser_->Parse(cluster->data(), cluster->size());
509 EXPECT_EQ(cluster->size(), result); 489 EXPECT_EQ(cluster->size(), result);
510 ASSERT_TRUE(VerifyBuffers(parser_, kDefaultBlockInfo, block_count)); 490 ASSERT_TRUE(VerifyBuffers(parser_, kDefaultBlockInfo, block_count));
511 } 491 }
512 492
513 TEST_F(WebMClusterParserTest, ParseClusterWithMultipleCalls) { 493 TEST_F(WebMClusterParserTest, ParseClusterWithMultipleCalls) {
514 int block_count = arraysize(kDefaultBlockInfo); 494 int block_count = arraysize(kDefaultBlockInfo);
515 std::unique_ptr<Cluster> cluster( 495 std::unique_ptr<Cluster> cluster(
516 CreateCluster(0, kDefaultBlockInfo, block_count)); 496 CreateCluster(0, kDefaultBlockInfo, block_count));
517 497
518 WebMClusterParser::BufferQueue audio_buffers;
519 WebMClusterParser::BufferQueue video_buffers;
520 const WebMClusterParser::BufferQueue no_text_buffers;
521
522 const uint8_t* data = cluster->data(); 498 const uint8_t* data = cluster->data();
523 int size = cluster->size(); 499 int size = cluster->size();
524 int default_parse_size = 3; 500 int default_parse_size = 3;
525 int parse_size = std::min(default_parse_size, size); 501 int parse_size = std::min(default_parse_size, size);
526 502
527 while (size > 0) { 503 while (size > 0) {
528 int result = parser_->Parse(data, parse_size); 504 int result = parser_->Parse(data, parse_size);
529 ASSERT_GE(result, 0); 505 ASSERT_GE(result, 0);
530 ASSERT_LE(result, parse_size); 506 ASSERT_LE(result, parse_size);
531 507
532 if (result == 0) { 508 if (result == 0) {
533 // The parser needs more data so increase the parse_size a little. 509 // The parser needs more data so increase the parse_size a little.
534 parse_size += default_parse_size; 510 parse_size += default_parse_size;
535 parse_size = std::min(parse_size, size); 511 parse_size = std::min(parse_size, size);
536 continue; 512 continue;
537 } 513 }
538 514
wolenetz 2016/08/23 21:31:21 The point of this unit test is to verify the behav
servolk 2016/08/23 23:56:32 Wait, perhaps I'm missing something. This test sti
wolenetz 2016/08/24 21:57:51 Hmm. I can see how this was confusing. WebMStreamP
servolk 2016/08/24 23:17:44 Ah, ok, restored calling GetBuffers after each Par
539 AppendToEnd(parser_->GetAudioBuffers(), &audio_buffers);
540 AppendToEnd(parser_->GetVideoBuffers(), &video_buffers);
541
542 parse_size = default_parse_size; 515 parse_size = default_parse_size;
543 516
544 data += result; 517 data += result;
545 size -= result; 518 size -= result;
546 } 519 }
547 ASSERT_TRUE(VerifyBuffers(audio_buffers, video_buffers, 520 StreamParser::BufferQueueMap buffers;
548 no_text_buffers, kDefaultBlockInfo, 521 parser_->GetBuffers(&buffers);
549 block_count)); 522 ASSERT_TRUE(VerifyBuffers(buffers, kDefaultBlockInfo, block_count));
550 } 523 }
551 524
552 // Verify that both BlockGroups with the BlockDuration before the Block 525 // Verify that both BlockGroups with the BlockDuration before the Block
553 // and BlockGroups with the BlockDuration after the Block are supported 526 // and BlockGroups with the BlockDuration after the Block are supported
554 // correctly. 527 // correctly.
555 // Note: Raw bytes are use here because ClusterBuilder only generates 528 // Note: Raw bytes are use here because ClusterBuilder only generates
556 // one of these scenarios. 529 // one of these scenarios.
557 TEST_F(WebMClusterParserTest, ParseBlockGroup) { 530 TEST_F(WebMClusterParserTest, ParseBlockGroup) {
558 const BlockInfo kBlockInfo[] = { 531 const BlockInfo kBlockInfo[] = {
559 {kAudioTrackNum, 0, 23, false, NULL, 0, true}, 532 {kAudioTrackNum, 0, 23, false, NULL, 0, true},
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 714
742 parser_.reset(CreateParserWithKeyIdsAndAudioCodec( 715 parser_.reset(CreateParserWithKeyIdsAndAudioCodec(
743 std::string(), "video_key_id", kUnknownAudioCodec)); 716 std::string(), "video_key_id", kUnknownAudioCodec));
744 717
745 // The encrypted cluster contains just one block, video. 718 // The encrypted cluster contains just one block, video.
746 EXPECT_MEDIA_LOG(WebMSimpleBlockDurationEstimated( 719 EXPECT_MEDIA_LOG(WebMSimpleBlockDurationEstimated(
747 WebMClusterParser::kDefaultVideoBufferDurationInMs)); 720 WebMClusterParser::kDefaultVideoBufferDurationInMs));
748 721
749 int result = parser_->Parse(cluster->data(), cluster->size()); 722 int result = parser_->Parse(cluster->data(), cluster->size());
750 EXPECT_EQ(cluster->size(), result); 723 EXPECT_EQ(cluster->size(), result);
751 ASSERT_EQ(1UL, parser_->GetVideoBuffers().size()); 724 StreamParser::BufferQueueMap buffers;
752 scoped_refptr<StreamParserBuffer> buffer = parser_->GetVideoBuffers()[0]; 725 parser_->GetBuffers(&buffers);
726 EXPECT_EQ(1UL, buffers[kVideoTrackNum].size());
727 scoped_refptr<StreamParserBuffer> buffer = buffers[kVideoTrackNum][0];
753 VerifyEncryptedBuffer(buffer); 728 VerifyEncryptedBuffer(buffer);
754 } 729 }
755 730
756 TEST_F(WebMClusterParserTest, ParseBadEncryptedBlock) { 731 TEST_F(WebMClusterParserTest, ParseBadEncryptedBlock) {
757 std::unique_ptr<Cluster> cluster( 732 std::unique_ptr<Cluster> cluster(
758 CreateEncryptedCluster(sizeof(kEncryptedFrame) - 1)); 733 CreateEncryptedCluster(sizeof(kEncryptedFrame) - 1));
759 734
760 parser_.reset(CreateParserWithKeyIdsAndAudioCodec( 735 parser_.reset(CreateParserWithKeyIdsAndAudioCodec(
761 std::string(), "video_key_id", kUnknownAudioCodec)); 736 std::string(), "video_key_id", kUnknownAudioCodec));
762 int result = parser_->Parse(cluster->data(), cluster->size()); 737 int result = parser_->Parse(cluster->data(), cluster->size());
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 847
873 // Send slightly less than the first full cluster so all but the last video 848 // Send slightly less than the first full cluster so all but the last video
874 // block is parsed. Verify the last fully parsed audio and video buffer are 849 // block is parsed. Verify the last fully parsed audio and video buffer are
875 // both missing from the result (parser should hold them aside for duration 850 // both missing from the result (parser should hold them aside for duration
876 // estimation prior to end of cluster detection in the absence of 851 // estimation prior to end of cluster detection in the absence of
877 // DefaultDurations.) 852 // DefaultDurations.)
878 int result = parser_->Parse(cluster1->data(), cluster1->size() - 1); 853 int result = parser_->Parse(cluster1->data(), cluster1->size() - 1);
879 EXPECT_GT(result, 0); 854 EXPECT_GT(result, 0);
880 EXPECT_LT(result, cluster1->size()); 855 EXPECT_LT(result, cluster1->size());
881 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo1, block_count1 - 3)); 856 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo1, block_count1 - 3));
882 EXPECT_EQ(3UL, parser_->GetAudioBuffers().size()); 857 StreamParser::BufferQueueMap buffers;
883 EXPECT_EQ(1UL, parser_->GetVideoBuffers().size()); 858 parser_->GetBuffers(&buffers);
859 EXPECT_EQ(3UL, buffers[kAudioTrackNum].size());
860 EXPECT_EQ(1UL, buffers[kVideoTrackNum].size());
884 861
885 parser_->Reset(); 862 parser_->Reset();
886 863
887 // Now parse the full first cluster and verify all the blocks are parsed. 864 // Now parse the full first cluster and verify all the blocks are parsed.
888 EXPECT_MEDIA_LOG( 865 EXPECT_MEDIA_LOG(
889 WebMSimpleBlockDurationEstimated(kExpectedAudioEstimationInMs)); 866 WebMSimpleBlockDurationEstimated(kExpectedAudioEstimationInMs));
890 EXPECT_MEDIA_LOG( 867 EXPECT_MEDIA_LOG(
891 WebMSimpleBlockDurationEstimated(kExpectedVideoEstimationInMs)); 868 WebMSimpleBlockDurationEstimated(kExpectedVideoEstimationInMs));
892 result = parser_->Parse(cluster1->data(), cluster1->size()); 869 result = parser_->Parse(cluster1->data(), cluster1->size());
893 EXPECT_EQ(cluster1->size(), result); 870 EXPECT_EQ(cluster1->size(), result);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 921
945 // Send slightly less than the first full cluster so all but the last video 922 // Send slightly less than the first full cluster so all but the last video
946 // block is parsed. Verify the last fully parsed audio and video buffer are 923 // block is parsed. Verify the last fully parsed audio and video buffer are
947 // both missing from the result (parser should hold them aside for duration 924 // both missing from the result (parser should hold them aside for duration
948 // estimation prior to end of cluster detection in the absence of 925 // estimation prior to end of cluster detection in the absence of
949 // DefaultDurations.) 926 // DefaultDurations.)
950 int result = parser_->Parse(cluster1->data(), cluster1->size() - 1); 927 int result = parser_->Parse(cluster1->data(), cluster1->size() - 1);
951 EXPECT_GT(result, 0); 928 EXPECT_GT(result, 0);
952 EXPECT_LT(result, cluster1->size()); 929 EXPECT_LT(result, cluster1->size());
953 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo1, block_count1 - 3)); 930 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo1, block_count1 - 3));
954 EXPECT_EQ(3UL, parser_->GetAudioBuffers().size()); 931 StreamParser::BufferQueueMap buffers;
955 EXPECT_EQ(1UL, parser_->GetVideoBuffers().size()); 932 parser_->GetBuffers(&buffers);
933 EXPECT_EQ(3UL, buffers[kAudioTrackNum].size());
934 EXPECT_EQ(1UL, buffers[kVideoTrackNum].size());
956 935
957 parser_->Reset(); 936 parser_->Reset();
958 937
959 // Now parse the full first cluster and verify all the blocks are parsed. 938 // Now parse the full first cluster and verify all the blocks are parsed.
960 EXPECT_MEDIA_LOG( 939 EXPECT_MEDIA_LOG(
961 WebMSimpleBlockDurationEstimated(kExpectedAudioEstimationInMs)); 940 WebMSimpleBlockDurationEstimated(kExpectedAudioEstimationInMs));
962 EXPECT_MEDIA_LOG( 941 EXPECT_MEDIA_LOG(
963 WebMSimpleBlockDurationEstimated(kExpectedVideoEstimationInMs)); 942 WebMSimpleBlockDurationEstimated(kExpectedVideoEstimationInMs));
964 result = parser_->Parse(cluster1->data(), cluster1->size()); 943 result = parser_->Parse(cluster1->data(), cluster1->size());
965 EXPECT_EQ(cluster1->size(), result); 944 EXPECT_EQ(cluster1->size(), result);
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 int block_count = arraysize(kBlockInfo); 1159 int block_count = arraysize(kBlockInfo);
1181 std::unique_ptr<Cluster> cluster(CreateCluster(0, kBlockInfo, block_count)); 1160 std::unique_ptr<Cluster> cluster(CreateCluster(0, kBlockInfo, block_count));
1182 int result = parser_->Parse(cluster->data(), cluster->size()); 1161 int result = parser_->Parse(cluster->data(), cluster->size());
1183 EXPECT_EQ(cluster->size(), result); 1162 EXPECT_EQ(cluster->size(), result);
1184 1163
1185 // Will verify that duration of buffer matches that of BlockDuration. 1164 // Will verify that duration of buffer matches that of BlockDuration.
1186 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo, block_count)); 1165 ASSERT_TRUE(VerifyBuffers(parser_, kBlockInfo, block_count));
1187 } 1166 }
1188 1167
1189 } // namespace media 1168 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698