diff --git a/dom/media/MediaDevices.cpp b/dom/media/MediaDevices.cpp index fd5649dd1ed2..cde504b897ad 100644 --- a/dom/media/MediaDevices.cpp +++ b/dom/media/MediaDevices.cpp @@ -52,31 +52,34 @@ public: OnSuccess(nsIVariant* aDevices) override { // Cribbed from MediaPermissionGonk.cpp - nsIID elementIID; - uint16_t elementType; // Create array for nsIMediaDevice nsTArray> devices; // Contain the fumes { - void* rawArray; - uint32_t arrayLen; - nsresult rv; - rv = aDevices->GetAsArray(&elementType, &elementIID, &arrayLen, &rawArray); + uint16_t vtype; + nsresult rv = aDevices->GetDataType(&vtype); NS_ENSURE_SUCCESS(rv, rv); + if (vtype != nsIDataType::VTYPE_EMPTY_ARRAY) { + nsIID elementIID; + uint16_t elementType; + void* rawArray; + uint32_t arrayLen; + rv = aDevices->GetAsArray(&elementType, &elementIID, &arrayLen, &rawArray); + NS_ENSURE_SUCCESS(rv, rv); + if (elementType != nsIDataType::VTYPE_INTERFACE) { + free(rawArray); + return NS_ERROR_FAILURE; + } - if (elementType != nsIDataType::VTYPE_INTERFACE) { - free(rawArray); - return NS_ERROR_FAILURE; + nsISupports **supportsArray = reinterpret_cast(rawArray); + for (uint32_t i = 0; i < arrayLen; ++i) { + nsCOMPtr device(do_QueryInterface(supportsArray[i])); + devices.AppendElement(device); + NS_IF_RELEASE(supportsArray[i]); // explicitly decrease refcount for rawptr + } + free(rawArray); // explicitly free memory from nsIVariant::GetAsArray } - - nsISupports **supportsArray = reinterpret_cast(rawArray); - for (uint32_t i = 0; i < arrayLen; ++i) { - nsCOMPtr device(do_QueryInterface(supportsArray[i])); - devices.AppendElement(device); - NS_IF_RELEASE(supportsArray[i]); // explicitly decrease refcount for rawptr - } - free(rawArray); // explicitly free memory from nsIVariant::GetAsArray } nsTArray> infos; for (auto& device : devices) { diff --git a/dom/media/tests/mochitest/test_enumerateDevices.html b/dom/media/tests/mochitest/test_enumerateDevices.html index 9582d91c15fa..6b623d1b7989 100644 --- a/dom/media/tests/mochitest/test_enumerateDevices.html +++ b/dom/media/tests/mochitest/test_enumerateDevices.html @@ -21,10 +21,10 @@ function mustFailWith(msg, reason, f) { e => is(e.name, reason, msg + " must fail: " + e.message)); } -var pushPrefs = dict => new Promise(res => SpecialPowers.pushPrefEnv(dict, res)); +var pushPrefs = (...p) => new Promise(r => SpecialPowers.pushPrefEnv({set: p}, r)); runTest(() => - pushPrefs({ set : [["media.navigator.streams.fake", true]] }) + pushPrefs(["media.navigator.streams.fake", true]) .then(() => navigator.mediaDevices.enumerateDevices()) .then(devices => { ok(devices.length > 0, "At least one device found"); @@ -58,7 +58,12 @@ runTest(() => () => navigator.mediaDevices.getUserMedia({ audio: { deviceId: { exact: "unknown9qHr8B0JIbcHlbl9xR+jMbZZ8WyoPfpCXPfc=" } }, fake: true, - })))); + }))) + // Check the special case of no devices found (these prefs override fake). + .then(() => pushPrefs(["media.audio_loopback_dev", "none"], + ["media.video_loopback_dev", "none"])) + .then(() => navigator.mediaDevices.enumerateDevices()) + .then(devices => ok(devices.length === 0, "No devices found")));