blob: 05fcfa45a96f215af0f079c64717fa8182e0e251 [file] [log] [blame]
crogers@google.com27356e42013-06-22 04:03:03 +00001// 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.com542a43a2013-07-31 05:16:49 +00007#include "base/bind.h"
8#include "base/bind_helpers.h"
9#include "base/threading/thread.h"
10
crogers@google.com27356e42013-06-22 04:03:03 +000011namespace media {
12
13#if !defined(OS_MACOSX)
14// TODO(crogers): implement MIDIManager for other platforms.
15MIDIManager* MIDIManager::Create() {
16 return NULL;
17}
18#endif
19
20MIDIManager::MIDIManager()
21 : initialized_(false) {
22}
23
24MIDIManager::~MIDIManager() {}
25
toyoshim@chromium.org235b09e2013-07-25 16:23:14 +000026bool MIDIManager::StartSession(MIDIManagerClient* client) {
crogers@google.com27356e42013-06-22 04:03:03 +000027 // 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.org235b09e2013-07-25 16:23:14 +000039void MIDIManager::EndSession(MIDIManagerClient* client) {
crogers@google.com27356e42013-06-22 04:03:03 +000040 base::AutoLock auto_lock(clients_lock_);
41 ClientList::iterator i = clients_.find(client);
42 if (i != clients_.end())
43 clients_.erase(i);
44}
45
46void MIDIManager::AddInputPort(const MIDIPortInfo& info) {
47 input_ports_.push_back(info);
48}
49
50void MIDIManager::AddOutputPort(const MIDIPortInfo& info) {
51 output_ports_.push_back(info);
52}
53
54void 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.com27356e42013-06-22 04:03:03 +000061 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i)
62 (*i)->ReceiveMIDIData(port_index, data, length, timestamp);
crogers@google.com542a43a2013-07-31 05:16:49 +000063}
64
65void 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.com27356e42013-06-22 04:03:03 +000082
83} // namespace media