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_mac.h" | 5 #include "media/midi/midi_manager_mac.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 | 14 |
15 #include <CoreAudio/HostTime.h> | 15 #include <CoreAudio/HostTime.h> |
16 #include <stddef.h> | 16 #include <stddef.h> |
17 | 17 |
18 using base::IntToString; | 18 using base::IntToString; |
19 using base::SysCFStringRefToUTF8; | 19 using base::SysCFStringRefToUTF8; |
20 using std::string; | 20 using std::string; |
| 21 using midi::mojom::PortState; |
21 using midi::mojom::Result; | 22 using midi::mojom::Result; |
22 | 23 |
23 // NB: System MIDI types are pointer types in 32-bit and integer types in | 24 // NB: System MIDI types are pointer types in 32-bit and integer types in |
24 // 64-bit. Therefore, the initialization is the simplest one that satisfies both | 25 // 64-bit. Therefore, the initialization is the simplest one that satisfies both |
25 // (if possible). | 26 // (if possible). |
26 | 27 |
27 namespace midi { | 28 namespace midi { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } else { | 81 } else { |
81 // On connecting some devices, e.g., nano KONTROL2, unknown endpoints | 82 // On connecting some devices, e.g., nano KONTROL2, unknown endpoints |
82 // appear and disappear quickly and they fail on queries. | 83 // appear and disappear quickly and they fail on queries. |
83 // Let's ignore such ghost devices. | 84 // Let's ignore such ghost devices. |
84 // Same problems will happen if the device is disconnected before finishing | 85 // Same problems will happen if the device is disconnected before finishing |
85 // all queries. | 86 // all queries. |
86 DLOG(WARNING) << "Failed to get kMIDIPropertyUniqueID with status " | 87 DLOG(WARNING) << "Failed to get kMIDIPropertyUniqueID with status " |
87 << result; | 88 << result; |
88 } | 89 } |
89 | 90 |
90 const MidiPortState state = MIDI_PORT_OPENED; | 91 const PortState state = PortState::OPENED; |
91 return MidiPortInfo(id, manufacturer, name, version, state); | 92 return MidiPortInfo(id, manufacturer, name, version, state); |
92 } | 93 } |
93 | 94 |
94 double MIDITimeStampToSeconds(MIDITimeStamp timestamp) { | 95 double MIDITimeStampToSeconds(MIDITimeStamp timestamp) { |
95 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); | 96 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); |
96 return static_cast<double>(nanoseconds) / 1.0e9; | 97 return static_cast<double>(nanoseconds) / 1.0e9; |
97 } | 98 } |
98 | 99 |
99 MIDITimeStamp SecondsToMIDITimeStamp(double seconds) { | 100 MIDITimeStamp SecondsToMIDITimeStamp(double seconds) { |
100 UInt64 nanos = UInt64(seconds * 1.0e9); | 101 UInt64 nanos = UInt64(seconds * 1.0e9); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 // On kMIDIMsgObjectRemoved, the entry will be ignored because it | 259 // On kMIDIMsgObjectRemoved, the entry will be ignored because it |
259 // will not be found in the pool. | 260 // will not be found in the pool. |
260 if (!info.id.empty()) { | 261 if (!info.id.empty()) { |
261 uint32_t index = source_map_.size(); | 262 uint32_t index = source_map_.size(); |
262 source_map_[endpoint] = index; | 263 source_map_[endpoint] = index; |
263 AddInputPort(info); | 264 AddInputPort(info); |
264 MIDIPortConnectSource( | 265 MIDIPortConnectSource( |
265 coremidi_input_, endpoint, reinterpret_cast<void*>(endpoint)); | 266 coremidi_input_, endpoint, reinterpret_cast<void*>(endpoint)); |
266 } | 267 } |
267 } else { | 268 } else { |
268 SetInputPortState(it->second, MIDI_PORT_OPENED); | 269 SetInputPortState(it->second, PortState::OPENED); |
269 } | 270 } |
270 } else if (notification->childType == kMIDIObjectType_Destination) { | 271 } else if (notification->childType == kMIDIObjectType_Destination) { |
271 // Attaching device is an output device. | 272 // Attaching device is an output device. |
272 auto it = std::find(destinations_.begin(), destinations_.end(), endpoint); | 273 auto it = std::find(destinations_.begin(), destinations_.end(), endpoint); |
273 if (it == destinations_.end()) { | 274 if (it == destinations_.end()) { |
274 MidiPortInfo info = GetPortInfoFromEndpoint(endpoint); | 275 MidiPortInfo info = GetPortInfoFromEndpoint(endpoint); |
275 // Skip cases that queries are not finished correctly. | 276 // Skip cases that queries are not finished correctly. |
276 if (!info.id.empty()) { | 277 if (!info.id.empty()) { |
277 destinations_.push_back(endpoint); | 278 destinations_.push_back(endpoint); |
278 AddOutputPort(info); | 279 AddOutputPort(info); |
279 } | 280 } |
280 } else { | 281 } else { |
281 SetOutputPortState(it - destinations_.begin(), MIDI_PORT_OPENED); | 282 SetOutputPortState(it - destinations_.begin(), PortState::OPENED); |
282 } | 283 } |
283 } | 284 } |
284 } else if (kMIDIMsgObjectRemoved == message->messageID) { | 285 } else if (kMIDIMsgObjectRemoved == message->messageID) { |
285 // Existing device is going to be detached. | 286 // Existing device is going to be detached. |
286 const MIDIObjectAddRemoveNotification* notification = | 287 const MIDIObjectAddRemoveNotification* notification = |
287 reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message); | 288 reinterpret_cast<const MIDIObjectAddRemoveNotification*>(message); |
288 MIDIEndpointRef endpoint = | 289 MIDIEndpointRef endpoint = |
289 static_cast<MIDIEndpointRef>(notification->child); | 290 static_cast<MIDIEndpointRef>(notification->child); |
290 if (notification->childType == kMIDIObjectType_Source) { | 291 if (notification->childType == kMIDIObjectType_Source) { |
291 // Detaching device is an input device. | 292 // Detaching device is an input device. |
292 auto it = source_map_.find(endpoint); | 293 auto it = source_map_.find(endpoint); |
293 if (it != source_map_.end()) | 294 if (it != source_map_.end()) |
294 SetInputPortState(it->second, MIDI_PORT_DISCONNECTED); | 295 SetInputPortState(it->second, PortState::DISCONNECTED); |
295 } else if (notification->childType == kMIDIObjectType_Destination) { | 296 } else if (notification->childType == kMIDIObjectType_Destination) { |
296 // Detaching device is an output device. | 297 // Detaching device is an output device. |
297 auto it = std::find(destinations_.begin(), destinations_.end(), endpoint); | 298 auto it = std::find(destinations_.begin(), destinations_.end(), endpoint); |
298 if (it != destinations_.end()) | 299 if (it != destinations_.end()) |
299 SetOutputPortState(it - destinations_.begin(), MIDI_PORT_DISCONNECTED); | 300 SetOutputPortState(it - destinations_.begin(), PortState::DISCONNECTED); |
300 } | 301 } |
301 } | 302 } |
302 } | 303 } |
303 | 304 |
304 // static | 305 // static |
305 void MidiManagerMac::ReadMidiDispatch(const MIDIPacketList* packet_list, | 306 void MidiManagerMac::ReadMidiDispatch(const MIDIPacketList* packet_list, |
306 void* read_proc_refcon, | 307 void* read_proc_refcon, |
307 void* src_conn_refcon) { | 308 void* src_conn_refcon) { |
308 // This method is called on a separate high-priority thread owned by CoreMIDI. | 309 // This method is called on a separate high-priority thread owned by CoreMIDI. |
309 | 310 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 &data[sent_size]); | 381 &data[sent_size]); |
381 DCHECK(midi_packet); | 382 DCHECK(midi_packet); |
382 | 383 |
383 MIDISend(coremidi_output_, destination, packet_list); | 384 MIDISend(coremidi_output_, destination, packet_list); |
384 } | 385 } |
385 | 386 |
386 AccumulateMidiBytesSent(client, data.size()); | 387 AccumulateMidiBytesSent(client, data.size()); |
387 } | 388 } |
388 | 389 |
389 } // namespace midi | 390 } // namespace midi |
OLD | NEW |