2012-11-23 01:44:20 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#if !defined(BufferMediaResource_h_)
|
|
|
|
#define BufferMediaResource_h_
|
|
|
|
|
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// A simple MediaResource based on an in memory buffer. This class accepts
|
|
|
|
// the address and the length of the buffer, and simulates a read/seek API
|
|
|
|
// on top of it. The Read implementation involves copying memory, which is
|
|
|
|
// unfortunate, but the MediaResource interface mandates that.
|
|
|
|
class BufferMediaResource : public MediaResource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BufferMediaResource(const uint8_t* aBuffer,
|
|
|
|
uint32_t aLength,
|
2013-02-15 05:10:58 +04:00
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aContentType) :
|
2012-11-23 01:44:20 +04:00
|
|
|
mBuffer(aBuffer),
|
|
|
|
mLength(aLength),
|
|
|
|
mOffset(0),
|
2013-02-15 05:10:58 +04:00
|
|
|
mPrincipal(aPrincipal),
|
|
|
|
mContentType(aContentType)
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BufferMediaResource);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2012-11-23 01:44:20 +04:00
|
|
|
virtual ~BufferMediaResource()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(BufferMediaResource);
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
private:
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual nsresult Close() MOZ_OVERRIDE { return NS_OK; }
|
|
|
|
virtual void Suspend(bool aCloseImmediately) MOZ_OVERRIDE {}
|
|
|
|
virtual void Resume() MOZ_OVERRIDE {}
|
2012-11-23 01:44:20 +04:00
|
|
|
// Get the current principal for the channel
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal() MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mPrincipal;
|
|
|
|
return principal.forget();
|
|
|
|
}
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual bool CanClone() MOZ_OVERRIDE { return false; }
|
|
|
|
virtual already_AddRefed<MediaResource> CloneData(MediaDecoder* aDecoder) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These methods are called off the main thread.
|
|
|
|
// The mode is initially MODE_PLAYBACK.
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual void SetReadMode(MediaCacheStream::ReadMode aMode) MOZ_OVERRIDE {}
|
|
|
|
virtual void SetPlaybackRate(uint32_t aBytesPerSecond) MOZ_OVERRIDE {}
|
|
|
|
virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
*aBytes = std::min(mLength - mOffset, aCount);
|
|
|
|
memcpy(aBuffer, mBuffer + mOffset, *aBytes);
|
|
|
|
mOffset += *aBytes;
|
|
|
|
MOZ_ASSERT(mOffset <= mLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-07-17 05:54:52 +04:00
|
|
|
virtual nsresult ReadAt(int64_t aOffset, char* aBuffer,
|
2015-01-03 05:08:06 +03:00
|
|
|
uint32_t aCount, uint32_t* aBytes) MOZ_OVERRIDE
|
2013-07-17 05:54:52 +04:00
|
|
|
{
|
|
|
|
nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return Read(aBuffer, aCount, aBytes);
|
|
|
|
}
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual nsresult Seek(int32_t aWhence, int64_t aOffset) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aOffset <= UINT32_MAX);
|
|
|
|
switch (aWhence) {
|
|
|
|
case nsISeekableStream::NS_SEEK_SET:
|
|
|
|
if (aOffset < 0 || aOffset > mLength) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mOffset = static_cast<uint32_t> (aOffset);
|
|
|
|
break;
|
|
|
|
case nsISeekableStream::NS_SEEK_CUR:
|
|
|
|
if (aOffset >= mLength - mOffset) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mOffset += static_cast<uint32_t> (aOffset);
|
|
|
|
break;
|
|
|
|
case nsISeekableStream::NS_SEEK_END:
|
|
|
|
if (aOffset < 0 || aOffset > mLength) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mOffset = mLength - aOffset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual int64_t Tell() MOZ_OVERRIDE { return mOffset; }
|
|
|
|
|
|
|
|
virtual void Pin() MOZ_OVERRIDE {}
|
|
|
|
virtual void Unpin() MOZ_OVERRIDE {}
|
|
|
|
virtual double GetDownloadRate(bool* aIsReliable) MOZ_OVERRIDE { *aIsReliable = false; return 0.; }
|
|
|
|
virtual int64_t GetLength() MOZ_OVERRIDE { return mLength; }
|
|
|
|
virtual int64_t GetNextCachedData(int64_t aOffset) MOZ_OVERRIDE { return aOffset; }
|
|
|
|
virtual int64_t GetCachedDataEnd(int64_t aOffset) MOZ_OVERRIDE { return mLength; }
|
|
|
|
virtual bool IsDataCachedToEndOfResource(int64_t aOffset) MOZ_OVERRIDE { return true; }
|
|
|
|
virtual bool IsSuspendedByCache() MOZ_OVERRIDE { return false; }
|
|
|
|
virtual bool IsSuspended() MOZ_OVERRIDE { return false; }
|
2012-11-23 01:44:20 +04:00
|
|
|
virtual nsresult ReadFromCache(char* aBuffer,
|
|
|
|
int64_t aOffset,
|
2015-01-03 05:08:06 +03:00
|
|
|
uint32_t aCount) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
if (aOffset < 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
|
|
|
|
memcpy(aBuffer, mBuffer + aOffset, bytes);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual nsresult Open(nsIStreamListener** aStreamListener) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-01-03 05:08:06 +03:00
|
|
|
virtual nsresult GetCachedRanges(nsTArray<MediaByteRange>& aRanges) MOZ_OVERRIDE
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
aRanges.AppendElement(MediaByteRange(0, mLength));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-05 01:34:23 +04:00
|
|
|
bool IsTransportSeekable() MOZ_OVERRIDE { return true; }
|
|
|
|
|
2013-02-26 15:02:20 +04:00
|
|
|
virtual const nsCString& GetContentType() const MOZ_OVERRIDE
|
2013-02-15 05:10:58 +04:00
|
|
|
{
|
|
|
|
return mContentType;
|
|
|
|
}
|
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
virtual size_t SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
// Not owned:
|
|
|
|
// - mBuffer
|
|
|
|
// - mPrincipal
|
|
|
|
size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mContentType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-11-23 01:44:20 +04:00
|
|
|
private:
|
|
|
|
const uint8_t * mBuffer;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint32_t mOffset;
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2013-02-15 05:10:58 +04:00
|
|
|
const nsAutoCString mContentType;
|
2012-11-23 01:44:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|