1 // Copyright 2015 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.
5 #include "media/midi/midi_scheduler.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "media/midi/midi_manager.h"
14 MidiScheduler::MidiScheduler(MidiManager
* manager
)
19 MidiScheduler::~MidiScheduler() {
22 // TODO(crbug.com/467442): Use CancelableTaskTracker once it supports
24 void MidiScheduler::PostSendDataTask(MidiManagerClient
* client
,
27 const base::Closure
& closure
) {
30 const base::Closure
& weak_closure
= base::Bind(
31 &MidiScheduler::InvokeClosure
,
32 weak_factory_
.GetWeakPtr(),
37 base::TimeDelta delay
;
38 if (timestamp
!= 0.0) {
39 base::TimeTicks time_to_send
=
40 base::TimeTicks() + base::TimeDelta::FromMicroseconds(
41 timestamp
* base::Time::kMicrosecondsPerSecond
);
42 delay
= std::max(time_to_send
- base::TimeTicks::Now(), base::TimeDelta());
44 base::MessageLoop::current()->task_runner()->PostDelayedTask(
45 FROM_HERE
, weak_closure
, delay
);
48 void MidiScheduler::InvokeClosure(MidiManagerClient
* client
,
50 const base::Closure
& closure
) {
52 manager_
->AccumulateMidiBytesSent(client
, length
);