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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright (c) 2011 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 <string>
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "content/common/media/media_stream_messages.h"
#include "content/renderer/media/media_stream_dispatcher.h"
#include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const int kRouteId = 0;
const int kAudioSessionId = 3;
const int kVideoSessionId = 5;
const int kRequestId1 = 10;
const int kRequestId2 = 20;
class MockMediaStreamDispatcherEventHandler
: public MediaStreamDispatcherEventHandler {
public:
MockMediaStreamDispatcherEventHandler()
: request_id_(-1),
audio_failed(false),
video_failed(false) {}
virtual void OnStreamGenerated(
int request_id,
const std::string &label,
const media_stream::StreamDeviceInfoArray& audio_device_array,
const media_stream::StreamDeviceInfoArray& video_device_array) {
request_id_ = request_id;
label_ = label;
}
virtual void OnStreamGenerationFailed(int request_id) {
request_id_ = request_id;
}
virtual void OnAudioDeviceFailed(const std::string& label,
int index) {
audio_failed = true;
}
virtual void OnVideoDeviceFailed(const std::string& label,
int index) {
video_failed = true;
}
int request_id_;
std::string label_;
bool audio_failed;
bool video_failed;
DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDispatcherEventHandler);
};
} // namespace
TEST(MediaStreamDispatcherTest, Basic) {
scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL));
scoped_ptr<MockMediaStreamDispatcherEventHandler>
handler(new MockMediaStreamDispatcherEventHandler);
media_stream::StreamOptions components(
true, media_stream::StreamOptions::kFacingUser);
std::string security_origin;
int ipc_request_id1 = dispatcher->next_ipc_id_;
dispatcher->GenerateStream(kRequestId1, handler.get(), components,
security_origin);
int ipc_request_id2 = dispatcher->next_ipc_id_;
EXPECT_NE(ipc_request_id1, ipc_request_id2);
dispatcher->GenerateStream(kRequestId2, handler.get(), components,
security_origin);
EXPECT_EQ(dispatcher->requests_.size(), size_t(2));
media_stream::StreamDeviceInfoArray audio_device_array(1);
media_stream::StreamDeviceInfo audio_device_info;
audio_device_info.name = "Microphone";
audio_device_info.stream_type = media_stream::kAudioCapture;
audio_device_info.session_id = kAudioSessionId;
audio_device_array[0] = audio_device_info;
media_stream::StreamDeviceInfoArray video_device_array(1);
media_stream::StreamDeviceInfo video_device_info;
video_device_info.name = "Camera";
video_device_info.stream_type = media_stream::kVideoCapture;
video_device_info.session_id = kVideoSessionId;
video_device_array[0] = video_device_info;
// Complete the creation of stream1.
std::string stream_label1 = std::string("stream1");
dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated(
kRouteId, ipc_request_id1, stream_label1,
audio_device_array, video_device_array));
EXPECT_EQ(handler->request_id_, kRequestId1);
EXPECT_EQ(handler->label_, stream_label1);
// Complete the creation of stream2.
std::string stream_label2 = std::string("stream2");
dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated(
kRouteId, ipc_request_id2, stream_label2,
audio_device_array, video_device_array));
EXPECT_EQ(handler->request_id_, kRequestId2);
EXPECT_EQ(handler->label_, stream_label2);
EXPECT_EQ(dispatcher->requests_.size(), size_t(0));
EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(2));
// Check the session_id of stream2.
EXPECT_EQ(dispatcher->audio_session_id(stream_label2, 0), kAudioSessionId);
EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0), kVideoSessionId);
// Stop stream2.
dispatcher->StopStream(stream_label2);
EXPECT_EQ(dispatcher->audio_session_id(stream_label2, 0),
media_stream::StreamDeviceInfo::kNoId);
EXPECT_EQ(dispatcher->video_session_id(stream_label2, 0),
media_stream::StreamDeviceInfo::kNoId);
// Stop stream1.
dispatcher->StopStream(stream_label1);
EXPECT_EQ(dispatcher->audio_session_id(stream_label1, 0),
media_stream::StreamDeviceInfo::kNoId);
EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0),
media_stream::StreamDeviceInfo::kNoId);
EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0));
// Verify that the request have been completed.
EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0));
EXPECT_EQ(dispatcher->requests_.size(), size_t(0));
}
TEST(MediaStreamDispatcherTest, TestFailure) {
scoped_ptr<MediaStreamDispatcher> dispatcher(new MediaStreamDispatcher(NULL));
scoped_ptr<MockMediaStreamDispatcherEventHandler>
handler(new MockMediaStreamDispatcherEventHandler);
media_stream::StreamOptions components(
true, media_stream::StreamOptions::kFacingUser);
std::string security_origin;
// Test failure when creating a stream.
int ipc_request_id1 = dispatcher->next_ipc_id_;
dispatcher->GenerateStream(kRequestId1, handler.get(), components,
security_origin);
dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerationFailed(
kRouteId, ipc_request_id1));
// Verify that the request have been completed.
EXPECT_EQ(handler->request_id_, kRequestId1);
EXPECT_EQ(dispatcher->requests_.size(), size_t(0));
// Create a new stream.
ipc_request_id1 = dispatcher->next_ipc_id_;
dispatcher->GenerateStream(kRequestId1, handler.get(), components,
security_origin);
media_stream::StreamDeviceInfoArray audio_device_array(1);
media_stream::StreamDeviceInfo audio_device_info;
audio_device_info.name = "Microphone";
audio_device_info.stream_type = media_stream::kAudioCapture;
audio_device_info.session_id = kAudioSessionId;
audio_device_array[0] = audio_device_info;
media_stream::StreamDeviceInfoArray video_device_array(1);
media_stream::StreamDeviceInfo video_device_info;
video_device_info.name = "Camera";
video_device_info.stream_type = media_stream::kVideoCapture;
video_device_info.session_id = kVideoSessionId;
video_device_array[0] = video_device_info;
// Complete the creation of stream1.
std::string stream_label1 = std::string("stream1");
dispatcher->OnMessageReceived(MediaStreamMsg_StreamGenerated(
kRouteId, ipc_request_id1, stream_label1,
audio_device_array, video_device_array));
EXPECT_EQ(handler->request_id_, kRequestId1);
EXPECT_EQ(handler->label_, stream_label1);
EXPECT_EQ(dispatcher->video_session_id(stream_label1, 0), kVideoSessionId);
// Test failure of the video device.
dispatcher->OnMessageReceived(
MediaStreamHostMsg_VideoDeviceFailed(kRouteId, stream_label1, 0));
EXPECT_EQ(handler->video_failed, true);
EXPECT_EQ(handler->audio_failed, false);
// Make sure the audio device still exist but not the video device.
EXPECT_EQ(dispatcher->audio_session_id(stream_label1, 0), kAudioSessionId);
// Test failure of the audio device.
dispatcher->OnMessageReceived(
MediaStreamHostMsg_AudioDeviceFailed(kRouteId, stream_label1, 0));
EXPECT_EQ(handler->audio_failed, true);
// Stop stream1.
dispatcher->StopStream(stream_label1);
EXPECT_EQ(dispatcher->label_stream_map_.size(), size_t(0));
}
|