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" | |
xiyuan
2014/07/25 21:02:08
nit: not used?
rkc
2014/07/28 21:02:01
Done.
| |
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, | |
xiyuan
2014/07/25 21:02:09
nit: #include "base/bind.h"
rkc
2014/07/28 21:02:00
Done.
| |
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, | |
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
| |
48 base::TimeDelta ttl) { | |
49 switch (instruction.token_instruction_type()) { | |
50 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
| |
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: | |
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 :)
| |
66 LOG(WARNING) << "Unknown Audio Transmit Directive received."; | |
67 break; | |
68 } | |
69 // 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.
| |
70 ExecuteNextReceive(); | |
71 } | |
72 | |
73 // Private methods. | |
74 | |
75 void AudioDirectiveHandler::ExecuteNextTransmit() { | |
76 scoped_ptr<AudioDirective> transmit(directive_list_.GetNextTransmit()); | |
77 if (transmit.get()) | |
xiyuan
2014/07/25 21:02:09
nit: get rid of ".get()"
rkc
2014/07/28 21:02:01
Done.
| |
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()) | |
xiyuan
2014/07/25 21:02:08
nit: get rid of ".get()"
rkc
2014/07/28 21:02:00
Done.
| |
84 RecordAudio(receive->end_time - base::Time::Now()); | |
85 } | |
86 | |
87 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.
| |
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 |