| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_ | 5 #ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_ |
| 6 #define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_ | 6 #define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_ |
| 7 | 7 |
| 8 #include <map> | |
| 9 #include <queue> | |
| 10 #include <string> | 8 #include <string> |
| 11 #include <vector> | 9 #include <vector> |
| 12 | 10 |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/callback.h" | 11 #include "base/callback.h" |
| 15 #include "base/macros.h" | 12 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 19 #include "components/copresence/timed_map.h" | |
| 20 | 15 |
| 21 namespace media { | 16 namespace media { |
| 22 class AudioBusRefCounted; | 17 class AudioBusRefCounted; |
| 23 } | 18 } |
| 24 | 19 |
| 25 namespace copresence { | 20 namespace copresence { |
| 26 | 21 |
| 27 struct AudioDirective { | 22 struct AudioDirective { |
| 28 // Default ctor, required by the priority queue. | 23 // Default ctor, required by the priority queue. |
| 29 AudioDirective(); | 24 AudioDirective(); |
| 30 // ctor used to store transmit directives that are awaiting samples. | 25 AudioDirective(const std::string& op_id, base::Time end_time); |
| 31 AudioDirective(const std::string& token, | |
| 32 const std::string& op_id, | |
| 33 base::Time end_time); | |
| 34 // ctor used to construct a complete transmit directive. | |
| 35 AudioDirective(const std::string& token, | |
| 36 const std::string& op_id, | |
| 37 base::Time end_time, | |
| 38 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
| 39 ~AudioDirective(); | |
| 40 | 26 |
| 41 std::string token; | |
| 42 std::string op_id; | 27 std::string op_id; |
| 43 base::Time end_time; | 28 base::Time end_time; |
| 44 scoped_refptr<media::AudioBusRefCounted> samples; | |
| 45 }; | 29 }; |
| 46 | 30 |
| 47 // This class maintains a list of active audio directives. It fetches the audio | 31 // This class maintains a list of active audio directives. It fetches the audio |
| 48 // samples associated with a audio transmit directives and expires directives | 32 // samples associated with a audio transmit directives and expires directives |
| 49 // that have outlived their TTL. | 33 // that have outlived their TTL. |
| 50 // TODO(rkc): Once we implement more token technologies, move reusable code | 34 // TODO(rkc): Once we implement more token technologies, move reusable code |
| 51 // from here to a base class and inherit various XxxxDirectiveList | 35 // from here to a base class and inherit various XxxxDirectiveList |
| 52 // classes from it. | 36 // classes from it. |
| 53 class AudioDirectiveList { | 37 class AudioDirectiveList { |
| 54 public: | 38 public: |
| 55 typedef base::Callback<void(const std::string&, | 39 AudioDirectiveList(); |
| 56 bool, | |
| 57 const scoped_refptr<media::AudioBusRefCounted>&)> | |
| 58 SamplesCallback; | |
| 59 typedef base::Callback<void(const std::string&, bool, const SamplesCallback&)> | |
| 60 EncodeTokenCallback; | |
| 61 | |
| 62 AudioDirectiveList(const EncodeTokenCallback& encode_token_callback, | |
| 63 const base::Closure& token_added_callback, | |
| 64 bool use_audible_encoding); | |
| 65 virtual ~AudioDirectiveList(); | 40 virtual ~AudioDirectiveList(); |
| 66 | 41 |
| 67 // Adds a token to the token queue, after getting its corresponding samples | 42 void AddDirective(const std::string& op_id, base::TimeDelta ttl); |
| 68 // from whispernet. | 43 void RemoveDirective(const std::string& op_id); |
| 69 void AddTransmitDirective(const std::string& token, | |
| 70 const std::string& op_id, | |
| 71 base::TimeDelta ttl); | |
| 72 | 44 |
| 73 void AddReceiveDirective(const std::string& op_id, base::TimeDelta ttl); | 45 scoped_ptr<AudioDirective> GetActiveDirective(); |
| 74 | |
| 75 // Returns the next audio token to play. This also cleans up expired tokens. | |
| 76 scoped_ptr<AudioDirective> GetNextTransmit(); | |
| 77 scoped_ptr<AudioDirective> GetNextReceive(); | |
| 78 | |
| 79 // This is the method that the whispernet client needs to call to return | |
| 80 // samples to us. | |
| 81 void OnTokenEncoded(const std::string& token, | |
| 82 bool audible, | |
| 83 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
| 84 | 46 |
| 85 private: | 47 private: |
| 86 // Comparator for comparing end_times on audio tokens. | 48 // Comparator for comparing end_times on audio tokens. |
| 87 class LatestFirstComparator { | 49 class LatestFirstComparator { |
| 88 public: | 50 public: |
| 89 // This will sort our queue with the 'latest' time being the top. | 51 // This will sort our queue with the 'latest' time being the top. |
| 90 bool operator()(const AudioDirective& left, | 52 bool operator()(const AudioDirective& left, |
| 91 const AudioDirective& right) const { | 53 const AudioDirective& right) const { |
| 92 return left.end_time < right.end_time; | 54 return left.end_time < right.end_time; |
| 93 } | 55 } |
| 94 }; | 56 }; |
| 95 | 57 |
| 96 typedef std::priority_queue<AudioDirective, | 58 std::vector<AudioDirective>::iterator FindDirectiveByOpId( |
| 97 std::vector<AudioDirective>, | 59 const std::string& op_id); |
| 98 LatestFirstComparator> AudioDirectiveQueue; | |
| 99 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted> > | |
| 100 SamplesMap; | |
| 101 | 60 |
| 102 scoped_ptr<AudioDirective> GetNextFromList(AudioDirectiveQueue* list); | 61 // This vector will be organized as a heap with the latest time as the first |
| 103 | 62 // element. Only currently active directives will exist in this list. |
| 104 // A map of tokens that are awaiting their samples before we can | 63 std::vector<AudioDirective> active_directives_; |
| 105 // add them to the active transmit tokens list. | |
| 106 std::map<std::string, AudioDirective> pending_transmit_tokens_; | |
| 107 | |
| 108 AudioDirectiveQueue active_transmit_tokens_; | |
| 109 AudioDirectiveQueue active_receive_tokens_; | |
| 110 | |
| 111 EncodeTokenCallback encode_token_callback_; | |
| 112 base::Closure token_added_callback_; | |
| 113 const bool use_audible_encoding_; | |
| 114 | |
| 115 // Cache that holds the encoded samples. After reaching its limit, the cache | |
| 116 // expires the oldest samples first. | |
| 117 SamplesMap samples_cache_; | |
| 118 | 64 |
| 119 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList); | 65 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList); |
| 120 }; | 66 }; |
| 121 | 67 |
| 122 } // namespace copresence | 68 } // namespace copresence |
| 123 | 69 |
| 124 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_ | 70 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_ |
| OLD | NEW |