crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 1 | // Copyright (c) 2013 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_manager.h" |
| 6 | |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/threading/thread.h" |
| 10 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 11 | namespace media { |
| 12 | |
| 13 | #if !defined(OS_MACOSX) |
| 14 | // TODO(crogers): implement MIDIManager for other platforms. |
| 15 | MIDIManager* MIDIManager::Create() { |
| 16 | return NULL; |
| 17 | } |
| 18 | #endif |
| 19 | |
| 20 | MIDIManager::MIDIManager() |
| 21 | : initialized_(false) { |
| 22 | } |
| 23 | |
| 24 | MIDIManager::~MIDIManager() {} |
| 25 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 26 | bool MIDIManager::StartSession(MIDIManagerClient* client) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 27 | // Lazily initialize the MIDI back-end. |
| 28 | if (!initialized_) |
| 29 | initialized_ = Initialize(); |
| 30 | |
| 31 | if (initialized_) { |
| 32 | base::AutoLock auto_lock(clients_lock_); |
| 33 | clients_.insert(client); |
| 34 | } |
| 35 | |
| 36 | return initialized_; |
| 37 | } |
| 38 | |
toyoshim@chromium.org | 235b09e | 2013-07-25 16:23:14 +0000 | [diff] [blame] | 39 | void MIDIManager::EndSession(MIDIManagerClient* client) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 40 | base::AutoLock auto_lock(clients_lock_); |
| 41 | ClientList::iterator i = clients_.find(client); |
| 42 | if (i != clients_.end()) |
| 43 | clients_.erase(i); |
| 44 | } |
| 45 | |
| 46 | void MIDIManager::AddInputPort(const MIDIPortInfo& info) { |
| 47 | input_ports_.push_back(info); |
| 48 | } |
| 49 | |
| 50 | void MIDIManager::AddOutputPort(const MIDIPortInfo& info) { |
| 51 | output_ports_.push_back(info); |
| 52 | } |
| 53 | |
| 54 | void MIDIManager::ReceiveMIDIData( |
| 55 | int port_index, |
| 56 | const uint8* data, |
| 57 | size_t length, |
| 58 | double timestamp) { |
| 59 | base::AutoLock auto_lock(clients_lock_); |
| 60 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 61 | for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
| 62 | (*i)->ReceiveMIDIData(port_index, data, length, timestamp); |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void MIDIManager::DispatchSendMIDIData(MIDIManagerClient* client, |
| 66 | int port_index, |
| 67 | const uint8* data, |
| 68 | size_t length, |
| 69 | double timestamp) { |
| 70 | // Lazily create the thread when first needed. |
| 71 | if (!send_thread_) { |
| 72 | send_thread_.reset(new base::Thread("MIDISendThread")); |
| 73 | send_thread_->Start(); |
| 74 | send_message_loop_ = send_thread_->message_loop_proxy(); |
| 75 | } |
| 76 | |
| 77 | send_message_loop_->PostTask( |
| 78 | FROM_HERE, |
| 79 | base::Bind(&MIDIManager::SendMIDIData, base::Unretained(this), |
| 80 | client, port_index, data, length, timestamp)); |
| 81 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 82 | |
| 83 | } // namespace media |