1/*
2 *  Copyright (c) 2012 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 "webrtc/base/arraysize.h"
12#include "webrtc/modules/include/module_common_types.h"
13#include "webrtc/voice_engine/include/voe_external_media.h"
14#include "webrtc/voice_engine/test/auto_test/fakes/fake_media_process.h"
15#include "webrtc/voice_engine/test/auto_test/fixtures/after_streaming_fixture.h"
16
17class ExternalMediaTest : public AfterStreamingFixture {
18 protected:
19  void TestRegisterExternalMedia(int channel, webrtc::ProcessingTypes type) {
20    FakeMediaProcess fake_media_process;
21    EXPECT_EQ(0, voe_xmedia_->RegisterExternalMediaProcessing(
22        channel, type, fake_media_process));
23    Sleep(2000);
24
25    TEST_LOG("Back to normal.\n");
26    EXPECT_EQ(0, voe_xmedia_->DeRegisterExternalMediaProcessing(
27        channel, type));
28    Sleep(2000);
29  }
30};
31
32TEST_F(ExternalMediaTest,
33    ManualRegisterExternalMediaProcessingOnAllChannelsAffectsPlayout) {
34  TEST_LOG("Enabling external media processing: audio should be affected.\n");
35  TestRegisterExternalMedia(-1, webrtc::kPlaybackAllChannelsMixed);
36}
37
38TEST_F(ExternalMediaTest,
39    ManualRegisterExternalMediaOnSingleChannelAffectsPlayout) {
40  TEST_LOG("Enabling external media processing: audio should be affected.\n");
41  TestRegisterExternalMedia(channel_, webrtc::kRecordingPerChannel);
42}
43
44TEST_F(ExternalMediaTest,
45    ManualRegisterExternalMediaOnAllChannelsMixedAffectsRecording) {
46  SwitchToManualMicrophone();
47  TEST_LOG("Speak and verify your voice is distorted.\n");
48  TestRegisterExternalMedia(-1, webrtc::kRecordingAllChannelsMixed);
49}
50
51TEST_F(ExternalMediaTest,
52       ExternalMixingCannotBeChangedDuringPlayback) {
53  EXPECT_EQ(-1, voe_xmedia_->SetExternalMixing(channel_, true));
54  EXPECT_EQ(-1, voe_xmedia_->SetExternalMixing(channel_, false));
55}
56
57TEST_F(ExternalMediaTest,
58       ExternalMixingIsRequiredForGetAudioFrame) {
59  webrtc::AudioFrame frame;
60  EXPECT_EQ(-1, voe_xmedia_->GetAudioFrame(channel_, 0, &frame));
61}
62
63TEST_F(ExternalMediaTest,
64       ExternalMixingPreventsAndRestoresRegularPlayback) {
65  PausePlaying();
66  ASSERT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, true));
67  TEST_LOG("Verify that no sound is played out.\n");
68  ResumePlaying();
69  Sleep(1000);
70  PausePlaying();
71  ASSERT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, false));
72  ResumePlaying();
73  TEST_LOG("Verify that sound is played out.\n");
74  ResumePlaying();
75  Sleep(1000);
76}
77
78TEST_F(ExternalMediaTest,
79       ExternalMixingWorks) {
80  webrtc::AudioFrame frame;
81  PausePlaying();
82  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, true));
83  ResumePlaying();
84  EXPECT_EQ(0, voe_xmedia_->GetAudioFrame(channel_, 0, &frame));
85  EXPECT_GT(frame.sample_rate_hz_, 0);
86  EXPECT_GT(frame.samples_per_channel_, 0U);
87  PausePlaying();
88  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, false));
89  ResumePlaying();
90}
91
92TEST_F(ExternalMediaTest,
93       ExternalMixingResamplesToDesiredFrequency) {
94  const int kValidFrequencies[] = {8000, 16000, 22000, 32000, 48000};
95  webrtc::AudioFrame frame;
96  PausePlaying();
97  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, true));
98  ResumePlaying();
99  for (size_t i = 0; i < arraysize(kValidFrequencies); i++) {
100    int f = kValidFrequencies[i];
101    EXPECT_EQ(0, voe_xmedia_->GetAudioFrame(channel_, f, &frame))
102       << "Resampling succeeds for freq=" << f;
103    EXPECT_EQ(f, frame.sample_rate_hz_);
104    EXPECT_EQ(static_cast<size_t>(f / 100), frame.samples_per_channel_);
105  }
106  PausePlaying();
107  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, false));
108  ResumePlaying();
109}
110
111TEST_F(ExternalMediaTest,
112       ExternalMixingResamplingToInvalidFrequenciesFails) {
113  const int kInvalidFrequencies[] = {-8000, -1};
114  webrtc::AudioFrame frame;
115  PausePlaying();
116  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, true));
117  ResumePlaying();
118  for (size_t i = 0; i < arraysize(kInvalidFrequencies); i++) {
119    int f = kInvalidFrequencies[i];
120    EXPECT_EQ(-1, voe_xmedia_->GetAudioFrame(channel_, f, &frame))
121        << "Resampling fails for freq=" << f;
122  }
123  PausePlaying();
124  EXPECT_EQ(0, voe_xmedia_->SetExternalMixing(channel_, false));
125  ResumePlaying();
126}
127