Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gtest/TestWebMBuffered.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "gtest/gtest.h"
6
#include "mozilla/ArrayUtils.h"
7
#include <stdio.h>
8
#include "nsTArray.h"
9
#include "WebMBufferedParser.h"
10
11
using namespace mozilla;
12
13
// "test.webm" contains 8 SimpleBlocks in a single Cluster.  The blocks with
14
// timecodes 100000000 and are 133000000 skipped by WebMBufferedParser
15
// because they occur after a block with timecode 160000000 and the parser
16
// expects in-order timecodes per the WebM spec.  The remaining 6
17
// SimpleBlocks have the following attributes:
18
static const uint64_t gTimecodes[] = { 66000000, 160000000, 166000000, 200000000, 233000000, 320000000 };
19
static const int64_t gEndOffsets[] = { 501, 772, 1244, 1380, 1543, 2015 };
20
21
TEST(WebMBuffered, BasicTests)
22
0
{
23
0
  ReentrantMonitor dummy("dummy");
24
0
  WebMBufferedParser parser(0);
25
0
26
0
  nsTArray<WebMTimeDataOffset> mapping;
27
0
  parser.Append(nullptr, 0, mapping, dummy);
28
0
  EXPECT_TRUE(mapping.IsEmpty());
29
0
  EXPECT_EQ(parser.mStartOffset, 0);
30
0
  EXPECT_EQ(parser.mCurrentOffset, 0);
31
0
32
0
  unsigned char buf[] = { 0x1a, 0x45, 0xdf, 0xa3 };
33
0
  parser.Append(buf, ArrayLength(buf), mapping, dummy);
34
0
  EXPECT_TRUE(mapping.IsEmpty());
35
0
  EXPECT_EQ(parser.mStartOffset, 0);
36
0
  EXPECT_EQ(parser.mCurrentOffset, 4);
37
0
}
38
39
static void
40
ReadFile(const char* aPath, nsTArray<uint8_t>& aBuffer)
41
0
{
42
0
  FILE* f = fopen(aPath, "rb");
43
0
  ASSERT_NE(f, (FILE *) nullptr);
44
0
45
0
  int r = fseek(f, 0, SEEK_END);
46
0
  ASSERT_EQ(r, 0);
47
0
48
0
  long size = ftell(f);
49
0
  ASSERT_NE(size, -1);
50
0
  aBuffer.SetLength(size);
51
0
52
0
  r = fseek(f, 0, SEEK_SET);
53
0
  ASSERT_EQ(r, 0);
54
0
55
0
  size_t got = fread(aBuffer.Elements(), 1, size, f);
56
0
  ASSERT_EQ(got, size_t(size));
57
0
58
0
  r = fclose(f);
59
0
  ASSERT_EQ(r, 0);
60
0
}
61
62
TEST(WebMBuffered, RealData)
63
0
{
64
0
  ReentrantMonitor dummy("dummy");
65
0
  WebMBufferedParser parser(0);
66
0
67
0
  nsTArray<uint8_t> webmData;
68
0
  ReadFile("test.webm", webmData);
69
0
70
0
  nsTArray<WebMTimeDataOffset> mapping;
71
0
  parser.Append(webmData.Elements(), webmData.Length(), mapping, dummy);
72
0
  EXPECT_EQ(mapping.Length(), 6u);
73
0
  EXPECT_EQ(parser.mStartOffset, 0);
74
0
  EXPECT_EQ(parser.mCurrentOffset, int64_t(webmData.Length()));
75
0
  EXPECT_EQ(parser.GetTimecodeScale(), 500000u);
76
0
77
0
  for (uint32_t i = 0; i < mapping.Length(); ++i) {
78
0
    EXPECT_EQ(mapping[i].mEndOffset, gEndOffsets[i]);
79
0
    EXPECT_EQ(mapping[i].mSyncOffset, 361);
80
0
    EXPECT_EQ(mapping[i].mTimecode, gTimecodes[i]);
81
0
  }
82
0
}
83
84
TEST(WebMBuffered, RealDataAppend)
85
0
{
86
0
  ReentrantMonitor dummy("dummy");
87
0
  WebMBufferedParser parser(0);
88
0
  nsTArray<WebMTimeDataOffset> mapping;
89
0
90
0
  nsTArray<uint8_t> webmData;
91
0
  ReadFile("test.webm", webmData);
92
0
93
0
  uint32_t arrayEntries = mapping.Length();
94
0
  size_t offset = 0;
95
0
  while (offset < webmData.Length()) {
96
0
    parser.Append(webmData.Elements() + offset, 1, mapping, dummy);
97
0
    offset += 1;
98
0
    EXPECT_EQ(parser.mCurrentOffset, int64_t(offset));
99
0
    if (mapping.Length() != arrayEntries) {
100
0
      arrayEntries = mapping.Length();
101
0
      ASSERT_LE(arrayEntries, 6u);
102
0
      uint32_t i = arrayEntries - 1;
103
0
      EXPECT_EQ(mapping[i].mEndOffset, gEndOffsets[i]);
104
0
      EXPECT_EQ(mapping[i].mSyncOffset, 361);
105
0
      EXPECT_EQ(mapping[i].mTimecode, gTimecodes[i]);
106
0
      EXPECT_EQ(parser.GetTimecodeScale(), 500000u);
107
0
    }
108
0
  }
109
0
  EXPECT_EQ(mapping.Length(), 6u);
110
0
  EXPECT_EQ(parser.mStartOffset, 0);
111
0
  EXPECT_EQ(parser.mCurrentOffset, int64_t(webmData.Length()));
112
0
  EXPECT_EQ(parser.GetTimecodeScale(), 500000u);
113
0
114
0
  for (uint32_t i = 0; i < mapping.Length(); ++i) {
115
0
    EXPECT_EQ(mapping[i].mEndOffset, gEndOffsets[i]);
116
0
    EXPECT_EQ(mapping[i].mSyncOffset, 361);
117
0
    EXPECT_EQ(mapping[i].mTimecode, gTimecodes[i]);
118
0
  }
119
0
}