Commit 6a0339c9 authored by guidou's avatar guidou Committed by Commit bot

Use mojo support for Blink devicechange event.

BUG=648183

Review-Url: https://codereview.chromium.org/2476153002
Cr-Commit-Position: refs/heads/master@{#430318}
parent e39ff7bb
......@@ -287,13 +287,26 @@ void UserMediaClientImpl::setMediaDeviceChangeObserver(
const blink::WebMediaDeviceChangeObserver& observer) {
media_device_change_observer_ = observer;
// Do nothing if setting a valid observer while already subscribed or setting
// no observer while unsubscribed.
if (media_device_change_observer_.isNull() ==
device_change_subscription_ids_.empty())
return;
base::WeakPtr<MediaDevicesEventDispatcher> event_dispatcher =
MediaDevicesEventDispatcher::GetForRenderFrame(render_frame());
if (media_device_change_observer_.isNull()) {
media_stream_dispatcher_->CancelDeviceChangeNotifications(
weak_factory_.GetWeakPtr());
event_dispatcher->UnsubscribeDeviceChangeNotifications(
device_change_subscription_ids_);
device_change_subscription_ids_.clear();
} else {
url::Origin origin = observer.getSecurityOrigin();
media_stream_dispatcher_->SubscribeToDeviceChangeNotifications(
weak_factory_.GetWeakPtr(), origin);
DCHECK(device_change_subscription_ids_.empty());
url::Origin security_origin =
media_device_change_observer_.getSecurityOrigin();
device_change_subscription_ids_ =
event_dispatcher->SubscribeDeviceChangeNotifications(
security_origin, base::Bind(&UserMediaClientImpl::DevicesChanged,
weak_factory_.GetWeakPtr()));
}
}
......@@ -599,6 +612,8 @@ void UserMediaClientImpl::OnCreateNativeTracksCompleted(
DeleteUserMediaRequestInfo(request);
}
// TODO(guidou): Remove once this method is removed from the
// MediaStreamDispatcherEventHandler interface. http://648183.
void UserMediaClientImpl::OnDevicesEnumerated(
int request_id,
const StreamDeviceInfoArray& device_array) {
......@@ -620,8 +635,15 @@ void UserMediaClientImpl::OnDeviceOpenFailed(int request_id) {
NOTIMPLEMENTED();
}
// TODO(guidou): Remove once this method is removed from the
// MediaStreamDispatcherEventHandler interface. http://648183.
void UserMediaClientImpl::OnDevicesChanged() {
DVLOG(1) << "UserMediaClientImpl::OnDevicesChanged()";
NOTREACHED();
}
void UserMediaClientImpl::DevicesChanged(
MediaDeviceType type,
const MediaDeviceInfoArray& device_infos) {
if (!media_device_change_observer_.isNull())
media_device_change_observer_.didChangeMediaDevices();
}
......
......@@ -19,6 +19,7 @@
#include "content/common/media/media_devices.h"
#include "content/common/media/media_devices.mojom.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/renderer/media/media_devices_event_dispatcher.h"
#include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
#include "content/renderer/media/media_stream_source.h"
#include "services/service_manager/public/cpp/interface_provider.h"
......@@ -224,7 +225,7 @@ class CONTENT_EXPORT UserMediaClientImpl
const StreamDeviceInfoArray& audio_array,
const StreamDeviceInfoArray& video_array);
using EnumerationResult = std::vector<std::vector<MediaDeviceInfo>>;
using EnumerationResult = std::vector<MediaDeviceInfoArray>;
void FinalizeEnumerateDevices(blink::WebMediaDevicesRequest request,
const EnumerationResult& result);
......@@ -244,6 +245,11 @@ class CONTENT_EXPORT UserMediaClientImpl
const ::mojom::MediaDevicesDispatcherHostPtr& GetMediaDevicesDispatcher();
// Callback invoked by MediaDevicesEventDispatcher when a device-change
// notification arrives.
void DevicesChanged(MediaDeviceType device_type,
const MediaDeviceInfoArray& device_infos);
// Weak ref to a PeerConnectionDependencyFactory, owned by the RenderThread.
// It's valid for the lifetime of RenderThread.
// TODO(xians): Remove this dependency once audio do not need it for local
......@@ -259,6 +265,8 @@ class CONTENT_EXPORT UserMediaClientImpl
LocalStreamSources local_sources_;
UserMediaRequests user_media_requests_;
MediaDevicesEventDispatcher::SubscriptionIdList
device_change_subscription_ids_;
blink::WebMediaDeviceChangeObserver media_device_change_observer_;
......
......@@ -32,6 +32,8 @@
#include "third_party/WebKit/public/platform/WebVector.h"
#include "third_party/WebKit/public/web/WebHeap.h"
using testing::_;
namespace content {
class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource {
......@@ -49,7 +51,7 @@ class MockMediaStreamVideoCapturerSource : public MockMediaStreamVideoSource {
class MockMediaDevicesDispatcherHost
: public ::mojom::MediaDevicesDispatcherHost {
public:
MockMediaDevicesDispatcherHost() : binding_(this) {}
MockMediaDevicesDispatcherHost() {}
void EnumerateDevices(bool request_audio_input,
bool request_video_input,
bool request_audio_output,
......@@ -81,13 +83,6 @@ class MockMediaDevicesDispatcherHost
const url::Origin& security_origin));
MOCK_METHOD2(UnsubscribeDeviceChangeNotifications,
void(MediaDeviceType type, uint32_t subscription_id));
::mojom::MediaDevicesDispatcherHostPtr CreateInterfacePtrAndBind() {
return binding_.CreateInterfacePtrAndBind();
}
private:
mojo::Binding<::mojom::MediaDevicesDispatcherHost> binding_;
};
class UserMediaClientImplUnderTest : public UserMediaClientImpl {
......@@ -241,6 +236,10 @@ class UserMediaClientImplUnderTest : public UserMediaClientImpl {
class UserMediaClientImplTest : public ::testing::Test {
public:
UserMediaClientImplTest()
: binding_user_media(&media_devices_dispatcher_),
binding_event_dispatcher_(&media_devices_dispatcher_) {}
void SetUp() override {
// Create our test object.
child_process_.reset(new ChildProcess());
......@@ -250,10 +249,15 @@ class UserMediaClientImplTest : public ::testing::Test {
dependency_factory_.get(),
std::unique_ptr<MediaStreamDispatcher>(ms_dispatcher_)));
used_media_impl_->SetMediaDevicesDispatcherForTesting(
media_devices_dispatcher_.CreateInterfacePtrAndBind());
binding_user_media.CreateInterfacePtrAndBind());
base::WeakPtr<MediaDevicesEventDispatcher> event_dispatcher =
MediaDevicesEventDispatcher::GetForRenderFrame(nullptr);
event_dispatcher->SetMediaDevicesDispatcherForTesting(
binding_event_dispatcher_.CreateInterfacePtrAndBind());
}
void TearDown() override {
MediaDevicesEventDispatcher::GetForRenderFrame(nullptr)->OnDestruct();
used_media_impl_.reset();
blink::WebHeap::collectAllGarbageForTesting();
}
......@@ -331,6 +335,9 @@ class UserMediaClientImplTest : public ::testing::Test {
std::unique_ptr<ChildProcess> child_process_;
MockMediaStreamDispatcher* ms_dispatcher_; // Owned by |used_media_impl_|.
MockMediaDevicesDispatcherHost media_devices_dispatcher_;
mojo::Binding<::mojom::MediaDevicesDispatcherHost> binding_user_media;
mojo::Binding<::mojom::MediaDevicesDispatcherHost> binding_event_dispatcher_;
std::unique_ptr<UserMediaClientImplUnderTest> used_media_impl_;
std::unique_ptr<MockPeerConnectionDependencyFactory> dependency_factory_;
};
......@@ -653,14 +660,37 @@ TEST_F(UserMediaClientImplTest, RenderToAssociatedSinkConstraint) {
}
TEST_F(UserMediaClientImplTest, ObserveMediaDeviceChanges) {
// For a null UserMediaRequest (no audio requested), we expect false.
EXPECT_EQ(0U, ms_dispatcher_->NumDeviceChangeSubscribers());
EXPECT_CALL(
media_devices_dispatcher_,
SubscribeDeviceChangeNotifications(MEDIA_DEVICE_TYPE_AUDIO_INPUT, _, _));
EXPECT_CALL(
media_devices_dispatcher_,
SubscribeDeviceChangeNotifications(MEDIA_DEVICE_TYPE_VIDEO_INPUT, _, _));
EXPECT_CALL(
media_devices_dispatcher_,
SubscribeDeviceChangeNotifications(MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, _, _));
used_media_impl_->SetMediaDeviceChangeObserver();
EXPECT_EQ(1U, ms_dispatcher_->NumDeviceChangeSubscribers());
used_media_impl_->OnDevicesChanged();
base::RunLoop().RunUntilIdle();
base::WeakPtr<MediaDevicesEventDispatcher> event_dispatcher =
MediaDevicesEventDispatcher::GetForRenderFrame(nullptr);
event_dispatcher->DispatchDevicesChangedEvent(MEDIA_DEVICE_TYPE_AUDIO_INPUT,
MediaDeviceInfoArray());
event_dispatcher->DispatchDevicesChangedEvent(MEDIA_DEVICE_TYPE_VIDEO_INPUT,
MediaDeviceInfoArray());
event_dispatcher->DispatchDevicesChangedEvent(MEDIA_DEVICE_TYPE_AUDIO_OUTPUT,
MediaDeviceInfoArray());
base::RunLoop().RunUntilIdle();
EXPECT_CALL(media_devices_dispatcher_, UnsubscribeDeviceChangeNotifications(
MEDIA_DEVICE_TYPE_AUDIO_INPUT, _));
EXPECT_CALL(media_devices_dispatcher_, UnsubscribeDeviceChangeNotifications(
MEDIA_DEVICE_TYPE_VIDEO_INPUT, _));
EXPECT_CALL(
media_devices_dispatcher_,
UnsubscribeDeviceChangeNotifications(MEDIA_DEVICE_TYPE_AUDIO_OUTPUT, _));
used_media_impl_->RemoveMediaDeviceChangeObserver();
EXPECT_EQ(0U, ms_dispatcher_->NumDeviceChangeSubscribers());
used_media_impl_->OnDevicesChanged();
base::RunLoop().RunUntilIdle();
}
// This test what happens if the audio stream has same id with video stream.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment