1 /*
2  *  Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "audio/voip/voip_core.h"
12 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
13 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
14 #include "api/task_queue/default_task_queue_factory.h"
15 #include "modules/audio_device/include/mock_audio_device.h"
16 #include "modules/audio_processing/include/mock_audio_processing.h"
17 #include "test/gtest.h"
18 #include "test/mock_transport.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 using ::testing::NiceMock;
24 using ::testing::Return;
25 
26 constexpr int kPcmuPayload = 0;
27 
28 class VoipCoreTest : public ::testing::Test {
29  public:
30   const SdpAudioFormat kPcmuFormat = {"pcmu", 8000, 1};
31 
VoipCoreTest()32   VoipCoreTest() { audio_device_ = test::MockAudioDeviceModule::CreateNice(); }
33 
SetUp()34   void SetUp() override {
35     auto encoder_factory = CreateBuiltinAudioEncoderFactory();
36     auto decoder_factory = CreateBuiltinAudioDecoderFactory();
37     rtc::scoped_refptr<AudioProcessing> audio_processing =
38         new rtc::RefCountedObject<test::MockAudioProcessing>();
39 
40     voip_core_ = std::make_unique<VoipCore>();
41     voip_core_->Init(std::move(encoder_factory), std::move(decoder_factory),
42                      CreateDefaultTaskQueueFactory(), audio_device_,
43                      std::move(audio_processing));
44   }
45 
46   std::unique_ptr<VoipCore> voip_core_;
47   NiceMock<MockTransport> transport_;
48   rtc::scoped_refptr<test::MockAudioDeviceModule> audio_device_;
49 };
50 
51 // Validate expected API calls that involves with VoipCore. Some verification is
52 // involved with checking mock audio device.
TEST_F(VoipCoreTest,BasicVoipCoreOperation)53 TEST_F(VoipCoreTest, BasicVoipCoreOperation) {
54   // Program mock as non-operational and ready to start.
55   EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(false));
56   EXPECT_CALL(*audio_device_, Playing()).WillOnce(Return(false));
57   EXPECT_CALL(*audio_device_, InitRecording()).WillOnce(Return(0));
58   EXPECT_CALL(*audio_device_, InitPlayout()).WillOnce(Return(0));
59   EXPECT_CALL(*audio_device_, StartRecording()).WillOnce(Return(0));
60   EXPECT_CALL(*audio_device_, StartPlayout()).WillOnce(Return(0));
61 
62   auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
63   EXPECT_TRUE(channel);
64 
65   voip_core_->SetSendCodec(*channel, kPcmuPayload, kPcmuFormat);
66   voip_core_->SetReceiveCodecs(*channel, {{kPcmuPayload, kPcmuFormat}});
67 
68   EXPECT_TRUE(voip_core_->StartSend(*channel));
69   EXPECT_TRUE(voip_core_->StartPlayout(*channel));
70 
71   // Program mock as operational that is ready to be stopped.
72   EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(true));
73   EXPECT_CALL(*audio_device_, Playing()).WillOnce(Return(true));
74   EXPECT_CALL(*audio_device_, StopRecording()).WillOnce(Return(0));
75   EXPECT_CALL(*audio_device_, StopPlayout()).WillOnce(Return(0));
76 
77   EXPECT_TRUE(voip_core_->StopSend(*channel));
78   EXPECT_TRUE(voip_core_->StopPlayout(*channel));
79   voip_core_->ReleaseChannel(*channel);
80 }
81 
TEST_F(VoipCoreTest,ExpectFailToUseReleasedChannelId)82 TEST_F(VoipCoreTest, ExpectFailToUseReleasedChannelId) {
83   auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
84   EXPECT_TRUE(channel);
85 
86   // Release right after creation.
87   voip_core_->ReleaseChannel(*channel);
88 
89   // Now use released channel.
90 
91   // These should be no-op.
92   voip_core_->SetSendCodec(*channel, kPcmuPayload, kPcmuFormat);
93   voip_core_->SetReceiveCodecs(*channel, {{kPcmuPayload, kPcmuFormat}});
94 
95   EXPECT_FALSE(voip_core_->StartSend(*channel));
96   EXPECT_FALSE(voip_core_->StartPlayout(*channel));
97 }
98 
99 }  // namespace
100 }  // namespace webrtc
101