summaryrefslogtreecommitdiffstats
path: root/components/test_runner/mock_web_media_stream_center.cc
blob: 6c9983e6f2a6d453166c9cd4cb62f0239dd74a58 (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
139
140
// Copyright 2014 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 "components/test_runner/mock_web_media_stream_center.h"

#include <stddef.h>

#include "base/logging.h"
#include "base/macros.h"
#include "components/test_runner/test_interfaces.h"
#include "components/test_runner/web_test_delegate.h"
#include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h"
#include "third_party/WebKit/public/platform/WebAudioSourceProvider.h"
#include "third_party/WebKit/public/platform/WebMediaStream.h"
#include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
#include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
#include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h"
#include "third_party/WebKit/public/platform/WebSourceInfo.h"
#include "third_party/WebKit/public/platform/WebVector.h"

namespace test_runner {

namespace {
class NewTrackTask : public WebMethodTask<MockWebMediaStreamCenter> {
 public:
  NewTrackTask(MockWebMediaStreamCenter* object,
               const blink::WebMediaStream& stream)
      : WebMethodTask<MockWebMediaStreamCenter>(object), stream_(stream) {
    DCHECK(!stream_.isNull());
  }

  ~NewTrackTask() override {}

  void RunIfValid() override {
    blink::WebMediaStreamSource source;
    blink::WebMediaStreamTrack track;
    source.initialize("MagicVideoDevice#1",
                      blink::WebMediaStreamSource::TypeVideo,
                      "Magic video track",
                      false /* remote */, true /* readonly */);
    track.initialize(source);
    stream_.addTrack(track);
  }

 private:
  blink::WebMediaStream stream_;

  DISALLOW_COPY_AND_ASSIGN(NewTrackTask);
};

class MockWebAudioDestinationConsumer
    : public blink::WebAudioDestinationConsumer {
 public:
  MockWebAudioDestinationConsumer() {}
  ~MockWebAudioDestinationConsumer() override {}
  void setFormat(size_t number_of_channels, float sample_rate) override {}
  void consumeAudio(const blink::WebVector<const float*>&,
                    size_t number_of_frames) override {}

  DISALLOW_COPY_AND_ASSIGN(MockWebAudioDestinationConsumer);
};

}  // namespace

MockWebMediaStreamCenter::MockWebMediaStreamCenter(
    blink::WebMediaStreamCenterClient* client,
    TestInterfaces* interfaces)
    : interfaces_(interfaces) {
}

MockWebMediaStreamCenter::~MockWebMediaStreamCenter() {
}

void MockWebMediaStreamCenter::didEnableMediaStreamTrack(
    const blink::WebMediaStreamTrack& track) {
  track.source().setReadyState(blink::WebMediaStreamSource::ReadyStateLive);
}

void MockWebMediaStreamCenter::didDisableMediaStreamTrack(
    const blink::WebMediaStreamTrack& track) {
  track.source().setReadyState(blink::WebMediaStreamSource::ReadyStateMuted);
}

bool MockWebMediaStreamCenter::didAddMediaStreamTrack(
    const blink::WebMediaStream& stream,
    const blink::WebMediaStreamTrack& track) {
  return true;
}

bool MockWebMediaStreamCenter::didRemoveMediaStreamTrack(
    const blink::WebMediaStream& stream,
    const blink::WebMediaStreamTrack& track) {
  return true;
}

void MockWebMediaStreamCenter::didStopLocalMediaStream(
    const blink::WebMediaStream& stream) {
  blink::WebVector<blink::WebMediaStreamTrack> tracks;
  stream.audioTracks(tracks);
  for (size_t i = 0; i < tracks.size(); ++i)
    tracks[i].source().setReadyState(
        blink::WebMediaStreamSource::ReadyStateEnded);
  stream.videoTracks(tracks);
  for (size_t i = 0; i < tracks.size(); ++i)
    tracks[i].source().setReadyState(
        blink::WebMediaStreamSource::ReadyStateEnded);
}

bool MockWebMediaStreamCenter::didStopMediaStreamTrack(
    const blink::WebMediaStreamTrack& track) {
  track.source().setReadyState(blink::WebMediaStreamSource::ReadyStateEnded);
  return true;
}

void MockWebMediaStreamCenter::didCreateMediaStream(
    blink::WebMediaStream& stream) {
  blink::WebVector<blink::WebMediaStreamTrack> audio_tracks;
  stream.audioTracks(audio_tracks);
  for (size_t i = 0; i < audio_tracks.size(); ++i) {
    blink::WebMediaStreamSource source = audio_tracks[i].source();
    if (source.requiresAudioConsumer()) {
      MockWebAudioDestinationConsumer* consumer =
          new MockWebAudioDestinationConsumer();
      source.addAudioConsumer(consumer);
      source.removeAudioConsumer(consumer);
      delete consumer;
    }
  }
  interfaces_->GetDelegate()->PostTask(new NewTrackTask(this, stream));
}

blink::WebAudioSourceProvider*
MockWebMediaStreamCenter::createWebAudioSourceFromMediaStreamTrack(
    const blink::WebMediaStreamTrack& track) {
  return NULL;
}

}  // namespace test_runner