cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / audio_modem / modem_impl.h
blobec6beb529999ecfe0161e34164e18c0eefa2d8c1
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.
5 #ifndef COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_
6 #define COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_
8 #include <string>
9 #include <vector>
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"
22 namespace base {
23 class Time;
26 namespace audio_modem {
28 class AudioPlayer;
29 class AudioRecorder;
30 class WhispernetClient;
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;
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;
52 void set_player_for_testing(AudioType type, AudioPlayer* player) {
53 player_[type] = player;
55 void set_recorder_for_testing(AudioRecorder* recorder) {
56 recorder_ = recorder;
59 private:
60 using SamplesMap = base::MRUCache<std::string,
61 scoped_refptr<media::AudioBusRefCounted>>;
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);
68 // Receives any tokens found by decoding audio samples.
69 void OnTokensFound(const std::vector<AudioToken>& tokens);
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);
76 void RestartPlaying(AudioType type);
78 void DecodeSamplesConnector(const std::string& samples);
80 void DumpToken(AudioType audio_type,
81 const std::string& token,
82 const media::AudioBus* samples);
84 WhispernetClient* client_;
86 // Callbacks to send tokens back to the client.
87 TokensCallback tokens_cb_;
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_;
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.");
98 // Indexed using enum AudioType.
99 bool player_enabled_[2];
100 bool should_be_playing_[2];
101 bool should_be_recording_[2];
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_;
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];
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_;
120 base::FilePath dump_tokens_dir_;
122 DISALLOW_COPY_AND_ASSIGN(ModemImpl);
125 } // namespace audio_modem
127 #endif // COMPONENTS_AUDIO_MODEM_MODEM_IMPL_H_