Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: components/copresence/handlers/audio/audio_directive_handler.cc

Issue 419073002: Add the copresence DirectiveHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_(new AudioPlayer()),
27 recorder_(new AudioRecorder(decode_cb)) {
28 player_->Initialize();
29 recorder_->Initialize();
30 }
31
32 AudioDirectiveHandler::~AudioDirectiveHandler() {
33 if (player_)
34 player_->Finalize();
35 if (recorder_)
36 recorder_->Finalize();
37 }
38
39 void AudioDirectiveHandler::AddInstruction(const TokenInstruction& instruction,
40 base::TimeDelta ttl) {
41 switch (instruction.token_instruction_type()) {
42 case TRANSMIT:
43 DVLOG(2) << "Audio Transmit Directive received. Token: "
44 << instruction.token_id()
45 << " with TTL=" << ttl.InMilliseconds();
46 // TODO(rkc): Fill in the op_id once we get it from the directive.
47 directive_list_.AddTransmitDirective(
48 instruction.token_id(), std::string(), ttl);
49 break;
50 case RECEIVE:
51 DVLOG(2) << "Audio Receive Directive received. TTL="
52 << ttl.InMilliseconds();
53 // TODO(rkc): Fill in the op_id once we get it from the directive.
54 directive_list_.AddReceiveDirective(std::string(), ttl);
55 break;
56 case UNKNOWN_TOKEN_INSTRUCTION_TYPE:
57 default:
58 LOG(WARNING) << "Unknown Audio Transmit Directive received.";
59 }
60 // ExecuteNextTransmit will be called by directive_list_ when Add is done.
61 ExecuteNextReceive();
62 }
63
64 // Protected methods.
65
66 AudioDirectiveHandler::AudioDirectiveHandler(
67 const AudioDirectiveList::EncodeTokenCallback& encode_cb)
68 : directive_list_(encode_cb,
69 base::Bind(&AudioDirectiveHandler::ExecuteNextTransmit,
70 base::Unretained(this))),
71 player_(NULL),
72 recorder_(NULL) {
73 }
74
75 void AudioDirectiveHandler::PlayAudio(
76 const scoped_refptr<media::AudioBusRefCounted>& samples,
77 base::TimeDelta duration) {
78 player_->Play(samples);
Daniel Erat 2014/07/31 22:31:14 should you be checking player_ and recorder_ for n
rkc 2014/08/01 21:08:56 Nope. In tests (the only time we have the player_
79 stop_playback_timer_.Start(
80 FROM_HERE, duration, this, &AudioDirectiveHandler::StopPlayback);
81 }
82
83 void AudioDirectiveHandler::RecordAudio(base::TimeDelta duration) {
84 recorder_->Record();
85 stop_recording_timer_.Start(
86 FROM_HERE, duration, this, &AudioDirectiveHandler::StopRecording);
87 }
88
89 // Private methods.
90
91 void AudioDirectiveHandler::StopPlayback() {
92 player_->Stop();
93 DVLOG(2) << "Done playing audio.";
94 ExecuteNextTransmit();
95 }
96
97 void AudioDirectiveHandler::StopRecording() {
98 recorder_->Stop();
99 DVLOG(2) << "Done recording audio.";
100 ExecuteNextReceive();
101 }
102
103 void AudioDirectiveHandler::ExecuteNextTransmit() {
104 scoped_ptr<AudioDirective> transmit(directive_list_.GetNextTransmit());
105 if (transmit)
106 PlayAudio(transmit->samples, transmit->end_time - base::Time::Now());
107 }
108
109 void AudioDirectiveHandler::ExecuteNextReceive() {
110 scoped_ptr<AudioDirective> receive(directive_list_.GetNextReceive());
111 if (receive)
112 RecordAudio(receive->end_time - base::Time::Now());
113 }
114
115 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698