| 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_H_ |
| 6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_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_recorder.h" |
| 16 #include "components/copresence/public/copresence_constants.h" |
| 17 #include "components/copresence/timed_map.h" |
| 18 |
| 19 namespace base { |
| 20 class MessageLoop; |
| 21 } |
| 22 |
| 23 namespace media { |
| 24 class AudioBus; |
| 25 } |
| 26 |
| 27 namespace copresence { |
| 28 |
| 29 class AudioPlayer; |
| 30 |
| 31 // The AudioManager class will record audio until told to stop. |
| 32 class AudioManager { |
| 33 public: |
| 34 typedef base::Callback<void(const std::string&, |
| 35 AudioType, |
| 36 const scoped_refptr<media::AudioBusRefCounted>&)> |
| 37 SamplesCallback; |
| 38 typedef base::Callback<void(const std::string&, |
| 39 AudioType, |
| 40 const SamplesCallback&)> EncodeTokenCallback; |
| 41 typedef base::Callback<void(AudioType, const std::string&)> |
| 42 DecodeSamplesCallback; |
| 43 |
| 44 AudioManager(); |
| 45 virtual ~AudioManager(); |
| 46 |
| 47 // Initializes the object. Do not use this object before calling this method. |
| 48 virtual void Initialize(const DecodeSamplesCallback& decode_cb, |
| 49 const EncodeTokenCallback& encode_cb); |
| 50 |
| 51 virtual void StartPlaying(AudioType type); |
| 52 virtual void StopPlaying(AudioType type); |
| 53 |
| 54 virtual void StartRecording(AudioType type); |
| 55 virtual void StopRecording(AudioType type); |
| 56 |
| 57 virtual void SetToken(AudioType type, const std::string& url_unsafe_token); |
| 58 |
| 59 const std::string& GetToken(AudioType type) { return token_[type]; } |
| 60 |
| 61 // For test. |
| 62 void set_player_for_testing(AudioType type, AudioPlayer* player) { |
| 63 player_[type] = player; |
| 64 } |
| 65 |
| 66 void set_recorder_for_testing(AudioRecorder* recorder) { |
| 67 recorder_ = recorder; |
| 68 } |
| 69 |
| 70 void set_recording_for_testing(AudioType type, bool is_recording) { |
| 71 recording_[type] = is_recording; |
| 72 } |
| 73 |
| 74 void set_playing_for_testing(AudioType type, bool is_playing) { |
| 75 playing_[type] = is_playing; |
| 76 } |
| 77 |
| 78 bool is_recording_for_testing(AudioType type) { return recording_[type]; } |
| 79 |
| 80 bool is_playing_for_testing(AudioType type) { return playing_[type]; } |
| 81 |
| 82 private: |
| 83 friend class AudioManagerTest; |
| 84 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, BasicRecordAndStop); |
| 85 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, OutOfOrderRecordAndStopMultiple); |
| 86 |
| 87 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>> |
| 88 SamplesMap; |
| 89 |
| 90 // This is the method that the whispernet client needs to call to return |
| 91 // samples to us. |
| 92 void OnTokenEncoded(const std::string& token, |
| 93 AudioType type, |
| 94 const scoped_refptr<media::AudioBusRefCounted>& samples); |
| 95 |
| 96 // Update our currently playing token with the new token. Change the playing |
| 97 // samples if needed. Prerequisite: Samples corresponding to this token |
| 98 // should already be in the samples cache. |
| 99 void UpdateToken(AudioType type, const std::string& token); |
| 100 |
| 101 // Connector callback to forward the audio samples to Whispernet, based on |
| 102 // the type of recording we've been instructed to do. |
| 103 void DecodeSamplesConnector(const std::string& samples); |
| 104 |
| 105 // Callbacks to speak with whispernet. |
| 106 DecodeSamplesCallback decode_cb_; |
| 107 EncodeTokenCallback encode_cb_; |
| 108 |
| 109 // We need this cancelable callback to pass to the recorder. The recorder's |
| 110 // destruction will happen on the audio thread, so it can outlive us. |
| 111 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; |
| 112 |
| 113 // 0 = AUDIBLE, 1 = INAUDIBLE. |
| 114 bool playing_[2]; |
| 115 bool recording_[2]; |
| 116 |
| 117 // AudioPlayer and AudioRecorder objects are self-deleting. When we call |
| 118 // Finalize on them, they clean themselves up on the Audio thread. |
| 119 // 0 = AUDIBLE, 1 = INAUDIBLE. |
| 120 std::vector<AudioPlayer*> player_; |
| 121 AudioRecorder* recorder_; |
| 122 |
| 123 // 0 = AUDIBLE, 1 = INAUDIBLE. |
| 124 std::string token_[2]; |
| 125 |
| 126 // Cache that holds the encoded samples. After reaching its limit, the cache |
| 127 // expires the oldest samples first. |
| 128 // 0 = AUDIBLE, 1 = INAUDIBLE. |
| 129 ScopedVector<SamplesMap> samples_cache_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(AudioManager); |
| 132 }; |
| 133 |
| 134 } // namespace copresence |
| 135 |
| 136 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ |
| OLD | NEW |