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