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/. */
|
|
|
|
|
|
|
|
#ifndef MP4_STREAM_H_
|
|
|
|
#define MP4_STREAM_H_
|
|
|
|
|
2015-07-21 03:48:00 +03:00
|
|
|
#include "mp4_demuxer/Stream.h"
|
2014-12-16 08:10:46 +03:00
|
|
|
|
2015-01-12 00:24:26 +03:00
|
|
|
#include "MediaResource.h"
|
|
|
|
|
|
|
|
#include "mozilla/Maybe.h"
|
2015-01-13 02:50:09 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2015-12-03 02:07:59 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2014-12-22 09:04:36 +03:00
|
|
|
|
2014-12-16 08:10:46 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-01-12 00:24:26 +03:00
|
|
|
class Monitor;
|
2014-12-16 08:10:46 +03:00
|
|
|
|
|
|
|
class MP4Stream : public mp4_demuxer::Stream {
|
|
|
|
public:
|
2015-01-12 00:24:26 +03:00
|
|
|
explicit MP4Stream(MediaResource* aResource);
|
2014-12-16 08:10:46 +03:00
|
|
|
virtual ~MP4Stream();
|
2015-01-13 02:50:09 +03:00
|
|
|
bool BlockingReadIntoCache(int64_t aOffset, size_t aCount, Monitor* aToUnlock);
|
2016-01-18 06:33:44 +03:00
|
|
|
bool ReadAt(int64_t aOffset, void* aBuffer, size_t aCount,
|
|
|
|
size_t* aBytesRead) override;
|
|
|
|
bool CachedReadAt(int64_t aOffset, void* aBuffer, size_t aCount,
|
|
|
|
size_t* aBytesRead) override;
|
|
|
|
bool Length(int64_t* aSize) override;
|
2014-12-16 08:10:46 +03:00
|
|
|
|
2015-01-12 00:24:26 +03:00
|
|
|
struct ReadRecord {
|
|
|
|
ReadRecord(int64_t aOffset, size_t aCount) : mOffset(aOffset), mCount(aCount) {}
|
|
|
|
bool operator==(const ReadRecord& aOther) { return mOffset == aOther.mOffset && mCount == aOther.mCount; }
|
|
|
|
int64_t mOffset;
|
|
|
|
size_t mCount;
|
|
|
|
};
|
|
|
|
bool LastReadFailed(ReadRecord* aOut)
|
|
|
|
{
|
|
|
|
if (mFailedRead.isSome()) {
|
|
|
|
*aOut = mFailedRead.ref();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-15 20:58:38 +03:00
|
|
|
void ClearFailedRead() { mFailedRead.reset(); }
|
|
|
|
|
2015-01-13 02:50:09 +03:00
|
|
|
void Pin()
|
|
|
|
{
|
2015-08-13 04:15:54 +03:00
|
|
|
mResource.GetResource()->Pin();
|
2015-01-13 02:50:09 +03:00
|
|
|
++mPinCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unpin()
|
|
|
|
{
|
2015-08-13 04:15:54 +03:00
|
|
|
mResource.GetResource()->Unpin();
|
2015-01-13 02:50:09 +03:00
|
|
|
MOZ_ASSERT(mPinCount);
|
|
|
|
--mPinCount;
|
|
|
|
if (mPinCount == 0) {
|
|
|
|
mCache.Clear();
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 00:24:26 +03:00
|
|
|
|
2014-12-16 08:10:46 +03:00
|
|
|
private:
|
2015-08-13 04:15:54 +03:00
|
|
|
MediaResourceIndex mResource;
|
2015-01-12 00:24:26 +03:00
|
|
|
Maybe<ReadRecord> mFailedRead;
|
2015-01-13 02:50:09 +03:00
|
|
|
uint32_t mPinCount;
|
|
|
|
|
|
|
|
struct CacheBlock {
|
|
|
|
CacheBlock(int64_t aOffset, size_t aCount)
|
2015-01-27 20:36:09 +03:00
|
|
|
: mOffset(aOffset), mCount(aCount), mBuffer(nullptr) {}
|
2015-01-13 02:50:09 +03:00
|
|
|
int64_t mOffset;
|
|
|
|
size_t mCount;
|
2015-01-27 20:36:09 +03:00
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
CacheBlock(CacheBlock&& aOther)
|
|
|
|
: mOffset(aOther.mOffset)
|
|
|
|
, mCount(aOther.mCount)
|
|
|
|
, mBuffer(Move(aOther.mBuffer))
|
|
|
|
{}
|
|
|
|
|
2015-01-27 20:36:09 +03:00
|
|
|
bool Init()
|
|
|
|
{
|
2015-12-03 02:07:59 +03:00
|
|
|
mBuffer = MakeUniqueFallible<char[]>(mCount);
|
2015-01-27 20:36:09 +03:00
|
|
|
return !!mBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* Buffer()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBuffer.get());
|
|
|
|
return mBuffer.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-12-03 02:07:59 +03:00
|
|
|
CacheBlock(const CacheBlock&) = delete;
|
|
|
|
CacheBlock& operator=(const CacheBlock&) = delete;
|
|
|
|
|
|
|
|
UniquePtr<char[]> mBuffer;
|
2015-01-13 02:50:09 +03:00
|
|
|
};
|
|
|
|
nsTArray<CacheBlock> mCache;
|
2014-12-16 08:10:46 +03:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2014-12-16 08:10:46 +03:00
|
|
|
|
|
|
|
#endif
|