summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/TestCubebInputStream.cpp
blob: 0488c2be1adced9f4f0190d041264de545639a72 (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
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "CubebInputStream.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "MockCubeb.h"
#include "WaitFor.h"

using namespace mozilla;

namespace {
#define DispatchFunction(f) \
  NS_DispatchToCurrentThread(NS_NewRunnableFunction(__func__, f))
}  // namespace

class MockListener : public CubebInputStream::Listener {
 public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MockListener, override);
  MOCK_METHOD2(DataCallback, long(const void* aBuffer, long aFrames));
  MOCK_METHOD1(StateCallback, void(cubeb_state aState));
  MOCK_METHOD0(DeviceChangedCallback, void());

 private:
  ~MockListener() = default;
};

TEST(TestCubebInputStream, DataCallback)
{
  using ::testing::Ne;
  using ::testing::NotNull;

  MockCubeb* cubeb = new MockCubeb();
  CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());

  const CubebUtils::AudioDeviceID deviceId = nullptr;
  const uint32_t channels = 2;

  uint32_t rate = 0;
  ASSERT_EQ(cubeb_get_preferred_sample_rate(cubeb->AsCubebContext(), &rate),
            CUBEB_OK);

  nsTArray<AudioDataValue> data;
  auto listener = MakeRefPtr<MockListener>();
  EXPECT_CALL(*listener, DataCallback(NotNull(), Ne(0)))
      .WillRepeatedly([&](const void* aBuffer, long aFrames) {
        const AudioDataValue* source =
            reinterpret_cast<const AudioDataValue*>(aBuffer);
        size_t sampleCount =
            static_cast<size_t>(aFrames) * static_cast<size_t>(channels);
        data.AppendElements(source, sampleCount);
        return aFrames;
      });

  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STARTED));
  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STOPPED)).Times(2);

  EXPECT_CALL(*listener, DeviceChangedCallback).Times(0);

  UniquePtr<CubebInputStream> cis;
  DispatchFunction([&] {
    cis = CubebInputStream::Create(deviceId, channels, rate, true,
                                   listener.get());
    ASSERT_TRUE(cis);
  });
  RefPtr<SmartMockCubebStream> stream = WaitFor(cubeb->StreamInitEvent());
  EXPECT_TRUE(stream->mHasInput);

  stream->SetInputRecordingEnabled(true);

  DispatchFunction([&] { ASSERT_EQ(cis->Start(), CUBEB_OK); });
  WaitFor(stream->FramesProcessedEvent());

  DispatchFunction([&] { ASSERT_EQ(cis->Stop(), CUBEB_OK); });
  WaitFor(stream->OutputVerificationEvent());

  nsTArray<AudioDataValue> record = stream->TakeRecordedInput();

  DispatchFunction([&] { cis = nullptr; });
  WaitFor(cubeb->StreamDestroyEvent());

  ASSERT_EQ(data, record);
}

TEST(TestCubebInputStream, ErrorCallback)
{
  using ::testing::Ne;
  using ::testing::NotNull;
  using ::testing::ReturnArg;

  MockCubeb* cubeb = new MockCubeb();
  CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());

  const CubebUtils::AudioDeviceID deviceId = nullptr;
  const uint32_t channels = 2;

  uint32_t rate = 0;
  ASSERT_EQ(cubeb_get_preferred_sample_rate(cubeb->AsCubebContext(), &rate),
            CUBEB_OK);

  auto listener = MakeRefPtr<MockListener>();
  EXPECT_CALL(*listener, DataCallback(NotNull(), Ne(0)))
      .WillRepeatedly(ReturnArg<1>());

  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STARTED));
  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_ERROR));
  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STOPPED));

  EXPECT_CALL(*listener, DeviceChangedCallback).Times(0);

  UniquePtr<CubebInputStream> cis;
  DispatchFunction([&] {
    cis = CubebInputStream::Create(deviceId, channels, rate, true,
                                   listener.get());
    ASSERT_TRUE(cis);
  });
  RefPtr<SmartMockCubebStream> stream = WaitFor(cubeb->StreamInitEvent());
  EXPECT_TRUE(stream->mHasInput);

  DispatchFunction([&] { ASSERT_EQ(cis->Start(), CUBEB_OK); });
  WaitFor(stream->FramesProcessedEvent());

  DispatchFunction([&] { stream->ForceError(); });
  WaitFor(stream->ErrorForcedEvent());

  // If stream ran into an error state, then it should be stopped.

  DispatchFunction([&] { cis = nullptr; });
  WaitFor(cubeb->StreamDestroyEvent());
}

TEST(TestCubebInputStream, DeviceChangedCallback)
{
  using ::testing::Ne;
  using ::testing::NotNull;
  using ::testing::ReturnArg;

  MockCubeb* cubeb = new MockCubeb();
  CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());

  const CubebUtils::AudioDeviceID deviceId = nullptr;
  const uint32_t channels = 2;

  uint32_t rate = 0;
  ASSERT_EQ(cubeb_get_preferred_sample_rate(cubeb->AsCubebContext(), &rate),
            CUBEB_OK);

  auto listener = MakeRefPtr<MockListener>();
  EXPECT_CALL(*listener, DataCallback(NotNull(), Ne(0)))
      .WillRepeatedly(ReturnArg<1>());

  // In real world, the stream might run into an error state when the
  // device-changed event is fired (e.g., the last default output device is
  // unplugged). But it's fine to not check here since we can control how
  // MockCubeb behaves.
  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STARTED));
  EXPECT_CALL(*listener, StateCallback(CUBEB_STATE_STOPPED)).Times(2);

  EXPECT_CALL(*listener, DeviceChangedCallback);

  UniquePtr<CubebInputStream> cis;
  DispatchFunction([&] {
    cis = CubebInputStream::Create(deviceId, channels, rate, true,
                                   listener.get());
    ASSERT_TRUE(cis);
  });
  RefPtr<SmartMockCubebStream> stream = WaitFor(cubeb->StreamInitEvent());
  EXPECT_TRUE(stream->mHasInput);

  DispatchFunction([&] { ASSERT_EQ(cis->Start(), CUBEB_OK); });
  WaitFor(stream->FramesProcessedEvent());

  DispatchFunction([&] { stream->ForceDeviceChanged(); });
  WaitFor(stream->DeviceChangeForcedEvent());

  // The stream can keep running when its device is changed.
  DispatchFunction([&] { ASSERT_EQ(cis->Stop(), CUBEB_OK); });
  cubeb_state state = WaitFor(stream->StateEvent());
  EXPECT_EQ(state, CUBEB_STATE_STOPPED);

  DispatchFunction([&] { cis = nullptr; });
  WaitFor(cubeb->StreamDestroyEvent());
}