OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/midi/midi_manager.h" | 5 #include "media/midi/midi_manager.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
11 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ | |
12 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | |
13 MidiManager* MidiManager::Create() { | |
14 return new MidiManager; | |
15 } | |
16 #endif | |
17 | |
18 MidiManager::MidiManager() | 11 MidiManager::MidiManager() |
19 : initialized_(false), | 12 : initialized_(false), |
20 result_(MIDI_NOT_SUPPORTED) { | 13 result_(MIDI_NOT_SUPPORTED) { |
21 } | 14 } |
22 | 15 |
23 MidiManager::~MidiManager() { | 16 MidiManager::~MidiManager() { |
24 } | 17 } |
25 | 18 |
| 19 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
| 20 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 21 MidiManager* MidiManager::Create() { |
| 22 return new MidiManager; |
| 23 } |
| 24 #endif |
| 25 |
26 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { | 26 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
27 bool session_is_ready; | 27 bool session_is_ready; |
28 bool session_needs_initialization = false; | 28 bool session_needs_initialization = false; |
| 29 bool too_many_pending_clients_exist = false; |
29 | 30 |
30 { | 31 { |
31 base::AutoLock auto_lock(clients_lock_); | 32 base::AutoLock auto_lock(clients_lock_); |
32 session_is_ready = initialized_; | 33 session_is_ready = initialized_; |
33 if (!session_is_ready) { | 34 if (!session_is_ready) { |
34 // Call StartInitialization() only for the first request. | 35 // Do not accept a new request if the pending client list contains too |
35 session_needs_initialization = pending_clients_.empty(); | 36 // many clients. |
36 pending_clients_.insert( | 37 too_many_pending_clients_exist = |
37 std::pair<int, MidiManagerClient*>(client_id, client)); | 38 pending_clients_.size() >= kMaxPendingClientCount; |
| 39 |
| 40 if (!too_many_pending_clients_exist) { |
| 41 // Call StartInitialization() only for the first request. |
| 42 session_needs_initialization = pending_clients_.empty(); |
| 43 pending_clients_.insert( |
| 44 std::pair<int, MidiManagerClient*>(client_id, client)); |
| 45 } |
38 } | 46 } |
39 } | 47 } |
40 | 48 |
41 // Lazily initialize the MIDI back-end. | 49 // Lazily initialize the MIDI back-end. |
42 if (!session_is_ready) { | 50 if (!session_is_ready) { |
43 if (session_needs_initialization) { | 51 if (session_needs_initialization) { |
44 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); | 52 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
45 StartInitialization(); | 53 StartInitialization(); |
46 } | 54 } |
| 55 if (too_many_pending_clients_exist) { |
| 56 // Return an error immediately if there are too many requests. |
| 57 client->CompleteStartSession(client_id, MIDI_INITIALIZATION_ERROR); |
| 58 return; |
| 59 } |
47 // CompleteInitialization() will be called asynchronously when platform | 60 // CompleteInitialization() will be called asynchronously when platform |
48 // dependent initialization is finished. | 61 // dependent initialization is finished. |
49 return; | 62 return; |
50 } | 63 } |
51 | 64 |
52 // Platform dependent initialization was already finished for previously | 65 // Platform dependent initialization was already finished for previously |
53 // initialized clients. | 66 // initialized clients. |
54 MidiResult result; | 67 MidiResult result; |
55 { | 68 { |
56 base::AutoLock auto_lock(clients_lock_); | 69 base::AutoLock auto_lock(clients_lock_); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const uint8* data, | 125 const uint8* data, |
113 size_t length, | 126 size_t length, |
114 double timestamp) { | 127 double timestamp) { |
115 base::AutoLock auto_lock(clients_lock_); | 128 base::AutoLock auto_lock(clients_lock_); |
116 | 129 |
117 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) | 130 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
118 (*i)->ReceiveMidiData(port_index, data, length, timestamp); | 131 (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
119 } | 132 } |
120 | 133 |
121 } // namespace media | 134 } // namespace media |
OLD | NEW |