Index: components/copresence/handlers/audio/audio_directive_handler.cc |
diff --git a/components/copresence/handlers/audio/audio_directive_handler.cc b/components/copresence/handlers/audio/audio_directive_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b367c3b33ee109e69d0543befd93ad4b6541ee85 |
--- /dev/null |
+++ b/components/copresence/handlers/audio/audio_directive_handler.cc |
@@ -0,0 +1,113 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/copresence/handlers/audio/audio_directive_handler.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
xiyuan
2014/07/25 21:02:08
nit: not used?
rkc
2014/07/28 21:02:01
Done.
|
+#include "base/time/time.h" |
+#include "components/copresence/mediums/audio/audio_player.h" |
+#include "components/copresence/mediums/audio/audio_recorder.h" |
+#include "components/copresence/proto/data.pb.h" |
+#include "media/base/audio_bus.h" |
+ |
+namespace copresence { |
+ |
+// Public methods. |
+ |
+AudioDirectiveHandler::AudioDirectiveHandler( |
+ const AudioRecorder::DecodeSamplesCallback& decode_cb, |
+ const AudioDirectiveList::EncodeTokenCallback& encode_cb) |
+ : directive_list_(encode_cb, |
+ base::Bind(&AudioDirectiveHandler::ExecuteNextTransmit, |
xiyuan
2014/07/25 21:02:09
nit: #include "base/bind.h"
rkc
2014/07/28 21:02:00
Done.
|
+ base::Unretained(this))), |
+ player_(new AudioPlayer()), |
+ recorder_(new AudioRecorder(decode_cb)) { |
+} |
+ |
+AudioDirectiveHandler::AudioDirectiveHandler( |
+ const AudioDirectiveList::EncodeTokenCallback& encode_cb) |
+ : directive_list_(encode_cb, |
+ base::Bind(&AudioDirectiveHandler::ExecuteNextTransmit, |
+ base::Unretained(this))), |
+ player_(NULL), |
+ recorder_(NULL) { |
+} |
+ |
+AudioDirectiveHandler::~AudioDirectiveHandler() { |
+ if (player_) |
+ player_->Finalize(); |
+ if (recorder_) |
+ recorder_->Finalize(); |
+} |
+ |
+void AudioDirectiveHandler::AddInstruction( |
+ const copresence::TokenInstruction& instruction, |
Daniel Erat
2014/07/25 23:09:01
isn't all this code already in the copresence name
rkc
2014/07/28 21:02:00
Yep, whoops. This is a vestige of the open sourcin
|
+ base::TimeDelta ttl) { |
+ switch (instruction.token_instruction_type()) { |
+ case copresence::TRANSMIT: |
Daniel Erat
2014/07/25 23:09:01
please rename these enums to be more descriptive,
rkc
2014/07/28 21:02:01
The enums are created by the protoc compiler. We p
|
+ DVLOG(2) << "Audio Transmit Directive received. Token: " |
+ << instruction.token_id() |
+ << " with TTL=" << ttl.InMilliseconds(); |
+ // TODO(rkc): Fill in the op_id once we get it from the directive. |
+ directive_list_.AddTransmitDirective( |
+ instruction.token_id(), std::string(), ttl); |
+ break; |
+ case copresence::RECEIVE: |
+ DVLOG(2) << "Audio Receive Directive received. TTL=" |
+ << ttl.InMilliseconds(); |
+ // TODO(rkc): Fill in the op_id once we get it from the directive. |
+ directive_list_.AddReceiveDirective(std::string(), ttl); |
+ break; |
+ case copresence::UNKNOWN_TOKEN_INSTRUCTION_TYPE: |
+ default: |
xiyuan
2014/07/25 21:02:09
nit: If we don't use "default" here, the compiler
Daniel Erat
2014/07/25 23:09:01
yeah, but then the code won't be able to detect in
rkc
2014/07/28 21:02:00
Done.
rkc
2014/07/28 21:02:01
So.. I'll keep this as is then :)
|
+ LOG(WARNING) << "Unknown Audio Transmit Directive received."; |
+ break; |
+ } |
+ // ExecuteNextTransmit will be called by the directive_list_ when Add is done. |
Daniel Erat
2014/07/25 23:09:01
s/the directive_list_/|directive_list_|/
rkc
2014/07/28 21:02:00
Done.
|
+ ExecuteNextReceive(); |
+} |
+ |
+// Private methods. |
+ |
+void AudioDirectiveHandler::ExecuteNextTransmit() { |
+ scoped_ptr<AudioDirective> transmit(directive_list_.GetNextTransmit()); |
+ if (transmit.get()) |
xiyuan
2014/07/25 21:02:09
nit: get rid of ".get()"
rkc
2014/07/28 21:02:01
Done.
|
+ PlayAudio(transmit->samples, transmit->end_time - base::Time::Now()); |
+} |
+ |
+void AudioDirectiveHandler::ExecuteNextReceive() { |
+ scoped_ptr<AudioDirective> receive(directive_list_.GetNextReceive()); |
+ if (receive.get()) |
xiyuan
2014/07/25 21:02:08
nit: get rid of ".get()"
rkc
2014/07/28 21:02:00
Done.
|
+ RecordAudio(receive->end_time - base::Time::Now()); |
+} |
+ |
+void AudioDirectiveHandler::PlayAudio( |
Daniel Erat
2014/07/25 23:09:01
please reorder the methods in the .cc file to matc
rkc
2014/07/28 21:02:00
Done.
|
+ const scoped_refptr<media::AudioBusRefCounted>& samples, |
+ base::TimeDelta duration) { |
+ player_->Play(samples); |
+ stop_playback_timer_.Start( |
+ FROM_HERE, duration, this, &AudioDirectiveHandler::StopPlayback); |
+} |
+ |
+void AudioDirectiveHandler::RecordAudio(base::TimeDelta duration) { |
+ recorder_->Record(); |
+ stop_recording_timer_.Start( |
+ FROM_HERE, duration, this, &AudioDirectiveHandler::StopRecording); |
+} |
+ |
+void AudioDirectiveHandler::StopPlayback() { |
+ player_->Stop(); |
+ DVLOG(2) << "Done playing audio."; |
+ ExecuteNextTransmit(); |
+} |
+ |
+void AudioDirectiveHandler::StopRecording() { |
+ recorder_->Stop(); |
+ DVLOG(2) << "Done recording audio."; |
+ ExecuteNextReceive(); |
+} |
+ |
+} // namespace copresence |