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