Chromium Code Reviews| Index: components/copresence/mediums/audio/audio_manager.h |
| diff --git a/components/copresence/mediums/audio/audio_manager.h b/components/copresence/mediums/audio/audio_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2450f2a0bb716f9a8f4482a554e00e9aa3653964 |
| --- /dev/null |
| +++ b/components/copresence/mediums/audio/audio_manager.h |
| @@ -0,0 +1,153 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ |
| +#define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ |
| + |
| +#include <string> |
| + |
| +#include "base/callback.h" |
| +#include "base/cancelable_callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "components/copresence/mediums/audio/audio_recorder.h" |
| +#include "components/copresence/public/copresence_constants.h" |
| +#include "components/copresence/timed_map.h" |
| + |
| +namespace base { |
| +class MessageLoop; |
| +} |
| + |
| +namespace media { |
| +class AudioBus; |
| +} |
| + |
| +namespace copresence { |
| + |
| +class AudioPlayer; |
| + |
| +class AudioManager { |
| + public: |
| + typedef base::Callback<void(const std::string&, |
| + AudioType, |
| + const scoped_refptr<media::AudioBusRefCounted>&)> |
| + SamplesCallback; |
|
Daniel Erat
2014/10/21 19:43:22
nit: rename this to something like HandleTokenCall
rkc
2014/10/22 00:06:20
Nope; EncodeTokenCallback generates encoded sample
|
| + typedef base::Callback<void(const std::string&, |
| + AudioType, |
| + const SamplesCallback&)> EncodeTokenCallback; |
| + typedef base::Callback<void(AudioType, const std::string&)> |
| + DecodeSamplesCallback; |
| + |
| + // Initializes the object. Do not use this object before calling this method. |
| + virtual void Initialize(const DecodeSamplesCallback& decode_cb, |
|
Daniel Erat
2014/10/21 19:43:22
as discussed earlier, does this need to be part of
rkc
2014/10/22 00:06:20
I changed my code around to remove Initialize from
Charlie
2014/10/22 00:28:06
This is for another CL, but can you remind me why
rkc
2014/10/22 00:47:08
We do this from tests all the time. We create the
Charlie
2014/10/22 01:03:29
I see. But if Initialize() is really just about se
rkc
2014/10/22 14:08:49
Initialize in this case is about setting callbacks
Charlie
2014/10/22 16:50:58
One of the principles of encapsulation is that obj
|
| + const EncodeTokenCallback& encode_cb) = 0; |
| + |
| + virtual void StartPlaying(AudioType type) = 0; |
| + virtual void StopPlaying(AudioType type) = 0; |
| + |
| + virtual void StartRecording(AudioType type) = 0; |
| + virtual void StopRecording(AudioType type) = 0; |
| + |
| + virtual void SetToken(AudioType type, |
| + const std::string& url_unsafe_token) = 0; |
| + |
| + virtual const std::string& get_token(AudioType type) = 0; |
|
Daniel Erat
2014/10/21 19:43:22
rename to GetToken()
rkc
2014/10/22 00:06:20
Renamed back to GetToken.
Done.
|
| + |
| + virtual bool IsRecording(AudioType type) = 0; |
| + virtual bool IsPlaying(AudioType type) = 0; |
| + |
| + virtual ~AudioManager(){}; |
|
Daniel Erat
2014/10/21 19:43:22
nit: move this up after the typedefs, add a space
rkc
2014/10/22 00:06:20
Done.
|
| +}; |
| + |
| +// The AudioManagerImpl class manages the playback and recording of tokens. Once |
| +// playback or recording is started, it is up to the audio manager to handle |
| +// the specifics of how this is done. In the future, for example, this class |
| +// may pause recording and playback to implement carrier sense. |
| +class AudioManagerImpl final : public AudioManager { |
|
Charlie
2014/10/21 19:59:42
Does this need to be in the same .h file? Who refe
rkc
2014/10/22 00:06:20
Done.
|
| + public: |
| + AudioManagerImpl(); |
| + virtual ~AudioManagerImpl(); |
| + |
| + // AudioManager overrides: |
| + virtual void Initialize(const DecodeSamplesCallback& decode_cb, |
| + const EncodeTokenCallback& encode_cb) override; |
| + virtual void StartPlaying(AudioType type) override; |
| + virtual void StopPlaying(AudioType type) override; |
| + virtual void StartRecording(AudioType type) override; |
| + virtual void StopRecording(AudioType type) override; |
| + virtual void SetToken(AudioType type, |
| + const std::string& url_unsafe_token) override; |
| + virtual const std::string& get_token(AudioType type) override; |
| + virtual bool IsRecording(AudioType type) override; |
| + virtual bool IsPlaying(AudioType type) override; |
| + |
| + void set_player_for_testing(AudioType type, AudioPlayer* player) { |
| + player_[type] = player; |
| + } |
| + void set_recorder_for_testing(AudioRecorder* recorder) { |
| + recorder_ = recorder; |
| + } |
| + |
| + private: |
| + friend class AudioManagerImplTest; |
| + FRIEND_TEST_ALL_PREFIXES(AudioManagerImplTest, BasicRecordAndStop); |
| + FRIEND_TEST_ALL_PREFIXES(AudioManagerImplTest, |
| + OutOfOrderRecordAndStopMultiple); |
| + |
| + typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>> |
| + SamplesMap; |
| + |
| + // This is the method that the whispernet client needs to call to return |
| + // samples to us. |
| + void OnTokenEncoded(const std::string& token, |
| + AudioType type, |
| + const scoped_refptr<media::AudioBusRefCounted>& samples); |
| + |
| + // Update our currently playing token with the new token. Change the playing |
| + // samples if needed. Prerequisite: Samples corresponding to this token |
| + // should already be in the samples cache. |
| + void UpdateToken(AudioType type, const std::string& token); |
| + |
| + // Connector callback to forward the audio samples to Whispernet, based on |
| + // the type of recording we've been instructed to do. |
| + void DecodeSamplesConnector(const std::string& samples); |
| + |
| + // Callbacks to speak with whispernet. |
| + DecodeSamplesCallback decode_cb_; |
| + EncodeTokenCallback encode_cb_; |
| + |
| + // This cancelable callback is passed to the recorder. The recorder's |
| + // destruction will happen on the audio thread, so it can outlive us. |
| + base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; |
| + |
| + // We use the AudioType enum to index into all our data structures that work |
| + // on values for both audible and inaudible. |
| + static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); |
| + static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); |
| + |
| + // Indexed using enum AudioType. |
| + bool playing_[2]; |
| + bool recording_[2]; |
| + |
| + // AudioPlayer and AudioRecorder objects are self-deleting. When we call |
| + // Finalize on them, they clean themselves up on the Audio thread. |
| + // Indexed using enum AudioType. |
| + AudioPlayer* player_[2]; |
| + AudioRecorder* recorder_; |
| + |
| + // Indexed using enum AudioType. |
| + std::string token_[2]; |
| + |
| + // Cache that holds the encoded samples. After reaching its limit, the cache |
| + // expires the oldest samples first. |
| + // Indexed using enum AudioType. |
| + ScopedVector<SamplesMap> samples_cache_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl); |
| +}; |
| + |
| +} // namespace copresence |
| + |
| +#endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ |