| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | |
| 6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/cancelable_callback.h" | |
| 13 #include "base/files/file_path.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_vector.h" | |
| 17 #include "components/copresence/mediums/audio/audio_manager.h" | |
| 18 #include "components/copresence/public/copresence_constants.h" | |
| 19 #include "components/copresence/timed_map.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class Time; | |
| 23 } | |
| 24 | |
| 25 namespace media { | |
| 26 class AudioBus; | |
| 27 } | |
| 28 | |
| 29 namespace copresence { | |
| 30 | |
| 31 class AudioPlayer; | |
| 32 class AudioRecorder; | |
| 33 class WhispernetClient; | |
| 34 | |
| 35 // The AudioManagerImpl class manages the playback and recording of tokens. Once | |
| 36 // playback or recording is started, it is up to the audio manager to handle | |
| 37 // the specifics of how this is done. In the future, for example, this class | |
| 38 // may pause recording and playback to implement carrier sense. | |
| 39 class AudioManagerImpl final : public AudioManager { | |
| 40 public: | |
| 41 AudioManagerImpl(); | |
| 42 ~AudioManagerImpl() override; | |
| 43 | |
| 44 // AudioManager overrides: | |
| 45 void Initialize(WhispernetClient* whispernet_client, | |
| 46 const TokensCallback& tokens_cb) override; | |
| 47 void StartPlaying(AudioType type) override; | |
| 48 void StopPlaying(AudioType type) override; | |
| 49 void StartRecording(AudioType type) override; | |
| 50 void StopRecording(AudioType type) override; | |
| 51 void SetToken(AudioType type, const std::string& url_safe_token) override; | |
| 52 const std::string GetToken(AudioType type) override; | |
| 53 bool IsPlayingTokenHeard(AudioType type) override; | |
| 54 void SetTokenLength(AudioType type, size_t token_length) override; | |
| 55 | |
| 56 void set_player_for_testing(AudioType type, AudioPlayer* player) { | |
| 57 player_[type] = player; | |
| 58 } | |
| 59 void set_recorder_for_testing(AudioRecorder* recorder) { | |
| 60 recorder_ = recorder; | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 using SamplesMap = TimedMap<std::string, | |
| 65 scoped_refptr<media::AudioBusRefCounted>>; | |
| 66 | |
| 67 // Receives the audio samples from encoding a token. | |
| 68 void OnTokenEncoded(AudioType type, | |
| 69 const std::string& token, | |
| 70 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
| 71 | |
| 72 // Receives any tokens found by decoding audio samples. | |
| 73 void OnTokensFound(const std::vector<AudioToken>& tokens); | |
| 74 | |
| 75 // Update our currently playing token with the new token. Change the playing | |
| 76 // samples if needed. Prerequisite: Samples corresponding to this token | |
| 77 // should already be in the samples cache. | |
| 78 void UpdateToken(AudioType type, const std::string& token); | |
| 79 | |
| 80 void RestartPlaying(AudioType type); | |
| 81 | |
| 82 void DecodeSamplesConnector(const std::string& samples); | |
| 83 | |
| 84 void DumpToken(AudioType audio_type, | |
| 85 const std::string& token, | |
| 86 const media::AudioBus* samples); | |
| 87 | |
| 88 WhispernetClient* whispernet_client_; | |
| 89 | |
| 90 // Callbacks to send tokens back to the CopresenceManager. | |
| 91 TokensCallback tokens_cb_; | |
| 92 | |
| 93 // This cancelable callback is passed to the recorder. The recorder's | |
| 94 // destruction will happen on the audio thread, so it can outlive us. | |
| 95 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; | |
| 96 | |
| 97 // We use the AudioType enum to index into all our data structures that work | |
| 98 // on values for both audible and inaudible. | |
| 99 static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); | |
| 100 static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); | |
| 101 | |
| 102 // Indexed using enum AudioType. | |
| 103 bool player_enabled_[2]; | |
| 104 bool should_be_playing_[2]; | |
| 105 bool should_be_recording_[2]; | |
| 106 | |
| 107 // AudioPlayer and AudioRecorder objects are self-deleting. When we call | |
| 108 // Finalize on them, they clean themselves up on the Audio thread. | |
| 109 // Indexed using enum AudioType. | |
| 110 AudioPlayer* player_[2]; | |
| 111 AudioRecorder* recorder_; | |
| 112 | |
| 113 // Indexed using enum AudioType. | |
| 114 std::string playing_token_[2]; | |
| 115 size_t token_length_[2]; | |
| 116 base::Time started_playing_[2]; | |
| 117 base::Time heard_own_token_[2]; | |
| 118 | |
| 119 // Cache that holds the encoded samples. After reaching its limit, the cache | |
| 120 // expires the oldest samples first. | |
| 121 // Indexed using enum AudioType. | |
| 122 ScopedVector<SamplesMap> samples_cache_; | |
| 123 | |
| 124 base::FilePath dump_tokens_dir_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl); | |
| 127 }; | |
| 128 | |
| 129 } // namespace copresence | |
| 130 | |
| 131 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | |
| OLD | NEW |