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/handlers/audio/audio_directive_handler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" |
| 11 #include "components/copresence/mediums/audio/audio_player.h" |
| 12 #include "components/copresence/mediums/audio/audio_recorder.h" |
| 13 #include "components/copresence/proto/data.pb.h" |
| 14 #include "media/base/audio_bus.h" |
| 15 |
| 16 namespace copresence { |
| 17 |
| 18 // Public methods. |
| 19 |
| 20 AudioDirectiveHandler::AudioDirectiveHandler( |
| 21 const AudioRecorder::DecodeSamplesCallback& decode_cb, |
| 22 const AudioDirectiveList::EncodeTokenCallback& encode_cb) |
| 23 : directive_list_(encode_cb, |
| 24 base::Bind(&AudioDirectiveHandler::ExecuteNextTransmit, |
| 25 base::Unretained(this))), |
| 26 player_(NULL), |
| 27 recorder_(NULL), |
| 28 decode_cb_(decode_cb) { |
| 29 } |
| 30 |
| 31 AudioDirectiveHandler::~AudioDirectiveHandler() { |
| 32 if (player_) |
| 33 player_->Finalize(); |
| 34 if (recorder_) |
| 35 recorder_->Finalize(); |
| 36 } |
| 37 |
| 38 void AudioDirectiveHandler::Initialize() { |
| 39 player_ = new AudioPlayer(); |
| 40 player_->Initialize(); |
| 41 |
| 42 recorder_ = new AudioRecorder(decode_cb_); |
| 43 recorder_->Initialize(); |
| 44 } |
| 45 |
| 46 void AudioDirectiveHandler::AddInstruction(const TokenInstruction& instruction, |
| 47 base::TimeDelta ttl) { |
| 48 switch (instruction.token_instruction_type()) { |
| 49 case TRANSMIT: |
| 50 DVLOG(2) << "Audio Transmit Directive received. Token: " |
| 51 << instruction.token_id() |
| 52 << " with TTL=" << ttl.InMilliseconds(); |
| 53 // TODO(rkc): Fill in the op_id once we get it from the directive. |
| 54 directive_list_.AddTransmitDirective( |
| 55 instruction.token_id(), std::string(), ttl); |
| 56 break; |
| 57 case RECEIVE: |
| 58 DVLOG(2) << "Audio Receive Directive received. TTL=" |
| 59 << ttl.InMilliseconds(); |
| 60 // TODO(rkc): Fill in the op_id once we get it from the directive. |
| 61 directive_list_.AddReceiveDirective(std::string(), ttl); |
| 62 break; |
| 63 case UNKNOWN_TOKEN_INSTRUCTION_TYPE: |
| 64 default: |
| 65 LOG(WARNING) << "Unknown Audio Transmit Directive received."; |
| 66 } |
| 67 // ExecuteNextTransmit will be called by directive_list_ when Add is done. |
| 68 ExecuteNextReceive(); |
| 69 } |
| 70 |
| 71 // Protected methods. |
| 72 |
| 73 void AudioDirectiveHandler::PlayAudio( |
| 74 const scoped_refptr<media::AudioBusRefCounted>& samples, |
| 75 base::TimeDelta duration) { |
| 76 player_->Play(samples); |
| 77 stop_playback_timer_.Start( |
| 78 FROM_HERE, duration, this, &AudioDirectiveHandler::StopPlayback); |
| 79 } |
| 80 |
| 81 void AudioDirectiveHandler::RecordAudio(base::TimeDelta duration) { |
| 82 recorder_->Record(); |
| 83 stop_recording_timer_.Start( |
| 84 FROM_HERE, duration, this, &AudioDirectiveHandler::StopRecording); |
| 85 } |
| 86 |
| 87 // Private methods. |
| 88 |
| 89 void AudioDirectiveHandler::StopPlayback() { |
| 90 player_->Stop(); |
| 91 DVLOG(2) << "Done playing audio."; |
| 92 ExecuteNextTransmit(); |
| 93 } |
| 94 |
| 95 void AudioDirectiveHandler::StopRecording() { |
| 96 recorder_->Stop(); |
| 97 DVLOG(2) << "Done recording audio."; |
| 98 ExecuteNextReceive(); |
| 99 } |
| 100 |
| 101 void AudioDirectiveHandler::ExecuteNextTransmit() { |
| 102 scoped_ptr<AudioDirective> transmit(directive_list_.GetNextTransmit()); |
| 103 if (transmit) |
| 104 PlayAudio(transmit->samples, transmit->end_time - base::Time::Now()); |
| 105 } |
| 106 |
| 107 void AudioDirectiveHandler::ExecuteNextReceive() { |
| 108 scoped_ptr<AudioDirective> receive(directive_list_.GetNextReceive()); |
| 109 if (receive) |
| 110 RecordAudio(receive->end_time - base::Time::Now()); |
| 111 } |
| 112 |
| 113 } // namespace copresence |
OLD | NEW |