content/media/fmp4/MP4Reader.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
     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 #if !defined(MP4Reader_h_)
     8 #define MP4Reader_h_
    10 #include "MediaDecoderReader.h"
    11 #include "nsAutoPtr.h"
    12 #include "PlatformDecoderModule.h"
    13 #include "mp4_demuxer/mp4_demuxer.h"
    14 #include "mp4_demuxer/box_definitions.h"
    15 #include "MediaTaskQueue.h"
    17 #include <deque>
    18 #include "mozilla/Monitor.h"
    20 namespace mozilla {
    22 namespace dom {
    23 class TimeRanges;
    24 }
    26 typedef std::deque<mp4_demuxer::MP4Sample*> MP4SampleQueue;
    28 class MP4Stream;
    30 class MP4Reader : public MediaDecoderReader
    31 {
    32 public:
    33   MP4Reader(AbstractMediaDecoder* aDecoder);
    35   virtual ~MP4Reader();
    37   virtual nsresult Init(MediaDecoderReader* aCloneDonor) MOZ_OVERRIDE;
    39   virtual bool DecodeAudioData() MOZ_OVERRIDE;
    40   virtual bool DecodeVideoFrame(bool &aKeyframeSkip,
    41                                 int64_t aTimeThreshold) MOZ_OVERRIDE;
    43   virtual bool HasAudio() MOZ_OVERRIDE;
    44   virtual bool HasVideo() MOZ_OVERRIDE;
    46   virtual nsresult ReadMetadata(MediaInfo* aInfo,
    47                                 MetadataTags** aTags) MOZ_OVERRIDE;
    49   virtual nsresult Seek(int64_t aTime,
    50                         int64_t aStartTime,
    51                         int64_t aEndTime,
    52                         int64_t aCurrentTime) MOZ_OVERRIDE;
    53 private:
    55   // Destroys all decoder resources.
    56   void Shutdown();
    58   // Initializes mLayersBackendType if possible.
    59   void InitLayersBackendType();
    61   // Blocks until the demuxer produces an sample of specified type.
    62   // Returns nullptr on error on EOS. Caller must delete sample.
    63   mp4_demuxer::MP4Sample* PopSample(mp4_demuxer::TrackType aTrack);
    65   bool SkipVideoDemuxToNextKeyFrame(int64_t aTimeThreshold, uint32_t& parsed);
    67   void Output(mp4_demuxer::TrackType aType, MediaData* aSample);
    68   void InputExhausted(mp4_demuxer::TrackType aTrack);
    69   void Error(mp4_demuxer::TrackType aTrack);
    70   bool Decode(mp4_demuxer::TrackType aTrack);
    71   void Flush(mp4_demuxer::TrackType aTrack);
    73   nsAutoPtr<mp4_demuxer::MP4Demuxer> mDemuxer;
    74   nsAutoPtr<MP4Stream> mMP4Stream;
    75   nsAutoPtr<PlatformDecoderModule> mPlatform;
    77   class DecoderCallback : public MediaDataDecoderCallback {
    78   public:
    79     DecoderCallback(MP4Reader* aReader,
    80                     mp4_demuxer::TrackType aType)
    81       : mReader(aReader)
    82       , mType(aType)
    83     {
    84     }
    85     virtual void Output(MediaData* aSample) MOZ_OVERRIDE {
    86       mReader->Output(mType, aSample);
    87     }
    88     virtual void InputExhausted() MOZ_OVERRIDE {
    89       mReader->InputExhausted(mType);
    90     }
    91     virtual void Error() MOZ_OVERRIDE {
    92       mReader->Error(mType);
    93     }
    94   private:
    95     MP4Reader* mReader;
    96     mp4_demuxer::TrackType mType;
    97   };
    99   struct DecoderData {
   100     DecoderData(const char* aMonitorName,
   101                 uint32_t aDecodeAhead)
   102       : mMonitor(aMonitorName)
   103       , mNumSamplesInput(0)
   104       , mNumSamplesOutput(0)
   105       , mDecodeAhead(aDecodeAhead)
   106       , mActive(false)
   107       , mInputExhausted(false)
   108       , mError(false)
   109       , mIsFlushing(false)
   110     {
   111     }
   113     // The platform decoder.
   114     RefPtr<MediaDataDecoder> mDecoder;
   115     // Queue of input extracted by the demuxer, but not yet sent to the
   116     // platform decoder.
   117     MP4SampleQueue mDemuxedSamples;
   118     // TaskQueue on which decoder can choose to decode.
   119     // Only non-null up until the decoder is created.
   120     RefPtr<MediaTaskQueue> mTaskQueue;
   121     // Callback that receives output and error notifications from the decoder.
   122     nsAutoPtr<DecoderCallback> mCallback;
   123     // Monitor that protects all non-threadsafe state; the primitives
   124     // that follow.
   125     Monitor mMonitor;
   126     uint64_t mNumSamplesInput;
   127     uint64_t mNumSamplesOutput;
   128     uint32_t mDecodeAhead;
   129     // Whether this stream exists in the media.
   130     bool mActive;
   131     bool mInputExhausted;
   132     bool mError;
   133     bool mIsFlushing;
   134   };
   135   DecoderData mAudio;
   136   DecoderData mVideo;
   138   // The last number of decoded output frames that we've reported to
   139   // MediaDecoder::NotifyDecoded(). We diff the number of output video
   140   // frames every time that DecodeVideoData() is called, and report the
   141   // delta there.
   142   uint64_t mLastReportedNumDecodedFrames;
   144   DecoderData& GetDecoderData(mp4_demuxer::TrackType aTrack);
   145   MP4SampleQueue& SampleQueue(mp4_demuxer::TrackType aTrack);
   146   MediaDataDecoder* Decoder(mp4_demuxer::TrackType aTrack);
   148   layers::LayersBackend mLayersBackendType;
   150 };
   152 } // namespace mozilla
   154 #endif

mercurial