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