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/. */
6 #ifndef MOZILLA_CROSS_GRAPH_TRACK_H_
7 #define MOZILLA_CROSS_GRAPH_TRACK_H_
9 #include "AudioDriftCorrection.h"
10 #include "AudioSegment.h"
11 #include "ForwardedInputTrack.h"
12 #include "mozilla/SPSCQueue.h"
13 #include "mozilla/UniquePtr.h"
16 class CrossGraphReceiver
;
19 namespace mozilla::dom
{
20 class AudioStreamTrack
;
26 * See MediaTrackGraph::CreateCrossGraphTransmitter()
28 class CrossGraphTransmitter
: public ProcessedMediaTrack
{
30 CrossGraphTransmitter(TrackRate aSampleRate
,
31 RefPtr
<CrossGraphReceiver
> aReceiver
);
32 CrossGraphTransmitter
* AsCrossGraphTransmitter() override
{ return this; }
34 uint32_t NumberOfChannels() const override
{
35 MOZ_CRASH("CrossGraphTransmitter has no segment. It cannot be played out.");
37 void ProcessInput(GraphTime aFrom
, GraphTime aTo
, uint32_t aFlags
) override
;
40 const RefPtr
<CrossGraphReceiver
> mReceiver
;
44 * See MediaTrackGraph::CreateCrossGraphReceiver()
46 class CrossGraphReceiver
: public ProcessedMediaTrack
{
48 CrossGraphReceiver(TrackRate aSampleRate
, TrackRate aTransmitterRate
);
49 CrossGraphReceiver
* AsCrossGraphReceiver() override
{ return this; }
51 uint32_t NumberOfChannels() const override
;
52 void ProcessInput(GraphTime aFrom
, GraphTime aTo
, uint32_t aFlags
) override
;
54 int EnqueueAudio(AudioChunk
& aChunk
);
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
;
68 class CrossGraphPort final
{
70 static UniquePtr
<CrossGraphPort
> Connect(
71 const RefPtr
<dom::AudioStreamTrack
>& aStreamTrack
,
72 MediaTrackGraph
* aPartnerGraph
);
73 static UniquePtr
<CrossGraphPort
> Connect(
74 const RefPtr
<dom::AudioStreamTrack
>& aStreamTrack
, AudioDeviceInfo
* aSink
,
75 nsPIDOMWindowInner
* aWindow
);
78 void AddAudioOutput(void* aKey
);
79 void RemoveAudioOutput(void* aKey
);
80 void SetAudioOutputVolume(void* aKey
, float aVolume
);
82 RefPtr
<GenericPromise
> EnsureConnected();
84 const RefPtr
<CrossGraphTransmitter
> mTransmitter
;
85 const RefPtr
<CrossGraphReceiver
> mReceiver
;
88 explicit CrossGraphPort(RefPtr
<MediaInputPort
> aTransmitterPort
,
89 RefPtr
<CrossGraphTransmitter
> aTransmitter
,
90 RefPtr
<CrossGraphReceiver
> aReceiver
)
91 : mTransmitter(std::move(aTransmitter
)),
92 mReceiver(std::move(aReceiver
)),
93 mTransmitterPort(std::move(aTransmitterPort
)) {
94 MOZ_ASSERT(mTransmitter
);
95 MOZ_ASSERT(mReceiver
);
96 MOZ_ASSERT(mTransmitterPort
);
99 // The port that connects the input track to the transmitter.
100 const RefPtr
<MediaInputPort
> mTransmitterPort
;
103 } // namespace mozilla
105 #endif /* MOZILLA_CROSS_GRAPH_TRACK_H_ */