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