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 #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 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(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))); |
| 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 |
OLD | NEW |