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