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

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},
Daniel Erat 2014/10/17 22:26:00 has this style been approved?
rkc 2014/10/18 00:21:55 I thought this works with the C++03 standard onwar
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 AudioPlayer();
63 player_[AUDIBLE]->Initialize();
64
65 if (!player_[INAUDIBLE])
66 player_[INAUDIBLE] = new AudioPlayer();
67 player_[INAUDIBLE]->Initialize();
68
69 decode_cancelable_cb_.Reset(base::Bind(&AudioManager::DecodeSamplesConnector,
70 base::Unretained(this)));
71 if (!recorder_)
72 recorder_ = new AudioRecorder();
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_LT(type, BOTH);
87 playing_[type] = true;
88 if (samples_cache_[type]->HasKey(token_[type]) &&
Charlie 2014/10/17 22:42:26 This looks like silent failure. Add a comment expl
rkc 2014/10/18 00:21:55 Done.
89 !player_[type]->IsPlaying()) {
90 DCHECK_GT(token_[type].size(), 0u);
91 player_[type]->Play(samples_cache_[type]->GetValue(token_[type]));
92 }
93 }
94
95 void AudioManager::StopPlaying(AudioType type) {
96 DCHECK_LT(type, BOTH);
97 playing_[type] = false;
98 if (player_[type]->IsPlaying())
99 player_[type]->Stop();
100 }
101
102 void AudioManager::StartRecording(AudioType type) {
103 DCHECK_LT(type, BOTH);
104 recording_[type] = true;
105 if (!recorder_->IsRecording())
106 recorder_->Record();
107 }
108
109 void AudioManager::StopRecording(AudioType type) {
110 DCHECK_LT(type, BOTH);
111 recording_[type] = false;
112 if (recorder_->IsRecording())
113 recorder_->Stop();
114 }
115
116 void AudioManager::SetToken(AudioType type,
117 const std::string& url_unsafe_token) {
118 DCHECK_LT(type, BOTH);
Charlie 2014/10/17 22:42:25 Why not DCHECK_NE?
rkc 2014/10/18 00:21:55 We specifically want to check that the type is not
Charlie 2014/10/18 00:44:11 Ah, I'm used to the proto convention where UNKNOWN
rkc 2014/10/18 00:51:30 Done.
119 std::string token = FromUrlSafe(url_unsafe_token);
120 if (!samples_cache_[type]->HasKey(token)) {
121 encode_cb_.Run(
122 token,
123 type,
124 base::Bind(&AudioManager::OnTokenEncoded, base::Unretained(this)));
125 } else {
126 UpdateToken(type, token);
127 }
128 }
129
130 // Private methods.
131
132 void AudioManager::OnTokenEncoded(
133 const std::string& token,
134 AudioType type,
135 const scoped_refptr<media::AudioBusRefCounted>& samples) {
136 samples_cache_[type]->Add(token, samples);
137 UpdateToken(type, token);
138 }
139
140 void AudioManager::UpdateToken(AudioType type, const std::string& token) {
141 DCHECK_LT(type, BOTH);
142 if (token_[type] == token)
143 return;
144
145 // Update token.
146 token_[type] = token;
147
148 // If we are supposed to be playing this token type at this moment, switch
149 // out playback with the new samples.
150 if (playing_[type]) {
151 if (player_[type]->IsPlaying())
152 player_[type]->Stop();
153 StartPlaying(type);
154 }
155 }
156
157 void AudioManager::DecodeSamplesConnector(const std::string& samples) {
158 AudioType decode_type = UNKNOWN;
159
160 if (recording_[AUDIBLE])
161 decode_type = AUDIBLE;
162 if (recording_[INAUDIBLE])
163 decode_type = INAUDIBLE;
164 if (recording_[AUDIBLE] && recording_[INAUDIBLE])
Daniel Erat 2014/10/17 22:26:00 overwriting the previous values looks a bit weird.
Charlie 2014/10/17 22:42:26 Or: if (recording_[AUDIBLE] && recording_[INAUDIB
rkc 2014/10/18 00:21:55 Done.
rkc 2014/10/18 00:21:55 Done.
165 decode_type = BOTH;
166
167 decode_cb_.Run(decode_type, samples);
168 }
169
170 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698