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.
5 #ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_HANDLER_H_
6 #define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_HANDLER_H_
10 #include "base/basictypes.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "components/copresence/handlers/audio/audio_directive_list.h"
16 #include "components/copresence/mediums/audio/audio_recorder.h"
17 #include "components/copresence/proto/data.pb.h"
18 #include "components/copresence/timed_map.h"
21 class AudioBusRefCounted;
24 namespace copresence {
28 // The AudioDirectiveHandler handles audio transmit and receive instructions.
29 // TODO(rkc): Currently since WhispernetClient can only have one token encoded
30 // callback at a time, we need to have both the audible and inaudible in this
31 // class. Investigate a better way to do this; a few options are abstracting
32 // out token encoding to a separate class, or allowing whispernet to have
33 // multiple callbacks for encoded tokens being sent back and have two versions
35 class AudioDirectiveHandler {
37 typedef base::Callback<void(const std::string&,
39 const scoped_refptr<media::AudioBusRefCounted>&)>
41 typedef base::Callback<void(const std::string&, bool, const SamplesCallback&)>
44 AudioDirectiveHandler(
45 const AudioRecorder::DecodeSamplesCallback& decode_cb,
46 const AudioDirectiveHandler::EncodeTokenCallback& encode_cb);
47 virtual ~AudioDirectiveHandler();
49 // Do not use this class before calling this.
52 // Adds an instruction to our handler. The instruction will execute and be
53 // removed after the ttl expires.
54 void AddInstruction(const copresence::TokenInstruction& instruction,
55 const std::string& op_id,
56 base::TimeDelta ttl_ms);
58 // Removes all instructions associated with this operation id.
59 void RemoveInstructions(const std::string& op_id);
61 // Returns the currently playing DTMF token.
62 const std::string& PlayingAudibleToken() const {
63 return current_token_audible_;
66 // Returns the currently playing DSSS token.
67 const std::string& PlayingInaudibleToken() const {
68 return current_token_inaudible_;
71 void set_player_audible_for_testing(AudioPlayer* player) {
72 player_audible_ = player;
75 void set_player_inaudible_for_testing(AudioPlayer* player) {
76 player_inaudible_ = player;
79 void set_recorder_for_testing(AudioRecorder* recorder) {
84 FRIEND_TEST_ALL_PREFIXES(AudioDirectiveHandlerTest, Basic);
86 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted>>
89 // Processes the next active transmit instruction.
90 void ProcessNextTransmit();
91 // Processes the next active receive instruction.
92 void ProcessNextReceive();
94 void PlayToken(const std::string token, bool audible);
96 // This is the method that the whispernet client needs to call to return
98 void PlayEncodedToken(
99 const std::string& token,
101 const scoped_refptr<media::AudioBusRefCounted>& samples);
103 AudioDirectiveList transmits_list_audible_;
104 AudioDirectiveList transmits_list_inaudible_;
105 AudioDirectiveList receives_list_;
107 // Currently playing tokens.
108 std::string current_token_audible_;
109 std::string current_token_inaudible_;
111 // AudioPlayer and AudioRecorder objects are self-deleting. When we call
112 // Finalize on them, they clean themselves up on the Audio thread.
113 AudioPlayer* player_audible_;
114 AudioPlayer* player_inaudible_;
115 AudioRecorder* recorder_;
117 AudioRecorder::DecodeSamplesCallback decode_cb_;
118 EncodeTokenCallback encode_cb_;
120 base::OneShotTimer<AudioDirectiveHandler> stop_audible_playback_timer_;
121 base::OneShotTimer<AudioDirectiveHandler> stop_inaudible_playback_timer_;
122 base::OneShotTimer<AudioDirectiveHandler> stop_recording_timer_;
124 // Cache that holds the encoded samples. After reaching its limit, the cache
125 // expires the oldest samples first.
126 SamplesMap samples_cache_audible_;
127 SamplesMap samples_cache_inaudible_;
129 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveHandler);
132 } // namespace copresence
134 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_HANDLER_H_