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"
15 MidiScheduler::MidiScheduler(MidiManager
* manager
)
20 MidiScheduler::~MidiScheduler() {
23 // TODO(crbug.com/467442): Use CancelableTaskTracker once it supports
25 void MidiScheduler::PostSendDataTask(MidiManagerClient
* client
,
28 const base::Closure
& closure
) {
31 const base::Closure
& weak_closure
= base::Bind(
32 &MidiScheduler::InvokeClosure
,
33 weak_factory_
.GetWeakPtr(),
38 base::TimeDelta delay
;
39 if (timestamp
!= 0.0) {
40 base::TimeTicks time_to_send
=
41 base::TimeTicks() + base::TimeDelta::FromMicroseconds(
42 timestamp
* base::Time::kMicrosecondsPerSecond
);
43 delay
= std::max(time_to_send
- base::TimeTicks::Now(), base::TimeDelta());
45 base::MessageLoop::current()->task_runner()->PostDelayedTask(
46 FROM_HERE
, weak_closure
, delay
);
49 void MidiScheduler::InvokeClosure(MidiManagerClient
* client
,
51 const base::Closure
& closure
) {
53 manager_
->AccumulateMidiBytesSent(client
, length
);