This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / media / midi / midi_scheduler.cc
blob31886747259e066585b19064c674e195629773f6
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"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "media/midi/midi_manager.h"
12 namespace media {
13 namespace midi {
15 MidiScheduler::MidiScheduler(MidiManager* manager)
16 : manager_(manager),
17 weak_factory_(this) {
20 MidiScheduler::~MidiScheduler() {
23 // TODO(crbug.com/467442): Use CancelableTaskTracker once it supports
24 // DelayedTask.
25 void MidiScheduler::PostSendDataTask(MidiManagerClient* client,
26 size_t length,
27 double timestamp,
28 const base::Closure& closure) {
29 DCHECK(client);
31 const base::Closure& weak_closure = base::Bind(
32 &MidiScheduler::InvokeClosure,
33 weak_factory_.GetWeakPtr(),
34 client,
35 length,
36 closure);
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,
50 size_t length,
51 const base::Closure& closure) {
52 closure.Run();
53 manager_->AccumulateMidiBytesSent(client, length);
56 } // namespace midi
57 } // namespace media