Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(701)

Side by Side Diff: components/copresence/mediums/audio/audio_manager.h

Issue 637223011: Redesign the copresence audio handlers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 will record audio until told to stop.
Charlie 2014/10/17 22:42:26 Provide some more detail. What is this class respo
rkc 2014/10/18 00:21:55 Done.
32 class AudioManager {
33 public:
34 typedef base::Callback<void(const std::string&,
35 AudioType,
36 const scoped_refptr<media::AudioBusRefCounted>&)>
37 SamplesCallback;
38 typedef base::Callback<void(const std::string&,
39 AudioType,
40 const SamplesCallback&)> EncodeTokenCallback;
41 typedef base::Callback<void(AudioType, const std::string&)>
42 DecodeSamplesCallback;
43
44 AudioManager();
45 virtual ~AudioManager();
46
47 // Initializes the object. Do not use this object before calling this method.
48 virtual void Initialize(const DecodeSamplesCallback& decode_cb,
Charlie 2014/10/17 22:42:26 In this new class, don't use the "new Foo(); foo->
rkc 2014/10/18 00:21:55 I am not quite convinced that is the way to go. Ag
49 const EncodeTokenCallback& encode_cb);
50
51 virtual void StartPlaying(AudioType type);
52 virtual void StopPlaying(AudioType type);
53
54 virtual void StartRecording(AudioType type);
55 virtual void StopRecording(AudioType type);
56
57 virtual void SetToken(AudioType type, const std::string& url_unsafe_token);
58
59 const std::string& GetToken(AudioType type) { return token_[type]; }
Daniel Erat 2014/10/17 22:26:00 nit: rename to get_token() since it's a simple inl
rkc 2014/10/18 00:21:55 Done.
60
61 // For test.
Daniel Erat 2014/10/17 22:26:00 nit: don't need this comment; method names already
Charlie 2014/10/17 22:42:26 The first two of these can be folded into the Crea
rkc 2014/10/18 00:21:55 Done.
rkc 2014/10/18 00:21:55 Acknowledged.
62 void set_player_for_testing(AudioType type, AudioPlayer* player) {
63 player_[type] = player;
64 }
65
Daniel Erat 2014/10/17 22:26:00 nit: i'd probably delete all the blank lines betwe
rkc 2014/10/18 00:21:55 Done.
66 void set_recorder_for_testing(AudioRecorder* recorder) {
67 recorder_ = recorder;
68 }
69
70 void set_recording_for_testing(AudioType type, bool is_recording) {
71 recording_[type] = is_recording;
72 }
73
74 void set_playing_for_testing(AudioType type, bool is_playing) {
75 playing_[type] = is_playing;
76 }
77
78 bool is_recording_for_testing(AudioType type) { return recording_[type]; }
79
80 bool is_playing_for_testing(AudioType type) { return playing_[type]; }
81
82 private:
83 friend class AudioManagerTest;
84 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, BasicRecordAndStop);
85 FRIEND_TEST_ALL_PREFIXES(AudioManagerTest, OutOfOrderRecordAndStopMultiple);
86
87 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>>
88 SamplesMap;
89
90 // This is the method that the whispernet client needs to call to return
91 // samples to us.
92 void OnTokenEncoded(const std::string& token,
93 AudioType type,
94 const scoped_refptr<media::AudioBusRefCounted>& samples);
95
96 // Update our currently playing token with the new token. Change the playing
97 // samples if needed. Prerequisite: Samples corresponding to this token
98 // should already be in the samples cache.
99 void UpdateToken(AudioType type, const std::string& token);
100
101 // Connector callback to forward the audio samples to Whispernet, based on
102 // the type of recording we've been instructed to do.
103 void DecodeSamplesConnector(const std::string& samples);
104
105 // Callbacks to speak with whispernet.
106 DecodeSamplesCallback decode_cb_;
107 EncodeTokenCallback encode_cb_;
108
109 // We need this cancelable callback to pass to the recorder. The recorder's
110 // destruction will happen on the audio thread, so it can outlive us.
111 base::CancelableCallback<void(const std::string&)> decode_cancelable_cb_;
112
113 // 0 = AUDIBLE, 1 = INAUDIBLE.
Daniel Erat 2014/10/17 22:26:00 please instead just document the name of the enum
rkc 2014/10/18 00:21:55 Done.
114 bool playing_[2];
115 bool recording_[2];
116
117 // AudioPlayer and AudioRecorder objects are self-deleting. When we call
118 // Finalize on them, they clean themselves up on the Audio thread.
119 // 0 = AUDIBLE, 1 = INAUDIBLE.
120 AudioPlayer* player_[2];
121 AudioRecorder* recorder_;
122
123 // 0 = AUDIBLE, 1 = INAUDIBLE.
124 std::string token_[2];
125
126 // Cache that holds the encoded samples. After reaching its limit, the cache
127 // expires the oldest samples first.
128 // 0 = AUDIBLE, 1 = INAUDIBLE.
129 ScopedVector<SamplesMap> samples_cache_;
130
131 DISALLOW_COPY_AND_ASSIGN(AudioManager);
132 };
133
134 } // namespace copresence
135
136 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698