summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/MockMediaResource.cpp
blob: 4e75f8d9d36acf7a7f797f4ddba8221028e6b419 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MockMediaResource.h"

#include <sys/types.h>
#include <sys/stat.h>

namespace mozilla
{

MockMediaResource::MockMediaResource(const char* aFileName, const nsACString& aContentType)
  : mFileHandle(nullptr)
  , mFileName(aFileName)
  , mContentType(aContentType)
{
}

nsresult
MockMediaResource::Open(nsIStreamListener** aStreamListener)
{
  mFileHandle = fopen(mFileName, "rb");
  if (mFileHandle == nullptr) {
    printf_stderr("Can't open %s\n", mFileName);
    return NS_ERROR_FAILURE;
  }
  return NS_OK;
}

MockMediaResource::~MockMediaResource()
{
  if (mFileHandle != nullptr) {
    fclose(mFileHandle);
  }
}

nsresult
MockMediaResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
                          uint32_t* aBytes)
{
  if (mFileHandle == nullptr) {
    return NS_ERROR_FAILURE;
  }

  // Make it fail if we're re-entrant
  if (mEntry++) {
    MOZ_ASSERT(false);
    return NS_ERROR_FAILURE;
  }

  fseek(mFileHandle, aOffset, SEEK_SET);
  size_t objectsRead = fread(aBuffer, aCount, 1, mFileHandle);
  *aBytes = objectsRead == 1 ? aCount : 0;

  mEntry--;

  return ferror(mFileHandle) ? NS_ERROR_FAILURE : NS_OK;
}

int64_t
MockMediaResource::GetLength()
{
  if (mFileHandle == nullptr) {
    return -1;
  }
  fseek(mFileHandle, 0, SEEK_END);
  return ftell(mFileHandle);
}

void
MockMediaResource::MockClearBufferedRanges()
{
  mRanges.Clear();
}

void
MockMediaResource::MockAddBufferedRange(int64_t aStart, int64_t aEnd)
{
  mRanges += MediaByteRange(aStart, aEnd);
}

int64_t
MockMediaResource::GetNextCachedData(int64_t aOffset)
{
  if (!aOffset) {
    return mRanges.Length() ? mRanges[0].mStart : -1;
  }
  for (size_t i = 0; i < mRanges.Length(); i++) {
    if (aOffset == mRanges[i].mStart) {
      ++i;
      return i < mRanges.Length() ? mRanges[i].mStart : -1;
    }
  }
  return -1;
}

int64_t
MockMediaResource::GetCachedDataEnd(int64_t aOffset)
{
  for (size_t i = 0; i < mRanges.Length(); i++) {
    if (aOffset == mRanges[i].mStart) {
      return mRanges[i].mEnd;
    }
  }
  return -1;
}

nsresult
MockMediaResource::GetCachedRanges(MediaByteRangeSet& aRanges)
{
  aRanges = mRanges;
  return NS_OK;
}

} // namespace mozilla