tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DecodedStream.h (4767B)


      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/. */
      6 
      7 #ifndef DecodedStream_h_
      8 #define DecodedStream_h_
      9 
     10 #include "AudibilityMonitor.h"
     11 #include "MediaEventSource.h"
     12 #include "MediaInfo.h"
     13 #include "MediaSink.h"
     14 #include "mozilla/AbstractThread.h"
     15 #include "mozilla/Maybe.h"
     16 #include "mozilla/MozPromise.h"
     17 #include "mozilla/RefPtr.h"
     18 #include "mozilla/StateMirroring.h"
     19 #include "mozilla/UniquePtr.h"
     20 
     21 namespace mozilla {
     22 
     23 class DecodedStreamData;
     24 class MediaDecoderStateMachine;
     25 class AudioData;
     26 class VideoData;
     27 struct PlaybackInfoInit;
     28 class ProcessedMediaTrack;
     29 struct SharedDummyTrack;
     30 class TimeStamp;
     31 
     32 template <class T>
     33 class MediaQueue;
     34 
     35 class DecodedStream : public MediaSink {
     36 public:
     37  DecodedStream(AbstractThread* aOwnerThread,
     38                nsMainThreadPtrHandle<SharedDummyTrack> aDummyTrack,
     39                CopyableTArray<RefPtr<ProcessedMediaTrack>> aOutputTracks,
     40                AbstractCanonical<PrincipalHandle>* aCanonicalOutputPrincipal,
     41                double aVolume, double aPlaybackRate, bool aPreservesPitch,
     42                MediaQueue<AudioData>& aAudioQueue,
     43                MediaQueue<VideoData>& aVideoQueue);
     44 
     45  RefPtr<EndedPromise> OnEnded(TrackType aType) override;
     46  media::TimeUnit GetEndTime(TrackType aType) const override;
     47  media::TimeUnit GetPosition(TimeStamp* aTimeStamp = nullptr) override;
     48  bool HasUnplayedFrames(TrackType aType) const override {
     49    // TODO: bug 1755026
     50    return false;
     51  }
     52 
     53  media::TimeUnit UnplayedDuration(TrackType aType) const override {
     54    // TODO: bug 1755026
     55    return media::TimeUnit::Zero();
     56  }
     57 
     58  void SetVolume(double aVolume) override;
     59  void SetPlaybackRate(double aPlaybackRate) override;
     60  void SetPreservesPitch(bool aPreservesPitch) override;
     61  void SetPlaying(bool aPlaying) override;
     62  RefPtr<GenericPromise> SetAudioDevice(
     63      RefPtr<AudioDeviceInfo> aDevice) override;
     64 
     65  double PlaybackRate() const override;
     66 
     67  nsresult Start(const media::TimeUnit& aStartTime,
     68                 const MediaInfo& aInfo) override;
     69  void Stop() override;
     70  bool IsStarted() const override;
     71  bool IsPlaying() const override;
     72  void Shutdown() override;
     73  void GetDebugInfo(dom::MediaSinkDebugInfo& aInfo) override;
     74 
     75  MediaEventSource<bool>& AudibleEvent() { return mAudibleEvent; }
     76 
     77 protected:
     78  virtual ~DecodedStream();
     79 
     80 private:
     81  void DestroyData(UniquePtr<DecodedStreamData>&& aData);
     82  void SendAudio(const PrincipalHandle& aPrincipalHandle);
     83  void SendVideo(const PrincipalHandle& aPrincipalHandle);
     84  void ResetAudio();
     85  void ResetVideo(const PrincipalHandle& aPrincipalHandle);
     86  void SendData();
     87  void NotifyOutput(int64_t aTime);
     88  void CheckIsDataAudible(const AudioData* aData);
     89 
     90  void AssertOwnerThread() const {
     91    MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
     92  }
     93 
     94  void PlayingChanged();
     95 
     96  void ConnectListener();
     97  void DisconnectListener();
     98 
     99  // Give the audio that is going to be appended next as an input, if there is
    100  // a gap between audio's time and the frames that we've written, then return
    101  // a silence data that has same amount of frames and can be used to fill the
    102  // gap. If no gap exists, return nullptr.
    103  already_AddRefed<AudioData> CreateSilenceDataIfGapExists(
    104      RefPtr<AudioData>& aNextAudio);
    105 
    106  const RefPtr<AbstractThread> mOwnerThread;
    107 
    108  // Used to access the graph.
    109  const nsMainThreadPtrHandle<SharedDummyTrack> mDummyTrack;
    110 
    111  /*
    112   * Worker thread only members.
    113   */
    114  WatchManager<DecodedStream> mWatchManager;
    115  UniquePtr<DecodedStreamData> mData;
    116  RefPtr<EndedPromise> mAudioEndedPromise;
    117  RefPtr<EndedPromise> mVideoEndedPromise;
    118 
    119  Watchable<bool> mPlaying;
    120  Mirror<PrincipalHandle> mPrincipalHandle;
    121  AbstractCanonical<PrincipalHandle>* mCanonicalOutputPrincipal;
    122  const nsTArray<RefPtr<ProcessedMediaTrack>> mOutputTracks;
    123 
    124  double mVolume;
    125  double mPlaybackRate;
    126  bool mPreservesPitch;
    127 
    128  media::NullableTimeUnit mStartTime;
    129  media::TimeUnit mLastOutputTime;
    130  MediaInfo mInfo;
    131  // True when stream is producing audible sound, false when stream is silent.
    132  bool mIsAudioDataAudible = false;
    133  Maybe<AudibilityMonitor> mAudibilityMonitor;
    134  MediaEventProducer<bool> mAudibleEvent;
    135 
    136  MediaQueue<AudioData>& mAudioQueue;
    137  MediaQueue<VideoData>& mVideoQueue;
    138 
    139  MediaEventListener mAudioPushListener;
    140  MediaEventListener mVideoPushListener;
    141  MediaEventListener mAudioFinishListener;
    142  MediaEventListener mVideoFinishListener;
    143  MediaEventListener mOutputListener;
    144 };
    145 
    146 }  // namespace mozilla
    147 
    148 #endif  // DecodedStream_h_