| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/copresence/mediums/audio/audio_manager.h" | 5 #include "components/audio_modem/public/modem.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "components/copresence/mediums/audio/audio_manager_impl.h" | 11 #include "components/audio_modem/audio_player.h" |
| 12 #include "components/copresence/mediums/audio/audio_player.h" | 12 #include "components/audio_modem/audio_recorder.h" |
| 13 #include "components/copresence/mediums/audio/audio_recorder.h" | 13 #include "components/audio_modem/modem_impl.h" |
| 14 #include "components/copresence/test/audio_test_support.h" | 14 #include "components/audio_modem/test/random_samples.h" |
| 15 #include "components/copresence/test/stub_whispernet_client.h" | 15 #include "components/audio_modem/test/stub_whispernet_client.h" |
| 16 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace copresence { | 19 namespace audio_modem { |
| 20 | 20 |
| 21 class AudioPlayerStub final : public AudioPlayer { | 21 class AudioPlayerStub final : public AudioPlayer { |
| 22 public: | 22 public: |
| 23 AudioPlayerStub() : is_playing_(false) {} | 23 AudioPlayerStub() : is_playing_(false) {} |
| 24 ~AudioPlayerStub() override {} | 24 ~AudioPlayerStub() override {} |
| 25 | 25 |
| 26 // AudioPlayer overrides: | 26 // AudioPlayer overrides: |
| 27 void Initialize() override {} | 27 void Initialize() override {} |
| 28 void Play(const scoped_refptr<media::AudioBusRefCounted>&) override { | 28 void Play(const scoped_refptr<media::AudioBusRefCounted>&) override { |
| 29 is_playing_ = true; | 29 is_playing_ = true; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 56 cb_.Run(std::string(0x1337, 'a')); | 56 cb_.Run(std::string(0x1337, 'a')); |
| 57 } | 57 } |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 RecordedSamplesCallback cb_; | 60 RecordedSamplesCallback cb_; |
| 61 bool is_recording_; | 61 bool is_recording_; |
| 62 | 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub); | 63 DISALLOW_COPY_AND_ASSIGN(AudioRecorderStub); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 class AudioManagerTest : public testing::Test { | 66 class ModemTest : public testing::Test { |
| 67 public: | 67 public: |
| 68 AudioManagerTest() | 68 ModemTest() |
| 69 : audio_manager_(new AudioManagerImpl()), | 69 : modem_(new ModemImpl), |
| 70 audible_player_(new AudioPlayerStub), | 70 audible_player_(new AudioPlayerStub), |
| 71 inaudible_player_(new AudioPlayerStub), | 71 inaudible_player_(new AudioPlayerStub), |
| 72 recorder_(new AudioRecorderStub), | 72 recorder_(new AudioRecorderStub), |
| 73 last_received_decode_type_(AUDIO_TYPE_UNKNOWN) { | 73 last_received_decode_type_(AUDIO_TYPE_UNKNOWN) { |
| 74 std::vector<AudioToken> tokens; | 74 std::vector<AudioToken> tokens; |
| 75 tokens.push_back(AudioToken("abcdef", true)); | 75 tokens.push_back(AudioToken("abcdef", true)); |
| 76 tokens.push_back(AudioToken("123456", false)); | 76 tokens.push_back(AudioToken("123456", false)); |
| 77 whispernet_client_.reset(new StubWhispernetClient( | 77 client_.reset(new StubWhispernetClient( |
| 78 CreateRandomAudioRefCounted(0x123, 1, 0x321), tokens)); | 78 CreateRandomAudioRefCounted(0x123, 1, 0x321), tokens)); |
| 79 | 79 |
| 80 audio_manager_->set_player_for_testing(AUDIBLE, audible_player_); | 80 // TODO(ckehoe): Pass these into the Modem constructor instead. |
| 81 audio_manager_->set_player_for_testing(INAUDIBLE, inaudible_player_); | 81 modem_->set_player_for_testing(AUDIBLE, audible_player_); |
| 82 audio_manager_->set_recorder_for_testing(recorder_); | 82 modem_->set_player_for_testing(INAUDIBLE, inaudible_player_); |
| 83 audio_manager_->Initialize( | 83 modem_->set_recorder_for_testing(recorder_); |
| 84 whispernet_client_.get(), | 84 modem_->Initialize( |
| 85 base::Bind(&AudioManagerTest::GetTokens, base::Unretained(this))); | 85 client_.get(), |
| 86 base::Bind(&ModemTest::GetTokens, base::Unretained(this))); |
| 86 } | 87 } |
| 87 | 88 |
| 88 ~AudioManagerTest() override {} | 89 ~ModemTest() override {} |
| 89 | 90 |
| 90 protected: | 91 protected: |
| 91 void GetTokens(const std::vector<AudioToken>& tokens) { | 92 void GetTokens(const std::vector<AudioToken>& tokens) { |
| 92 last_received_decode_type_ = AUDIO_TYPE_UNKNOWN; | 93 last_received_decode_type_ = AUDIO_TYPE_UNKNOWN; |
| 93 for (const auto& token : tokens) { | 94 for (const auto& token : tokens) { |
| 94 if (token.audible && last_received_decode_type_ == INAUDIBLE) { | 95 if (token.audible && last_received_decode_type_ == INAUDIBLE) { |
| 95 last_received_decode_type_ = BOTH; | 96 last_received_decode_type_ = BOTH; |
| 96 } else if (!token.audible && last_received_decode_type_ == AUDIBLE) { | 97 } else if (!token.audible && last_received_decode_type_ == AUDIBLE) { |
| 97 last_received_decode_type_ = BOTH; | 98 last_received_decode_type_ = BOTH; |
| 98 } else if (token.audible) { | 99 } else if (token.audible) { |
| 99 last_received_decode_type_ = AUDIBLE; | 100 last_received_decode_type_ = AUDIBLE; |
| 100 } else { | 101 } else { |
| 101 last_received_decode_type_ = INAUDIBLE; | 102 last_received_decode_type_ = INAUDIBLE; |
| 102 } | 103 } |
| 103 } | 104 } |
| 104 } | 105 } |
| 105 | 106 |
| 106 base::MessageLoop message_loop_; | 107 base::MessageLoop message_loop_; |
| 107 // Order is important, |whispernet_client_| needs to get destructed *after* | 108 // This order is important. The WhispernetClient needs to outlive the Modem. |
| 108 // |audio_manager_|. | 109 scoped_ptr<WhispernetClient> client_; |
| 109 scoped_ptr<WhispernetClient> whispernet_client_; | 110 scoped_ptr<ModemImpl> modem_; |
| 110 scoped_ptr<AudioManagerImpl> audio_manager_; | |
| 111 | 111 |
| 112 // These will be deleted by |audio_manager_|'s dtor calling finalize on them. | 112 // These will be deleted by the Modem's destructor calling finalize on them. |
| 113 AudioPlayerStub* audible_player_; | 113 AudioPlayerStub* audible_player_; |
| 114 AudioPlayerStub* inaudible_player_; | 114 AudioPlayerStub* inaudible_player_; |
| 115 AudioRecorderStub* recorder_; | 115 AudioRecorderStub* recorder_; |
| 116 | 116 |
| 117 AudioType last_received_decode_type_; | 117 AudioType last_received_decode_type_; |
| 118 | 118 |
| 119 private: | 119 private: |
| 120 DISALLOW_COPY_AND_ASSIGN(AudioManagerTest); | 120 DISALLOW_COPY_AND_ASSIGN(ModemTest); |
| 121 }; | 121 }; |
| 122 | 122 |
| 123 TEST_F(AudioManagerTest, EncodeToken) { | 123 TEST_F(ModemTest, EncodeToken) { |
| 124 audio_manager_->StartPlaying(AUDIBLE); | 124 modem_->StartPlaying(AUDIBLE); |
| 125 // No token yet, player shouldn't be playing. | 125 // No token yet, player shouldn't be playing. |
| 126 EXPECT_FALSE(audible_player_->IsPlaying()); | 126 EXPECT_FALSE(audible_player_->IsPlaying()); |
| 127 | 127 |
| 128 audio_manager_->SetToken(INAUDIBLE, "abcd"); | 128 modem_->SetToken(INAUDIBLE, "abcd"); |
| 129 // No *audible* token yet, so player still shouldn't be playing. | 129 // No *audible* token yet, so player still shouldn't be playing. |
| 130 EXPECT_FALSE(audible_player_->IsPlaying()); | 130 EXPECT_FALSE(audible_player_->IsPlaying()); |
| 131 | 131 |
| 132 audio_manager_->SetToken(AUDIBLE, "abcd"); | 132 modem_->SetToken(AUDIBLE, "abcd"); |
| 133 EXPECT_TRUE(audible_player_->IsPlaying()); | 133 EXPECT_TRUE(audible_player_->IsPlaying()); |
| 134 } | 134 } |
| 135 | 135 |
| 136 TEST_F(AudioManagerTest, Record) { | 136 TEST_F(ModemTest, Record) { |
| 137 recorder_->TriggerDecodeRequest(); | 137 recorder_->TriggerDecodeRequest(); |
| 138 EXPECT_EQ(AUDIO_TYPE_UNKNOWN, last_received_decode_type_); | 138 EXPECT_EQ(AUDIO_TYPE_UNKNOWN, last_received_decode_type_); |
| 139 | 139 |
| 140 audio_manager_->StartRecording(AUDIBLE); | 140 modem_->StartRecording(AUDIBLE); |
| 141 recorder_->TriggerDecodeRequest(); | 141 recorder_->TriggerDecodeRequest(); |
| 142 EXPECT_EQ(AUDIBLE, last_received_decode_type_); | 142 EXPECT_EQ(AUDIBLE, last_received_decode_type_); |
| 143 | 143 |
| 144 audio_manager_->StartRecording(INAUDIBLE); | 144 modem_->StartRecording(INAUDIBLE); |
| 145 recorder_->TriggerDecodeRequest(); | 145 recorder_->TriggerDecodeRequest(); |
| 146 EXPECT_EQ(BOTH, last_received_decode_type_); | 146 EXPECT_EQ(BOTH, last_received_decode_type_); |
| 147 | 147 |
| 148 audio_manager_->StopRecording(AUDIBLE); | 148 modem_->StopRecording(AUDIBLE); |
| 149 recorder_->TriggerDecodeRequest(); | 149 recorder_->TriggerDecodeRequest(); |
| 150 EXPECT_EQ(INAUDIBLE, last_received_decode_type_); | 150 EXPECT_EQ(INAUDIBLE, last_received_decode_type_); |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace copresence | 153 } // namespace audio_modem |
| OLD | NEW |