forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi_scheduler.cc
61 lines (50 loc) · 1.84 KB
/
midi_scheduler.cc
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
// Copyright 2015 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 "media/midi/midi_scheduler.h"
#include <algorithm>
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "media/midi/midi_manager.h"
namespace media {
namespace midi {
MidiScheduler::MidiScheduler(MidiManager* manager)
: manager_(manager),
weak_factory_(this) {
}
MidiScheduler::~MidiScheduler() {
}
// TODO(crbug.com/467442): Use CancelableTaskTracker once it supports
// DelayedTask.
void MidiScheduler::PostSendDataTask(MidiManagerClient* client,
size_t length,
double timestamp,
const base::Closure& closure) {
DCHECK(client);
const base::Closure& weak_closure = base::Bind(
&MidiScheduler::InvokeClosure,
weak_factory_.GetWeakPtr(),
client,
length,
closure);
base::TimeDelta delay;
if (timestamp != 0.0) {
base::TimeTicks time_to_send =
base::TimeTicks() + base::TimeDelta::FromMicroseconds(
timestamp * base::Time::kMicrosecondsPerSecond);
delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta());
}
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, weak_closure,
delay);
}
void MidiScheduler::InvokeClosure(MidiManagerClient* client,
size_t length,
const base::Closure& closure) {
closure.Run();
manager_->AccumulateMidiBytesSent(client, length);
}
} // namespace midi
} // namespace media