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),
|
2015-08-20 05:00:34 +03:00
|
|
|
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:
|
2016-01-18 06:50:29 +03:00
|
|
|
nsresult Close() override { return NS_OK; }
|
|
|
|
void Suspend(bool aCloseImmediately) override {}
|
|
|
|
void Resume() override {}
|
2012-11-23 01:44:20 +04:00
|
|
|
// Get the current principal for the channel
|
2016-01-18 06:50:29 +03:00
|
|
|
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = mPrincipal;
|
|
|
|
return principal.forget();
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
bool CanClone() override { return false; }
|
|
|
|
already_AddRefed<MediaResource> CloneData(MediaResourceCallback*) 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.
|
2016-01-18 06:50:29 +03:00
|
|
|
void SetReadMode(MediaCacheStream::ReadMode aMode) override {}
|
|
|
|
void SetPlaybackRate(uint32_t aBytesPerSecond) override {}
|
|
|
|
nsresult ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes) override
|
2013-07-17 05:54:52 +04:00
|
|
|
{
|
2015-08-13 04:15:48 +03:00
|
|
|
if (aOffset < 0 || aOffset > mLength) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-11-23 01:44:20 +04:00
|
|
|
}
|
2015-08-13 04:15:48 +03:00
|
|
|
*aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
|
|
|
|
memcpy(aBuffer, mBuffer + aOffset, *aBytes);
|
|
|
|
mOffset = aOffset + *aBytes;
|
2012-11-23 01:44:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-18 06:50:29 +03:00
|
|
|
int64_t Tell() override { return mOffset; }
|
|
|
|
|
|
|
|
void Pin() override {}
|
|
|
|
void Unpin() override {}
|
|
|
|
double GetDownloadRate(bool* aIsReliable) override { *aIsReliable = false; return 0.; }
|
|
|
|
int64_t GetLength() override { return mLength; }
|
|
|
|
int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
|
|
|
|
int64_t GetCachedDataEnd(int64_t aOffset) override { return mLength; }
|
|
|
|
bool IsDataCachedToEndOfResource(int64_t aOffset) override { return true; }
|
|
|
|
bool IsSuspendedByCache() override { return false; }
|
|
|
|
bool IsSuspended() override { return false; }
|
|
|
|
nsresult ReadFromCache(char* aBuffer,
|
|
|
|
int64_t aOffset,
|
|
|
|
uint32_t aCount) 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;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
nsresult Open(nsIStreamListener** aStreamListener) override
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
|
2012-11-23 01:44:20 +04:00
|
|
|
{
|
2015-11-24 12:37:21 +03:00
|
|
|
aRanges += MediaByteRange(0, int64_t(mLength));
|
2012-11-23 01:44:20 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
bool IsTransportSeekable() override { return true; }
|
2013-02-05 01:34:23 +04:00
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
const nsCString& GetContentType() const override
|
2013-02-15 05:10:58 +04:00
|
|
|
{
|
|
|
|
return mContentType;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-03-06 01:31:04 +04:00
|
|
|
{
|
|
|
|
// Not owned:
|
|
|
|
// - mBuffer
|
|
|
|
// - mPrincipal
|
|
|
|
size_t size = MediaResource::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mContentType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2016-01-18 06:50:29 +03:00
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
|
2014-03-06 01:31:04 +04:00
|
|
|
{
|
|
|
|
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;
|
2015-11-19 12:00:39 +03:00
|
|
|
const nsCString mContentType;
|
2012-11-23 01:44:20 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2012-11-23 01:44:20 +04:00
|
|
|
|
|
|
|
#endif
|