Bug 1373577. P2 - move the code of MediaResource::MediaReadAt() into MediaResourceIndex.
MediaReadAt() accesses only public members of MediaResource. It doesn't have
to be a member of MediaResource.
MozReview-Commit-ID: D7ByCLiAF4X
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -201,41 +201,16 @@ 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;
// Indicate whether caching data in advance of reads is worth it.
// E.g. Caching lockless and memory-based MediaResource subclasses would be a
// waste, but caching lock/IO-bound resources means reducing the impact of
// each read.
virtual bool ShouldCacheReads() = 0;
- // This method returns nullptr if anything fails.
- // Otherwise, it returns an owned buffer.
- // MediaReadAt may return fewer bytes than requested if end of stream is
- // encountered. There is no need to call it again to get more data.
- already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount)
- {
- RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
- bool ok = bytes->SetLength(aCount, fallible);
- NS_ENSURE_TRUE(ok, nullptr);
- char* curr = reinterpret_cast<char*>(bytes->Elements());
- const char* start = curr;
- while (aCount > 0) {
- uint32_t bytesRead;
- nsresult rv = ReadAt(aOffset, curr, aCount, &bytesRead);
- NS_ENSURE_SUCCESS(rv, nullptr);
- if (!bytesRead) {
- break;
- }
- aOffset += bytesRead;
- aCount -= bytesRead;
- curr += bytesRead;
- }
- bytes->SetLength(curr - start);
- return bytes.forget();
- }
already_AddRefed<MediaByteBuffer> CachedReadAt(int64_t aOffset, uint32_t aCount)
{
RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
bool ok = bytes->SetLength(aCount, fallible);
NS_ENSURE_TRUE(ok, nullptr);
char* curr = reinterpret_cast<char*>(bytes->Elements());
nsresult rv = ReadFromCache(curr, aOffset, aCount);
@@ -823,17 +798,34 @@ public:
// by Tell().
// This method returns nullptr if anything fails.
// Otherwise, it returns an owned buffer.
// MediaReadAt may return fewer bytes than requested if end of stream is
// encountered. There is no need to call it again to get more data.
already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount) const
{
- return mResource->MediaReadAt(aOffset, aCount);
+ RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
+ bool ok = bytes->SetLength(aCount, fallible);
+ NS_ENSURE_TRUE(ok, nullptr);
+ char* curr = reinterpret_cast<char*>(bytes->Elements());
+ const char* start = curr;
+ while (aCount > 0) {
+ uint32_t bytesRead;
+ nsresult rv = mResource->ReadAt(aOffset, curr, aCount, &bytesRead);
+ NS_ENSURE_SUCCESS(rv, nullptr);
+ if (!bytesRead) {
+ break;
+ }
+ aOffset += bytesRead;
+ aCount -= bytesRead;
+ curr += bytesRead;
+ }
+ bytes->SetLength(curr - start);
+ return bytes.forget();
}
// Get the length of the stream in bytes. Returns -1 if not known.
// This can change over time; after a seek operation, a misbehaving
// server may give us a resource of a different length to what it had
// reported previously --- or it may just lie in its Content-Length
// header and give us more or less data than it reported. We will adjust
// the result of GetLength to reflect the data that's actually arriving.
int64_t GetLength() const { return mResource->GetLength(); }