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 receive directives. | |
31 AudioDirective(const std::string& op_id, base::Time end_time); | |
32 // ctor used to store transmit directives that are awaiting samples. | |
33 AudioDirective(const std::string& token, | |
34 const std::string& op_id, | |
35 base::Time end_time); | |
36 // ctor used to construct a complete transmit directive. | |
37 AudioDirective(const std::string& token, | |
38 const std::string& op_id, | |
39 base::Time end_time, | |
40 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
41 virtual ~AudioDirective(); | |
Daniel Erat
2014/07/31 22:31:15
why virtual?
rkc
2014/08/01 21:08:56
No need to, changed.
Done.
| |
42 | |
43 std::string token; | |
44 std::string op_id; | |
45 base::Time end_time; | |
46 scoped_refptr<media::AudioBusRefCounted> samples; | |
47 }; | |
48 | |
49 // This class maintains a list of active audio directives. It fetches the audio | |
50 // samples associated with a audio transmit directives and expires directives | |
51 // that have outlived their TTL. | |
52 // TODO(rkc): Once we implement more token technologies, move reusable code | |
53 // from here to a base class and inherit various XxxxDirectiveList | |
54 // classes from it. | |
55 class AudioDirectiveList { | |
56 public: | |
57 typedef base::Callback< | |
58 void(const std::string&, const scoped_refptr<media::AudioBusRefCounted>&)> | |
59 SamplesCallback; | |
60 typedef base::Callback<void(const std::string&, const SamplesCallback&)> | |
61 EncodeTokenCallback; | |
62 | |
63 AudioDirectiveList(const EncodeTokenCallback& encode_token_callback, | |
64 const base::Closure& token_added_callback); | |
65 virtual ~AudioDirectiveList(); | |
66 | |
67 // Adds a token to the token queue, after getting its corresponding samples | |
68 // from whispernet. | |
69 void AddTransmitDirective(const std::string& token, | |
70 const std::string& op_id, | |
71 base::TimeDelta ttl); | |
72 | |
73 void AddReceiveDirective(const std::string& op_id, base::TimeDelta ttl); | |
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 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
83 | |
84 private: | |
85 // Comparator for comparing end_times on audio tokens. | |
86 class LatestFirstComparator { | |
87 public: | |
88 // This will sort our queue with the 'latest' time being the top. | |
89 bool operator()(const AudioDirective& left, | |
90 const AudioDirective& right) const { | |
91 return left.end_time < right.end_time; | |
92 } | |
93 }; | |
94 | |
95 typedef std::priority_queue<AudioDirective, | |
96 std::vector<AudioDirective>, | |
97 LatestFirstComparator> AudioDirectiveQueue; | |
98 typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted> > | |
99 SamplesMap; | |
100 | |
101 scoped_ptr<AudioDirective> GetNextFromList(AudioDirectiveQueue* list); | |
102 | |
103 // A map of tokens that are awaiting their samples before we can | |
104 // add them to the active transmit tokens list. | |
105 std::map<std::string, AudioDirective> pending_transmit_tokens_; | |
106 | |
107 AudioDirectiveQueue active_transmit_tokens_; | |
108 AudioDirectiveQueue active_receive_tokens_; | |
109 | |
110 EncodeTokenCallback encode_token_callback_; | |
111 | |
112 base::Closure token_added_callback_; | |
113 | |
114 // Cache that holds 10k encoded samples. After reaching its limit, the cache | |
115 // expires the oldest samples first. | |
116 SamplesMap samples_cache_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList); | |
119 }; | |
120 | |
121 } // namespace copresence | |
122 | |
123 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_ | |
OLD | NEW |