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

Unified Diff: components/copresence/handlers/audio/audio_directive_list.h

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 side-by-side diff with in-line comments
Download patch
Index: components/copresence/handlers/audio/audio_directive_list.h
diff --git a/components/copresence/handlers/audio/audio_directive_list.h b/components/copresence/handlers/audio/audio_directive_list.h
new file mode 100644
index 0000000000000000000000000000000000000000..ee0f05fc5a50fa375989f507ea4e33ea7b3acea0
--- /dev/null
+++ b/components/copresence/handlers/audio/audio_directive_list.h
@@ -0,0 +1,123 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_
+#define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_
+
+#include <map>
+#include <queue>
+#include <string>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+#include "components/copresence/timed_map.h"
+
+namespace media {
+class AudioBusRefCounted;
+}
+
+namespace copresence {
+
+struct AudioDirective {
+ // Default ctor, required by the priority queue.
+ AudioDirective();
+ // ctor used to store receive directives.
+ AudioDirective(const std::string& op_id, base::Time end_time);
Daniel Erat 2014/08/05 16:38:51 the style guide says "Use overloaded functions (in
rkc 2014/08/05 18:00:35 Makes sense. Done.
+ // ctor used to store transmit directives that are awaiting samples.
+ AudioDirective(const std::string& token,
+ const std::string& op_id,
+ base::Time end_time);
+ // ctor used to construct a complete transmit directive.
+ AudioDirective(const std::string& token,
+ const std::string& op_id,
+ base::Time end_time,
+ const scoped_refptr<media::AudioBusRefCounted>& samples);
+ ~AudioDirective();
+
+ std::string token;
+ std::string op_id;
+ base::Time end_time;
+ scoped_refptr<media::AudioBusRefCounted> samples;
+};
+
+// This class maintains a list of active audio directives. It fetches the audio
+// samples associated with a audio transmit directives and expires directives
+// that have outlived their TTL.
+// TODO(rkc): Once we implement more token technologies, move reusable code
+// from here to a base class and inherit various XxxxDirectiveList
+// classes from it.
+class AudioDirectiveList {
+ public:
+ typedef base::Callback<
+ void(const std::string&, const scoped_refptr<media::AudioBusRefCounted>&)>
+ SamplesCallback;
+ typedef base::Callback<void(const std::string&, const SamplesCallback&)>
+ EncodeTokenCallback;
+
+ AudioDirectiveList(const EncodeTokenCallback& encode_token_callback,
+ const base::Closure& token_added_callback);
+ virtual ~AudioDirectiveList();
+
+ // Adds a token to the token queue, after getting its corresponding samples
+ // from whispernet.
+ void AddTransmitDirective(const std::string& token,
+ const std::string& op_id,
+ base::TimeDelta ttl);
+
+ void AddReceiveDirective(const std::string& op_id, base::TimeDelta ttl);
+
+ // Returns the next audio token to play. This also cleans up expired tokens.
+ scoped_ptr<AudioDirective> GetNextTransmit();
+ scoped_ptr<AudioDirective> GetNextReceive();
+
+ // This is the method that the whispernet client needs to call to return
+ // samples to us.
+ void OnTokenEncoded(const std::string& token,
+ const scoped_refptr<media::AudioBusRefCounted>& samples);
+
+ private:
+ // Comparator for comparing end_times on audio tokens.
+ class LatestFirstComparator {
+ public:
+ // This will sort our queue with the 'latest' time being the top.
+ bool operator()(const AudioDirective& left,
+ const AudioDirective& right) const {
+ return left.end_time < right.end_time;
+ }
+ };
+
+ typedef std::priority_queue<AudioDirective,
+ std::vector<AudioDirective>,
+ LatestFirstComparator> AudioDirectiveQueue;
+ typedef TimedMap<std::string, scoped_refptr<media::AudioBusRefCounted> >
+ SamplesMap;
+
+ scoped_ptr<AudioDirective> GetNextFromList(AudioDirectiveQueue* list);
+
+ // A map of tokens that are awaiting their samples before we can
+ // add them to the active transmit tokens list.
+ std::map<std::string, AudioDirective> pending_transmit_tokens_;
+
+ AudioDirectiveQueue active_transmit_tokens_;
+ AudioDirectiveQueue active_receive_tokens_;
+
+ EncodeTokenCallback encode_token_callback_;
+
+ base::Closure token_added_callback_;
+
+ // Cache that holds 10k encoded samples. After reaching its limit, the cache
Daniel Erat 2014/08/05 16:38:51 nit: if there's a chance that the size will change
rkc 2014/08/05 18:00:35 Done.
+ // expires the oldest samples first.
+ SamplesMap samples_cache_;
+
+ DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList);
+};
+
+} // namespace copresence
+
+#endif // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_

Powered by Google App Engine
This is Rietveld 408576698