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