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

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

Issue 419073002: Add the copresence DirectiveHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months 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 | Annotate | Revision Log
OLDNEW
(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 #include "components/copresence/handlers/audio/audio_directive_list.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "media/base/audio_bus.h"
11
12 namespace {
13
14 // UrlSafe is defined as:
15 // '/' represented by a '_' and '+' represented by a '-'
16 // TODO(rkc): Move this processing to the whispernet wrapper.
17 std::string FromUrlSafe(std::string token) {
Daniel Erat 2014/07/28 21:18:17 const std::string&
rkc 2014/07/29 00:33:34 I need to change the string in the function so I'd
18 base::ReplaceChars(token, "-", "+", &token);
19 base::ReplaceChars(token, "_", "/", &token);
20 return token;
21 }
22
23 const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes.
24 const int kMaxSamples = 10000;
25
26 } // namespace
27
28 namespace copresence {
29
30 // Public methods.
31
32 AudioDirective::AudioDirective() {
33 }
34
35 AudioDirective::AudioDirective(const std::string& op_id, base::Time end_time)
36 : op_id(op_id), end_time(end_time) {
Daniel Erat 2014/07/28 21:18:17 nit: one per line if they don't all fit on the sam
rkc 2014/07/29 00:33:34 BTW, git cl format keeps overriding this to put th
37 }
38
39 AudioDirective::AudioDirective(const std::string& token,
40 const std::string& op_id,
41 base::Time end_time)
42 : token(token), op_id(op_id), end_time(end_time) {
43 }
44
45 AudioDirective::AudioDirective(
46 const std::string& token,
47 const std::string& op_id,
48 base::Time end_time,
49 const scoped_refptr<media::AudioBusRefCounted>& samples)
50 : token(token), op_id(op_id), end_time(end_time), samples(samples) {
Daniel Erat 2014/07/28 21:18:17 same here
rkc 2014/07/29 00:33:34 Done.
51 }
52
53 AudioDirective::~AudioDirective() {
54 }
55
56 AudioDirectiveList::AudioDirectiveList(
57 const EncodeTokenCallback& encode_token_callback,
58 const base::Closure& token_added_callback)
59 : encode_token_callback_(encode_token_callback),
60 token_added_callback_(token_added_callback),
61 samples_cache_(base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs),
62 kMaxSamples) {
63 }
64
65 AudioDirectiveList::~AudioDirectiveList() {
66 }
67
68 void AudioDirectiveList::AddTransmitDirective(const std::string& token,
69 const std::string& op_id,
70 base::TimeDelta ttl) {
71 std::string valid_token = FromUrlSafe(token);
72 base::Time end_time = base::Time::Now() + ttl;
73
74 if (samples_cache_.HasKey(valid_token)) {
75 active_transmit_tokens_.push(AudioDirective(
76 valid_token, op_id, end_time, samples_cache_.GetValue(valid_token)));
77 return;
78 }
79
80 // If an encode request for this token has been sent, don't send it again.
81 if (pending_transmit_tokens_.find(valid_token) !=
82 pending_transmit_tokens_.end()) {
83 return;
84 }
85
86 pending_transmit_tokens_[valid_token] =
87 AudioDirective(valid_token, op_id, end_time);
88 // All whispernet callbacks will be cleared before we are destructed, so
89 // unretained is safe to use here.
90 encode_token_callback_.Run(
91 valid_token,
92 base::Bind(&AudioDirectiveList::OnTokenEncoded, base::Unretained(this)));
93 }
94
95 void AudioDirectiveList::AddReceiveDirective(const std::string& op_id,
96 base::TimeDelta ttl) {
97 active_receive_tokens_.push(AudioDirective(op_id, base::Time::Now() + ttl));
98 }
99
100 scoped_ptr<AudioDirective> AudioDirectiveList::GetNextTransmit() {
101 return GetNextFromList(&active_transmit_tokens_);
102 }
103
104 scoped_ptr<AudioDirective> AudioDirectiveList::GetNextReceive() {
105 return GetNextFromList(&active_receive_tokens_);
106 }
107
108 scoped_ptr<AudioDirective> AudioDirectiveList::GetNextFromList(
109 AudioDirectiveQueue* list) {
110 CHECK(list);
111
112 while (!list->empty() && list->top().end_time < base::Time::Now())
xiyuan 2014/07/25 21:02:09 AudioDirectiveQueue is sorted by LatestFirstCompar
rkc 2014/07/28 21:02:02 No, you're right. Added a comment explaining that
113 list->pop();
114
115 if (list->empty())
116 return make_scoped_ptr<AudioDirective>(NULL);
117
118 return make_scoped_ptr(new AudioDirective(list->top()));
119 }
120
121 void AudioDirectiveList::OnTokenEncoded(
122 const std::string& token,
123 const scoped_refptr<media::AudioBusRefCounted>& samples) {
124 // We shouldn't re-encode a token if it's already in the cache.
125 DCHECK(!samples_cache_.HasKey(token));
126 DVLOG(3) << "Token: " << token << " encoded.";
127 samples_cache_.Add(token, samples);
128
129 // Copy the samples into its corresponding directive object and move
130 // that object into the active queue. TODO(rkc): The actual directive without
131 // the samples is small, but we should find a better way to move around
132 // the samples vector.
133 pending_transmit_tokens_[token].samples = samples;
Daniel Erat 2014/07/28 21:18:17 use find() to avoid doing the same lookup three ti
rkc 2014/07/29 00:33:34 Done.
134 active_transmit_tokens_.push(pending_transmit_tokens_[token]);
135 pending_transmit_tokens_.erase(token);
136
137 if (!token_added_callback_.is_null())
138 token_added_callback_.Run();
139 }
140
141 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698