Bug 1404977 - Part 11 - Make sure the default device is the first element in the list. r?pehrsons draft
authorPaul Adenot <paul@paul.cx>
Tue, 29 May 2018 15:20:47 +0200
changeset 825392 5a7fd45932e83797595811040d42e6da7acdf1fb
parent 824651 df30601c4ac9f7e02dec32ac62d1621c23708a13
child 825393 a58d90d41c640c38000bf3f6ca444ee573cc9c5c
push id118096
push userpaul@paul.cx
push dateWed, 01 Aug 2018 16:55:07 +0000
reviewerspehrsons
bugs1404977
milestone63.0a1
Bug 1404977 - Part 11 - Make sure the default device is the first element in the list. r?pehrsons MozReview-Commit-ID: LTJErFTm1wN
dom/media/AudioDeviceInfo.cpp
dom/media/AudioDeviceInfo.h
dom/media/webrtc/MediaEngineWebRTC.cpp
--- a/dom/media/AudioDeviceInfo.cpp
+++ b/dom/media/AudioDeviceInfo.cpp
@@ -96,16 +96,21 @@ uint32_t AudioDeviceInfo::Type() const
 {
   return mType;
 }
 uint32_t AudioDeviceInfo::State() const
 {
   return mState;
 }
 
+bool AudioDeviceInfo::Preferred() const
+{
+  return mPreferred;
+}
+
 /* readonly attribute DOMString name; */
 NS_IMETHODIMP
 AudioDeviceInfo::GetName(nsAString& aName)
 {
   aName = mName;
   return NS_OK;
 }
 
--- a/dom/media/AudioDeviceInfo.h
+++ b/dom/media/AudioDeviceInfo.h
@@ -36,16 +36,17 @@ public:
                   uint32_t aMinLatency);
   explicit AudioDeviceInfo(cubeb_device_info* aInfo);
 
   AudioDeviceID DeviceID() const;
   const nsString& Name() const;
   uint32_t MaxChannels() const;
   uint32_t Type() const;
   uint32_t State() const;
+  bool Preferred() const;
 private:
   virtual ~AudioDeviceInfo() = default;
 
   const AudioDeviceID mDeviceId;
   const nsString mName;
   const nsString mGroupId;
   const nsString mVendor;
   const uint16_t mType;
--- a/dom/media/webrtc/MediaEngineWebRTC.cpp
+++ b/dom/media/webrtc/MediaEngineWebRTC.cpp
@@ -201,45 +201,61 @@ MediaEngineWebRTC::EnumerateMicrophoneDe
           devices[i]->State(),
           NS_ConvertUTF16toUTF8(devices[i]->Name()).get(),
           devices[i]->DeviceID()));
 
     if (devices[i]->State() == CUBEB_DEVICE_STATE_ENABLED) {
       MOZ_ASSERT(devices[i]->Type() == CUBEB_DEVICE_TYPE_INPUT);
       RefPtr<MediaEngineSource> source =
         new MediaEngineWebRTCMicrophoneSource(
-          devices[i],
-          devices[i]->Name(),
-          // Lie and provide the name as UUID
-          NS_LossyConvertUTF16toASCII(devices[i]->Name()),
-          devices[i]->MaxChannels(),
-          mDelayAgnostic,
-          mExtendedFilter);
-      aDevices->AppendElement(source);
+            devices[i],
+            devices[i]->Name(),
+            // Lie and provide the name as UUID
+            NS_ConvertUTF16toUTF8(devices[i]->Name()),
+            devices[i]->MaxChannels(),
+            mDelayAgnostic,
+            mExtendedFilter);
+      RefPtr<MediaDevice> device = MakeRefPtr<MediaDevice>(
+                                     source,
+                                     source->GetName(),
+                                     NS_ConvertUTF8toUTF16(source->GetUUID()));
+      if (devices[i]->Preferred()) {
+#ifdef DEBUG
+        if (!foundPreferredDevice) {
+          foundPreferredDevice = true;
+        } else {
+          MOZ_ASSERT(!foundPreferredDevice,
+              "Found more than one preferred audio input device"
+              "while enumerating");
+        }
+#endif
+        aDevices->InsertElementAt(0, device);
+      } else {
+        aDevices->AppendElement(device);
+      }
     }
   }
 }
 
 void
 MediaEngineWebRTC::EnumerateSpeakerDevices(uint64_t aWindowId,
                                            nsTArray<RefPtr<MediaDevice> >* aDevices)
 {
   nsTArray<RefPtr<AudioDeviceInfo>> devices;
   CubebUtils::GetDeviceCollection(devices, CubebUtils::Output);
   for (auto& device : devices) {
-    MOZ_ASSERT(device->GetDeviceID().isSome());
     if (device->State() == CUBEB_DEVICE_STATE_ENABLED) {
       MOZ_ASSERT(device->Type() == CUBEB_DEVICE_TYPE_OUTPUT);
-      nsString uuid(device->FriendlyName());
+      nsString uuid(device->Name());
       // If, for example, input and output are in the same device, uuid
       // would be the same for both which ends up to create the same
       // deviceIDs (in JS).
       uuid.Append(NS_LITERAL_STRING("_Speaker"));
       aDevices->AppendElement(MakeRefPtr<MediaDevice>(
-                                device->FriendlyName(),
+                                device->Name(),
                                 dom::MediaDeviceKind::Audiooutput,
                                 uuid));
     }
   }
 }
 
 
 void