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