2018-11-08 15:33:18 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef CUBEBDEVICEENUMERATOR_H_
|
|
|
|
#define CUBEBDEVICEENUMERATOR_H_
|
|
|
|
|
|
|
|
#include "AudioDeviceInfo.h"
|
2019-03-18 19:53:33 +03:00
|
|
|
#include "CubebUtils.h"
|
2019-03-19 13:40:05 +03:00
|
|
|
#include "cubeb/cubeb.h"
|
Bug 1582637 - Move internal DeviceChange events to higher order functions. r=jib,achronop
This does three major things:
1) Moves the DeviceChange events from manual callbacks/listeners to
MediaEventSource/MediaEventListener. This is the reason this patch is so
large, as it traverses a lot of files.
There are four layers (from low to high):
- CamerasChild for camera device list changes, and CubebDeviceEnumerator for
microphone and speaker device list changes
- MediaEngineWebRTC, which gathers these into a single listener
- MediaManager, which owns the MediaEngineWebRTC backend
- MediaDevices, where the events from MediaManager are exposed to js
2) Changes the fake event triggering from starting a 30-event burst on setting
the js event listener, to a toggle, so that while the pref is on the events
keep coming.
3) Moves the fake event generation from CamerasChild to MediaEngineWebRTC, since
that's the lowest level where we are aware of both video and audio events.
The fake event generation is also greatly simplified. From being a dedicated
thread with periodic runnables, it is now a periodic timer on main thread
that fires while fake events are enabled. MediaEventProducer gracefully
handles thread safety.
Differential Revision: https://phabricator.services.mozilla.com/D48516
--HG--
extra : moz-landing-system : lando
2019-10-29 16:01:43 +03:00
|
|
|
#include "MediaEventSource.h"
|
2018-11-08 15:33:18 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
// This class implements a cache for accessing the audio device list.
|
|
|
|
// It can be accessed on any thread.
|
Bug 1582637 - Move internal DeviceChange events to higher order functions. r=jib,achronop
This does three major things:
1) Moves the DeviceChange events from manual callbacks/listeners to
MediaEventSource/MediaEventListener. This is the reason this patch is so
large, as it traverses a lot of files.
There are four layers (from low to high):
- CamerasChild for camera device list changes, and CubebDeviceEnumerator for
microphone and speaker device list changes
- MediaEngineWebRTC, which gathers these into a single listener
- MediaManager, which owns the MediaEngineWebRTC backend
- MediaDevices, where the events from MediaManager are exposed to js
2) Changes the fake event triggering from starting a 30-event burst on setting
the js event listener, to a toggle, so that while the pref is on the events
keep coming.
3) Moves the fake event generation from CamerasChild to MediaEngineWebRTC, since
that's the lowest level where we are aware of both video and audio events.
The fake event generation is also greatly simplified. From being a dedicated
thread with periodic runnables, it is now a periodic timer on main thread
that fires while fake events are enabled. MediaEventProducer gracefully
handles thread safety.
Differential Revision: https://phabricator.services.mozilla.com/D48516
--HG--
extra : moz-landing-system : lando
2019-10-29 16:01:43 +03:00
|
|
|
class CubebDeviceEnumerator final {
|
2018-11-08 15:33:18 +03:00
|
|
|
public:
|
2018-11-08 15:33:26 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CubebDeviceEnumerator)
|
|
|
|
|
2019-09-20 13:13:51 +03:00
|
|
|
static CubebDeviceEnumerator* GetInstance();
|
2018-11-08 15:33:26 +03:00
|
|
|
static void Shutdown();
|
2018-11-08 15:33:33 +03:00
|
|
|
// This method returns a list of all the input audio devices
|
|
|
|
// (sources) available on this machine.
|
2018-11-08 15:33:18 +03:00
|
|
|
// This method is safe to call from all threads.
|
|
|
|
void EnumerateAudioInputDevices(
|
|
|
|
nsTArray<RefPtr<AudioDeviceInfo>>& aOutDevices);
|
2018-11-08 15:33:33 +03:00
|
|
|
// Similar for the audio audio devices (sinks). Also thread safe.
|
|
|
|
void EnumerateAudioOutputDevices(
|
|
|
|
nsTArray<RefPtr<AudioDeviceInfo>>& aOutDevices);
|
2018-11-08 15:33:18 +03:00
|
|
|
// From a cubeb device id, return the info for this device, if it's still a
|
|
|
|
// valid id, or nullptr otherwise.
|
|
|
|
// This method is safe to call from any thread.
|
|
|
|
already_AddRefed<AudioDeviceInfo> DeviceInfoFromID(
|
|
|
|
CubebUtils::AudioDeviceID aID);
|
2019-03-19 13:40:05 +03:00
|
|
|
// From a device name, return the info for this device, if it's a valid name,
|
|
|
|
// or nullptr otherwise.
|
|
|
|
// This method is safe to call from any thread.
|
|
|
|
already_AddRefed<AudioDeviceInfo> DeviceInfoFromName(const nsString& aName);
|
|
|
|
enum class Side {
|
|
|
|
INPUT,
|
|
|
|
OUTPUT,
|
|
|
|
};
|
|
|
|
already_AddRefed<AudioDeviceInfo> DeviceInfoFromName(const nsString& aName,
|
|
|
|
Side aSide);
|
Bug 1582637 - Move internal DeviceChange events to higher order functions. r=jib,achronop
This does three major things:
1) Moves the DeviceChange events from manual callbacks/listeners to
MediaEventSource/MediaEventListener. This is the reason this patch is so
large, as it traverses a lot of files.
There are four layers (from low to high):
- CamerasChild for camera device list changes, and CubebDeviceEnumerator for
microphone and speaker device list changes
- MediaEngineWebRTC, which gathers these into a single listener
- MediaManager, which owns the MediaEngineWebRTC backend
- MediaDevices, where the events from MediaManager are exposed to js
2) Changes the fake event triggering from starting a 30-event burst on setting
the js event listener, to a toggle, so that while the pref is on the events
keep coming.
3) Moves the fake event generation from CamerasChild to MediaEngineWebRTC, since
that's the lowest level where we are aware of both video and audio events.
The fake event generation is also greatly simplified. From being a dedicated
thread with periodic runnables, it is now a periodic timer on main thread
that fires while fake events are enabled. MediaEventProducer gracefully
handles thread safety.
Differential Revision: https://phabricator.services.mozilla.com/D48516
--HG--
extra : moz-landing-system : lando
2019-10-29 16:01:43 +03:00
|
|
|
// Event source to listen for changes to the audio input device list on.
|
|
|
|
MediaEventSource<void>& OnAudioInputDeviceListChange() {
|
|
|
|
return mOnInputDeviceListChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event source to listen for changes to the audio output device list on.
|
|
|
|
MediaEventSource<void>& OnAudioOutputDeviceListChange() {
|
|
|
|
return mOnOutputDeviceListChange;
|
|
|
|
}
|
2018-11-08 15:33:18 +03:00
|
|
|
|
2020-03-27 21:41:07 +03:00
|
|
|
// Return the default device for a particular side.
|
|
|
|
RefPtr<AudioDeviceInfo> DefaultDevice(Side aSide);
|
|
|
|
|
2018-11-08 15:33:18 +03:00
|
|
|
private:
|
2018-11-08 15:33:26 +03:00
|
|
|
CubebDeviceEnumerator();
|
|
|
|
~CubebDeviceEnumerator();
|
2018-11-08 15:33:33 +03:00
|
|
|
// Static functions called by cubeb when the audio device list changes
|
2018-11-08 15:33:18 +03:00
|
|
|
// (i.e. when a new device is made available, or non-available). This
|
2018-11-08 15:33:33 +03:00
|
|
|
// simply calls `AudioDeviceListChanged` below.
|
|
|
|
static void InputAudioDeviceListChanged_s(cubeb* aContext, void* aUser);
|
|
|
|
static void OutputAudioDeviceListChanged_s(cubeb* aContext, void* aUser);
|
2018-11-08 15:33:18 +03:00
|
|
|
// Invalidates the cached audio input device list, can be called on any
|
|
|
|
// thread.
|
2018-11-08 15:33:33 +03:00
|
|
|
void AudioDeviceListChanged(Side aSide);
|
|
|
|
void EnumerateAudioDevices(Side aSide);
|
|
|
|
// Synchronize access to mInputDevices and mOutputDevices;
|
2018-11-08 15:33:18 +03:00
|
|
|
Mutex mMutex;
|
2018-11-08 15:33:33 +03:00
|
|
|
nsTArray<RefPtr<AudioDeviceInfo>> mInputDevices;
|
|
|
|
nsTArray<RefPtr<AudioDeviceInfo>> mOutputDevices;
|
|
|
|
// If mManual*Invalidation is true, then it is necessary to query the device
|
2018-11-08 15:33:18 +03:00
|
|
|
// list each time instead of relying on automatic invalidation of the cache by
|
|
|
|
// cubeb itself. Set in the constructor and then can be access on any thread.
|
2018-11-08 15:33:33 +03:00
|
|
|
bool mManualInputInvalidation;
|
|
|
|
bool mManualOutputInvalidation;
|
Bug 1582637 - Move internal DeviceChange events to higher order functions. r=jib,achronop
This does three major things:
1) Moves the DeviceChange events from manual callbacks/listeners to
MediaEventSource/MediaEventListener. This is the reason this patch is so
large, as it traverses a lot of files.
There are four layers (from low to high):
- CamerasChild for camera device list changes, and CubebDeviceEnumerator for
microphone and speaker device list changes
- MediaEngineWebRTC, which gathers these into a single listener
- MediaManager, which owns the MediaEngineWebRTC backend
- MediaDevices, where the events from MediaManager are exposed to js
2) Changes the fake event triggering from starting a 30-event burst on setting
the js event listener, to a toggle, so that while the pref is on the events
keep coming.
3) Moves the fake event generation from CamerasChild to MediaEngineWebRTC, since
that's the lowest level where we are aware of both video and audio events.
The fake event generation is also greatly simplified. From being a dedicated
thread with periodic runnables, it is now a periodic timer on main thread
that fires while fake events are enabled. MediaEventProducer gracefully
handles thread safety.
Differential Revision: https://phabricator.services.mozilla.com/D48516
--HG--
extra : moz-landing-system : lando
2019-10-29 16:01:43 +03:00
|
|
|
MediaEventProducer<void> mOnInputDeviceListChange;
|
|
|
|
MediaEventProducer<void> mOnOutputDeviceListChange;
|
2018-11-08 15:33:18 +03:00
|
|
|
};
|
|
|
|
|
2019-03-19 13:40:05 +03:00
|
|
|
typedef CubebDeviceEnumerator Enumerator;
|
|
|
|
typedef CubebDeviceEnumerator::Side EnumeratorSide;
|
2018-11-08 15:33:18 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // CUBEBDEVICEENUMERATOR_H_
|