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