OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ | |
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/cancelable_callback.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_vector.h" | |
15 #include "components/copresence/mediums/audio/audio_recorder.h" | |
16 #include "components/copresence/public/copresence_constants.h" | |
17 #include "components/copresence/timed_map.h" | |
18 | |
19 namespace base { | |
20 class MessageLoop; | |
21 } | |
22 | |
23 namespace media { | |
24 class AudioBus; | |
25 } | |
26 | |
27 namespace copresence { | |
28 | |
29 class AudioPlayer; | |
30 | |
31 class AudioManager { | |
32 public: | |
33 typedef base::Callback<void(const std::string&, | |
34 AudioType, | |
35 const scoped_refptr<media::AudioBusRefCounted>&)> | |
36 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
| |
37 typedef base::Callback<void(const std::string&, | |
38 AudioType, | |
39 const SamplesCallback&)> EncodeTokenCallback; | |
40 typedef base::Callback<void(AudioType, const std::string&)> | |
41 DecodeSamplesCallback; | |
42 | |
43 // Initializes the object. Do not use this object before calling this method. | |
44 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
| |
45 const EncodeTokenCallback& encode_cb) = 0; | |
46 | |
47 virtual void StartPlaying(AudioType type) = 0; | |
48 virtual void StopPlaying(AudioType type) = 0; | |
49 | |
50 virtual void StartRecording(AudioType type) = 0; | |
51 virtual void StopRecording(AudioType type) = 0; | |
52 | |
53 virtual void SetToken(AudioType type, | |
54 const std::string& url_unsafe_token) = 0; | |
55 | |
56 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.
| |
57 | |
58 virtual bool IsRecording(AudioType type) = 0; | |
59 virtual bool IsPlaying(AudioType type) = 0; | |
60 | |
61 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.
| |
62 }; | |
63 | |
64 // The AudioManagerImpl class manages the playback and recording of tokens. Once | |
65 // playback or recording is started, it is up to the audio manager to handle | |
66 // the specifics of how this is done. In the future, for example, this class | |
67 // may pause recording and playback to implement carrier sense. | |
68 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.
| |
69 public: | |
70 AudioManagerImpl(); | |
71 virtual ~AudioManagerImpl(); | |
72 | |
73 // AudioManager overrides: | |
74 virtual void Initialize(const DecodeSamplesCallback& decode_cb, | |
75 const EncodeTokenCallback& encode_cb) override; | |
76 virtual void StartPlaying(AudioType type) override; | |
77 virtual void StopPlaying(AudioType type) override; | |
78 virtual void StartRecording(AudioType type) override; | |
79 virtual void StopRecording(AudioType type) override; | |
80 virtual void SetToken(AudioType type, | |
81 const std::string& url_unsafe_token) override; | |
82 virtual const std::string& get_token(AudioType type) override; | |
83 virtual bool IsRecording(AudioType type) override; | |
84 virtual bool IsPlaying(AudioType type) override; | |
85 | |
86 void set_player_for_testing(AudioType type, AudioPlayer* player) { | |
87 player_[type] = player; | |
88 } | |
89 void set_recorder_for_testing(AudioRecorder* recorder) { | |
90 recorder_ = recorder; | |
91 } | |
92 | |
93 private: | |
94 friend class AudioManagerImplTest; | |
95 FRIEND_TEST_ALL_PREFIXES(AudioManagerImplTest, BasicRecordAndStop); | |
96 FRIEND_TEST_ALL_PREFIXES(AudioManagerImplTest, | |
97 OutOfOrderRecordAndStopMultiple); | |
98 | |
99 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>> | |
100 SamplesMap; | |
101 | |
102 // This is the method that the whispernet client needs to call to return | |
103 // samples to us. | |
104 void OnTokenEncoded(const std::string& token, | |
105 AudioType type, | |
106 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
107 | |
108 // Update our currently playing token with the new token. Change the playing | |
109 // samples if needed. Prerequisite: Samples corresponding to this token | |
110 // should already be in the samples cache. | |
111 void UpdateToken(AudioType type, const std::string& token); | |
112 | |
113 // Connector callback to forward the audio samples to Whispernet, based on | |
114 // the type of recording we've been instructed to do. | |
115 void DecodeSamplesConnector(const std::string& samples); | |
116 | |
117 // Callbacks to speak with whispernet. | |
118 DecodeSamplesCallback decode_cb_; | |
119 EncodeTokenCallback encode_cb_; | |
120 | |
121 // This cancelable callback is passed to the recorder. The recorder's | |
122 // destruction will happen on the audio thread, so it can outlive us. | |
123 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_; | |
124 | |
125 // We use the AudioType enum to index into all our data structures that work | |
126 // on values for both audible and inaudible. | |
127 static_assert(AUDIBLE == 0, "AudioType::AUDIBLE should be 0."); | |
128 static_assert(INAUDIBLE == 1, "AudioType::INAUDIBLE should be 1."); | |
129 | |
130 // Indexed using enum AudioType. | |
131 bool playing_[2]; | |
132 bool recording_[2]; | |
133 | |
134 // AudioPlayer and AudioRecorder objects are self-deleting. When we call | |
135 // Finalize on them, they clean themselves up on the Audio thread. | |
136 // Indexed using enum AudioType. | |
137 AudioPlayer* player_[2]; | |
138 AudioRecorder* recorder_; | |
139 | |
140 // Indexed using enum AudioType. | |
141 std::string token_[2]; | |
142 | |
143 // Cache that holds the encoded samples. After reaching its limit, the cache | |
144 // expires the oldest samples first. | |
145 // Indexed using enum AudioType. | |
146 ScopedVector<SamplesMap> samples_cache_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(AudioManagerImpl); | |
149 }; | |
150 | |
151 } // namespace copresence | |
152 | |
153 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_ | |
OLD | NEW |