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