OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | 5 #ifndef COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_ |
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | 6 #define COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
| 13 #include "base/containers/mru_cache.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
17 #include "components/copresence/mediums/audio/audio_manager.h" | 18 #include "components/audio_modem/public/audio_modem_types.h" |
18 #include "components/copresence/public/copresence_constants.h" | 19 #include "components/audio_modem/public/modem.h" |
19 #include "components/copresence/timed_map.h" | 20 #include "media/base/audio_bus.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 class Time; | 23 class Time; |
23 } | 24 } |
24 | 25 |
25 namespace media { | 26 namespace audio_modem { |
26 class AudioBus; | |
27 } | |
28 | |
29 namespace copresence { | |
30 | 27 |
31 class AudioPlayer; | 28 class AudioPlayer; |
32 class AudioRecorder; | 29 class AudioRecorder; |
33 class WhispernetClient; | 30 class WhispernetClient; |
34 | 31 |
35 // The AudioManagerImpl class manages the playback and recording of tokens. Once | 32 // The ModemImpl class manages the playback and recording of tokens. |
36 // playback or recording is started, it is up to the audio manager to handle | 33 // Clients should not necessary expect the modem to play or record continuously. |
37 // the specifics of how this is done. In the future, for example, this class | 34 // In the future, it may timeslice multiple tokens, or implement carrier sense. |
38 // may pause recording and playback to implement carrier sense. | 35 class ModemImpl final : public Modem { |
39 class AudioManagerImpl final : public AudioManager { | |
40 public: | 36 public: |
41 AudioManagerImpl(); | 37 ModemImpl(); |
42 ~AudioManagerImpl() override; | 38 ~ModemImpl() override; |
43 | 39 |
44 // AudioManager overrides: | 40 // Modem overrides: |
45 void Initialize(WhispernetClient* whispernet_client, | 41 void Initialize(WhispernetClient* client, |
46 const TokensCallback& tokens_cb) override; | 42 const TokensCallback& tokens_cb) override; |
47 void StartPlaying(AudioType type) override; | 43 void StartPlaying(AudioType type) override; |
48 void StopPlaying(AudioType type) override; | 44 void StopPlaying(AudioType type) override; |
49 void StartRecording(AudioType type) override; | 45 void StartRecording(AudioType type) override; |
50 void StopRecording(AudioType type) override; | 46 void StopRecording(AudioType type) override; |
51 void SetToken(AudioType type, const std::string& url_safe_token) override; | 47 void SetToken(AudioType type, const std::string& url_safe_token) override; |
52 const std::string GetToken(AudioType type) override; | 48 const std::string GetToken(AudioType type) override; |
53 bool IsPlayingTokenHeard(AudioType type) override; | 49 bool IsPlayingTokenHeard(AudioType type) override; |
54 void SetTokenLength(AudioType type, size_t token_length) override; | 50 void SetTokenLength(AudioType type, size_t token_length) override; |
55 | 51 |
56 void set_player_for_testing(AudioType type, AudioPlayer* player) { | 52 void set_player_for_testing(AudioType type, AudioPlayer* player) { |
57 player_[type] = player; | 53 player_[type] = player; |
58 } | 54 } |
59 void set_recorder_for_testing(AudioRecorder* recorder) { | 55 void set_recorder_for_testing(AudioRecorder* recorder) { |
60 recorder_ = recorder; | 56 recorder_ = recorder; |
61 } | 57 } |
62 | 58 |
63 private: | 59 private: |
64 using SamplesMap = TimedMap<std::string, | 60 using SamplesMap = base::MRUCache<std::string, |
65 scoped_refptr<media::AudioBusRefCounted>>; | 61 scoped_refptr<media::AudioBusRefCounted>>; |
66 | 62 |
67 // Receives the audio samples from encoding a token. | 63 // Receives the audio samples from encoding a token. |
68 void OnTokenEncoded(AudioType type, | 64 void OnTokenEncoded(AudioType type, |
69 const std::string& token, | 65 const std::string& token, |
70 const scoped_refptr<media::AudioBusRefCounted>& samples); | 66 const scoped_refptr<media::AudioBusRefCounted>& samples); |
71 | 67 |
72 // Receives any tokens found by decoding audio samples. | 68 // Receives any tokens found by decoding audio samples. |
73 void OnTokensFound(const std::vector<AudioToken>& tokens); | 69 void OnTokensFound(const std::vector<AudioToken>& tokens); |
74 | 70 |
75 // Update our currently playing token with the new token. Change the playing | 71 // Update our currently playing token with the new token. Change the playing |
76 // samples if needed. Prerequisite: Samples corresponding to this token | 72 // samples if needed. Prerequisite: Samples corresponding to this token |
77 // should already be in the samples cache. | 73 // should already be in the samples cache. |
78 void UpdateToken(AudioType type, const std::string& token); | 74 void UpdateToken(AudioType type, const std::string& token); |
79 | 75 |
80 void RestartPlaying(AudioType type); | 76 void RestartPlaying(AudioType type); |
81 | 77 |
82 void DecodeSamplesConnector(const std::string& samples); | 78 void DecodeSamplesConnector(const std::string& samples); |
83 | 79 |
84 void DumpToken(AudioType audio_type, | 80 void DumpToken(AudioType audio_type, |
85 const std::string& token, | 81 const std::string& token, |
86 const media::AudioBus* samples); | 82 const media::AudioBus* samples); |
87 | 83 |
88 WhispernetClient* whispernet_client_; | 84 WhispernetClient* client_; |
89 | 85 |
90 // Callbacks to send tokens back to the CopresenceManager. | 86 // Callbacks to send tokens back to the client. |
91 TokensCallback tokens_cb_; | 87 TokensCallback tokens_cb_; |
92 | 88 |
93 // This cancelable callback is passed to the recorder. The recorder's | 89 // This cancelable callback is passed to the recorder. The recorder's |
94 // destruction will happen on the audio thread, so it can outlive us. | 90 // destruction will happen on the audio thread, so it can outlive us. |
95 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; | 91 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; |
96 | 92 |
97 // We use the AudioType enum to index into all our data structures that work | 93 // We use the AudioType enum to index into all our data structures that work |
98 // on values for both audible and inaudible. | 94 // on values for both audible and inaudible. |
99 static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); | 95 static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); |
100 static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); | 96 static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); |
(...skipping 11 matching lines...) Expand all Loading... |
112 | 108 |
113 // Indexed using enum AudioType. | 109 // Indexed using enum AudioType. |
114 std::string playing_token_[2]; | 110 std::string playing_token_[2]; |
115 size_t token_length_[2]; | 111 size_t token_length_[2]; |
116 base::Time started_playing_[2]; | 112 base::Time started_playing_[2]; |
117 base::Time heard_own_token_[2]; | 113 base::Time heard_own_token_[2]; |
118 | 114 |
119 // Cache that holds the encoded samples. After reaching its limit, the cache | 115 // Cache that holds the encoded samples. After reaching its limit, the cache |
120 // expires the oldest samples first. | 116 // expires the oldest samples first. |
121 // Indexed using enum AudioType. | 117 // Indexed using enum AudioType. |
122 ScopedVector<SamplesMap> samples_cache_; | 118 ScopedVector<SamplesMap> samples_caches_; |
123 | 119 |
124 base::FilePath dump_tokens_dir_; | 120 base::FilePath dump_tokens_dir_; |
125 | 121 |
126 DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl); | 122 DISALLOW_COPY_AND_ASSIGN(ModemImpl); |
127 }; | 123 }; |
128 | 124 |
129 } // namespace copresence | 125 } // namespace audio_modem |
130 | 126 |
131 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_IMPL_H_ | 127 #endif // COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_ |
OLD | NEW |