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
3 // found in the LICENSE file.
5 #include "content/renderer/media/mock_media_stream_dispatcher.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "content/public/common/media_stream_request.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // Used for ID for output devices and for matching output device ID for input
13 const char kAudioOutputDeviceIdPrefix[] = "audio_output_device_id";
17 MockMediaStreamDispatcher::MockMediaStreamDispatcher()
18 : MediaStreamDispatcher(NULL),
19 audio_input_request_id_(-1),
20 audio_output_request_id_(-1),
21 video_request_id_(-1),
22 request_stream_counter_(0),
23 stop_audio_device_counter_(0),
24 stop_video_device_counter_(0),
28 MockMediaStreamDispatcher::~MockMediaStreamDispatcher() {}
30 void MockMediaStreamDispatcher::GenerateStream(
32 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
33 const StreamOptions& components,
35 // Audio and video share the same request so we use |audio_input_request_id_|
37 audio_input_request_id_ = request_id;
39 stream_label_ = "local_stream" + base::IntToString(request_id);
40 audio_input_array_.clear();
43 if (components.audio_requested) {
44 AddAudioInputDeviceToArray(false);
46 if (components.video_requested) {
47 AddVideoDeviceToArray();
49 ++request_stream_counter_;
52 void MockMediaStreamDispatcher::CancelGenerateStream(
54 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) {
55 EXPECT_EQ(request_id, audio_input_request_id_);
58 void MockMediaStreamDispatcher::EnumerateDevices(
60 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
62 const GURL& security_origin) {
63 if (type == MEDIA_DEVICE_AUDIO_CAPTURE) {
64 audio_input_request_id_ = request_id;
65 audio_input_array_.clear();
66 AddAudioInputDeviceToArray(true);
67 AddAudioInputDeviceToArray(false);
68 } else if (type == MEDIA_DEVICE_AUDIO_OUTPUT) {
69 audio_output_request_id_ = request_id;
70 audio_output_array_.clear();
71 AddAudioOutputDeviceToArray();
72 } else if (type == MEDIA_DEVICE_VIDEO_CAPTURE) {
73 video_request_id_ = request_id;
75 AddVideoDeviceToArray();
79 void MockMediaStreamDispatcher::StopStreamDevice(
80 const StreamDeviceInfo& device_info) {
81 if (IsAudioInputMediaType(device_info.device.type)) {
82 ++stop_audio_device_counter_;
85 if (IsVideoMediaType(device_info.device.type)) {
86 ++stop_video_device_counter_;
92 bool MockMediaStreamDispatcher::IsStream(const std::string& label) {
96 int MockMediaStreamDispatcher::video_session_id(const std::string& label,
101 int MockMediaStreamDispatcher::audio_session_id(const std::string& label,
106 void MockMediaStreamDispatcher::AddAudioInputDeviceToArray(
107 bool matched_output) {
108 StreamDeviceInfo audio;
109 audio.device.id = "audio_input_device_id" + base::IntToString(session_id_);
110 audio.device.name = "microphone";
111 audio.device.type = MEDIA_DEVICE_AUDIO_CAPTURE;
112 if (matched_output) {
113 audio.device.matched_output_device_id =
114 kAudioOutputDeviceIdPrefix + base::IntToString(session_id_);
116 audio.session_id = session_id_;
117 audio_input_array_.push_back(audio);
120 void MockMediaStreamDispatcher::AddAudioOutputDeviceToArray() {
121 StreamDeviceInfo audio;
122 audio.device.id = kAudioOutputDeviceIdPrefix + base::IntToString(session_id_);
123 audio.device.name = "speaker";
124 audio.device.type = MEDIA_DEVICE_AUDIO_OUTPUT;
125 audio.session_id = session_id_;
126 audio_output_array_.push_back(audio);
129 void MockMediaStreamDispatcher::AddVideoDeviceToArray() {
130 StreamDeviceInfo video;
131 video.device.id = "video_device_id" + base::IntToString(session_id_);
132 video.device.name = "usb video camera";
133 video.device.type = MEDIA_DEVICE_VIDEO_CAPTURE;
134 video.session_id = session_id_;
135 video_array_.push_back(video);
138 } // namespace content