Bug 1650277 - P1. Let CubebDeviceEnumerator worry about MTA requirements. r=kinetik

Ths helps not having to worry about how to create the thread; which could be probablematic when running off a thread pool.

Differential Revision: https://phabricator.services.mozilla.com/D82142
This commit is contained in:
Jean-Yves Avenard 2020-07-08 03:59:22 +00:00
Родитель 30590c42ac
Коммит bd343a92a2
2 изменённых файлов: 82 добавлений и 87 удалений

Просмотреть файл

@ -1969,28 +1969,6 @@ bool MediaManager::IsInMediaThread() {
}
#endif
#ifdef XP_WIN
class MTAThread : public base::Thread {
public:
explicit MTAThread(const char* aName)
: base::Thread(aName), mResult(E_FAIL) {}
protected:
virtual void Init() override {
mResult = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
}
virtual void CleanUp() override {
if (SUCCEEDED(mResult)) {
CoUninitialize();
}
}
private:
HRESULT mResult;
};
#endif
// NOTE: never Dispatch(....,NS_DISPATCH_SYNC) to the MediaManager
// thread from the MainThread, as we NS_DISPATCH_SYNC to MainThread
// from MediaManager thread.
@ -2008,11 +1986,7 @@ MediaManager* MediaManager::Get() {
{
UniquePtr<base::Thread> mediaThread =
#ifdef XP_WIN
MakeUnique<MTAThread>("MediaManager");
#else
MakeUnique<base::Thread>("MediaManager");
#endif
base::Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_MOZILLA_NONMAINTHREAD;

Просмотреть файл

@ -5,8 +5,13 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CubebDeviceEnumerator.h"
#include "mozilla/Atomics.h"
#include "mozilla/StaticPtr.h"
#include "nsThreadUtils.h"
#ifdef XP_WIN
# include "mozilla/mscom/EnsureMTA.h"
#endif
namespace mozilla {
@ -27,24 +32,30 @@ CubebDeviceEnumerator::CubebDeviceEnumerator()
: mMutex("CubebDeviceListMutex"),
mManualInputInvalidation(false),
mManualOutputInvalidation(false) {
int rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT,
&OutputAudioDeviceListChanged_s, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not register the audio output"
" device collection changed callback.");
mManualOutputInvalidation = true;
}
rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT,
&InputAudioDeviceListChanged_s, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not register the audio input"
" device collection changed callback.");
mManualInputInvalidation = true;
}
#ifdef XP_WIN
mozilla::mscom::EnsureMTA([&]() -> void {
#endif
int rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT,
&OutputAudioDeviceListChanged_s, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not register the audio output"
" device collection changed callback.");
mManualOutputInvalidation = true;
}
rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT,
&InputAudioDeviceListChanged_s, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not register the audio input"
" device collection changed callback.");
mManualInputInvalidation = true;
}
#ifdef XP_WIN
});
#endif
}
/* static */
@ -55,20 +66,26 @@ void CubebDeviceEnumerator::Shutdown() {
}
CubebDeviceEnumerator::~CubebDeviceEnumerator() {
int rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT, nullptr, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not unregister the audio output"
" device collection changed callback.");
}
rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT, nullptr, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not unregister the audio input"
" device collection changed callback.");
}
#ifdef XP_WIN
mozilla::mscom::EnsureMTA([&]() -> void {
#endif
int rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_OUTPUT, nullptr, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not unregister the audio output"
" device collection changed callback.");
}
rv = cubeb_register_device_collection_changed(
GetCubebContext(), CUBEB_DEVICE_TYPE_INPUT, nullptr, this);
if (rv != CUBEB_OK) {
NS_WARNING(
"Could not unregister the audio input"
" device collection changed callback.");
}
#ifdef XP_WIN
});
#endif
}
void CubebDeviceEnumerator::EnumerateAudioInputDevices(
@ -149,26 +166,32 @@ static void GetDeviceCollection(nsTArray<RefPtr<AudioDeviceInfo>>& aDeviceInfos,
cubeb* context = GetCubebContext();
if (context) {
cubeb_device_collection collection = {nullptr, 0};
if (cubeb_enumerate_devices(
context,
aSide == Input ? CUBEB_DEVICE_TYPE_INPUT : CUBEB_DEVICE_TYPE_OUTPUT,
&collection) == CUBEB_OK) {
for (unsigned int i = 0; i < collection.count; ++i) {
auto device = collection.device[i];
RefPtr<AudioDeviceInfo> info = new AudioDeviceInfo(
device.devid, NS_ConvertUTF8toUTF16(device.friendly_name),
NS_ConvertUTF8toUTF16(device.group_id),
NS_ConvertUTF8toUTF16(device.vendor_name),
ConvertCubebType(device.type), ConvertCubebState(device.state),
ConvertCubebPreferred(device.preferred),
ConvertCubebFormat(device.format),
ConvertCubebFormat(device.default_format), device.max_channels,
device.default_rate, device.max_rate, device.min_rate,
device.latency_hi, device.latency_lo);
aDeviceInfos.AppendElement(info);
# ifdef XP_WIN
mozilla::mscom::EnsureMTA([&]() -> void {
# endif
if (cubeb_enumerate_devices(context,
aSide == Input ? CUBEB_DEVICE_TYPE_INPUT
: CUBEB_DEVICE_TYPE_OUTPUT,
&collection) == CUBEB_OK) {
for (unsigned int i = 0; i < collection.count; ++i) {
auto device = collection.device[i];
RefPtr<AudioDeviceInfo> info = new AudioDeviceInfo(
device.devid, NS_ConvertUTF8toUTF16(device.friendly_name),
NS_ConvertUTF8toUTF16(device.group_id),
NS_ConvertUTF8toUTF16(device.vendor_name),
ConvertCubebType(device.type), ConvertCubebState(device.state),
ConvertCubebPreferred(device.preferred),
ConvertCubebFormat(device.format),
ConvertCubebFormat(device.default_format), device.max_channels,
device.default_rate, device.max_rate, device.min_rate,
device.latency_hi, device.latency_lo);
aDeviceInfos.AppendElement(info);
}
}
}
cubeb_device_collection_destroy(context, &collection);
cubeb_device_collection_destroy(context, &collection);
# ifdef XP_WIN
});
# endif
}
}
#endif // non ANDROID
@ -327,16 +350,14 @@ void CubebDeviceEnumerator::OutputAudioDeviceListChanged_s(cubeb* aContext,
}
void CubebDeviceEnumerator::AudioDeviceListChanged(Side aSide) {
{
MutexAutoLock lock(mMutex);
if (aSide == Side::INPUT) {
mInputDevices.Clear();
mOnInputDeviceListChange.Notify();
} else {
MOZ_ASSERT(aSide == Side::OUTPUT);
mOutputDevices.Clear();
mOnOutputDeviceListChange.Notify();
}
MutexAutoLock lock(mMutex);
if (aSide == Side::INPUT) {
mInputDevices.Clear();
mOnInputDeviceListChange.Notify();
} else {
MOZ_ASSERT(aSide == Side::OUTPUT);
mOutputDevices.Clear();
mOnOutputDeviceListChange.Notify();
}
}