2014-06-02 19:38:04 +04:00
|
|
|
/* 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)
|
2015-10-20 17:47:00 +03:00
|
|
|
: mFileHandle(nullptr)
|
|
|
|
, mFileName(aFileName)
|
2014-06-02 19:38:04 +04:00
|
|
|
, mContentType(NS_LITERAL_CSTRING("video/mp4"))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-01-10 13:05:28 +03:00
|
|
|
size_t objectsRead = fread(aBuffer, aCount, 1, mFileHandle);
|
|
|
|
*aBytes = objectsRead == 1 ? aCount : 0;
|
2014-06-02 19:38:04 +04:00
|
|
|
|
|
|
|
mEntry--;
|
|
|
|
|
|
|
|
return ferror(mFileHandle) ? NS_ERROR_FAILURE : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
MockMediaResource::GetLength()
|
|
|
|
{
|
2015-11-17 18:37:20 +03:00
|
|
|
if (mFileHandle == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-02 03:48:36 +03:00
|
|
|
fseek(mFileHandle, 0, SEEK_END);
|
|
|
|
return ftell(mFileHandle);
|
2014-06-02 19:38:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MockMediaResource::MockClearBufferedRanges()
|
|
|
|
{
|
|
|
|
mRanges.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MockMediaResource::MockAddBufferedRange(int64_t aStart, int64_t aEnd)
|
|
|
|
{
|
2015-11-24 12:16:52 +03:00
|
|
|
mRanges += MediaByteRange(aStart, aEnd);
|
2014-06-02 19:38:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-11-24 12:16:52 +03:00
|
|
|
MockMediaResource::GetCachedRanges(MediaByteRangeSet& aRanges)
|
2014-06-02 19:38:04 +04:00
|
|
|
{
|
2015-11-24 12:16:52 +03:00
|
|
|
aRanges = mRanges;
|
2014-06-02 19:38:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|