1 // Copyright 2013 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.
4
5 #include "content/shell/renderer/test_runner/MockWebMediaStreamCenter.h"
6
7 #include "base/logging.h"
8 #include "content/shell/renderer/test_runner/TestInterfaces.h"
9 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
10 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h"
11 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
12 #include "third_party/WebKit/public/platform/WebMediaStream.h"
13 #include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
16 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h"
17 #include "third_party/WebKit/public/platform/WebSourceInfo.h"
18 #include "third_party/WebKit/public/platform/WebVector.h"
19
20 using namespace blink;
21
22 namespace content {
23
24 class NewTrackTask : public WebMethodTask<MockWebMediaStreamCenter> {
25 public:
NewTrackTask(MockWebMediaStreamCenter * object,const WebMediaStream & stream)26 NewTrackTask(MockWebMediaStreamCenter* object, const WebMediaStream& stream)
27 : WebMethodTask<MockWebMediaStreamCenter>(object)
28 , m_stream(stream)
29 {
30 DCHECK(!m_stream.isNull());
31 }
32
runIfValid()33 virtual void runIfValid() OVERRIDE
34 {
35 WebMediaStreamSource source;
36 WebMediaStreamTrack track;
37 source.initialize("MagicVideoDevice#1", WebMediaStreamSource::TypeVideo, "Magic video track");
38 track.initialize(source);
39 m_stream.addTrack(track);
40 }
41
42 private:
43 WebMediaStream m_stream;
44 };
45
MockWebMediaStreamCenter(WebMediaStreamCenterClient * client,TestInterfaces * interfaces)46 MockWebMediaStreamCenter::MockWebMediaStreamCenter(WebMediaStreamCenterClient* client, TestInterfaces* interfaces)
47 : m_interfaces(interfaces)
48 {
49 }
50
getMediaStreamTrackSources(const WebMediaStreamTrackSourcesRequest & request)51 bool MockWebMediaStreamCenter::getMediaStreamTrackSources(const WebMediaStreamTrackSourcesRequest& request)
52 {
53 size_t size = 2;
54 WebVector<WebSourceInfo> results(size);
55 results[0].initialize("MockAudioDevice#1", WebSourceInfo::SourceKindAudio, "Mock audio device", WebSourceInfo::VideoFacingModeNone);
56 results[1].initialize("MockVideoDevice#1", WebSourceInfo::SourceKindVideo, "Mock video device", WebSourceInfo::VideoFacingModeEnvironment);
57 request.requestSucceeded(results);
58 return true;
59 }
60
didEnableMediaStreamTrack(const WebMediaStreamTrack & track)61 void MockWebMediaStreamCenter::didEnableMediaStreamTrack(const WebMediaStreamTrack& track)
62 {
63 track.source().setReadyState(WebMediaStreamSource::ReadyStateLive);
64 }
65
didDisableMediaStreamTrack(const WebMediaStreamTrack & track)66 void MockWebMediaStreamCenter::didDisableMediaStreamTrack(const WebMediaStreamTrack& track)
67 {
68 track.source().setReadyState(WebMediaStreamSource::ReadyStateMuted);
69 }
70
didAddMediaStreamTrack(const WebMediaStream &,const WebMediaStreamTrack &)71 bool MockWebMediaStreamCenter::didAddMediaStreamTrack(const WebMediaStream&, const WebMediaStreamTrack&)
72 {
73 return true;
74 }
75
didRemoveMediaStreamTrack(const WebMediaStream &,const WebMediaStreamTrack &)76 bool MockWebMediaStreamCenter::didRemoveMediaStreamTrack(const WebMediaStream&, const WebMediaStreamTrack&)
77 {
78 return true;
79 }
80
didStopLocalMediaStream(const WebMediaStream & stream)81 void MockWebMediaStreamCenter::didStopLocalMediaStream(const WebMediaStream& stream)
82 {
83 WebVector<WebMediaStreamTrack> tracks;
84 stream.audioTracks(tracks);
85 for (size_t i = 0; i < tracks.size(); ++i)
86 tracks[i].source().setReadyState(WebMediaStreamSource::ReadyStateEnded);
87 stream.videoTracks(tracks);
88 for (size_t i = 0; i < tracks.size(); ++i)
89 tracks[i].source().setReadyState(WebMediaStreamSource::ReadyStateEnded);
90 }
91
didStopMediaStreamTrack(const blink::WebMediaStreamTrack & track)92 bool MockWebMediaStreamCenter::didStopMediaStreamTrack(const blink::WebMediaStreamTrack& track)
93 {
94 track.source().setReadyState(WebMediaStreamSource::ReadyStateEnded);
95 return true;
96 }
97
98 class MockWebAudioDestinationConsumer : public WebAudioDestinationConsumer {
99 public:
~MockWebAudioDestinationConsumer()100 virtual ~MockWebAudioDestinationConsumer() { }
setFormat(size_t numberOfChannels,float sampleRate)101 virtual void setFormat(size_t numberOfChannels, float sampleRate) OVERRIDE { }
consumeAudio(const WebVector<const float * > &,size_t number_of_frames)102 virtual void consumeAudio(const WebVector<const float*>&, size_t number_of_frames) OVERRIDE { }
103 };
104
didCreateMediaStream(WebMediaStream & stream)105 void MockWebMediaStreamCenter::didCreateMediaStream(WebMediaStream& stream)
106 {
107 WebVector<WebMediaStreamTrack> audioTracks;
108 stream.audioTracks(audioTracks);
109 for (size_t i = 0; i < audioTracks.size(); ++i) {
110 WebMediaStreamSource source = audioTracks[i].source();
111 if (source.requiresAudioConsumer()) {
112 MockWebAudioDestinationConsumer* consumer = new MockWebAudioDestinationConsumer();
113 source.addAudioConsumer(consumer);
114 source.removeAudioConsumer(consumer);
115 delete consumer;
116 }
117 }
118 m_interfaces->delegate()->postTask(new NewTrackTask(this, stream));
119 }
120
createWebAudioSourceFromMediaStreamTrack(const blink::WebMediaStreamTrack & track)121 blink::WebAudioSourceProvider* MockWebMediaStreamCenter::createWebAudioSourceFromMediaStreamTrack(const blink::WebMediaStreamTrack& track)
122 {
123 return NULL;
124 }
125
126 } // namespace content
127