summaryrefslogtreecommitdiffstats
path: root/components/copresence/handlers/audio/audio_directive_list.h
blob: e9d0634204d7a7f3d7b9d857a6b8cb58fb9a3189 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// 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_H_
#define COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_

#include <string>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"

namespace base {
class TickClock;
}

namespace media {
class AudioBusRefCounted;
}

namespace copresence {

struct AudioDirective final {
  // Default ctor, required by the priority queue.
  AudioDirective();
  AudioDirective(const std::string& op_id, base::TimeTicks end_time);

  std::string op_id;
  // We're currently using TimeTicks to track time. This may not work for cases
  // where your machine suspends. See crbug.com/426136
  base::TimeTicks end_time;
};

// 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:
  AudioDirectiveList();
  ~AudioDirectiveList();

  void AddDirective(const std::string& op_id, base::TimeDelta ttl);
  void RemoveDirective(const std::string& op_id);

  scoped_ptr<AudioDirective> GetActiveDirective();

 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;
    }
  };

  std::vector<AudioDirective>::iterator FindDirectiveByOpId(
      const std::string& op_id);

  // This vector will be organized as a heap with the latest time as the first
  // element. Only currently active directives will exist in this list.
  std::vector<AudioDirective> active_directives_;

  scoped_ptr<base::TickClock> clock_;

  DISALLOW_COPY_AND_ASSIGN(AudioDirectiveList);
};

}  // namespace copresence

#endif  // COMPONENTS_COPRESENCE_HANDLERS_AUDIO_AUDIO_DIRECTIVE_LIST_H_