OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/media_stream_dispatcher.h" | 5 #include "content/renderer/media/media_stream_dispatcher.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "content/common/media/media_stream_messages.h" | 15 #include "content/common/media/media_stream_messages.h" |
16 #include "content/public/common/media_stream_request.h" | 16 #include "content/public/common/media_stream_request.h" |
17 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" | 17 #include "content/renderer/media/media_stream_dispatcher_eventhandler.h" |
18 #include "media/base/audio_parameters.h" | 18 #include "media/base/audio_parameters.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 #include "url/origin.h" | 21 #include "url/origin.h" |
22 | 22 |
23 namespace content { | 23 namespace content { |
24 namespace { | 24 namespace { |
25 | 25 |
26 const int kRouteId = 0; | 26 const int kRouteId = 0; |
27 const int kAudioSessionId = 3; | 27 const int kAudioSessionId = 3; |
28 const int kVideoSessionId = 5; | 28 const int kVideoSessionId = 5; |
| 29 const int kScreenSessionId = 7; |
29 const int kRequestId1 = 10; | 30 const int kRequestId1 = 10; |
30 const int kRequestId2 = 20; | 31 const int kRequestId2 = 20; |
31 | 32 |
32 const MediaStreamType kAudioType = MEDIA_DEVICE_AUDIO_CAPTURE; | 33 const MediaStreamType kAudioType = MEDIA_DEVICE_AUDIO_CAPTURE; |
33 const MediaStreamType kVideoType = MEDIA_DEVICE_VIDEO_CAPTURE; | 34 const MediaStreamType kVideoType = MEDIA_DEVICE_VIDEO_CAPTURE; |
| 35 const MediaStreamType kScreenType = MEDIA_DESKTOP_VIDEO_CAPTURE; |
34 | 36 |
35 class MockMediaStreamDispatcherEventHandler | 37 class MockMediaStreamDispatcherEventHandler |
36 : public MediaStreamDispatcherEventHandler, | 38 : public MediaStreamDispatcherEventHandler, |
37 public base::SupportsWeakPtr<MockMediaStreamDispatcherEventHandler> { | 39 public base::SupportsWeakPtr<MockMediaStreamDispatcherEventHandler> { |
38 public: | 40 public: |
39 MockMediaStreamDispatcherEventHandler() : request_id_(-1) {} | 41 MockMediaStreamDispatcherEventHandler() : request_id_(-1) {} |
40 | 42 |
41 void OnStreamGenerated( | 43 void OnStreamGenerated( |
42 int request_id, | 44 int request_id, |
43 const std::string& label, | 45 const std::string& label, |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 | 373 |
372 dispatcher_->OnMessageReceived( | 374 dispatcher_->OnMessageReceived( |
373 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); | 375 MediaStreamMsg_DeviceStopped(kRouteId, label, handler_->video_device_)); |
374 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been | 376 // Verify that MediaStreamDispatcherEventHandler::OnDeviceStopped has been |
375 // called. | 377 // called. |
376 EXPECT_EQ(label, handler_->device_stopped_label_); | 378 EXPECT_EQ(label, handler_->device_stopped_label_); |
377 EXPECT_EQ(dispatcher_->video_session_id(label, 0), | 379 EXPECT_EQ(dispatcher_->video_session_id(label, 0), |
378 StreamDeviceInfo::kNoId); | 380 StreamDeviceInfo::kNoId); |
379 } | 381 } |
380 | 382 |
| 383 TEST_F(MediaStreamDispatcherTest, GetNonScreenCaptureDevices) { |
| 384 std::unique_ptr<MediaStreamDispatcher> dispatcher( |
| 385 new MediaStreamDispatcher(nullptr)); |
| 386 std::unique_ptr<MockMediaStreamDispatcherEventHandler> handler( |
| 387 new MockMediaStreamDispatcherEventHandler); |
| 388 url::Origin security_origin; |
| 389 |
| 390 StreamDeviceInfo video_device_info; |
| 391 video_device_info.device.name = "Camera"; |
| 392 video_device_info.device.id = "device_path"; |
| 393 video_device_info.device.type = kVideoType; |
| 394 video_device_info.session_id = kVideoSessionId; |
| 395 |
| 396 StreamDeviceInfo screen_device_info; |
| 397 screen_device_info.device.name = "Screen"; |
| 398 screen_device_info.device.id = "screen_capture"; |
| 399 screen_device_info.device.type = kScreenType; |
| 400 screen_device_info.session_id = kScreenSessionId; |
| 401 |
| 402 EXPECT_EQ(dispatcher->requests_.size(), 0u); |
| 403 EXPECT_EQ(dispatcher->label_stream_map_.size(), 0u); |
| 404 |
| 405 int ipc_request_id1 = dispatcher->next_ipc_id_; |
| 406 dispatcher->OpenDevice(kRequestId1, handler.get()->AsWeakPtr(), |
| 407 video_device_info.device.id, kVideoType, |
| 408 security_origin); |
| 409 int ipc_request_id2 = dispatcher->next_ipc_id_; |
| 410 EXPECT_NE(ipc_request_id1, ipc_request_id2); |
| 411 dispatcher->OpenDevice(kRequestId2, handler.get()->AsWeakPtr(), |
| 412 screen_device_info.device.id, kScreenType, |
| 413 security_origin); |
| 414 EXPECT_EQ(dispatcher->requests_.size(), 2u); |
| 415 |
| 416 // Complete the OpenDevice of request 1. |
| 417 std::string stream_label1 = std::string("stream1"); |
| 418 dispatcher->OnMessageReceived(MediaStreamMsg_DeviceOpened( |
| 419 kRouteId, ipc_request_id1, stream_label1, video_device_info)); |
| 420 EXPECT_EQ(handler->request_id_, kRequestId1); |
| 421 |
| 422 // Complete the OpenDevice of request 2. |
| 423 std::string stream_label2 = std::string("stream2"); |
| 424 dispatcher->OnMessageReceived(MediaStreamMsg_DeviceOpened( |
| 425 kRouteId, ipc_request_id2, stream_label2, screen_device_info)); |
| 426 EXPECT_EQ(handler->request_id_, kRequestId2); |
| 427 |
| 428 EXPECT_EQ(dispatcher->requests_.size(), 0u); |
| 429 EXPECT_EQ(dispatcher->label_stream_map_.size(), 2u); |
| 430 |
| 431 // Only the device with |kVideoType| will be returned. |
| 432 StreamDeviceInfoArray video_device_array = |
| 433 dispatcher->GetNonScreenCaptureDevices(); |
| 434 EXPECT_EQ(video_device_array.size(), 1u); |
| 435 |
| 436 // Close the device from request 2. |
| 437 dispatcher->CloseDevice(stream_label2); |
| 438 EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), |
| 439 StreamDeviceInfo::kNoId); |
| 440 |
| 441 // Close the device from request 1. |
| 442 dispatcher->CloseDevice(stream_label1); |
| 443 EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), |
| 444 StreamDeviceInfo::kNoId); |
| 445 |
| 446 // Verify that the request have been completed. |
| 447 EXPECT_EQ(dispatcher->label_stream_map_.size(), 0u); |
| 448 EXPECT_EQ(dispatcher->requests_.size(), 0u); |
| 449 } |
| 450 |
381 } // namespace content | 451 } // namespace content |
OLD | NEW |