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 bytesRead = 0;
|
2015-08-13 04:15:54 +03:00
|
|
|
{
|
2015-01-13 02:50:09 +03:00
|
|
|
MonitorAutoUnlock unlock(*aToUnlock);
|
2015-08-13 04:15:54 +03:00
|
|
|
nsresult rv = mResource.ReadAt(aOffset, block.Buffer(), aCount, &bytesRead);
|
2014-12-16 08:10:46 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-13 04:15:54 +03:00
|
|
|
}
|
2015-01-13 02:50:09 +03:00
|
|
|
|
2015-08-13 04:15:54 +03:00
|
|
|
MOZ_ASSERT(block.mCount >= bytesRead);
|
|
|
|
block.mCount = bytesRead;
|
2015-01-13 02:50:09 +03:00
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
mCache.AppendElement(Move(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
|
|
|
|
2015-08-13 04:15:54 +03:00
|
|
|
nsresult rv =
|
|
|
|
mResource.GetResource()->ReadFromCache(reinterpret_cast<char*>(aBuffer),
|
|
|
|
aOffset, aCount);
|
2014-12-16 08:10:46 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*aBytesRead = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aBytesRead = aCount;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MP4Stream::Length(int64_t* aSize)
|
|
|
|
{
|
2015-08-13 04:15:54 +03:00
|
|
|
if (mResource.GetLength() < 0)
|
2014-12-16 08:10:46 +03:00
|
|
|
return false;
|
2015-08-13 04:15:54 +03:00
|
|
|
*aSize = mResource.GetLength();
|
2014-12-16 08:10:46 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|