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

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 AudioManager::AudioManager()
45 : playing_{false, false},
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 AudioManager::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(&AudioManager::DecodeSamplesConnector,
70 base::Unretained(this)));
71 if (!recorder_)
72 recorder_ = new AudioRecorderImpl();
73 recorder_->Initialize(decode_cancelable_cb_.callback());
74 }
75
76 AudioManager::~AudioManager() {
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 AudioManager::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 AudioManager::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 AudioManager::StartRecording(AudioType type) {
106 DCHECK(type == AUDIBLE || type == INAUDIBLE);
107 recording_[type] = true;
108 if (!recorder_->IsRecording())
109 recorder_->Record();
110 }
111
112 void AudioManager::StopRecording(AudioType type) {
113 DCHECK(type == AUDIBLE || type == INAUDIBLE);
114 recording_[type] = false;
115 if (recorder_->IsRecording())
116 recorder_->Stop();
117 }
118
119 void AudioManager::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 encode_cb_.Run(
125 token,
126 type,
127 base::Bind(&AudioManager::OnTokenEncoded, base::Unretained(this)));
xiyuan 2014/10/20 19:26:46 base::Unretained makes me nervous. Could we use We
rkc 2014/10/20 20:17:53 This cancelable callback will get automatically ca
xiyuan 2014/10/20 21:29:29 Sorry if it is a dumb question. Why base::Bind() h
rkc 2014/10/21 01:25:04 Really not my best day. This "should" be cancelabl
128 } else {
129 UpdateToken(type, token);
130 }
131 }
132
133 // Private methods.
134
135 void AudioManager::OnTokenEncoded(
136 const std::string& token,
137 AudioType type,
138 const scoped_refptr<media::AudioBusRefCounted>& samples) {
139 samples_cache_[type]->Add(token, samples);
140 UpdateToken(type, token);
141 }
142
143 void AudioManager::UpdateToken(AudioType type, const std::string& token) {
144 DCHECK(type == AUDIBLE || type == INAUDIBLE);
145 if (token_[type] == token)
146 return;
147
148 // Update token.
149 token_[type] = token;
150
151 // If we are supposed to be playing this token type at this moment, switch
152 // out playback with the new samples.
153 if (playing_[type]) {
154 if (player_[type]->IsPlaying())
155 player_[type]->Stop();
156 StartPlaying(type);
157 }
158 }
159
160 void AudioManager::DecodeSamplesConnector(const std::string& samples) {
161 AudioType decode_type = UNKNOWN;
162
163 if (recording_[AUDIBLE] && recording_[INAUDIBLE])
164 decode_type = BOTH;
165 else if (recording_[AUDIBLE])
166 decode_type = AUDIBLE;
167 else if (recording_[INAUDIBLE])
168 decode_type = INAUDIBLE;
169
170 decode_cb_.Run(decode_type, samples);
171 }
172
173 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698