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.
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_vector.h"
16 #include "components/copresence/mediums/audio/audio_manager.h"
17 #include "components/copresence/public/copresence_constants.h"
18 #include "components/copresence/timed_map.h"
24 namespace copresence
{
28 class WhispernetClient
;
30 // The AudioManagerImpl class manages the playback and recording of tokens. Once
31 // playback or recording is started, it is up to the audio manager to handle
32 // the specifics of how this is done. In the future, for example, this class
33 // may pause recording and playback to implement carrier sense.
34 class AudioManagerImpl final
: public AudioManager
{
37 ~AudioManagerImpl() override
;
39 // AudioManager overrides:
40 void Initialize(WhispernetClient
* whispernet_client
,
41 const TokensCallback
& tokens_cb
) override
;
42 void StartPlaying(AudioType type
) override
;
43 void StopPlaying(AudioType type
) override
;
44 void StartRecording(AudioType type
) override
;
45 void StopRecording(AudioType type
) override
;
46 void SetToken(AudioType type
, const std::string
& url_safe_token
) override
;
47 const std::string
GetToken(AudioType type
) override
;
48 bool IsPlayingTokenHeard(AudioType type
) override
;
49 void SetTokenLength(AudioType type
, size_t token_length
) override
;
51 void set_player_for_testing(AudioType type
, AudioPlayer
* player
) {
52 player_
[type
] = player
;
54 void set_recorder_for_testing(AudioRecorder
* recorder
) {
59 using SamplesMap
= TimedMap
<std::string
,
60 scoped_refptr
<media::AudioBusRefCounted
>>;
62 // Receives the audio samples from encoding a token.
63 void OnTokenEncoded(AudioType type
,
64 const std::string
& token
,
65 const scoped_refptr
<media::AudioBusRefCounted
>& samples
);
67 // Receives any tokens found by decoding audio samples.
68 void OnTokensFound(const std::vector
<AudioToken
>& tokens
);
70 // Update our currently playing token with the new token. Change the playing
71 // samples if needed. Prerequisite: Samples corresponding to this token
72 // should already be in the samples cache.
73 void UpdateToken(AudioType type
, const std::string
& token
);
75 void RestartPlaying(AudioType type
);
77 void DecodeSamplesConnector(const std::string
& samples
);
79 WhispernetClient
* whispernet_client_
;
81 // Callbacks to send tokens back to the CopresenceManager.
82 TokensCallback tokens_cb_
;
84 // This cancelable callback is passed to the recorder. The recorder's
85 // destruction will happen on the audio thread, so it can outlive us.
86 base::CancelableCallback
<void(const std::string
&)> decode_cancelable_cb_
;
88 // We use the AudioType enum to index into all our data structures that work
89 // on values for both audible and inaudible.
90 static_assert(AUDIBLE
== 0, "AudioType::AUDIBLE should be 0.");
91 static_assert(INAUDIBLE
== 1, "AudioType::INAUDIBLE should be 1.");
93 // Indexed using enum AudioType.
94 bool should_be_playing_
[2];
95 bool should_be_recording_
[2];
97 // AudioPlayer and AudioRecorder objects are self-deleting. When we call
98 // Finalize on them, they clean themselves up on the Audio thread.
99 // Indexed using enum AudioType.
100 AudioPlayer
* player_
[2];
101 AudioRecorder
* recorder_
;
103 // Indexed using enum AudioType.
104 std::string playing_token_
[2];
105 base::Time started_playing_
[2];
106 base::Time heard_own_token_
[2];
108 // Cache that holds the encoded samples. After reaching its limit, the cache
109 // expires the oldest samples first.
110 // Indexed using enum AudioType.
111 ScopedVector
<SamplesMap
> samples_cache_
;
113 size_t token_length_
[2];
115 DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl
);
118 } // namespace copresence
120 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_