tor-browser

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

CrossGraphPort.h (3096B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef MOZILLA_CROSS_GRAPH_TRACK_H_
      7 #define MOZILLA_CROSS_GRAPH_TRACK_H_
      8 
      9 #include "AudioDriftCorrection.h"
     10 #include "AudioSegment.h"
     11 #include "ForwardedInputTrack.h"
     12 #include "mozilla/SPSCQueue.h"
     13 #include "mozilla/UniquePtr.h"
     14 
     15 namespace mozilla {
     16 class CrossGraphReceiver;
     17 }
     18 
     19 namespace mozilla::dom {
     20 class AudioStreamTrack;
     21 }
     22 
     23 namespace mozilla {
     24 
     25 /**
     26 * Create with MediaTrackGraph::CreateCrossGraphTransmitter()
     27 */
     28 class CrossGraphTransmitter : public ProcessedMediaTrack {
     29 public:
     30  CrossGraphTransmitter(TrackRate aSampleRate,
     31                        RefPtr<CrossGraphReceiver> aReceiver);
     32  CrossGraphTransmitter* AsCrossGraphTransmitter() override { return this; }
     33 
     34  uint32_t NumberOfChannels() const override {
     35    MOZ_CRASH("CrossGraphTransmitter has no segment. It cannot be played out.");
     36  }
     37  void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) override;
     38 
     39 private:
     40  const RefPtr<CrossGraphReceiver> mReceiver;
     41 };
     42 
     43 /**
     44 * Create with MediaTrackGraph::CreateCrossGraphReceiver()
     45 */
     46 class CrossGraphReceiver : public ProcessedMediaTrack {
     47 public:
     48  CrossGraphReceiver(TrackRate aSampleRate, TrackRate aTransmitterRate);
     49  CrossGraphReceiver* AsCrossGraphReceiver() override { return this; }
     50 
     51  uint32_t NumberOfChannels() const override;
     52  void ProcessInput(GraphTime aFrom, GraphTime aTo, uint32_t aFlags) override;
     53 
     54  int EnqueueAudio(AudioChunk& aChunk);
     55 
     56 private:
     57  SPSCQueue<AudioChunk> mCrossThreadFIFO{30};
     58  // Indicates that tre CrossGraphTransmitter has started sending frames. It
     59  // is false untill the point, transmitter has sent the first valid frame.
     60  // Accessed in GraphThread only.
     61  bool mTransmitterHasStarted = false;
     62  // Correct the drift between transmitter and receiver. Reciever (this class)
     63  // is considered as the master clock.
     64  // Accessed in GraphThread only.
     65  AudioDriftCorrection mDriftCorrection;
     66 };
     67 
     68 class CrossGraphPort final {
     69 public:
     70  static UniquePtr<CrossGraphPort> Connect(
     71      const RefPtr<dom::AudioStreamTrack>& aStreamTrack,
     72      MediaTrackGraph* aPartnerGraph);
     73  ~CrossGraphPort();
     74 
     75  const RefPtr<CrossGraphTransmitter> mTransmitter;
     76  const RefPtr<CrossGraphReceiver> mReceiver;
     77 
     78 private:
     79  explicit CrossGraphPort(RefPtr<MediaInputPort> aTransmitterPort,
     80                          RefPtr<CrossGraphTransmitter> aTransmitter,
     81                          RefPtr<CrossGraphReceiver> aReceiver)
     82      : mTransmitter(std::move(aTransmitter)),
     83        mReceiver(std::move(aReceiver)),
     84        mTransmitterPort(std::move(aTransmitterPort)) {
     85    MOZ_ASSERT(mTransmitter);
     86    MOZ_ASSERT(mReceiver);
     87    MOZ_ASSERT(mTransmitterPort);
     88  }
     89 
     90  // The port that connects the input track to the transmitter.
     91  const RefPtr<MediaInputPort> mTransmitterPort;
     92 };
     93 
     94 }  // namespace mozilla
     95 
     96 #endif /* MOZILLA_CROSS_GRAPH_TRACK_H_ */