Bug 1175768 - Implement SilentReadAt. r=jya

This commit is contained in:
Bobby Holley 2015-06-16 15:30:11 -07:00 коммит произвёл Jean-Yves Avenard
Родитель 56422fb439
Коммит 880d8829bc
2 изменённых файлов: 81 добавлений и 0 удалений

Просмотреть файл

@ -757,6 +757,28 @@ nsresult ChannelMediaResource::ReadAt(int64_t aOffset,
return rv;
}
already_AddRefed<MediaByteBuffer>
ChannelMediaResource::SilentReadAt(int64_t aOffset, uint32_t aCount)
{
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
nsRefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
bool ok = bytes->SetCapacity(aCount, fallible);
NS_ENSURE_TRUE(ok, nullptr);
int64_t pos = mCacheStream.Tell();
char* curr = reinterpret_cast<char*>(bytes->Elements());
while (aCount > 0) {
uint32_t bytesRead;
nsresult rv = mCacheStream.ReadAt(aOffset, curr, aCount, &bytesRead);
NS_ENSURE_SUCCESS(rv, nullptr);
NS_ENSURE_TRUE(bytesRead > 0, nullptr);
aCount -= bytesRead;
curr += bytesRead;
}
mCacheStream.Seek(nsISeekableStream::NS_SEEK_SET, pos);
return bytes.forget();
}
nsresult ChannelMediaResource::Seek(int32_t aWhence, int64_t aOffset)
{
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
@ -1193,6 +1215,7 @@ public:
virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) override;
virtual nsresult ReadAt(int64_t aOffset, char* aBuffer,
uint32_t aCount, uint32_t* aBytes) override;
virtual already_AddRefed<MediaByteBuffer> SilentReadAt(int64_t aOffset, uint32_t aCount) override;
virtual nsresult Seek(int32_t aWhence, int64_t aOffset) override;
virtual int64_t Tell() override;
@ -1516,6 +1539,34 @@ nsresult FileMediaResource::ReadAt(int64_t aOffset, char* aBuffer,
return rv;
}
already_AddRefed<MediaByteBuffer>
FileMediaResource::SilentReadAt(int64_t aOffset, uint32_t aCount)
{
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
MutexAutoLock lock(mLock);
nsRefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
bool ok = bytes->SetCapacity(aCount, fallible);
NS_ENSURE_TRUE(ok, nullptr);
int64_t pos = 0;
NS_ENSURE_TRUE(mSeekable, nullptr);
nsresult rv = mSeekable->Tell(&pos);
NS_ENSURE_SUCCESS(rv, nullptr);
rv = UnsafeSeek(nsISeekableStream::NS_SEEK_SET, aOffset);
NS_ENSURE_SUCCESS(rv, nullptr);
char* curr = reinterpret_cast<char*>(bytes->Elements());
while (aCount > 0) {
uint32_t bytesRead;
rv = UnsafeRead(curr, aCount, &bytesRead);
NS_ENSURE_SUCCESS(rv, nullptr);
NS_ENSURE_TRUE(bytesRead > 0, nullptr);
aCount -= bytesRead;
curr += bytesRead;
}
UnsafeSeek(nsISeekableStream::NS_SEEK_SET, pos);
return bytes.forget();
}
nsresult FileMediaResource::Seek(int32_t aWhence, int64_t aOffset)
{
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");

Просмотреть файл

@ -9,11 +9,13 @@
#include "mozilla/Mutex.h"
#include "nsIChannel.h"
#include "nsIURI.h"
#include "nsISeekableStream.h"
#include "nsIStreamingProtocolController.h"
#include "nsIStreamListener.h"
#include "nsIChannelEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "MediaCache.h"
#include "MediaData.h"
#include "mozilla/Attributes.h"
#include "mozilla/TimeStamp.h"
#include "nsThreadUtils.h"
@ -287,6 +289,33 @@ public:
// results and requirements are the same as per the Read method.
virtual nsresult ReadAt(int64_t aOffset, char* aBuffer,
uint32_t aCount, uint32_t* aBytes) = 0;
// ReadAt without side-effects. Given that our MediaResource infrastructure
// is very side-effecty, this accomplishes its job by checking the initial
// position and seeking back to it. If the seek were to fail, a side-effect
// might be observable.
//
// This method returns null if anything fails, including the failure to read
// aCount bytes. Otherwise, it returns an owned buffer.
virtual already_AddRefed<MediaByteBuffer> SilentReadAt(int64_t aOffset, uint32_t aCount)
{
nsRefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
bool ok = bytes->SetCapacity(aCount, fallible);
NS_ENSURE_TRUE(ok, nullptr);
int64_t pos = Tell();
char* curr = reinterpret_cast<char*>(bytes->Elements());
while (aCount > 0) {
uint32_t bytesRead;
nsresult rv = ReadAt(aOffset, curr, aCount, &bytesRead);
NS_ENSURE_SUCCESS(rv, nullptr);
NS_ENSURE_TRUE(bytesRead > 0, nullptr);
aCount -= bytesRead;
curr += bytesRead;
}
Seek(nsISeekableStream::NS_SEEK_SET, pos);
return bytes.forget();
}
// Seek to the given bytes offset in the stream. aWhence can be
// one of:
// NS_SEEK_SET
@ -610,6 +639,7 @@ public:
virtual nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes) override;
virtual nsresult ReadAt(int64_t offset, char* aBuffer,
uint32_t aCount, uint32_t* aBytes) override;
virtual already_AddRefed<MediaByteBuffer> SilentReadAt(int64_t aOffset, uint32_t aCount) override;
virtual nsresult Seek(int32_t aWhence, int64_t aOffset) override;
virtual int64_t Tell() override;