summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/mock_media_stream_dispatcher.cc
blob: 7854a8d6574bbd60bfe53e5d92357ba4945e297d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/media/mock_media_stream_dispatcher.h"

#include "base/strings/string_number_conversions.h"
#include "content/public/common/media_stream_request.h"
#include "testing/gtest/include/gtest/gtest.h"

// Used for ID for output devices and for matching output device ID for input
// devices.
const char kAudioOutputDeviceIdPrefix[] = "audio_output_device_id";

namespace content {

MockMediaStreamDispatcher::MockMediaStreamDispatcher()
    : MediaStreamDispatcher(NULL),
      audio_input_request_id_(-1),
      audio_output_request_id_(-1),
      video_request_id_(-1),
      request_stream_counter_(0),
      stop_audio_device_counter_(0),
      stop_video_device_counter_(0),
      session_id_(0) {
}

MockMediaStreamDispatcher::~MockMediaStreamDispatcher() {}

void MockMediaStreamDispatcher::GenerateStream(
    int request_id,
    const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
    const StreamOptions& components,
    const GURL& url) {
  // Audio and video share the same request so we use |audio_input_request_id_|
  // only.
  audio_input_request_id_ = request_id;

  stream_label_ = "local_stream" + base::IntToString(request_id);
  audio_input_array_.clear();
  video_array_.clear();

  if (components.audio_requested) {
    AddAudioInputDeviceToArray(false);
  }
  if (components.video_requested) {
    AddVideoDeviceToArray();
  }
  ++request_stream_counter_;
}

void MockMediaStreamDispatcher::CancelGenerateStream(
    int request_id,
    const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) {
  EXPECT_EQ(request_id, audio_input_request_id_);
}

void MockMediaStreamDispatcher::EnumerateDevices(
    int request_id,
    const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
    MediaStreamType type,
    const GURL& security_origin) {
  if (type == MEDIA_DEVICE_AUDIO_CAPTURE) {
    audio_input_request_id_ = request_id;
    audio_input_array_.clear();
    AddAudioInputDeviceToArray(true);
    AddAudioInputDeviceToArray(false);
  } else if (type == MEDIA_DEVICE_AUDIO_OUTPUT) {
    audio_output_request_id_ = request_id;
    audio_output_array_.clear();
    AddAudioOutputDeviceToArray();
  } else if (type == MEDIA_DEVICE_VIDEO_CAPTURE) {
    video_request_id_ = request_id;
    video_array_.clear();
    AddVideoDeviceToArray();
  }
}

void MockMediaStreamDispatcher::StopStreamDevice(
    const StreamDeviceInfo& device_info) {
  if (IsAudioInputMediaType(device_info.device.type)) {
    ++stop_audio_device_counter_;
    return;
  }
  if (IsVideoMediaType(device_info.device.type)) {
    ++stop_video_device_counter_;
    return;
  }
  NOTREACHED();
}

bool MockMediaStreamDispatcher::IsStream(const std::string& label) {
  return true;
}

int MockMediaStreamDispatcher::video_session_id(const std::string& label,
                                                int index) {
  return -1;
}

int MockMediaStreamDispatcher::audio_session_id(const std::string& label,
                                                int index) {
  return -1;
}

void MockMediaStreamDispatcher::AddAudioInputDeviceToArray(
    bool matched_output) {
  StreamDeviceInfo audio;
  audio.device.id = "audio_input_device_id" + base::IntToString(session_id_);
  audio.device.name = "microphone";
  audio.device.type = MEDIA_DEVICE_AUDIO_CAPTURE;
  if (matched_output) {
    audio.device.matched_output_device_id =
        kAudioOutputDeviceIdPrefix + base::IntToString(session_id_);
  }
  audio.session_id = session_id_;
  audio_input_array_.push_back(audio);
}

void MockMediaStreamDispatcher::AddAudioOutputDeviceToArray() {
  StreamDeviceInfo audio;
  audio.device.id = kAudioOutputDeviceIdPrefix + base::IntToString(session_id_);
  audio.device.name = "speaker";
  audio.device.type = MEDIA_DEVICE_AUDIO_OUTPUT;
  audio.session_id = session_id_;
  audio_output_array_.push_back(audio);
}

void MockMediaStreamDispatcher::AddVideoDeviceToArray() {
  StreamDeviceInfo video;
  video.device.id = "video_device_id" + base::IntToString(session_id_);
  video.device.name = "usb video camera";
  video.device.type = MEDIA_DEVICE_VIDEO_CAPTURE;
  video.session_id = session_id_;
  video_array_.push_back(video);
}

}  // namespace content