Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / dom / media / AudioInputSource.h
blobb44a3ae43a99193ae35a9084b15694ef44b21287
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 https://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_MEDIA_AudioInputSource_H_
8 #define DOM_MEDIA_AudioInputSource_H_
10 #include "AudioDriftCorrection.h"
11 #include "AudioSegment.h"
12 #include "CubebInputStream.h"
13 #include "CubebUtils.h"
14 #include "TimeUnits.h"
15 #include "mozilla/ProfilerUtils.h"
16 #include "mozilla/RefPtr.h"
17 #include "mozilla/SPSCQueue.h"
18 #include "mozilla/SharedThreadPool.h"
19 #include "mozilla/Variant.h"
21 namespace mozilla {
23 // This is an interface to operate an input-only audio stream within a
24 // cubeb-task thread on a specific thread. Once the class instance is created,
25 // all its operations must be called on the same thread.
27 // The audio data is periodically produced by the underlying audio stream on the
28 // stream's callback thread, and can be safely read by GetAudioSegment() on a
29 // specific thread.
30 class AudioInputSource : public CubebInputStream::Listener {
31 public:
32 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AudioInputSource, override);
34 using Id = uint32_t;
36 class EventListener {
37 public:
38 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING;
40 // This two events will be fired on main thread.
41 virtual void AudioDeviceChanged(Id aId) = 0;
42 enum class State { Started, Stopped, Drained, Error };
43 virtual void AudioStateCallback(Id aId, State aState) = 0;
45 protected:
46 EventListener() = default;
47 virtual ~EventListener() = default;
50 AudioInputSource(RefPtr<EventListener>&& aListener, Id aSourceId,
51 CubebUtils::AudioDeviceID aDeviceId, uint32_t aChannelCount,
52 bool aIsVoice, const PrincipalHandle& aPrincipalHandle,
53 TrackRate aSourceRate, TrackRate aTargetRate);
55 // The following functions should always be called in the same thread: They
56 // are always run on MediaTrackGraph's graph thread.
57 // Starts producing audio data.
58 void Start();
59 // Stops producing audio data.
60 void Stop();
61 // Returns the AudioSegment with aDuration of data inside.
62 // The graph thread can change behind the scene, e.g., cubeb stream reinit due
63 // to default output device changed). When this happens, we need to notify
64 // mSPSCQueue to change its data consumer.
65 enum class Consumer { Same, Changed };
66 AudioSegment GetAudioSegment(TrackTime aDuration, Consumer aConsumer);
68 // CubebInputStream::Listener interface: These are used only for the
69 // underlying audio stream. No user should call these APIs.
70 // This will be fired on audio callback thread.
71 long DataCallback(const void* aBuffer, long aFrames) override;
72 // This can be fired on any thread.
73 void StateCallback(cubeb_state aState) override;
74 // This can be fired on any thread.
75 void DeviceChangedCallback() override;
77 // Any threads:
78 // The unique id of this source.
79 const Id mId;
80 // The id of this audio device producing the data.
81 const CubebUtils::AudioDeviceID mDeviceId;
82 // The channel count of audio data produced.
83 const uint32_t mChannelCount;
84 // The sample rate of the audio data produced.
85 const TrackRate mRate;
86 // Indicate whether the audio stream is for voice or not.
87 const bool mIsVoice;
88 // The principal of the audio data produced.
89 const PrincipalHandle mPrincipalHandle;
91 protected:
92 ~AudioInputSource() = default;
94 private:
95 // Underlying audio thread only.
96 bool CheckThreadIdChanged();
98 // Any thread.
99 const bool mSandboxed;
101 // Thread id of the underlying audio thread. Underlying audio thread only.
102 std::atomic<ProfilerThreadId> mAudioThreadId;
104 // Forward the underlying event from main thread.
105 const RefPtr<EventListener> mEventListener;
107 // Shared thread pool containing only one thread for cubeb operations.
108 // The cubeb operations: Start() and Stop() will be called on
109 // MediaTrackGraph's graph thread, which can be the cubeb stream's callback
110 // thread. Running cubeb operations within cubeb stream callback thread can
111 // cause the deadlock on Linux, so we dispatch those operations to the task
112 // thread.
113 const RefPtr<SharedThreadPool> mTaskThread;
115 // Correct the drift between the underlying audio stream and its reader.
116 AudioDriftCorrection mDriftCorrector;
118 // An input-only cubeb stream operated within mTaskThread.
119 UniquePtr<CubebInputStream> mStream;
121 struct Empty {};
123 struct LatencyChangeData {
124 media::TimeUnit mLatency;
127 struct Data : public Variant<AudioChunk, LatencyChangeData, Empty> {
128 Data() : Variant(AsVariant(Empty())) {}
129 explicit Data(AudioChunk aChunk) : Variant(AsVariant(std::move(aChunk))) {}
130 explicit Data(LatencyChangeData aLatencyChangeData)
131 : Variant(AsVariant(std::move(aLatencyChangeData))) {}
134 // A single-producer-single-consumer lock-free queue whose data is produced by
135 // the audio callback thread and consumed by AudioInputSource's data reader.
136 SPSCQueue<Data> mSPSCQueue{30};
139 } // namespace mozilla
141 #endif // DOM_MEDIA_AudioInputSource_H_