Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(229)

Side by Side Diff: components/copresence/handlers/audio/audio_directive_list.h

Issue 690213004: Refactoring AudioDirectiveHandler to support testing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixing crash Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_H_ 5 #ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_
6 #define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_ 6 #define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h" 11 #include "base/callback_forward.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/default_tick_clock.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "components/copresence/handlers/audio/tick_clock_ref_counted.h"
16 18
17 namespace media { 19 namespace media {
18 class AudioBusRefCounted; 20 class AudioBusRefCounted;
19 } 21 }
20 22
21 namespace copresence { 23 namespace copresence {
22 24
23 class TickClockRefCounted; 25 class TickClockRefCounted;
24 26
25 struct AudioDirective final { 27 struct AudioDirective final {
26 // Default ctor, required by the priority queue. 28 // Default ctor, required by the priority queue.
27 AudioDirective(); 29 AudioDirective();
28 AudioDirective(const std::string& op_id, base::TimeTicks end_time); 30 AudioDirective(const std::string& op_id, base::TimeTicks end_time);
29 31
30 std::string op_id; 32 std::string op_id;
31 // We're currently using TimeTicks to track time. This may not work for cases 33 // We're currently using TimeTicks to track time. This may not work for cases
32 // where your machine suspends. See crbug.com/426136 34 // where your machine suspends. See crbug.com/426136
33 base::TimeTicks end_time; 35 base::TimeTicks end_time;
34 }; 36 };
35 37
36 // This class maintains a list of active audio directives. It fetches the audio 38 // This class maintains a list of active audio directives. It fetches the audio
37 // samples associated with a audio transmit directives and expires directives 39 // samples associated with a audio transmit directives and expires directives
38 // that have outlived their TTL. 40 // that have outlived their TTL.
39 // TODO(rkc): Once we implement more token technologies, move reusable code 41 // TODO(rkc): Once we implement more token technologies, move reusable code
40 // from here to a base class and inherit various XxxxDirectiveList 42 // from here to a base class and inherit various XxxxDirectiveList
41 // classes from it. 43 // classes from it.
42 class AudioDirectiveList { 44 class AudioDirectiveList {
43 public: 45 public:
44 AudioDirectiveList(); 46 explicit AudioDirectiveList(const scoped_refptr<TickClockRefCounted>& clock =
Charlie 2014/10/31 20:55:17 I think we're always passing in a clock now. So we
rkc 2014/11/03 20:14:43 Sure.
Charlie 2014/11/03 22:45:13 I take it back. The unit test gets ugly without th
47 make_scoped_refptr(new TickClockRefCounted(new base::DefaultTickClock)));
45 ~AudioDirectiveList(); 48 ~AudioDirectiveList();
46 49
47 void AddDirective(const std::string& op_id, base::TimeDelta ttl); 50 void AddDirective(const std::string& op_id, base::TimeDelta ttl);
48 void RemoveDirective(const std::string& op_id); 51 void RemoveDirective(const std::string& op_id);
49 52
50 scoped_ptr<AudioDirective> GetActiveDirective(); 53 scoped_ptr<AudioDirective> GetActiveDirective();
51 54
52 void set_clock_for_testing(const scoped_refptr<TickClockRefCounted>& clock);
53
54 private: 55 private:
55 // Comparator for comparing end_times on audio tokens. 56 // Comparator for comparing end_times on audio tokens.
56 class LatestFirstComparator { 57 class LatestFirstComparator {
57 public: 58 public:
58 // This will sort our queue with the 'latest' time being the top. 59 // This will sort our queue with the 'latest' time being the top.
59 bool operator()(const AudioDirective& left, 60 bool operator()(const AudioDirective& left,
60 const AudioDirective& right) const { 61 const AudioDirective& right) const {
61 return left.end_time < right.end_time; 62 return left.end_time < right.end_time;
62 } 63 }
63 }; 64 };
64 65
65 std::vector<AudioDirective>::iterator FindDirectiveByOpId( 66 std::vector<AudioDirective>::iterator FindDirectiveByOpId(
66 const std::string& op_id); 67 const std::string& op_id);
67 68
68 // This vector will be organized as a heap with the latest time as the first 69 // This vector will be organized as a heap with the latest time as the first
69 // element. Only currently active directives will exist in this list. 70 // element. Only currently active directives will exist in this list.
70 std::vector<AudioDirective> active_directives_; 71 std::vector<AudioDirective> active_directives_;
71 72
72 scoped_refptr<TickClockRefCounted> clock_; 73 scoped_refptr<TickClockRefCounted> clock_;
73 74
74 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList); 75 DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList);
75 }; 76 };
76 77
77 } // namespace copresence 78 } // namespace copresence
78 79
79 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_ 80 #endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698