OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/dynamically_initialized_midi_manager_win.h" | 5 #include "media/midi/dynamically_initialized_midi_manager_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <mmreg.h> | 9 #include <mmreg.h> |
10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/bind_helpers.h" | |
15 #include "base/callback.h" | 16 #include "base/callback.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
18 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
23 #include "media/midi/message_util.h" | |
22 #include "media/midi/midi_port_info.h" | 24 #include "media/midi/midi_port_info.h" |
23 #include "media/midi/midi_service.h" | 25 #include "media/midi/midi_service.h" |
24 | 26 |
25 namespace midi { | 27 namespace midi { |
26 | 28 |
29 // Forward declaration of PortManager for anonymous functions and internal | |
30 // classes to use it. | |
31 class DynamicallyInitializedMidiManagerWin::PortManager { | |
32 public: | |
33 // Calculates event time from elapsed time that system provides. | |
34 base::TimeTicks CalculateInEventTime(size_t index, uint32_t elapsed_ms) const; | |
35 | |
36 // Registers HMIDIIN handle to resolve port index. | |
37 void RegisterInHandle(HMIDIIN handle, size_t index); | |
38 | |
39 // Unregisters HMIDIIN handle. | |
40 void UnregisterInHandle(HMIDIIN handle); | |
41 | |
42 // Finds HMIDIIN handle and fullfil |out_index| with the port index. | |
43 bool FindHandle(HMIDIIN hmi, size_t* out_index); | |
44 | |
45 // Restores used input buffer for the next data receive. | |
46 void RestoreInBuffer(size_t index); | |
47 | |
48 // Ports accessors. | |
49 std::vector<std::unique_ptr<InPort>>* inputs() { return &input_ports_; } | |
50 std::vector<std::unique_ptr<OutPort>>* outputs() { return &output_ports_; } | |
51 | |
52 // Handles MIDI input port callbacks that runs on a system provided thread. | |
53 static void CALLBACK HandleMidiInCallback(HMIDIIN hmi, | |
54 UINT msg, | |
55 DWORD_PTR instance, | |
56 DWORD_PTR param1, | |
57 DWORD_PTR param2); | |
58 | |
59 private: | |
60 // Holds all MIDI input or output ports connected once. | |
61 std::vector<std::unique_ptr<InPort>> input_ports_; | |
62 std::vector<std::unique_ptr<OutPort>> output_ports_; | |
63 | |
64 // Map to resolve MIDI input port index from HMIDIIN. | |
65 std::map<HMIDIIN, size_t> hmidiin_to_index_map_; | |
66 }; | |
67 | |
27 namespace { | 68 namespace { |
28 | 69 |
29 // Assumes that nullptr represents an invalid MIDI handle. | 70 // Assumes that nullptr represents an invalid MIDI handle. |
30 constexpr HMIDIIN kInvalidInHandle = nullptr; | 71 constexpr HMIDIIN kInvalidInHandle = nullptr; |
31 constexpr HMIDIOUT kInvalidOutHandle = nullptr; | 72 constexpr HMIDIOUT kInvalidOutHandle = nullptr; |
32 | 73 |
74 // Defines input buffer size. | |
75 constexpr size_t kBufferLength = 32 * 1024; | |
76 | |
33 // Global variables to identify MidiManager instance. | 77 // Global variables to identify MidiManager instance. |
34 constexpr int kInvalidInstanceId = -1; | 78 constexpr int kInvalidInstanceId = -1; |
35 int g_active_instance_id = kInvalidInstanceId; | 79 int g_active_instance_id = kInvalidInstanceId; |
36 DynamicallyInitializedMidiManagerWin* g_manager_instance = nullptr; | 80 DynamicallyInitializedMidiManagerWin* g_manager_instance = nullptr; |
37 | 81 |
38 // Obtains base::Lock instance pointer to lock instance_id. | 82 // Obtains base::Lock instance pointer to lock instance_id. |
39 base::Lock* GetInstanceIdLock() { | 83 base::Lock* GetInstanceIdLock() { |
40 static base::Lock* lock = new base::Lock; | 84 static base::Lock* lock = new base::Lock; |
41 return lock; | 85 return lock; |
42 } | 86 } |
(...skipping 26 matching lines...) Expand all Loading... | |
69 base::AutoLock lock(*GetInstanceIdLock()); | 113 base::AutoLock lock(*GetInstanceIdLock()); |
70 if (instance_id != g_active_instance_id) | 114 if (instance_id != g_active_instance_id) |
71 return; | 115 return; |
72 } | 116 } |
73 task.Run(); | 117 task.Run(); |
74 } | 118 } |
75 | 119 |
76 // TODO(toyoshim): Factor out TaskRunner related functionaliries above, and | 120 // TODO(toyoshim): Factor out TaskRunner related functionaliries above, and |
77 // deprecate MidiScheduler. It should be available via MidiManager::scheduler(). | 121 // deprecate MidiScheduler. It should be available via MidiManager::scheduler(). |
78 | 122 |
123 // Utility class to handle MIDIHDR struct safely. | |
124 class MIDIHDRDeleter { | |
125 public: | |
126 void operator()(LPMIDIHDR header) { | |
127 if (!header) | |
128 return; | |
129 delete[] static_cast<char*>(header->lpData); | |
130 delete header; | |
131 } | |
132 }; | |
133 | |
134 using ScopedMIDIHDR = std::unique_ptr<MIDIHDR, MIDIHDRDeleter>; | |
135 | |
136 ScopedMIDIHDR CreateMIDIHDR(size_t size) { | |
137 ScopedMIDIHDR hdr(new MIDIHDR); | |
138 ZeroMemory(hdr.get(), sizeof(*hdr)); | |
139 hdr->lpData = new char[size]; | |
140 hdr->dwBufferLength = static_cast<DWORD>(size); | |
141 return hdr; | |
142 } | |
143 | |
79 // Helper functions to close MIDI device handles on TaskRunner asynchronously. | 144 // Helper functions to close MIDI device handles on TaskRunner asynchronously. |
80 void FinalizeInPort(HMIDIIN handle) { | 145 void FinalizeInPort(HMIDIIN handle, ScopedMIDIHDR hdr) { |
146 // Resets the device. This stops receiving messages, and allows to release | |
147 // registered buffer headers. Otherwise, midiInUnprepareHeader() and | |
148 // midiInClose() will fail with MIDIERR_STILLPLAYING. | |
149 midiInReset(handle); | |
150 | |
151 if (hdr) | |
152 midiInUnprepareHeader(handle, hdr.get(), sizeof(*hdr)); | |
81 midiInClose(handle); | 153 midiInClose(handle); |
82 } | 154 } |
83 | 155 |
84 void FinalizeOutPort(HMIDIOUT handle) { | 156 void FinalizeOutPort(HMIDIOUT handle) { |
85 midiOutClose(handle); | 157 midiOutClose(handle); |
86 } | 158 } |
87 | 159 |
88 // Handles MIDI input port callbacks that runs on a system provided thread. | |
89 void CALLBACK HandleMidiInCallback(HMIDIIN hmi, | |
90 UINT msg, | |
91 DWORD_PTR instance, | |
92 DWORD_PTR param1, | |
93 DWORD_PTR param2) { | |
94 // TODO(toyoshim): Following patches will implement actual functions. | |
95 } | |
96 | |
97 // Handles MIDI output port callbacks that runs on a system provided thread. | 160 // Handles MIDI output port callbacks that runs on a system provided thread. |
98 void CALLBACK HandleMidiOutCallback(HMIDIOUT hmo, | 161 void CALLBACK HandleMidiOutCallback(HMIDIOUT hmo, |
99 UINT msg, | 162 UINT msg, |
100 DWORD_PTR instance, | 163 DWORD_PTR instance, |
101 DWORD_PTR param1, | 164 DWORD_PTR param1, |
102 DWORD_PTR param2) { | 165 DWORD_PTR param2) { |
103 // TODO(toyoshim): Following patches will implement actual functions. | 166 // TODO(toyoshim): Following patches will implement actual functions. |
104 } | 167 } |
105 | 168 |
106 // All instances of Port subclasses are always accessed behind a lock of | 169 // All instances of Port subclasses are always accessed behind a lock of |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 const uint32_t driver_version_; | 248 const uint32_t driver_version_; |
186 const std::string product_name_; | 249 const std::string product_name_; |
187 MidiPortInfo info_; | 250 MidiPortInfo info_; |
188 }; // class Port | 251 }; // class Port |
189 | 252 |
190 } // namespace | 253 } // namespace |
191 | 254 |
192 // TODO(toyoshim): Following patches will implement actual functions. | 255 // TODO(toyoshim): Following patches will implement actual functions. |
193 class DynamicallyInitializedMidiManagerWin::InPort final : public Port { | 256 class DynamicallyInitializedMidiManagerWin::InPort final : public Port { |
194 public: | 257 public: |
195 InPort(UINT device_id, const MIDIINCAPS2W& caps) | 258 InPort(DynamicallyInitializedMidiManagerWin* manager, |
259 int instance_id, | |
260 UINT device_id, | |
261 const MIDIINCAPS2W& caps) | |
196 : Port("input", | 262 : Port("input", |
197 device_id, | 263 device_id, |
198 caps.wMid, | 264 caps.wMid, |
199 caps.wPid, | 265 caps.wPid, |
200 caps.vDriverVersion, | 266 caps.vDriverVersion, |
201 base::WideToUTF8( | 267 base::WideToUTF8( |
202 base::string16(caps.szPname, wcslen(caps.szPname)))), | 268 base::string16(caps.szPname, wcslen(caps.szPname)))), |
203 in_handle_(kInvalidInHandle) {} | 269 manager_(manager), |
270 in_handle_(kInvalidInHandle), | |
271 instance_id_(instance_id) {} | |
204 | 272 |
205 static std::vector<std::unique_ptr<InPort>> EnumerateActivePorts() { | 273 static std::vector<std::unique_ptr<InPort>> EnumerateActivePorts( |
274 DynamicallyInitializedMidiManagerWin* manager, | |
275 int instance_id) { | |
206 std::vector<std::unique_ptr<InPort>> ports; | 276 std::vector<std::unique_ptr<InPort>> ports; |
207 const UINT num_devices = midiInGetNumDevs(); | 277 const UINT num_devices = midiInGetNumDevs(); |
208 for (UINT device_id = 0; device_id < num_devices; ++device_id) { | 278 for (UINT device_id = 0; device_id < num_devices; ++device_id) { |
209 MIDIINCAPS2W caps; | 279 MIDIINCAPS2W caps; |
210 MMRESULT result = midiInGetDevCaps( | 280 MMRESULT result = midiInGetDevCaps( |
211 device_id, reinterpret_cast<LPMIDIINCAPSW>(&caps), sizeof(caps)); | 281 device_id, reinterpret_cast<LPMIDIINCAPSW>(&caps), sizeof(caps)); |
212 if (result != MMSYSERR_NOERROR) { | 282 if (result != MMSYSERR_NOERROR) { |
213 LOG(ERROR) << "midiInGetDevCaps fails on device " << device_id; | 283 LOG(ERROR) << "midiInGetDevCaps fails on device " << device_id; |
214 continue; | 284 continue; |
215 } | 285 } |
216 ports.push_back(base::MakeUnique<InPort>(device_id, caps)); | 286 ports.push_back( |
287 base::MakeUnique<InPort>(manager, instance_id, device_id, caps)); | |
217 } | 288 } |
218 return ports; | 289 return ports; |
219 } | 290 } |
220 | 291 |
221 void Finalize(scoped_refptr<base::SingleThreadTaskRunner> runner) { | 292 void Finalize(scoped_refptr<base::SingleThreadTaskRunner> runner) { |
222 if (in_handle_ != kInvalidInHandle) { | 293 if (in_handle_ != kInvalidInHandle) { |
223 runner->PostTask(FROM_HERE, base::Bind(&FinalizeInPort, in_handle_)); | 294 runner->PostTask( |
295 FROM_HERE, | |
296 base::Bind(&FinalizeInPort, in_handle_, base::Passed(&hdr_))); | |
297 manager_->port_manager()->UnregisterInHandle(in_handle_); | |
224 in_handle_ = kInvalidInHandle; | 298 in_handle_ = kInvalidInHandle; |
225 } | 299 } |
226 } | 300 } |
227 | 301 |
302 base::TimeTicks CalculateInEventTime(uint32_t elapsed_ms) const { | |
303 return start_time_ + base::TimeDelta::FromMilliseconds(elapsed_ms); | |
304 } | |
305 | |
306 void RestoreBuffer() { | |
307 if (in_handle_ == kInvalidInHandle || !hdr_) | |
308 return; | |
309 midiInAddBuffer(in_handle_, hdr_.get(), sizeof(*hdr_)); | |
310 } | |
311 | |
228 void NotifyPortStateSet(DynamicallyInitializedMidiManagerWin* manager) { | 312 void NotifyPortStateSet(DynamicallyInitializedMidiManagerWin* manager) { |
229 manager->PostReplyTask( | 313 manager->PostReplyTask( |
230 base::Bind(&DynamicallyInitializedMidiManagerWin::SetInputPortState, | 314 base::Bind(&DynamicallyInitializedMidiManagerWin::SetInputPortState, |
231 base::Unretained(manager), index_, info_.state)); | 315 base::Unretained(manager), index_, info_.state)); |
232 } | 316 } |
233 | 317 |
234 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) { | 318 void NotifyPortAdded(DynamicallyInitializedMidiManagerWin* manager) { |
235 manager->PostReplyTask( | 319 manager->PostReplyTask( |
236 base::Bind(&DynamicallyInitializedMidiManagerWin::AddInputPort, | 320 base::Bind(&DynamicallyInitializedMidiManagerWin::AddInputPort, |
237 base::Unretained(manager), info_)); | 321 base::Unretained(manager), info_)); |
238 } | 322 } |
239 | 323 |
240 // Port overrides: | 324 // Port overrides: |
241 bool Disconnect() override { | 325 bool Disconnect() override { |
242 if (in_handle_ != kInvalidInHandle) { | 326 if (in_handle_ != kInvalidInHandle) { |
243 // Following API call may fail because device was already disconnected. | 327 // Following API call may fail because device was already disconnected. |
244 // But just in case. | 328 // But just in case. |
245 midiInClose(in_handle_); | 329 midiInClose(in_handle_); |
330 manager_->port_manager()->UnregisterInHandle(in_handle_); | |
246 in_handle_ = kInvalidInHandle; | 331 in_handle_ = kInvalidInHandle; |
247 } | 332 } |
248 return Port::Disconnect(); | 333 return Port::Disconnect(); |
249 } | 334 } |
250 | 335 |
251 void Open() override { | 336 void Open() override { |
252 // TODO(toyoshim): Pass instance_id to implement HandleMidiInCallback. | 337 MMRESULT result = midiInOpen( |
253 MMRESULT result = | 338 &in_handle_, device_id_, |
254 midiInOpen(&in_handle_, device_id_, | 339 reinterpret_cast<DWORD_PTR>(&PortManager::HandleMidiInCallback), |
255 reinterpret_cast<DWORD_PTR>(&HandleMidiInCallback), 0, | 340 instance_id_, CALLBACK_FUNCTION); |
256 CALLBACK_FUNCTION); | |
257 if (result == MMSYSERR_NOERROR) { | 341 if (result == MMSYSERR_NOERROR) { |
342 hdr_ = CreateMIDIHDR(kBufferLength); | |
343 result = midiInPrepareHeader(in_handle_, hdr_.get(), sizeof(*hdr_)); | |
344 } | |
345 if (result != MMSYSERR_NOERROR) | |
346 in_handle_ = kInvalidInHandle; | |
347 if (result == MMSYSERR_NOERROR) | |
348 result = midiInAddBuffer(in_handle_, hdr_.get(), sizeof(*hdr_)); | |
349 if (result == MMSYSERR_NOERROR) | |
350 result = midiInStart(in_handle_); | |
351 if (result == MMSYSERR_NOERROR) { | |
352 start_time_ = base::TimeTicks::Now(); | |
353 manager_->port_manager()->RegisterInHandle(in_handle_, index_); | |
yukawa
2017/04/25 17:28:47
If |HandleMidiInCallback| receives |MIM_LONGDATA|
Takashi Toyoshima
2017/04/26 05:35:15
TaskLock is used to avoid such race situations.
O
yukawa
2017/04/27 19:45:50
Thank you for the explanation.
On 2017/04/26 05:3
| |
258 Port::Open(); | 354 Port::Open(); |
259 } else { | 355 } else { |
260 in_handle_ = kInvalidInHandle; | 356 if (in_handle_ != kInvalidInHandle) { |
357 midiInUnprepareHeader(in_handle_, hdr_.get(), sizeof(*hdr_)); | |
358 hdr_.reset(); | |
359 midiInClose(in_handle_); | |
360 in_handle_ = kInvalidInHandle; | |
361 } | |
261 Disconnect(); | 362 Disconnect(); |
262 } | 363 } |
263 } | 364 } |
264 | 365 |
265 private: | 366 private: |
367 DynamicallyInitializedMidiManagerWin* manager_; | |
266 HMIDIIN in_handle_; | 368 HMIDIIN in_handle_; |
369 ScopedMIDIHDR hdr_; | |
370 base::TimeTicks start_time_; | |
371 const int instance_id_; | |
267 }; | 372 }; |
268 | 373 |
269 // TODO(toyoshim): Following patches will implement actual functions. | 374 // TODO(toyoshim): Following patches will implement actual functions. |
270 class DynamicallyInitializedMidiManagerWin::OutPort final : public Port { | 375 class DynamicallyInitializedMidiManagerWin::OutPort final : public Port { |
271 public: | 376 public: |
272 OutPort(UINT device_id, const MIDIOUTCAPS2W& caps) | 377 OutPort(UINT device_id, const MIDIOUTCAPS2W& caps) |
273 : Port("output", | 378 : Port("output", |
274 device_id, | 379 device_id, |
275 caps.wMid, | 380 caps.wMid, |
276 caps.wPid, | 381 caps.wPid, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 } else { | 452 } else { |
348 out_handle_ = kInvalidOutHandle; | 453 out_handle_ = kInvalidOutHandle; |
349 Disconnect(); | 454 Disconnect(); |
350 } | 455 } |
351 } | 456 } |
352 | 457 |
353 const bool software_; | 458 const bool software_; |
354 HMIDIOUT out_handle_; | 459 HMIDIOUT out_handle_; |
355 }; | 460 }; |
356 | 461 |
462 base::TimeTicks | |
463 DynamicallyInitializedMidiManagerWin::PortManager::CalculateInEventTime( | |
464 size_t index, | |
465 uint32_t elapsed_ms) const { | |
466 GetTaskLock()->AssertAcquired(); | |
467 CHECK_GT(input_ports_.size(), index); | |
468 return input_ports_[index]->CalculateInEventTime(elapsed_ms); | |
469 } | |
470 | |
471 void DynamicallyInitializedMidiManagerWin::PortManager::RegisterInHandle( | |
472 HMIDIIN handle, | |
473 size_t index) { | |
474 GetTaskLock()->AssertAcquired(); | |
475 hmidiin_to_index_map_[handle] = index; | |
476 } | |
477 | |
478 void DynamicallyInitializedMidiManagerWin::PortManager::UnregisterInHandle( | |
479 HMIDIIN handle) { | |
480 GetTaskLock()->AssertAcquired(); | |
481 hmidiin_to_index_map_.erase(handle); | |
482 } | |
483 | |
484 bool DynamicallyInitializedMidiManagerWin::PortManager::FindHandle( | |
485 HMIDIIN hmi, | |
486 size_t* out_index) { | |
487 GetTaskLock()->AssertAcquired(); | |
488 auto found = hmidiin_to_index_map_.find(hmi); | |
489 if (found == hmidiin_to_index_map_.end()) | |
490 return false; | |
491 *out_index = found->second; | |
492 return true; | |
493 } | |
494 | |
495 void DynamicallyInitializedMidiManagerWin::PortManager::RestoreInBuffer( | |
496 size_t index) { | |
497 GetTaskLock()->AssertAcquired(); | |
498 CHECK_GT(input_ports_.size(), index); | |
499 input_ports_[index]->RestoreBuffer(); | |
500 } | |
501 | |
502 void CALLBACK | |
503 DynamicallyInitializedMidiManagerWin::PortManager::HandleMidiInCallback( | |
504 HMIDIIN hmi, | |
505 UINT msg, | |
506 DWORD_PTR instance, | |
507 DWORD_PTR param1, | |
508 DWORD_PTR param2) { | |
509 if (msg != MIM_DATA && msg != MIM_LONGDATA) | |
510 return; | |
511 int instance_id = static_cast<int>(instance); | |
512 DynamicallyInitializedMidiManagerWin* manager = nullptr; | |
513 | |
514 // Use |g_task_lock| so to ensure the instance can keep alive while running, | |
515 // and to access member variables that are used on TaskRunner. | |
516 base::AutoLock task_lock(*GetTaskLock()); | |
517 { | |
518 base::AutoLock lock(*GetInstanceIdLock()); | |
519 if (instance_id != g_active_instance_id) | |
520 return; | |
521 manager = g_manager_instance; | |
522 } | |
523 | |
524 size_t index; | |
525 if (!manager->port_manager()->FindHandle(hmi, &index)) | |
526 return; | |
527 | |
528 DCHECK(msg == MIM_DATA || msg == MIM_LONGDATA); | |
529 if (msg == MIM_DATA) { | |
530 const uint8_t status_byte = static_cast<uint8_t>(param1 & 0xff); | |
531 const uint8_t first_data_byte = static_cast<uint8_t>((param1 >> 8) & 0xff); | |
532 const uint8_t second_data_byte = | |
533 static_cast<uint8_t>((param1 >> 16) & 0xff); | |
534 const uint8_t kData[] = {status_byte, first_data_byte, second_data_byte}; | |
535 const size_t len = GetMessageLength(status_byte); | |
536 DCHECK_LE(len, arraysize(kData)); | |
537 std::vector<uint8_t> data; | |
538 data.assign(kData, kData + len); | |
539 manager->PostReplyTask(base::Bind( | |
540 &DynamicallyInitializedMidiManagerWin::ReceiveMidiData, | |
541 base::Unretained(manager), index, data, | |
542 manager->port_manager()->CalculateInEventTime(index, param2))); | |
543 } else { | |
544 DCHECK_EQ(static_cast<UINT>(MIM_LONGDATA), msg); | |
545 LPMIDIHDR hdr = reinterpret_cast<LPMIDIHDR>(param1); | |
546 if (hdr->dwBytesRecorded > 0) { | |
547 const uint8_t* src = reinterpret_cast<const uint8_t*>(hdr->lpData); | |
548 std::vector<uint8_t> data; | |
549 data.assign(src, src + hdr->dwBytesRecorded); | |
550 manager->PostReplyTask(base::Bind( | |
551 &DynamicallyInitializedMidiManagerWin::ReceiveMidiData, | |
552 base::Unretained(manager), index, data, | |
553 manager->port_manager()->CalculateInEventTime(index, param2))); | |
554 } | |
555 manager->PostTask(base::Bind( | |
556 &DynamicallyInitializedMidiManagerWin::PortManager::RestoreInBuffer, | |
557 base::Unretained(manager->port_manager()), index)); | |
558 } | |
559 } | |
560 | |
357 DynamicallyInitializedMidiManagerWin::DynamicallyInitializedMidiManagerWin( | 561 DynamicallyInitializedMidiManagerWin::DynamicallyInitializedMidiManagerWin( |
358 MidiService* service) | 562 MidiService* service) |
359 : MidiManager(service), instance_id_(IssueNextInstanceId()) { | 563 : MidiManager(service), |
564 instance_id_(IssueNextInstanceId()), | |
565 port_manager_(base::MakeUnique<PortManager>()) { | |
360 base::AutoLock lock(*GetInstanceIdLock()); | 566 base::AutoLock lock(*GetInstanceIdLock()); |
361 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | 567 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
362 | 568 |
363 // Obtains the task runner for the current thread that hosts this instnace. | 569 // Obtains the task runner for the current thread that hosts this instnace. |
364 thread_runner_ = base::ThreadTaskRunnerHandle::Get(); | 570 thread_runner_ = base::ThreadTaskRunnerHandle::Get(); |
365 } | 571 } |
366 | 572 |
367 DynamicallyInitializedMidiManagerWin::~DynamicallyInitializedMidiManagerWin() { | 573 DynamicallyInitializedMidiManagerWin::~DynamicallyInitializedMidiManagerWin() { |
368 base::AutoLock lock(*GetInstanceIdLock()); | 574 base::AutoLock lock(*GetInstanceIdLock()); |
369 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | 575 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
370 CHECK(thread_runner_->BelongsToCurrentThread()); | 576 CHECK(thread_runner_->BelongsToCurrentThread()); |
371 } | 577 } |
372 | 578 |
373 void DynamicallyInitializedMidiManagerWin::PostReplyTask( | |
374 const base::Closure& task) { | |
375 thread_runner_->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); | |
376 } | |
377 | |
378 void DynamicallyInitializedMidiManagerWin::StartInitialization() { | 579 void DynamicallyInitializedMidiManagerWin::StartInitialization() { |
379 { | 580 { |
380 base::AutoLock lock(*GetInstanceIdLock()); | 581 base::AutoLock lock(*GetInstanceIdLock()); |
381 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); | 582 CHECK_EQ(kInvalidInstanceId, g_active_instance_id); |
382 g_active_instance_id = instance_id_; | 583 g_active_instance_id = instance_id_; |
383 CHECK_EQ(nullptr, g_manager_instance); | 584 CHECK_EQ(nullptr, g_manager_instance); |
384 g_manager_instance = this; | 585 g_manager_instance = this; |
385 } | 586 } |
386 // Registers on the I/O thread to be notified on the I/O thread. | 587 // Registers on the I/O thread to be notified on the I/O thread. |
387 CHECK(thread_runner_->BelongsToCurrentThread()); | 588 CHECK(thread_runner_->BelongsToCurrentThread()); |
(...skipping 21 matching lines...) Expand all Loading... | |
409 // Tasks that did not started yet will do nothing after invalidate the | 610 // Tasks that did not started yet will do nothing after invalidate the |
410 // instance ID above. | 611 // instance ID above. |
411 // Behind the lock below, we can safely access all members for finalization | 612 // Behind the lock below, we can safely access all members for finalization |
412 // even on the I/O thread. | 613 // even on the I/O thread. |
413 base::AutoLock lock(*GetTaskLock()); | 614 base::AutoLock lock(*GetTaskLock()); |
414 | 615 |
415 // Posts tasks that finalize each device port without MidiManager instance | 616 // Posts tasks that finalize each device port without MidiManager instance |
416 // on TaskRunner. If another MidiManager instance is created, its | 617 // on TaskRunner. If another MidiManager instance is created, its |
417 // initialization runs on the same task runner after all tasks posted here | 618 // initialization runs on the same task runner after all tasks posted here |
418 // finish. | 619 // finish. |
419 for (const auto& port : input_ports_) | 620 for (const auto& port : *port_manager_->inputs()) |
420 port->Finalize(service()->GetTaskRunner(kTaskRunner)); | 621 port->Finalize(service()->GetTaskRunner(kTaskRunner)); |
421 for (const auto& port : output_ports_) | 622 for (const auto& port : *port_manager_->outputs()) |
422 port->Finalize(service()->GetTaskRunner(kTaskRunner)); | 623 port->Finalize(service()->GetTaskRunner(kTaskRunner)); |
423 } | 624 } |
424 | 625 |
425 void DynamicallyInitializedMidiManagerWin::DispatchSendMidiData( | 626 void DynamicallyInitializedMidiManagerWin::DispatchSendMidiData( |
426 MidiManagerClient* client, | 627 MidiManagerClient* client, |
427 uint32_t port_index, | 628 uint32_t port_index, |
428 const std::vector<uint8_t>& data, | 629 const std::vector<uint8_t>& data, |
429 double timestamp) { | 630 double timestamp) { |
430 // TODO(toyoshim): Following patches will implement. | 631 // TODO(toyoshim): Following patches will implement. |
431 } | 632 } |
(...skipping 10 matching lines...) Expand all Loading... | |
442 return; | 643 return; |
443 case base::SystemMonitor::DEVTYPE_UNKNOWN: { | 644 case base::SystemMonitor::DEVTYPE_UNKNOWN: { |
444 PostTask(base::Bind( | 645 PostTask(base::Bind( |
445 &DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner, | 646 &DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner, |
446 base::Unretained(this))); | 647 base::Unretained(this))); |
447 break; | 648 break; |
448 } | 649 } |
449 } | 650 } |
450 } | 651 } |
451 | 652 |
653 void DynamicallyInitializedMidiManagerWin::ReceiveMidiData( | |
654 uint32_t index, | |
655 const std::vector<uint8_t>& data, | |
656 base::TimeTicks time) { | |
657 MidiManager::ReceiveMidiData(index, data.data(), data.size(), time); | |
658 } | |
659 | |
452 void DynamicallyInitializedMidiManagerWin::PostTask(const base::Closure& task) { | 660 void DynamicallyInitializedMidiManagerWin::PostTask(const base::Closure& task) { |
453 service() | 661 service() |
454 ->GetTaskRunner(kTaskRunner) | 662 ->GetTaskRunner(kTaskRunner) |
455 ->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); | 663 ->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); |
456 } | 664 } |
457 | 665 |
666 void DynamicallyInitializedMidiManagerWin::PostReplyTask( | |
667 const base::Closure& task) { | |
668 thread_runner_->PostTask(FROM_HERE, base::Bind(&RunTask, instance_id_, task)); | |
669 } | |
670 | |
458 void DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner() { | 671 void DynamicallyInitializedMidiManagerWin::InitializeOnTaskRunner() { |
459 UpdateDeviceListOnTaskRunner(); | 672 UpdateDeviceListOnTaskRunner(); |
460 PostReplyTask( | 673 PostReplyTask( |
461 base::Bind(&DynamicallyInitializedMidiManagerWin::CompleteInitialization, | 674 base::Bind(&DynamicallyInitializedMidiManagerWin::CompleteInitialization, |
462 base::Unretained(this), mojom::Result::OK)); | 675 base::Unretained(this), mojom::Result::OK)); |
463 } | 676 } |
464 | 677 |
465 void DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner() { | 678 void DynamicallyInitializedMidiManagerWin::UpdateDeviceListOnTaskRunner() { |
466 std::vector<std::unique_ptr<InPort>> active_input_ports = | 679 std::vector<std::unique_ptr<InPort>> active_input_ports = |
467 InPort::EnumerateActivePorts(); | 680 InPort::EnumerateActivePorts(this, instance_id_); |
468 ReflectActiveDeviceList(this, &input_ports_, &active_input_ports); | 681 ReflectActiveDeviceList(this, port_manager_->inputs(), &active_input_ports); |
469 | 682 |
470 std::vector<std::unique_ptr<OutPort>> active_output_ports = | 683 std::vector<std::unique_ptr<OutPort>> active_output_ports = |
471 OutPort::EnumerateActivePorts(); | 684 OutPort::EnumerateActivePorts(); |
472 ReflectActiveDeviceList(this, &output_ports_, &active_output_ports); | 685 ReflectActiveDeviceList(this, port_manager_->outputs(), &active_output_ports); |
473 | 686 |
474 // TODO(toyoshim): This method may run before internal MIDI device lists that | 687 // TODO(toyoshim): This method may run before internal MIDI device lists that |
475 // Windows manages were updated. This may be because MIDI driver may be loaded | 688 // Windows manages were updated. This may be because MIDI driver may be loaded |
476 // after the raw device list was updated. To avoid this problem, we may want | 689 // after the raw device list was updated. To avoid this problem, we may want |
477 // to retry device check later if no changes are detected here. | 690 // to retry device check later if no changes are detected here. |
478 } | 691 } |
479 | 692 |
480 template <typename T> | 693 template <typename T> |
481 void DynamicallyInitializedMidiManagerWin::ReflectActiveDeviceList( | 694 void DynamicallyInitializedMidiManagerWin::ReflectActiveDeviceList( |
482 DynamicallyInitializedMidiManagerWin* manager, | 695 DynamicallyInitializedMidiManagerWin* manager, |
(...skipping 23 matching lines...) Expand all Loading... | |
506 size_t index = known_ports->size(); | 719 size_t index = known_ports->size(); |
507 port->set_index(index); | 720 port->set_index(index); |
508 known_ports->push_back(std::move(port)); | 721 known_ports->push_back(std::move(port)); |
509 (*known_ports)[index]->Connect(); | 722 (*known_ports)[index]->Connect(); |
510 (*known_ports)[index]->NotifyPortAdded(this); | 723 (*known_ports)[index]->NotifyPortAdded(this); |
511 } | 724 } |
512 } | 725 } |
513 } | 726 } |
514 | 727 |
515 } // namespace midi | 728 } // namespace midi |
OLD | NEW |