summaryrefslogtreecommitdiffstats
path: root/components/copresence/handlers/audio/audio_directive_list.cc
blob: 8b9b76ec21d1945b9955ea17bba27e41e8b892c1 (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
79
80
81
82
83
84
85
86
87
88
89
// 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.

#include "components/copresence/handlers/audio/audio_directive_list.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"

namespace copresence {

// Public methods.

AudioDirective::AudioDirective() {
}

AudioDirective::AudioDirective(const std::string& op_id,
                               base::TimeTicks end_time)
    : op_id(op_id), end_time(end_time) {
}

AudioDirectiveList::AudioDirectiveList() : clock_(new base::DefaultTickClock) {
}

AudioDirectiveList::~AudioDirectiveList() {
}

void AudioDirectiveList::AddDirective(const std::string& op_id,
                                      base::TimeDelta ttl) {
  base::TimeTicks end_time = clock_->NowTicks() + ttl;

  // In case this op is already in the list, update it instead of adding
  // it again.
  std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id);
  if (it != active_directives_.end()) {
    it->end_time = end_time;
    std::make_heap(active_directives_.begin(),
                   active_directives_.end(),
                   LatestFirstComparator());
    return;
  }

  active_directives_.push_back(AudioDirective(op_id, end_time));
  std::push_heap(active_directives_.begin(),
                 active_directives_.end(),
                 LatestFirstComparator());
}

void AudioDirectiveList::RemoveDirective(const std::string& op_id) {
  std::vector<AudioDirective>::iterator it = FindDirectiveByOpId(op_id);
  if (it != active_directives_.end())
    active_directives_.erase(it);

  std::make_heap(active_directives_.begin(),
                 active_directives_.end(),
                 LatestFirstComparator());
}

scoped_ptr<AudioDirective> AudioDirectiveList::GetActiveDirective() {
  // The top is always the instruction that is ending the latest. If that time
  // has passed, means all our previous instructions have expired too, hence
  // clear the list.
  if (!active_directives_.empty() &&
      active_directives_.front().end_time < clock_->NowTicks()) {
    active_directives_.clear();
  }

  if (active_directives_.empty())
    return make_scoped_ptr<AudioDirective>(NULL);

  return make_scoped_ptr(new AudioDirective(active_directives_.front()));
}

std::vector<AudioDirective>::iterator AudioDirectiveList::FindDirectiveByOpId(
    const std::string& op_id) {
  for (std::vector<AudioDirective>::iterator it = active_directives_.begin();
       it != active_directives_.end();
       ++it) {
    if (it->op_id == op_id)
      return it;
  }
  return active_directives_.end();
}

}  // namespace copresence