tor-browser

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

AudioDestinationNode.h (4213B)


      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/. */
      6 
      7 #ifndef AudioDestinationNode_h_
      8 #define AudioDestinationNode_h_
      9 
     10 #include "AudioChannelAgent.h"
     11 #include "AudioChannelService.h"
     12 #include "AudioNode.h"
     13 #include "mozilla/TimeStamp.h"
     14 
     15 namespace mozilla::dom {
     16 
     17 class AudioContext;
     18 class WakeLock;
     19 
     20 class AudioDestinationNode final : public AudioNode,
     21                                   public nsIAudioChannelAgentCallback,
     22                                   public MainThreadMediaTrackListener {
     23 public:
     24  // This node type knows what MediaTrackGraph to use based on
     25  // whether it's in offline mode.
     26  AudioDestinationNode(AudioContext* aContext, bool aIsOffline,
     27                       uint32_t aNumberOfChannels, uint32_t aLength);
     28 
     29  void DestroyMediaTrack() override;
     30 
     31  NS_DECL_ISUPPORTS_INHERITED
     32  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioDestinationNode, AudioNode)
     33  NS_DECL_NSIAUDIOCHANNELAGENTCALLBACK
     34 
     35  JSObject* WrapObject(JSContext* aCx,
     36                       JS::Handle<JSObject*> aGivenProto) override;
     37 
     38  uint16_t NumberOfOutputs() const final { return 0; }
     39 
     40  uint32_t MaxChannelCount() const;
     41  void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv) override;
     42 
     43  void Init();
     44  void Close();
     45 
     46  // Returns the track or null after unlink.
     47  AudioNodeTrack* Track();
     48 
     49  void Mute();
     50  void Unmute();
     51 
     52  void Suspend();
     53  void Resume();
     54 
     55  void StartRendering(Promise* aPromise);
     56 
     57  void OfflineShutdown();
     58 
     59  void NotifyMainThreadTrackEnded() override;
     60  void FireOfflineCompletionEvent();
     61 
     62  const char* NodeType() const override { return "AudioDestinationNode"; }
     63 
     64  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
     65  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
     66 
     67  void NotifyDataAudibleStateChanged(bool aAudible);
     68  void ResolvePromise(AudioBuffer* aRenderedBuffer);
     69 
     70  unsigned long Length() {
     71    MOZ_ASSERT(mIsOffline);
     72    return mFramesToProduce;
     73  }
     74 
     75  void NotifyAudioContextStateChanged();
     76 
     77 protected:
     78  virtual ~AudioDestinationNode();
     79 
     80 private:
     81  // This would be created for non-offline audio context in order to receive
     82  // tab's mute/suspend/audio capture state change and update the audible state
     83  // to the tab.
     84  void CreateAndStartAudioChannelAgent();
     85  void DestroyAudioChannelAgentIfExists();
     86  RefPtr<AudioChannelAgent> mAudioChannelAgent;
     87 
     88  // These members are related to audio capturing. We would start capturing
     89  // audio if we're starting capturing audio from whole window, and MUST stop
     90  // capturing explicitly when we don't need to capture audio any more, because
     91  // we have to release the resource we allocated before.
     92  bool IsCapturingAudio() const;
     93  void StartAudioCapturingTrack();
     94  void StopAudioCapturingTrack();
     95  RefPtr<MediaInputPort> mCaptureTrackPort;
     96 
     97  // These members are used to determine if the destination node is actual
     98  // audible and `mFinalAudibleState` represents the final result.
     99  using AudibleChangedReasons = AudioChannelService::AudibleChangedReasons;
    100  using AudibleState = AudioChannelService::AudibleState;
    101  void UpdateFinalAudibleStateIfNeeded(AudibleChangedReasons aReason);
    102  bool IsAudible() const;
    103  bool mFinalAudibleState = false;
    104  bool mIsDataAudible = false;
    105  float mAudioChannelVolume = 1.0;
    106 
    107  // True if the audio channel disables the track for unvisited tab, and the
    108  // track will be enabled again when the tab gets first visited, or a user
    109  // presses the tab play icon.
    110  bool mAudioChannelDisabled = false;
    111 
    112  // When the destination node is audible, we would request a wakelock to
    113  // prevent computer from sleeping in order to keep audio playing.
    114  void CreateAudioWakeLockIfNeeded();
    115  void ReleaseAudioWakeLockIfExists();
    116  RefPtr<WakeLock> mWakeLock;
    117 
    118  SelfReference<AudioDestinationNode> mOfflineRenderingRef;
    119  uint32_t mFramesToProduce;
    120 
    121  RefPtr<Promise> mOfflineRenderingPromise;
    122 
    123  bool mIsOffline;
    124 };
    125 
    126 }  // namespace mozilla::dom
    127 
    128 #endif