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

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

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 #include "components/copresence/mediums/audio/audio_manager.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_util.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "components/copresence/mediums/audio/audio_player.h"
17 #include "components/copresence/mediums/audio/audio_recorder.h"
18 #include "components/copresence/public/copresence_constants.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "media/audio/audio_manager.h"
21 #include "media/audio/audio_manager_base.h"
22 #include "media/base/audio_bus.h"
23
24 namespace copresence {
25
26 namespace {
27
28 // UrlSafe is defined as:
29 // '/' represented by a '_' and '+' represented by a '-'
30 // TODO(rkc): Move this processing to the whispernet wrapper.
31 std::string FromUrlSafe(std::string token) {
32 base::ReplaceChars(token, "-", "+", &token);
33 base::ReplaceChars(token, "_", "/", &token);
34 return token;
35 }
36
37 const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes.
38 const int kMaxSamples = 10000;
39
40 } // namespace
41
42 // Public methods.
43
44 AudioManagerImpl::AudioManagerImpl()
45 : playing_{false, false},
Daniel Erat 2014/10/21 19:43:22 this is banned, at least at the moment: https://gr
rkc 2014/10/22 00:06:20 Done.
46 recording_{false, false},
47 player_{NULL, NULL},
48 recorder_(NULL) {
49 samples_cache_.resize(2);
50 samples_cache_[AUDIBLE] = new SamplesMap(
51 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples);
52 samples_cache_[INAUDIBLE] = new SamplesMap(
53 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples);
54 }
55
56 void AudioManagerImpl::Initialize(const DecodeSamplesCallback& decode_cb,
57 const EncodeTokenCallback& encode_cb) {
58 decode_cb_ = decode_cb;
59 encode_cb_ = encode_cb;
60
61 if (!player_[AUDIBLE])
62 player_[AUDIBLE] = new AudioPlayerImpl();
63 player_[AUDIBLE]->Initialize();
64
65 if (!player_[INAUDIBLE])
66 player_[INAUDIBLE] = new AudioPlayerImpl();
67 player_[INAUDIBLE]->Initialize();
68
69 decode_cancelable_cb_.Reset(base::Bind(
70 &AudioManagerImpl::DecodeSamplesConnector, base::Unretained(this)));
71 if (!recorder_)
72 recorder_ = new AudioRecorderImpl();
73 recorder_->Initialize(decode_cancelable_cb_.callback());
74 }
75
76 AudioManagerImpl::~AudioManagerImpl() {
77 if (player_[AUDIBLE])
78 player_[AUDIBLE]->Finalize();
79 if (player_[INAUDIBLE])
80 player_[INAUDIBLE]->Finalize();
81 if (recorder_)
82 recorder_->Finalize();
83 }
84
85 void AudioManagerImpl::StartPlaying(AudioType type) {
86 DCHECK(type == AUDIBLE || type == INAUDIBLE);
87 playing_[type] = true;
88 // If we don't have our token encoded yet, this check will be false, for now.
89 // Once our token is encoded, OnTokenEncoded will call UpdateToken, which
90 // will call this code again (if we're still supposed to be playing).
91 if (samples_cache_[type]->HasKey(token_[type]) &&
92 !player_[type]->IsPlaying()) {
93 DCHECK_GT(token_[type].size(), 0u);
94 player_[type]->Play(samples_cache_[type]->GetValue(token_[type]));
95 }
96 }
97
98 void AudioManagerImpl::StopPlaying(AudioType type) {
99 DCHECK(type == AUDIBLE || type == INAUDIBLE);
100 playing_[type] = false;
101 if (player_[type]->IsPlaying())
102 player_[type]->Stop();
103 }
104
105 void AudioManagerImpl::StartRecording(AudioType type) {
106 DCHECK(type == AUDIBLE || type == INAUDIBLE);
107 recording_[type] = true;
108 if (!recorder_->IsRecording())
109 recorder_->Record();
110 }
111
112 void AudioManagerImpl::StopRecording(AudioType type) {
113 DCHECK(type == AUDIBLE || type == INAUDIBLE);
114 recording_[type] = false;
115 if (recorder_->IsRecording())
116 recorder_->Stop();
117 }
118
119 void AudioManagerImpl::SetToken(AudioType type,
120 const std::string& url_unsafe_token) {
121 DCHECK(type == AUDIBLE || type == INAUDIBLE);
122 std::string token = FromUrlSafe(url_unsafe_token);
123 if (!samples_cache_[type]->HasKey(token)) {
124 // We're destructed by the destruction chain of
125 // RpcHandler->DirectiveHandler->AudioDirectiveHandler. The RpcHandler
126 // unsets any callbacks that were set on the Whispernet client before it
127 // destructs, unsetting this callback too - making unretained safe to use.
128 encode_cb_.Run(
129 token,
130 type,
131 base::Bind(&AudioManagerImpl::OnTokenEncoded, base::Unretained(this)));
132 } else {
133 UpdateToken(type, token);
134 }
135 }
136
137 const std::string& AudioManagerImpl::get_token(AudioType type) {
138 return token_[type];
139 }
Daniel Erat 2014/10/21 19:43:22 nit: add blank line
rkc 2014/10/22 00:06:20 Done.
140 bool AudioManagerImpl::IsRecording(AudioType type) {
141 return recording_[type];
142 }
Daniel Erat 2014/10/21 19:43:22 nit: add blank line
rkc 2014/10/22 00:06:20 Done.
143 bool AudioManagerImpl::IsPlaying(AudioType type) {
144 return playing_[type];
145 }
146
147 // Private methods.
148
149 void AudioManagerImpl::OnTokenEncoded(
150 const std::string& token,
151 AudioType type,
152 const scoped_refptr<media::AudioBusRefCounted>& samples) {
153 samples_cache_[type]->Add(token, samples);
154 UpdateToken(type, token);
155 }
156
157 void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) {
158 DCHECK(type == AUDIBLE || type == INAUDIBLE);
159 if (token_[type] == token)
160 return;
161
162 // Update token.
163 token_[type] = token;
164
165 // If we are supposed to be playing this token type at this moment, switch
166 // out playback with the new samples.
167 if (playing_[type]) {
168 if (player_[type]->IsPlaying())
169 player_[type]->Stop();
170 StartPlaying(type);
171 }
172 }
173
174 void AudioManagerImpl::DecodeSamplesConnector(const std::string& samples) {
175 AudioType decode_type = UNKNOWN;
176
177 if (recording_[AUDIBLE] && recording_[INAUDIBLE])
178 decode_type = BOTH;
179 else if (recording_[AUDIBLE])
180 decode_type = AUDIBLE;
181 else if (recording_[INAUDIBLE])
182 decode_type = INAUDIBLE;
183
184 decode_cb_.Run(decode_type, samples);
185 }
186
187 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698