| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/copresence/handlers/audio/audio_directive_handler.h" | 5 #include "components/copresence/handlers/audio/audio_directive_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 const std::string& op_id, | 74 const std::string& op_id, |
| 75 base::TimeDelta ttl) { | 75 base::TimeDelta ttl) { |
| 76 switch (instruction.token_instruction_type()) { | 76 switch (instruction.token_instruction_type()) { |
| 77 case TRANSMIT: | 77 case TRANSMIT: |
| 78 DVLOG(2) << "Audio Transmit Directive received. Token: " | 78 DVLOG(2) << "Audio Transmit Directive received. Token: " |
| 79 << instruction.token_id() | 79 << instruction.token_id() |
| 80 << " with TTL=" << ttl.InMilliseconds(); | 80 << " with TTL=" << ttl.InMilliseconds(); |
| 81 switch (instruction.medium()) { | 81 switch (instruction.medium()) { |
| 82 case AUDIO_ULTRASOUND_PASSBAND: | 82 case AUDIO_ULTRASOUND_PASSBAND: |
| 83 transmits_list_inaudible_.AddDirective(op_id, ttl); | 83 transmits_list_inaudible_.AddDirective(op_id, ttl); |
| 84 HandleToken(instruction.token_id(), false); | 84 PlayToken(instruction.token_id(), false); |
| 85 break; | 85 break; |
| 86 case AUDIO_AUDIBLE_DTMF: | 86 case AUDIO_AUDIBLE_DTMF: |
| 87 transmits_list_audible_.AddDirective(op_id, ttl); | 87 transmits_list_audible_.AddDirective(op_id, ttl); |
| 88 HandleToken(instruction.token_id(), true); | 88 PlayToken(instruction.token_id(), true); |
| 89 break; | 89 break; |
| 90 default: | 90 default: |
| 91 NOTREACHED(); | 91 NOTREACHED(); |
| 92 } | 92 } |
| 93 break; | 93 break; |
| 94 case RECEIVE: | 94 case RECEIVE: |
| 95 DVLOG(2) << "Audio Receive Directive received. TTL=" | 95 DVLOG(2) << "Audio Receive Directive received. TTL=" |
| 96 << ttl.InMilliseconds(); | 96 << ttl.InMilliseconds(); |
| 97 receives_list_.AddDirective(op_id, ttl); | 97 receives_list_.AddDirective(op_id, ttl); |
| 98 ProcessNextReceive(); |
| 98 break; | 99 break; |
| 99 case UNKNOWN_TOKEN_INSTRUCTION_TYPE: | 100 case UNKNOWN_TOKEN_INSTRUCTION_TYPE: |
| 100 default: | 101 default: |
| 101 LOG(WARNING) << "Unknown Audio Transmit Directive received."; | 102 LOG(WARNING) << "Unknown Audio Transmit Directive received."; |
| 102 } | 103 } |
| 103 // ExecuteNextTransmit will be called by directive_list_ when Add is done. | |
| 104 ProcessNextReceive(); | |
| 105 } | 104 } |
| 106 | 105 |
| 107 void AudioDirectiveHandler::RemoveInstructions(const std::string& op_id) { | 106 void AudioDirectiveHandler::RemoveInstructions(const std::string& op_id) { |
| 108 transmits_list_audible_.RemoveDirective(op_id); | 107 transmits_list_audible_.RemoveDirective(op_id); |
| 109 transmits_list_inaudible_.RemoveDirective(op_id); | 108 transmits_list_inaudible_.RemoveDirective(op_id); |
| 110 receives_list_.RemoveDirective(op_id); | 109 receives_list_.RemoveDirective(op_id); |
| 111 | 110 |
| 112 ProcessNextTransmit(); | 111 ProcessNextTransmit(); |
| 113 ProcessNextReceive(); | 112 ProcessNextReceive(); |
| 114 } | 113 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 receive->end_time - base::Time::Now(), | 167 receive->end_time - base::Time::Now(), |
| 169 this, | 168 this, |
| 170 &AudioDirectiveHandler::ProcessNextReceive); | 169 &AudioDirectiveHandler::ProcessNextReceive); |
| 171 } else if (!receive && recorder_->IsRecording()) { | 170 } else if (!receive && recorder_->IsRecording()) { |
| 172 DVLOG(3) << "Stopping Recording"; | 171 DVLOG(3) << "Stopping Recording"; |
| 173 stop_recording_timer_.Stop(); | 172 stop_recording_timer_.Stop(); |
| 174 recorder_->Stop(); | 173 recorder_->Stop(); |
| 175 } | 174 } |
| 176 } | 175 } |
| 177 | 176 |
| 178 void AudioDirectiveHandler::HandleToken(const std::string token, bool audible) { | 177 void AudioDirectiveHandler::PlayToken(const std::string token, bool audible) { |
| 179 std::string valid_token = FromUrlSafe(token); | 178 std::string valid_token = FromUrlSafe(token); |
| 180 | 179 |
| 180 // If the token has been encoded already, use the cached samples. |
| 181 if (audible && samples_cache_audible_.HasKey(valid_token)) { | 181 if (audible && samples_cache_audible_.HasKey(valid_token)) { |
| 182 current_token_audible_ = token; | 182 current_token_audible_ = token; |
| 183 ProcessNextTransmit(); | 183 ProcessNextTransmit(); |
| 184 return; | 184 } else if (!audible && samples_cache_inaudible_.HasKey(valid_token)) { |
| 185 } | |
| 186 | |
| 187 if (!audible && samples_cache_inaudible_.HasKey(valid_token)) { | |
| 188 current_token_inaudible_ = token; | 185 current_token_inaudible_ = token; |
| 189 ProcessNextTransmit(); | 186 ProcessNextTransmit(); |
| 190 return; | 187 } else { |
| 188 // Otherwise, encode the token and then play it. |
| 189 encode_cb_.Run(valid_token, |
| 190 audible, |
| 191 base::Bind(&AudioDirectiveHandler::PlayEncodedToken, |
| 192 base::Unretained(this))); |
| 191 } | 193 } |
| 192 | |
| 193 encode_cb_.Run(valid_token, | |
| 194 audible, | |
| 195 base::Bind(&AudioDirectiveHandler::OnTokenEncoded, | |
| 196 base::Unretained(this))); | |
| 197 } | 194 } |
| 198 | 195 |
| 199 void AudioDirectiveHandler::OnTokenEncoded( | 196 void AudioDirectiveHandler::PlayEncodedToken( |
| 200 const std::string& token, | 197 const std::string& token, |
| 201 bool audible, | 198 bool audible, |
| 202 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 199 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 203 DVLOG(3) << "Token: " << token << "[audible:" << audible << "] encoded."; | 200 DVLOG(3) << "Token " << token << "[audible:" << audible << "] encoded."; |
| 204 if (audible) { | 201 if (audible) { |
| 205 samples_cache_audible_.Add(token, samples); | 202 samples_cache_audible_.Add(token, samples); |
| 206 current_token_audible_ = token; | 203 current_token_audible_ = token; |
| 207 // Force process transmits to pick up the new token. | 204 // Force process transmits to pick up the new token. |
| 208 if (player_audible_->IsPlaying()) | 205 if (player_audible_->IsPlaying()) |
| 209 player_audible_->Stop(); | 206 player_audible_->Stop(); |
| 210 } else { | 207 } else { |
| 211 samples_cache_inaudible_.Add(token, samples); | 208 samples_cache_inaudible_.Add(token, samples); |
| 212 current_token_inaudible_ = token; | 209 current_token_inaudible_ = token; |
| 213 // Force process transmits to pick up the new token. | 210 // Force process transmits to pick up the new token. |
| 214 if (player_inaudible_->IsPlaying()) | 211 if (player_inaudible_->IsPlaying()) |
| 215 player_inaudible_->Stop(); | 212 player_inaudible_->Stop(); |
| 216 } | 213 } |
| 217 | 214 |
| 218 ProcessNextTransmit(); | 215 ProcessNextTransmit(); |
| 219 } | 216 } |
| 220 | 217 |
| 221 } // namespace copresence | 218 } // namespace copresence |
| OLD | NEW |