Upstream version 11.40.277.0
[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/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11
12 namespace media {
13
14 MidiManager::MidiManager()
15     : initialized_(false),
16       result_(MIDI_NOT_SUPPORTED) {
17 }
18
19 MidiManager::~MidiManager() {
20 }
21
22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
23     !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
24 MidiManager* MidiManager::Create() {
25   return new MidiManager;
26 }
27 #endif
28
29 void MidiManager::StartSession(MidiManagerClient* client) {
30   bool session_is_ready;
31   bool session_needs_initialization = false;
32   bool too_many_pending_clients_exist = false;
33
34   {
35     base::AutoLock auto_lock(lock_);
36     session_is_ready = initialized_;
37     if (clients_.find(client) != clients_.end() ||
38         pending_clients_.find(client) != pending_clients_.end()) {
39       // Should not happen. But just in case the renderer is compromised.
40       NOTREACHED();
41       return;
42     }
43     if (!session_is_ready) {
44       // Do not accept a new request if the pending client list contains too
45       // many clients.
46       too_many_pending_clients_exist =
47           pending_clients_.size() >= kMaxPendingClientCount;
48
49       if (!too_many_pending_clients_exist) {
50         // Call StartInitialization() only for the first request.
51         session_needs_initialization = pending_clients_.empty();
52         pending_clients_.insert(client);
53       }
54     }
55   }
56
57   // Lazily initialize the MIDI back-end.
58   if (!session_is_ready) {
59     if (session_needs_initialization) {
60       TRACE_EVENT0("midi", "MidiManager::StartInitialization");
61       session_thread_runner_ =
62           base::MessageLoop::current()->message_loop_proxy();
63       StartInitialization();
64     }
65     if (too_many_pending_clients_exist) {
66       // Return an error immediately if there are too many requests.
67       client->CompleteStartSession(MIDI_INITIALIZATION_ERROR);
68       return;
69     }
70     // CompleteInitialization() will be called asynchronously when platform
71     // dependent initialization is finished.
72     return;
73   }
74
75   // Platform dependent initialization was already finished for previously
76   // initialized clients.
77   MidiResult result;
78   {
79     base::AutoLock auto_lock(lock_);
80     if (result_ == MIDI_OK) {
81       AddInitialPorts(client);
82       clients_.insert(client);
83     }
84     result = result_;
85   }
86   client->CompleteStartSession(result);
87 }
88
89 void MidiManager::EndSession(MidiManagerClient* client) {
90   // At this point, |client| can be in the destruction process, and calling
91   // any method of |client| is dangerous.
92   base::AutoLock auto_lock(lock_);
93   clients_.erase(client);
94   pending_clients_.erase(client);
95 }
96
97 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
98                                        uint32 port_index,
99                                        const std::vector<uint8>& data,
100                                        double timestamp) {
101   NOTREACHED();
102 }
103
104 void MidiManager::StartInitialization() {
105   CompleteInitialization(MIDI_NOT_SUPPORTED);
106 }
107
108 void MidiManager::CompleteInitialization(MidiResult result) {
109   DCHECK(session_thread_runner_.get());
110   // It is safe to post a task to the IO thread from here because the IO thread
111   // should have stopped if the MidiManager is going to be destructed.
112   session_thread_runner_->PostTask(
113       FROM_HERE,
114       base::Bind(&MidiManager::CompleteInitializationInternal,
115                  base::Unretained(this),
116                  result));
117 }
118
119 void MidiManager::AddInputPort(const MidiPortInfo& info) {
120   base::AutoLock auto_lock(lock_);
121   input_ports_.push_back(info);
122   for (auto client : clients_)
123     client->AddInputPort(info);
124 }
125
126 void MidiManager::AddOutputPort(const MidiPortInfo& info) {
127   base::AutoLock auto_lock(lock_);
128   output_ports_.push_back(info);
129   for (auto client : clients_)
130     client->AddOutputPort(info);
131 }
132
133 void MidiManager::ReceiveMidiData(
134     uint32 port_index,
135     const uint8* data,
136     size_t length,
137     double timestamp) {
138   base::AutoLock auto_lock(lock_);
139
140   for (auto client : clients_)
141     client->ReceiveMidiData(port_index, data, length, timestamp);
142 }
143
144 void MidiManager::CompleteInitializationInternal(MidiResult result) {
145   TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
146
147   base::AutoLock auto_lock(lock_);
148   DCHECK(clients_.empty());
149   DCHECK(!initialized_);
150   initialized_ = true;
151   result_ = result;
152
153   for (auto client : pending_clients_) {
154     if (result_ == MIDI_OK) {
155       AddInitialPorts(client);
156       clients_.insert(client);
157     }
158     client->CompleteStartSession(result_);
159   }
160   pending_clients_.clear();
161 }
162
163 void MidiManager::AddInitialPorts(MidiManagerClient* client) {
164   lock_.AssertAcquired();
165
166   for (const auto& info : input_ports_)
167     client->AddInputPort(info);
168   for (const auto& info : output_ports_)
169     client->AddOutputPort(info);
170 }
171
172 }  // namespace media