- add sources.
[platform/framework/web/crosswalk.git] / src / media / midi / midi_manager.cc
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
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread.h"
11
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
27 bool MIDIManager::StartSession(MIDIManagerClient* client) {
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
40 void MIDIManager::EndSession(MIDIManagerClient* client) {
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(
56     uint32 port_index,
57     const uint8* data,
58     size_t length,
59     double timestamp) {
60   base::AutoLock auto_lock(clients_lock_);
61
62   for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i)
63     (*i)->ReceiveMIDIData(port_index, data, length, timestamp);
64 }
65
66 bool MIDIManager::CurrentlyOnMIDISendThread() {
67   return send_thread_->message_loop() == base::MessageLoop::current();
68 }
69
70 void MIDIManager::DispatchSendMIDIData(MIDIManagerClient* client,
71                                        uint32 port_index,
72                                        const std::vector<uint8>& data,
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),
84          client, port_index, data, timestamp));
85 }
86
87 }  // namespace media