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/debug/trace_event.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" | |
| 8 | 11 |
| 9 namespace media { | 12 namespace media { |
| 10 | 13 |
| 11 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ | 14 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
| 12 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) | 15 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 13 MidiManager* MidiManager::Create() { | 16 MidiManager* MidiManager::Create() { |
| 14 return new MidiManager; | 17 return new MidiManager; |
| 15 } | 18 } |
| 16 #endif | 19 #endif |
| 17 | 20 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 35 session_needs_initialization = pending_clients_.empty(); | 38 session_needs_initialization = pending_clients_.empty(); |
| 36 pending_clients_.insert( | 39 pending_clients_.insert( |
| 37 std::pair<int, MidiManagerClient*>(client_id, client)); | 40 std::pair<int, MidiManagerClient*>(client_id, client)); |
| 38 } | 41 } |
| 39 } | 42 } |
| 40 | 43 |
| 41 // Lazily initialize the MIDI back-end. | 44 // Lazily initialize the MIDI back-end. |
| 42 if (!session_is_ready) { | 45 if (!session_is_ready) { |
| 43 if (session_needs_initialization) { | 46 if (session_needs_initialization) { |
| 44 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); | 47 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
| 48 session_thread_runner_ = | |
| 49 base::MessageLoop::current()->message_loop_proxy(); | |
| 45 StartInitialization(); | 50 StartInitialization(); |
| 46 } | 51 } |
| 47 // CompleteInitialization() will be called asynchronously when platform | 52 // CompleteInitialization() will be called asynchronously when platform |
| 48 // dependent initialization is finished. | 53 // dependent initialization is finished. |
| 49 return; | 54 return; |
| 50 } | 55 } |
| 51 | 56 |
| 52 // Platform dependent initialization was already finished for previously | 57 // Platform dependent initialization was already finished for previously |
| 53 // initialized clients. | 58 // initialized clients. |
| 54 MidiResult result; | 59 MidiResult result; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 73 const std::vector<uint8>& data, | 78 const std::vector<uint8>& data, |
| 74 double timestamp) { | 79 double timestamp) { |
| 75 NOTREACHED(); | 80 NOTREACHED(); |
| 76 } | 81 } |
| 77 | 82 |
| 78 void MidiManager::StartInitialization() { | 83 void MidiManager::StartInitialization() { |
| 79 CompleteInitialization(MIDI_NOT_SUPPORTED); | 84 CompleteInitialization(MIDI_NOT_SUPPORTED); |
| 80 } | 85 } |
| 81 | 86 |
| 82 void MidiManager::CompleteInitialization(MidiResult result) { | 87 void MidiManager::CompleteInitialization(MidiResult result) { |
| 83 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); | 88 DCHECK(session_thread_runner_.get()); |
| 84 | 89 // It is safe to post a task to the IO thread from here because the IO thread |
| 85 base::AutoLock auto_lock(clients_lock_); | 90 // should have stopped if the MidiManager is going to be destructed. |
| 86 DCHECK(clients_.empty()); | 91 session_thread_runner_->PostTask( |
| 87 DCHECK(!pending_clients_.empty()); | 92 FROM_HERE, |
| 88 DCHECK(!initialized_); | 93 base::Bind(&MidiManager::CompleteInitializationInternal, |
| 89 initialized_ = true; | 94 base::Unretained(this), |
| 90 result_ = result; | 95 result)); |
| 91 | 96 CompleteInitializationInternal(result); |
|
yukawa
2014/05/04 23:16:02
Do we intentionally call CompleteInitializationInt
Takashi Toyoshima
2014/05/05 00:45:08
Oops, I removed it once, but somehow restored...
T
| |
| 92 for (PendingClientMap::iterator it = pending_clients_.begin(); | |
| 93 it != pending_clients_.end(); | |
| 94 ++it) { | |
| 95 if (result_ == MIDI_OK) | |
| 96 clients_.insert(it->second); | |
| 97 it->second->CompleteStartSession(it->first, result_); | |
| 98 } | |
| 99 pending_clients_.clear(); | |
| 100 } | 97 } |
| 101 | 98 |
| 102 void MidiManager::AddInputPort(const MidiPortInfo& info) { | 99 void MidiManager::AddInputPort(const MidiPortInfo& info) { |
| 103 input_ports_.push_back(info); | 100 input_ports_.push_back(info); |
| 104 } | 101 } |
| 105 | 102 |
| 106 void MidiManager::AddOutputPort(const MidiPortInfo& info) { | 103 void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
| 107 output_ports_.push_back(info); | 104 output_ports_.push_back(info); |
| 108 } | 105 } |
| 109 | 106 |
| 110 void MidiManager::ReceiveMidiData( | 107 void MidiManager::ReceiveMidiData( |
| 111 uint32 port_index, | 108 uint32 port_index, |
| 112 const uint8* data, | 109 const uint8* data, |
| 113 size_t length, | 110 size_t length, |
| 114 double timestamp) { | 111 double timestamp) { |
| 115 base::AutoLock auto_lock(clients_lock_); | 112 base::AutoLock auto_lock(clients_lock_); |
| 116 | 113 |
| 117 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) | 114 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
| 118 (*i)->ReceiveMidiData(port_index, data, length, timestamp); | 115 (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
| 119 } | 116 } |
| 120 | 117 |
| 118 void MidiManager::CompleteInitializationInternal(MidiResult result) { | |
| 119 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); | |
| 120 | |
| 121 base::AutoLock auto_lock(clients_lock_); | |
| 122 DCHECK(clients_.empty()); | |
| 123 DCHECK(!pending_clients_.empty()); | |
| 124 DCHECK(!initialized_); | |
| 125 initialized_ = true; | |
| 126 result_ = result; | |
| 127 | |
| 128 for (PendingClientMap::iterator it = pending_clients_.begin(); | |
| 129 it != pending_clients_.end(); | |
| 130 ++it) { | |
| 131 if (result_ == MIDI_OK) | |
| 132 clients_.insert(it->second); | |
| 133 it->second->CompleteStartSession(it->first, result_); | |
| 134 } | |
| 135 pending_clients_.clear(); | |
| 136 } | |
| 137 | |
| 121 } // namespace media | 138 } // namespace media |
| OLD | NEW |