Chromium Code Reviews| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 MidiManager::MidiManager() | 14 MidiManager::MidiManager() |
| 15 : initialized_(false), | 15 : pending_clients_size_(0), |
| 16 initialized_(false), | |
| 16 result_(MIDI_NOT_SUPPORTED) { | 17 result_(MIDI_NOT_SUPPORTED) { |
| 17 } | 18 } |
| 18 | 19 |
| 19 MidiManager::~MidiManager() { | 20 MidiManager::~MidiManager() { |
| 20 } | 21 } |
| 21 | 22 |
| 22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ | 23 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
| 23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | 24 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 24 MidiManager* MidiManager::Create() { | 25 MidiManager* MidiManager::Create() { |
| 25 return new MidiManager; | 26 return new MidiManager; |
| 26 } | 27 } |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { | 30 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
| 30 bool session_is_ready; | 31 bool session_is_ready; |
| 31 bool session_needs_initialization = false; | 32 bool session_needs_initialization = false; |
| 32 bool too_many_pending_clients_exist = false; | 33 bool too_many_pending_clients_exist = false; |
| 33 | 34 |
| 34 { | 35 { |
| 35 base::AutoLock auto_lock(lock_); | 36 base::AutoLock auto_lock(lock_); |
| 36 session_is_ready = initialized_; | 37 session_is_ready = initialized_; |
| 37 if (!session_is_ready) { | 38 if (!session_is_ready) { |
| 38 // Do not accept a new request if the pending client list contains too | 39 // Do not accept a new request if the pending client list contains too |
| 39 // many clients. | 40 // many clients. |
| 40 too_many_pending_clients_exist = | 41 too_many_pending_clients_exist = |
| 41 pending_clients_.size() >= kMaxPendingClientCount; | 42 pending_clients_size_ >= kMaxPendingClientCount; |
| 42 | 43 |
| 43 if (!too_many_pending_clients_exist) { | 44 if (!too_many_pending_clients_exist) { |
| 44 // Call StartInitialization() only for the first request. | 45 // Call StartInitialization() only for the first request. |
| 45 session_needs_initialization = pending_clients_.empty(); | 46 session_needs_initialization = pending_clients_.empty(); |
| 46 pending_clients_.insert( | 47 PendingClientMap::iterator pending_client_iterator = |
|
yhirano
2014/06/11 12:11:57
pending_clients_[client].push_back(client_id) for
Takashi Toyoshima
2014/06/12 04:41:02
thanks.
Now that it is multiset, I just insert a p
| |
| 47 std::pair<int, MidiManagerClient*>(client_id, client)); | 48 pending_clients_.find(client); |
| 49 if (pending_client_iterator != pending_clients_.end()) { | |
| 50 pending_client_iterator->second.push_back(client_id); | |
| 51 } else { | |
| 52 std::pair<MidiManagerClient*, std::vector<int> > entry = | |
| 53 std::pair<MidiManagerClient*, std::vector<int> >( | |
| 54 client, std::vector<int>()); | |
| 55 entry.second.push_back(client_id); | |
| 56 pending_clients_.insert(entry); | |
| 57 } | |
| 58 pending_clients_size_++; | |
| 48 } | 59 } |
| 49 } | 60 } |
| 50 } | 61 } |
| 51 | 62 |
| 52 // Lazily initialize the MIDI back-end. | 63 // Lazily initialize the MIDI back-end. |
| 53 if (!session_is_ready) { | 64 if (!session_is_ready) { |
| 54 if (session_needs_initialization) { | 65 if (session_needs_initialization) { |
| 55 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); | 66 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
| 56 session_thread_runner_ = | 67 session_thread_runner_ = |
| 57 base::MessageLoop::current()->message_loop_proxy(); | 68 base::MessageLoop::current()->message_loop_proxy(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 74 base::AutoLock auto_lock(lock_); | 85 base::AutoLock auto_lock(lock_); |
| 75 if (result_ == MIDI_OK) | 86 if (result_ == MIDI_OK) |
| 76 clients_.insert(client); | 87 clients_.insert(client); |
| 77 result = result_; | 88 result = result_; |
| 78 } | 89 } |
| 79 client->CompleteStartSession(client_id, result); | 90 client->CompleteStartSession(client_id, result); |
| 80 } | 91 } |
| 81 | 92 |
| 82 void MidiManager::EndSession(MidiManagerClient* client) { | 93 void MidiManager::EndSession(MidiManagerClient* client) { |
| 83 base::AutoLock auto_lock(lock_); | 94 base::AutoLock auto_lock(lock_); |
| 84 ClientList::iterator i = clients_.find(client); | 95 ClientList::iterator client_iterator = clients_.find(client); |
|
yhirano
2014/06/11 12:11:57
std::set<T>::erase(key) works even when key doesn'
Takashi Toyoshima
2014/06/12 04:41:02
Done.
| |
| 85 if (i != clients_.end()) | 96 if (client_iterator != clients_.end()) |
| 86 clients_.erase(i); | 97 clients_.erase(client_iterator); |
| 98 | |
| 99 PendingClientMap::iterator pending_client_iterator = | |
| 100 pending_clients_.find(client); | |
| 101 if (pending_client_iterator != pending_clients_.end()) { | |
| 102 pending_clients_size_ -= pending_client_iterator->second.size(); | |
| 103 pending_clients_.erase(pending_client_iterator); | |
| 104 } | |
| 87 } | 105 } |
| 88 | 106 |
| 89 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, | 107 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
| 90 uint32 port_index, | 108 uint32 port_index, |
| 91 const std::vector<uint8>& data, | 109 const std::vector<uint8>& data, |
| 92 double timestamp) { | 110 double timestamp) { |
| 93 NOTREACHED(); | 111 NOTREACHED(); |
| 94 } | 112 } |
| 95 | 113 |
| 96 void MidiManager::StartInitialization() { | 114 void MidiManager::StartInitialization() { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 125 | 143 |
| 126 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) | 144 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
| 127 (*i)->ReceiveMidiData(port_index, data, length, timestamp); | 145 (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
| 128 } | 146 } |
| 129 | 147 |
| 130 void MidiManager::CompleteInitializationInternal(MidiResult result) { | 148 void MidiManager::CompleteInitializationInternal(MidiResult result) { |
| 131 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); | 149 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
| 132 | 150 |
| 133 base::AutoLock auto_lock(lock_); | 151 base::AutoLock auto_lock(lock_); |
| 134 DCHECK(clients_.empty()); | 152 DCHECK(clients_.empty()); |
| 135 DCHECK(!pending_clients_.empty()); | |
| 136 DCHECK(!initialized_); | 153 DCHECK(!initialized_); |
| 137 initialized_ = true; | 154 initialized_ = true; |
| 138 result_ = result; | 155 result_ = result; |
| 139 | 156 |
| 140 for (PendingClientMap::iterator it = pending_clients_.begin(); | 157 for (PendingClientMap::iterator it = pending_clients_.begin(); |
| 141 it != pending_clients_.end(); | 158 it != pending_clients_.end(); |
| 142 ++it) { | 159 ++it) { |
| 143 if (result_ == MIDI_OK) | 160 if (result_ == MIDI_OK) |
| 144 clients_.insert(it->second); | 161 clients_.insert(it->first); |
| 145 it->second->CompleteStartSession(it->first, result_); | 162 for (size_t i = 0; i < it->second.size(); ++i) |
| 163 it->first->CompleteStartSession(it->second[i], result_); | |
| 146 } | 164 } |
| 147 pending_clients_.clear(); | 165 pending_clients_.clear(); |
| 166 pending_clients_size_ = 0; | |
| 148 } | 167 } |
| 149 | 168 |
| 150 } // namespace media | 169 } // namespace media |
| OLD | NEW |