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