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 |
18 MidiManager::MidiManager() | 21 MidiManager::MidiManager() |
19 : initialized_(false), | 22 : initialized_(false), |
20 result_(MIDI_NOT_SUPPORTED) { | 23 result_(MIDI_NOT_SUPPORTED) { |
21 } | 24 } |
22 | 25 |
23 MidiManager::~MidiManager() { | 26 MidiManager::~MidiManager() { |
24 } | 27 } |
25 | 28 |
26 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { | 29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
27 bool session_is_ready; | 30 bool session_is_ready; |
28 bool session_needs_initialization = false; | 31 bool session_needs_initialization = false; |
29 | 32 |
30 { | 33 { |
31 base::AutoLock auto_lock(clients_lock_); | 34 base::AutoLock auto_lock(lock_); |
32 session_is_ready = initialized_; | 35 session_is_ready = initialized_; |
33 if (!session_is_ready) { | 36 if (!session_is_ready) { |
34 // Call StartInitialization() only for the first request. | 37 // Call StartInitialization() only for the first request. |
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; |
55 { | 60 { |
56 base::AutoLock auto_lock(clients_lock_); | 61 base::AutoLock auto_lock(lock_); |
57 if (result_ == MIDI_OK) | 62 if (result_ == MIDI_OK) |
58 clients_.insert(client); | 63 clients_.insert(client); |
59 result = result_; | 64 result = result_; |
60 } | 65 } |
61 client->CompleteStartSession(client_id, result); | 66 client->CompleteStartSession(client_id, result); |
62 } | 67 } |
63 | 68 |
64 void MidiManager::EndSession(MidiManagerClient* client) { | 69 void MidiManager::EndSession(MidiManagerClient* client) { |
65 base::AutoLock auto_lock(clients_lock_); | 70 base::AutoLock auto_lock(lock_); |
66 ClientList::iterator i = clients_.find(client); | 71 ClientList::iterator i = clients_.find(client); |
67 if (i != clients_.end()) | 72 if (i != clients_.end()) |
68 clients_.erase(i); | 73 clients_.erase(i); |
69 } | 74 } |
70 | 75 |
71 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, | 76 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
72 uint32 port_index, | 77 uint32 port_index, |
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 | |
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 } | 96 } |
101 | 97 |
102 void MidiManager::AddInputPort(const MidiPortInfo& info) { | 98 void MidiManager::AddInputPort(const MidiPortInfo& info) { |
103 input_ports_.push_back(info); | 99 input_ports_.push_back(info); |
104 } | 100 } |
105 | 101 |
106 void MidiManager::AddOutputPort(const MidiPortInfo& info) { | 102 void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
107 output_ports_.push_back(info); | 103 output_ports_.push_back(info); |
108 } | 104 } |
109 | 105 |
110 void MidiManager::ReceiveMidiData( | 106 void MidiManager::ReceiveMidiData( |
111 uint32 port_index, | 107 uint32 port_index, |
112 const uint8* data, | 108 const uint8* data, |
113 size_t length, | 109 size_t length, |
114 double timestamp) { | 110 double timestamp) { |
115 base::AutoLock auto_lock(clients_lock_); | 111 base::AutoLock auto_lock(lock_); |
116 | 112 |
117 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) | 113 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
118 (*i)->ReceiveMidiData(port_index, data, length, timestamp); | 114 (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
119 } | 115 } |
120 | 116 |
| 117 void MidiManager::CompleteInitializationInternal(MidiResult result) { |
| 118 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
| 119 |
| 120 base::AutoLock auto_lock(lock_); |
| 121 DCHECK(clients_.empty()); |
| 122 DCHECK(!pending_clients_.empty()); |
| 123 DCHECK(!initialized_); |
| 124 initialized_ = true; |
| 125 result_ = result; |
| 126 |
| 127 for (PendingClientMap::iterator it = pending_clients_.begin(); |
| 128 it != pending_clients_.end(); |
| 129 ++it) { |
| 130 if (result_ == MIDI_OK) |
| 131 clients_.insert(it->second); |
| 132 it->second->CompleteStartSession(it->first, result_); |
| 133 } |
| 134 pending_clients_.clear(); |
| 135 } |
| 136 |
121 } // namespace media | 137 } // namespace media |
OLD | NEW |