2013-09-26 22:22:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "SourceBufferResource.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "nsISeekableStream.h"
|
2014-02-26 13:36:35 -08:00
|
|
|
#include "nsISupportsImpl.h"
|
2013-09-26 22:22:37 -07:00
|
|
|
#include "prenv.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* gMediaSourceLog;
|
2014-03-04 19:35:46 -08:00
|
|
|
#define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__))
|
2013-09-26 22:22:37 -07:00
|
|
|
#else
|
2014-03-04 19:35:46 -08:00
|
|
|
#define MSE_DEBUG(...)
|
2013-09-26 22:22:37 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class SourceBuffer;
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SourceBufferResource::Close()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::Close", this);
|
2013-09-26 22:22:37 -07:00
|
|
|
//MOZ_ASSERT(!mClosed);
|
|
|
|
mClosed = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
bool blockingRead = !!aBytes;
|
|
|
|
|
2014-02-27 16:54:48 -08:00
|
|
|
while (blockingRead &&
|
|
|
|
!mEnded &&
|
|
|
|
mOffset + aCount > static_cast<uint64_t>(GetLength())) {
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::Read waiting for data", this);
|
2013-09-26 22:22:37 -07:00
|
|
|
mon.Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t available = GetLength() - mOffset;
|
|
|
|
uint32_t count = std::min(aCount, available);
|
|
|
|
if (!PR_GetEnv("MOZ_QUIET")) {
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::Read aCount=%u length=%u offset=%u "
|
|
|
|
"available=%u count=%u, blocking=%d bufComplete=%d",
|
|
|
|
this, aCount, GetLength(), mOffset, available, count,
|
|
|
|
blockingRead, mEnded);
|
2013-09-26 22:22:37 -07:00
|
|
|
}
|
|
|
|
if (available == 0) {
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::Read EOF", this);
|
2013-09-26 22:22:37 -07:00
|
|
|
*aBytes = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:54:48 -08:00
|
|
|
mInputBuffer.CopyData(mOffset, count, aBuffer);
|
2013-09-26 22:22:37 -07:00
|
|
|
*aBytes = count;
|
|
|
|
mOffset += count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return Read(aBuffer, aCount, aBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
if (mClosed) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t newOffset = mOffset;
|
|
|
|
switch (aWhence) {
|
|
|
|
case nsISeekableStream::NS_SEEK_END:
|
|
|
|
newOffset = GetLength() - aOffset;
|
|
|
|
break;
|
|
|
|
case nsISeekableStream::NS_SEEK_CUR:
|
|
|
|
newOffset += aOffset;
|
|
|
|
break;
|
|
|
|
case nsISeekableStream::NS_SEEK_SET:
|
|
|
|
newOffset = aOffset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-27 01:35:00 -07:00
|
|
|
if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) {
|
2013-09-26 22:22:37 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOffset = newOffset;
|
|
|
|
mon.NotifyAll();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return Read(aBuffer, aCount, nullptr);
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:54:48 -08:00
|
|
|
bool
|
|
|
|
SourceBufferResource::EvictData(uint32_t aThreshold)
|
|
|
|
{
|
|
|
|
return mInputBuffer.Evict(mOffset, aThreshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceBufferResource::EvictBefore(uint64_t aOffset)
|
|
|
|
{
|
|
|
|
// If aOffset is past the current playback offset we don't evict.
|
|
|
|
if (aOffset < mOffset) {
|
|
|
|
mInputBuffer.Evict(aOffset, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:22:37 -07:00
|
|
|
void
|
|
|
|
SourceBufferResource::AppendData(const uint8_t* aData, uint32_t aLength)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
2014-02-27 16:54:48 -08:00
|
|
|
mInputBuffer.PushBack(new ResourceItem(aData, aLength));
|
2013-09-26 22:22:37 -07:00
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SourceBufferResource::Ended()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
mEnded = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceBufferResource::~SourceBufferResource()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SourceBufferResource);
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::~SBR", this);
|
2013-09-26 22:22:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceBufferResource::SourceBufferResource(nsIPrincipal* aPrincipal,
|
|
|
|
const nsACString& aType)
|
|
|
|
: mPrincipal(aPrincipal)
|
|
|
|
, mType(aType)
|
|
|
|
, mMonitor("mozilla::SourceBufferResource::mMonitor")
|
|
|
|
, mOffset(0)
|
|
|
|
, mClosed(false)
|
|
|
|
, mEnded(false)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(SourceBufferResource);
|
2014-03-04 19:35:46 -08:00
|
|
|
MSE_DEBUG("%p SBR::SBR()", this);
|
2013-09-26 22:22:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|