aboutsummaryrefslogtreecommitdiff
path: root/audio/voip/test/voip_core_unittest.cc
blob: 896d0d98bb5f24156ceb7dd415111214160581f3 (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
189
190
/*
 *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "audio/voip/voip_core.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "api/task_queue/default_task_queue_factory.h"
#include "modules/audio_device/include/mock_audio_device.h"
#include "modules/audio_processing/include/mock_audio_processing.h"
#include "test/gtest.h"
#include "test/mock_transport.h"

namespace webrtc {
namespace {

using ::testing::NiceMock;
using ::testing::Return;

constexpr int kPcmuPayload = 0;
constexpr int kPcmuSampleRateHz = 8000;
constexpr int kDtmfEventDurationMs = 1000;
constexpr DtmfEvent kDtmfEventCode = DtmfEvent::kDigitZero;

class VoipCoreTest : public ::testing::Test {
 public:
  const SdpAudioFormat kPcmuFormat = {"pcmu", 8000, 1};

  VoipCoreTest() { audio_device_ = test::MockAudioDeviceModule::CreateNice(); }

  void SetUp() override {
    auto encoder_factory = CreateBuiltinAudioEncoderFactory();
    auto decoder_factory = CreateBuiltinAudioDecoderFactory();
    rtc::scoped_refptr<AudioProcessing> audio_processing =
        rtc::make_ref_counted<NiceMock<test::MockAudioProcessing>>();

    voip_core_ = std::make_unique<VoipCore>(
        std::move(encoder_factory), std::move(decoder_factory),
        CreateDefaultTaskQueueFactory(), audio_device_,
        std::move(audio_processing));
  }

  std::unique_ptr<VoipCore> voip_core_;
  NiceMock<MockTransport> transport_;
  rtc::scoped_refptr<test::MockAudioDeviceModule> audio_device_;
};

// Validate expected API calls that involves with VoipCore. Some verification is
// involved with checking mock audio device.
TEST_F(VoipCoreTest, BasicVoipCoreOperation) {
  // Program mock as non-operational and ready to start.
  EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(false));
  EXPECT_CALL(*audio_device_, Playing()).WillOnce(Return(false));
  EXPECT_CALL(*audio_device_, InitRecording()).WillOnce(Return(0));
  EXPECT_CALL(*audio_device_, InitPlayout()).WillOnce(Return(0));
  EXPECT_CALL(*audio_device_, StartRecording()).WillOnce(Return(0));
  EXPECT_CALL(*audio_device_, StartPlayout()).WillOnce(Return(0));

  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
            VoipResult::kOk);
  EXPECT_EQ(
      voip_core_->SetReceiveCodecs(channel, {{kPcmuPayload, kPcmuFormat}}),
      VoipResult::kOk);

  EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kOk);
  EXPECT_EQ(voip_core_->StartPlayout(channel), VoipResult::kOk);

  EXPECT_EQ(voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
                                                   kPcmuSampleRateHz),
            VoipResult::kOk);

  EXPECT_EQ(
      voip_core_->SendDtmfEvent(channel, kDtmfEventCode, kDtmfEventDurationMs),
      VoipResult::kOk);

  // Program mock as operational that is ready to be stopped.
  EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(true));
  EXPECT_CALL(*audio_device_, Playing()).WillOnce(Return(true));
  EXPECT_CALL(*audio_device_, StopRecording()).WillOnce(Return(0));
  EXPECT_CALL(*audio_device_, StopPlayout()).WillOnce(Return(0));

  EXPECT_EQ(voip_core_->StopSend(channel), VoipResult::kOk);
  EXPECT_EQ(voip_core_->StopPlayout(channel), VoipResult::kOk);
  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);
}

TEST_F(VoipCoreTest, ExpectFailToUseReleasedChannelId) {
  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  // Release right after creation.
  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);

  // Now use released channel.

  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
            VoipResult::kInvalidArgument);
  EXPECT_EQ(
      voip_core_->SetReceiveCodecs(channel, {{kPcmuPayload, kPcmuFormat}}),
      VoipResult::kInvalidArgument);
  EXPECT_EQ(voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
                                                   kPcmuSampleRateHz),
            VoipResult::kInvalidArgument);
  EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kInvalidArgument);
  EXPECT_EQ(voip_core_->StartPlayout(channel), VoipResult::kInvalidArgument);
  EXPECT_EQ(
      voip_core_->SendDtmfEvent(channel, kDtmfEventCode, kDtmfEventDurationMs),
      VoipResult::kInvalidArgument);
}

TEST_F(VoipCoreTest, SendDtmfEventWithoutRegistering) {
  // Program mock as non-operational and ready to start send.
  EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(false));
  EXPECT_CALL(*audio_device_, InitRecording()).WillOnce(Return(0));
  EXPECT_CALL(*audio_device_, StartRecording()).WillOnce(Return(0));

  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
            VoipResult::kOk);

  EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kOk);
  // Send Dtmf event without registering beforehand, thus payload
  // type is not set and kFailedPrecondition is expected.
  EXPECT_EQ(
      voip_core_->SendDtmfEvent(channel, kDtmfEventCode, kDtmfEventDurationMs),
      VoipResult::kFailedPrecondition);

  // Program mock as sending and is ready to be stopped.
  EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(true));
  EXPECT_CALL(*audio_device_, StopRecording()).WillOnce(Return(0));

  EXPECT_EQ(voip_core_->StopSend(channel), VoipResult::kOk);
  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);
}

TEST_F(VoipCoreTest, SendDtmfEventWithoutStartSend) {
  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  EXPECT_EQ(voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
                                                   kPcmuSampleRateHz),
            VoipResult::kOk);

  // Send Dtmf event without calling StartSend beforehand, thus
  // Dtmf events cannot be sent and kFailedPrecondition is expected.
  EXPECT_EQ(
      voip_core_->SendDtmfEvent(channel, kDtmfEventCode, kDtmfEventDurationMs),
      VoipResult::kFailedPrecondition);

  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);
}

TEST_F(VoipCoreTest, StartSendAndPlayoutWithoutSettingCodec) {
  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  // Call StartSend and StartPlayout without setting send/receive
  // codec. Code should see that codecs aren't set and return false.
  EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kFailedPrecondition);
  EXPECT_EQ(voip_core_->StartPlayout(channel), VoipResult::kFailedPrecondition);

  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);
}

TEST_F(VoipCoreTest, StopSendAndPlayoutWithoutStarting) {
  auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);

  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
            VoipResult::kOk);
  EXPECT_EQ(
      voip_core_->SetReceiveCodecs(channel, {{kPcmuPayload, kPcmuFormat}}),
      VoipResult::kOk);

  // Call StopSend and StopPlayout without starting them in
  // the first place. Should see that it is already in the
  // stopped state and return true.
  EXPECT_EQ(voip_core_->StopSend(channel), VoipResult::kOk);
  EXPECT_EQ(voip_core_->StopPlayout(channel), VoipResult::kOk);

  EXPECT_EQ(voip_core_->ReleaseChannel(channel), VoipResult::kOk);
}

}  // namespace
}  // namespace webrtc