/src/mozilla-central/dom/media/gtest/MockMediaResource.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 "MockMediaResource.h" |
6 | | |
7 | | #include <sys/types.h> |
8 | | #include <sys/stat.h> |
9 | | |
10 | | namespace mozilla |
11 | | { |
12 | | |
13 | | MockMediaResource::MockMediaResource(const char* aFileName) |
14 | | : mFileHandle(nullptr) |
15 | | , mFileName(aFileName) |
16 | 0 | { |
17 | 0 | } |
18 | | |
19 | | nsresult |
20 | | MockMediaResource::Open() |
21 | 0 | { |
22 | 0 | mFileHandle = fopen(mFileName, "rb"); |
23 | 0 | if (mFileHandle == nullptr) { |
24 | 0 | printf_stderr("Can't open %s\n", mFileName); |
25 | 0 | return NS_ERROR_FAILURE; |
26 | 0 | } |
27 | 0 | return NS_OK; |
28 | 0 | } |
29 | | |
30 | | MockMediaResource::~MockMediaResource() |
31 | 0 | { |
32 | 0 | if (mFileHandle != nullptr) { |
33 | 0 | fclose(mFileHandle); |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | | nsresult |
38 | | MockMediaResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, |
39 | | uint32_t* aBytes) |
40 | 0 | { |
41 | 0 | if (mFileHandle == nullptr) { |
42 | 0 | return NS_ERROR_FAILURE; |
43 | 0 | } |
44 | 0 | |
45 | 0 | // Make it fail if we're re-entrant |
46 | 0 | if (mEntry++) { |
47 | 0 | MOZ_ASSERT(false); |
48 | 0 | return NS_ERROR_FAILURE; |
49 | 0 | } |
50 | 0 |
|
51 | 0 | fseek(mFileHandle, aOffset, SEEK_SET); |
52 | 0 | *aBytes = fread(aBuffer, 1, aCount, mFileHandle); |
53 | 0 |
|
54 | 0 | mEntry--; |
55 | 0 |
|
56 | 0 | return ferror(mFileHandle) ? NS_ERROR_FAILURE : NS_OK; |
57 | 0 | } |
58 | | |
59 | | int64_t |
60 | | MockMediaResource::GetLength() |
61 | 0 | { |
62 | 0 | if (mFileHandle == nullptr) { |
63 | 0 | return -1; |
64 | 0 | } |
65 | 0 | fseek(mFileHandle, 0, SEEK_END); |
66 | 0 | return ftell(mFileHandle); |
67 | 0 | } |
68 | | |
69 | | void |
70 | | MockMediaResource::MockClearBufferedRanges() |
71 | 0 | { |
72 | 0 | mRanges.Clear(); |
73 | 0 | } |
74 | | |
75 | | void |
76 | | MockMediaResource::MockAddBufferedRange(int64_t aStart, int64_t aEnd) |
77 | 0 | { |
78 | 0 | mRanges += MediaByteRange(aStart, aEnd); |
79 | 0 | } |
80 | | |
81 | | int64_t |
82 | | MockMediaResource::GetNextCachedData(int64_t aOffset) |
83 | 0 | { |
84 | 0 | if (!aOffset) { |
85 | 0 | return mRanges.Length() ? mRanges[0].mStart : -1; |
86 | 0 | } |
87 | 0 | for (size_t i = 0; i < mRanges.Length(); i++) { |
88 | 0 | if (aOffset == mRanges[i].mStart) { |
89 | 0 | ++i; |
90 | 0 | return i < mRanges.Length() ? mRanges[i].mStart : -1; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | return -1; |
94 | 0 | } |
95 | | |
96 | | int64_t |
97 | | MockMediaResource::GetCachedDataEnd(int64_t aOffset) |
98 | 0 | { |
99 | 0 | for (size_t i = 0; i < mRanges.Length(); i++) { |
100 | 0 | if (aOffset == mRanges[i].mStart) { |
101 | 0 | return mRanges[i].mEnd; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | return aOffset; |
105 | 0 | } |
106 | | |
107 | | nsresult |
108 | | MockMediaResource::GetCachedRanges(MediaByteRangeSet& aRanges) |
109 | 0 | { |
110 | 0 | aRanges = mRanges; |
111 | 0 | return NS_OK; |
112 | 0 | } |
113 | | |
114 | | } // namespace mozilla |