| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "media/midi/midi_scheduler.h" |
| 6 |
| 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" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 MidiScheduler::MidiScheduler() : weak_factory_(this) { |
| 15 } |
| 16 |
| 17 MidiScheduler::~MidiScheduler() { |
| 18 } |
| 19 |
| 20 // TODO(crbug.com/467442): Use CancelableTaskTracker once it supports |
| 21 // DelayedTask. |
| 22 void MidiScheduler::PostSendDataTask(MidiManagerClient* client, |
| 23 size_t length, |
| 24 double timestamp, |
| 25 const base::Closure& closure) { |
| 26 DCHECK(client); |
| 27 |
| 28 const base::Closure& weak_closure = base::Bind( |
| 29 &MidiScheduler::InvokeClosure, weak_factory_.GetWeakPtr(), closure); |
| 30 |
| 31 base::TimeDelta delay; |
| 32 if (timestamp != 0.0) { |
| 33 base::TimeTicks time_to_send = |
| 34 base::TimeTicks() + base::TimeDelta::FromMicroseconds( |
| 35 timestamp * base::Time::kMicrosecondsPerSecond); |
| 36 delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); |
| 37 } |
| 38 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 39 FROM_HERE, weak_closure, delay); |
| 40 |
| 41 // TODO(crbug.com/467442): AccumulateMidiBytesSent should be called in |
| 42 // InvokeClosure. But for now, we call it here since |client| may be deleted |
| 43 // at that time. |
| 44 client->AccumulateMidiBytesSent(length); |
| 45 } |
| 46 |
| 47 void MidiScheduler::InvokeClosure(const base::Closure& closure) { |
| 48 closure.Run(); |
| 49 } |
| 50 |
| 51 } // namespace media |
| OLD | NEW |