2014-12-16 08:10:46 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "MP4Stream.h"
|
|
|
|
#include "MediaResource.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-01-12 00:24:26 +03:00
|
|
|
MP4Stream::MP4Stream(MediaResource* aResource)
|
2014-12-22 09:04:36 +03:00
|
|
|
: mResource(aResource)
|
2015-01-13 02:50:09 +03:00
|
|
|
, mPinCount(0)
|
2014-12-16 08:10:46 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(MP4Stream);
|
|
|
|
MOZ_ASSERT(aResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
MP4Stream::~MP4Stream()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MP4Stream);
|
2015-01-13 02:50:09 +03:00
|
|
|
MOZ_ASSERT(mPinCount == 0);
|
2014-12-16 08:10:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-13 02:50:09 +03:00
|
|
|
MP4Stream::BlockingReadIntoCache(int64_t aOffset, size_t aCount, Monitor* aToUnlock)
|
2014-12-16 08:10:46 +03:00
|
|
|
{
|
2015-01-13 02:50:09 +03:00
|
|
|
MOZ_ASSERT(mPinCount > 0);
|
|
|
|
CacheBlock block(aOffset, aCount);
|
2015-01-27 20:36:09 +03:00
|
|
|
if (!block.Init()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-13 02:50:09 +03:00
|
|
|
|
2014-12-16 08:10:46 +03:00
|
|
|
uint32_t sum = 0;
|
|
|
|
uint32_t bytesRead = 0;
|
|
|
|
do {
|
|
|
|
uint64_t offset = aOffset + sum;
|
2015-01-27 20:36:09 +03:00
|
|
|
char* buffer = block.Buffer() + sum;
|
2014-12-16 08:10:46 +03:00
|
|
|
uint32_t toRead = aCount - sum;
|
2015-01-13 02:50:09 +03:00
|
|
|
MonitorAutoUnlock unlock(*aToUnlock);
|
2014-12-16 08:10:46 +03:00
|
|
|
nsresult rv = mResource->ReadAt(offset, buffer, toRead, &bytesRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sum += bytesRead;
|
|
|
|
} while (sum < aCount && bytesRead > 0);
|
2015-01-13 02:50:09 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(block.mCount >= sum);
|
|
|
|
block.mCount = sum;
|
|
|
|
|
|
|
|
mCache.AppendElement(block);
|
2014-12-16 08:10:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-12 00:24:26 +03:00
|
|
|
// We surreptitiously reimplement the supposedly-blocking ReadAt as a non-
|
|
|
|
// blocking CachedReadAt, and record when it fails. This allows MP4Reader
|
|
|
|
// to retry the read as an actual blocking read without holding the lock.
|
|
|
|
bool
|
|
|
|
MP4Stream::ReadAt(int64_t aOffset, void* aBuffer, size_t aCount,
|
|
|
|
size_t* aBytesRead)
|
|
|
|
{
|
|
|
|
if (mFailedRead.isSome()) {
|
|
|
|
mFailedRead.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CachedReadAt(aOffset, aBuffer, aCount, aBytesRead)) {
|
|
|
|
mFailedRead.emplace(aOffset, aCount);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-16 08:10:46 +03:00
|
|
|
bool
|
|
|
|
MP4Stream::CachedReadAt(int64_t aOffset, void* aBuffer, size_t aCount,
|
|
|
|
size_t* aBytesRead)
|
|
|
|
{
|
2015-01-13 02:50:09 +03:00
|
|
|
// First, check our local cache.
|
|
|
|
for (size_t i = 0; i < mCache.Length(); ++i) {
|
|
|
|
if (mCache[i].mOffset == aOffset && mCache[i].mCount >= aCount) {
|
2015-01-27 20:36:09 +03:00
|
|
|
memcpy(aBuffer, mCache[i].Buffer(), aCount);
|
2015-01-13 02:50:09 +03:00
|
|
|
*aBytesRead = aCount;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 09:04:36 +03:00
|
|
|
|
2014-12-16 08:10:46 +03:00
|
|
|
nsresult rv = mResource->ReadFromCache(reinterpret_cast<char*>(aBuffer),
|
|
|
|
aOffset, aCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*aBytesRead = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aBytesRead = aCount;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MP4Stream::Length(int64_t* aSize)
|
|
|
|
{
|
|
|
|
if (mResource->GetLength() < 0)
|
|
|
|
return false;
|
|
|
|
*aSize = mResource->GetLength();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|