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 "mozilla/ProfilerUtils.h"
15 #include "mozilla/RefPtr.h"
16 #include "mozilla/SPSCQueue.h"
17 #include "mozilla/SharedThreadPool.h"
21 // This is an interface to operate an input-only audio stream within a
22 // cubeb-task thread on a specific thread. Once the class instance is created,
23 // all its operations must be called on the same thread.
25 // The audio data is periodically produced by the underlying audio stream on the
26 // stream's callback thread, and can be safely read by GetAudioSegment() on a
28 class AudioInputSource
: public CubebInputStream::Listener
{
30 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(AudioInputSource
, override
);
36 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
;
38 // This two events will be fired on main thread.
39 virtual void AudioDeviceChanged(Id aId
) = 0;
40 enum class State
{ Started
, Stopped
, Drained
, Error
};
41 virtual void AudioStateCallback(Id aId
, State aState
) = 0;
44 EventListener() = default;
45 virtual ~EventListener() = default;
48 AudioInputSource(RefPtr
<EventListener
>&& aListener
, Id aSourceId
,
49 CubebUtils::AudioDeviceID aDeviceId
, uint32_t aChannelCount
,
50 bool aIsVoice
, const PrincipalHandle
& aPrincipalHandle
,
51 TrackRate aSourceRate
, TrackRate aTargetRate
,
54 // The following functions should always be called in the same thread: They
55 // are always run on MediaTrackGraph's graph thread.
56 // Starts producing audio data.
58 // Stops producing audio data.
60 // Returns the AudioSegment with aDuration of data inside.
61 // The graph thread can change behind the scene, e.g., cubeb stream reinit due
62 // to default output device changed). When this happens, we need to notify
63 // mSPSCQueue to change its data consumer.
64 enum class Consumer
{ Same
, Changed
};
65 AudioSegment
GetAudioSegment(TrackTime aDuration
, Consumer aConsumer
);
67 // CubebInputStream::Listener interface: These are used only for the
68 // underlying audio stream. No user should call these APIs.
69 // This will be fired on audio callback thread.
70 long DataCallback(const void* aBuffer
, long aFrames
) override
;
71 // This can be fired on any thread.
72 void StateCallback(cubeb_state aState
) override
;
73 // This can be fired on any thread.
74 void DeviceChangedCallback() override
;
77 // The unique id of this source.
79 // The id of this audio device producing the data.
80 const CubebUtils::AudioDeviceID mDeviceId
;
81 // The channel count of audio data produced.
82 const uint32_t mChannelCount
;
83 // The sample rate of the audio data produced.
84 const TrackRate mRate
;
85 // Indicate whether the audio stream is for voice or not.
87 // The principal of the audio data produced.
88 const PrincipalHandle mPrincipalHandle
;
91 ~AudioInputSource() = default;
94 // Underlying audio thread only.
95 bool CheckThreadIdChanged();
98 const bool mSandboxed
;
100 // Thread id of the underlying audio thread. Underlying audio thread only.
101 std::atomic
<ProfilerThreadId
> mAudioThreadId
;
103 // Forward the underlying event from main thread.
104 const RefPtr
<EventListener
> mEventListener
;
106 // Shared thread pool containing only one thread for cubeb operations.
107 // The cubeb operations: Start() and Stop() will be called on
108 // MediaTrackGraph's graph thread, which can be the cubeb stream's callback
109 // thread. Running cubeb operations within cubeb stream callback thread can
110 // cause the deadlock on Linux, so we dispatch those operations to the task
112 const RefPtr
<SharedThreadPool
> mTaskThread
;
114 // Correct the drift between the underlying audio stream and its reader.
115 AudioDriftCorrection mDriftCorrector
;
117 // An input-only cubeb stream operated within mTaskThread.
118 UniquePtr
<CubebInputStream
> mStream
;
120 // A single-producer-single-consumer lock-free queue whose data is produced by
121 // the audio callback thread and consumed by AudioInputSource's data reader.
122 SPSCQueue
<AudioChunk
> mSPSCQueue
{30};
125 } // namespace mozilla
127 #endif // DOM_MEDIA_AudioInputSource_H_