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 |
| 10 #include "base/callback.h" |
| 11 #include "base/cancelable_callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 15 #include "components/copresence/mediums/audio/audio_manager.h" |
| 16 #include "components/copresence/public/copresence_constants.h" |
| 17 #include "components/copresence/timed_map.h" |
| 18 |
| 19 namespace copresence { |
| 20 |
| 21 class AudioPlayer; |
| 22 class AudioRecorder; |
| 23 |
| 24 // 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 // 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 class AudioManagerImpl final : public AudioManager { |
| 29 public: |
| 30 AudioManagerImpl(); |
| 31 ~AudioManagerImpl(); |
| 32 |
| 33 // AudioManager overrides: |
| 34 void Initialize(const DecodeSamplesCallback& decode_cb, |
| 35 const EncodeTokenCallback& encode_cb) override; |
| 36 void StartPlaying(AudioType type) override; |
| 37 void StopPlaying(AudioType type) override; |
| 38 void StartRecording(AudioType type) override; |
| 39 void StopRecording(AudioType type) override; |
| 40 void SetToken(AudioType type, const std::string& url_unsafe_token) override; |
| 41 const std::string GetToken(AudioType type) override; |
| 42 bool IsRecording(AudioType type) override; |
| 43 bool IsPlaying(AudioType type) override; |
| 44 |
| 45 void set_player_for_testing(AudioType type, AudioPlayer* player) { |
| 46 player_[type] = player; |
| 47 } |
| 48 void set_recorder_for_testing(AudioRecorder* recorder) { |
| 49 recorder_ = recorder; |
| 50 } |
| 51 |
| 52 private: |
| 53 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>> |
| 54 SamplesMap; |
| 55 |
| 56 // This is the method that the whispernet client needs to call to return |
| 57 // samples to us. |
| 58 void OnTokenEncoded(const std::string& token, |
| 59 AudioType type, |
| 60 const scoped_refptr<media::AudioBusRefCounted>& samples); |
| 61 |
| 62 // Update our currently playing token with the new token. Change the playing |
| 63 // samples if needed. Prerequisite: Samples corresponding to this token |
| 64 // should already be in the samples cache. |
| 65 void UpdateToken(AudioType type, const std::string& token); |
| 66 |
| 67 // Connector callback to forward the audio samples to Whispernet, based on |
| 68 // the type of recording we've been instructed to do. |
| 69 void DecodeSamplesConnector(const std::string& samples); |
| 70 |
| 71 // Callbacks to speak with whispernet. |
| 72 DecodeSamplesCallback decode_cb_; |
| 73 EncodeTokenCallback encode_cb_; |
| 74 |
| 75 // 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 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; |
| 78 |
| 79 // We use the AudioType enum to index into all our data structures that work |
| 80 // on values for both audible and inaudible. |
| 81 static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); |
| 82 static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); |
| 83 |
| 84 // Indexed using enum AudioType. |
| 85 bool playing_[2]; |
| 86 bool recording_[2]; |
| 87 |
| 88 // AudioPlayer and AudioRecorder objects are self-deleting. When we call |
| 89 // Finalize on them, they clean themselves up on the Audio thread. |
| 90 // Indexed using enum AudioType. |
| 91 AudioPlayer* player_[2]; |
| 92 AudioRecorder* recorder_; |
| 93 |
| 94 // Indexed using enum AudioType. |
| 95 std::string token_[2]; |
| 96 |
| 97 // Cache that holds the encoded samples. After reaching its limit, the cache |
| 98 // expires the oldest samples first. |
| 99 // Indexed using enum AudioType. |
| 100 ScopedVector<SamplesMap> samples_cache_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl); |
| 103 }; |
| 104 |
| 105 } // namespace copresence |
| 106 |
| 107 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ |
OLD | NEW |