content/media/mediasource/SourceBufferResource.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "SourceBufferResource.h"
     9 #include <string.h>
    10 #include <algorithm>
    12 #include "nsISeekableStream.h"
    13 #include "nsISupportsImpl.h"
    14 #include "prenv.h"
    15 #include "prlog.h"
    17 #ifdef PR_LOGGING
    18 extern PRLogModuleInfo* gMediaSourceLog;
    19 #define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__))
    20 #else
    21 #define MSE_DEBUG(...)
    22 #endif
    24 namespace mozilla {
    26 namespace dom {
    28 class SourceBuffer;
    30 }  // namespace dom
    32 nsresult
    33 SourceBufferResource::Close()
    34 {
    35   ReentrantMonitorAutoEnter mon(mMonitor);
    36   MSE_DEBUG("%p SBR::Close", this);
    37   //MOZ_ASSERT(!mClosed);
    38   mClosed = true;
    39   mon.NotifyAll();
    40   return NS_OK;
    41 }
    43 nsresult
    44 SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
    45 {
    46   ReentrantMonitorAutoEnter mon(mMonitor);
    47   bool blockingRead = !!aBytes;
    49   while (blockingRead &&
    50          !mEnded &&
    51          mOffset + aCount > static_cast<uint64_t>(GetLength())) {
    52     MSE_DEBUG("%p SBR::Read waiting for data", this);
    53     mon.Wait();
    54   }
    56   uint32_t available = GetLength() - mOffset;
    57   uint32_t count = std::min(aCount, available);
    58   if (!PR_GetEnv("MOZ_QUIET")) {
    59     MSE_DEBUG("%p SBR::Read aCount=%u length=%u offset=%u "
    60               "available=%u count=%u, blocking=%d bufComplete=%d",
    61               this, aCount, GetLength(), mOffset, available, count,
    62               blockingRead, mEnded);
    63   }
    64   if (available == 0) {
    65     MSE_DEBUG("%p SBR::Read EOF", this);
    66     *aBytes = 0;
    67     return NS_OK;
    68   }
    70   mInputBuffer.CopyData(mOffset, count, aBuffer);
    71   *aBytes = count;
    72   mOffset += count;
    73   return NS_OK;
    74 }
    76 nsresult
    77 SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes)
    78 {
    79   ReentrantMonitorAutoEnter mon(mMonitor);
    80   nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
    81   if (NS_FAILED(rv)) {
    82     return rv;
    83   }
    84   return Read(aBuffer, aCount, aBytes);
    85 }
    87 nsresult
    88 SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset)
    89 {
    90   ReentrantMonitorAutoEnter mon(mMonitor);
    91   if (mClosed) {
    92     return NS_ERROR_FAILURE;
    93   }
    95   int64_t newOffset = mOffset;
    96   switch (aWhence) {
    97   case nsISeekableStream::NS_SEEK_END:
    98     newOffset = GetLength() - aOffset;
    99     break;
   100   case nsISeekableStream::NS_SEEK_CUR:
   101     newOffset += aOffset;
   102     break;
   103   case nsISeekableStream::NS_SEEK_SET:
   104     newOffset = aOffset;
   105     break;
   106   }
   108   if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) {
   109     return NS_ERROR_FAILURE;
   110   }
   112   mOffset = newOffset;
   113   mon.NotifyAll();
   115   return NS_OK;
   116 }
   118 nsresult
   119 SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
   120 {
   121   ReentrantMonitorAutoEnter mon(mMonitor);
   122   nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
   123   if (NS_FAILED(rv)) {
   124     return rv;
   125   }
   126   return Read(aBuffer, aCount, nullptr);
   127 }
   129 bool
   130 SourceBufferResource::EvictData(uint32_t aThreshold)
   131 {
   132   return mInputBuffer.Evict(mOffset, aThreshold);
   133 }
   135 void
   136 SourceBufferResource::EvictBefore(uint64_t aOffset)
   137 {
   138   // If aOffset is past the current playback offset we don't evict.
   139   if (aOffset < mOffset) {
   140     mInputBuffer.Evict(aOffset, 0);
   141   }
   142 }
   144 void
   145 SourceBufferResource::AppendData(const uint8_t* aData, uint32_t aLength)
   146 {
   147   ReentrantMonitorAutoEnter mon(mMonitor);
   148   mInputBuffer.PushBack(new ResourceItem(aData, aLength));
   149   mon.NotifyAll();
   150 }
   152 void
   153 SourceBufferResource::Ended()
   154 {
   155   ReentrantMonitorAutoEnter mon(mMonitor);
   156   mEnded = true;
   157   mon.NotifyAll();
   158 }
   160 SourceBufferResource::~SourceBufferResource()
   161 {
   162   MOZ_COUNT_DTOR(SourceBufferResource);
   163   MSE_DEBUG("%p SBR::~SBR", this);
   164 }
   166 SourceBufferResource::SourceBufferResource(nsIPrincipal* aPrincipal,
   167                                            const nsACString& aType)
   168   : mPrincipal(aPrincipal)
   169   , mType(aType)
   170   , mMonitor("mozilla::SourceBufferResource::mMonitor")
   171   , mOffset(0)
   172   , mClosed(false)
   173   , mEnded(false)
   174 {
   175   MOZ_COUNT_CTOR(SourceBufferResource);
   176   MSE_DEBUG("%p SBR::SBR()", this);
   177 }
   179 } // namespace mozilla

mercurial