| 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 <iostream> | 7 #include <iostream> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include <CoreAudio/HostTime.h> | 13 #include <CoreAudio/HostTime.h> |
| 14 | 14 |
| 15 using base::IntToString; | 15 using base::IntToString; |
| 16 using base::SysCFStringRefToUTF8; | 16 using base::SysCFStringRefToUTF8; |
| 17 using std::string; | 17 using std::string; |
| 18 | 18 |
| 19 // NB: System MIDI types are pointer types in 32-bit and integer types in |
| 20 // 64-bit. Therefore, the initialization is the simplest one that satisfies both |
| 21 // (if possible). |
| 22 |
| 19 namespace media { | 23 namespace media { |
| 20 | 24 |
| 21 MIDIManager* MIDIManager::Create() { | 25 MIDIManager* MIDIManager::Create() { |
| 22 return new MIDIManagerMac(); | 26 return new MIDIManagerMac(); |
| 23 } | 27 } |
| 24 | 28 |
| 25 MIDIManagerMac::MIDIManagerMac() | 29 MIDIManagerMac::MIDIManagerMac() |
| 26 : midi_client_(NULL), | 30 : midi_client_(0), |
| 27 coremidi_input_(NULL), | 31 coremidi_input_(0), |
| 28 coremidi_output_(NULL), | 32 coremidi_output_(0), |
| 29 packet_list_(NULL), | 33 packet_list_(NULL), |
| 30 midi_packet_(NULL) { | 34 midi_packet_(NULL) { |
| 31 } | 35 } |
| 32 | 36 |
| 33 bool MIDIManagerMac::Initialize() { | 37 bool MIDIManagerMac::Initialize() { |
| 34 TRACE_EVENT0("midi", "MIDIManagerMac::Initialize"); | 38 TRACE_EVENT0("midi", "MIDIManagerMac::Initialize"); |
| 35 | 39 |
| 36 // CoreMIDI registration. | 40 // CoreMIDI registration. |
| 37 midi_client_ = NULL; | 41 midi_client_ = 0; |
| 38 OSStatus result = MIDIClientCreate( | 42 OSStatus result = MIDIClientCreate( |
| 39 CFSTR("Google Chrome"), | 43 CFSTR("Google Chrome"), |
| 40 NULL, | 44 NULL, |
| 41 NULL, | 45 NULL, |
| 42 &midi_client_); | 46 &midi_client_); |
| 43 | 47 |
| 44 if (result != noErr) | 48 if (result != noErr) |
| 45 return false; | 49 return false; |
| 46 | 50 |
| 47 coremidi_input_ = NULL; | 51 coremidi_input_ = 0; |
| 48 | 52 |
| 49 // Create input and output port. | 53 // Create input and output port. |
| 50 result = MIDIInputPortCreate( | 54 result = MIDIInputPortCreate( |
| 51 midi_client_, | 55 midi_client_, |
| 52 CFSTR("MIDI Input"), | 56 CFSTR("MIDI Input"), |
| 53 ReadMidiDispatch, | 57 ReadMidiDispatch, |
| 54 this, | 58 this, |
| 55 &coremidi_input_); | 59 &coremidi_input_); |
| 56 if (result != noErr) | 60 if (result != noErr) |
| 57 return false; | 61 return false; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 76 MIDIPortInfo info = GetPortInfoFromEndpoint(destination); | 80 MIDIPortInfo info = GetPortInfoFromEndpoint(destination); |
| 77 AddOutputPort(info); | 81 AddOutputPort(info); |
| 78 } | 82 } |
| 79 | 83 |
| 80 // Open connections from all sources. | 84 // Open connections from all sources. |
| 81 int source_count = MIDIGetNumberOfSources(); | 85 int source_count = MIDIGetNumberOfSources(); |
| 82 | 86 |
| 83 for (int i = 0; i < source_count; ++i) { | 87 for (int i = 0; i < source_count; ++i) { |
| 84 // Receive from all sources. | 88 // Receive from all sources. |
| 85 MIDIEndpointRef src = MIDIGetSource(i); | 89 MIDIEndpointRef src = MIDIGetSource(i); |
| 86 MIDIPortConnectSource(coremidi_input_, src, src); | 90 MIDIPortConnectSource(coremidi_input_, src, reinterpret_cast<void*>(src)); |
| 87 | 91 |
| 88 // Keep track of all sources (known as inputs in Web MIDI API terminology). | 92 // Keep track of all sources (known as inputs in Web MIDI API terminology). |
| 89 source_map_[src] = i; | 93 source_map_[src] = i; |
| 90 | 94 |
| 91 MIDIPortInfo info = GetPortInfoFromEndpoint(src); | 95 MIDIPortInfo info = GetPortInfoFromEndpoint(src); |
| 92 AddInputPort(info); | 96 AddInputPort(info); |
| 93 } | 97 } |
| 94 | 98 |
| 95 // TODO(crogers): Fix the memory management here! | 99 // TODO(crogers): Fix the memory management here! |
| 96 packet_list_ = reinterpret_cast<MIDIPacketList*>(midi_buffer_); | 100 packet_list_ = reinterpret_cast<MIDIPacketList*>(midi_buffer_); |
| 97 midi_packet_ = MIDIPacketListInit(packet_list_); | 101 midi_packet_ = MIDIPacketListInit(packet_list_); |
| 98 | 102 |
| 99 return true; | 103 return true; |
| 100 } | 104 } |
| 101 | 105 |
| 102 MIDIManagerMac::~MIDIManagerMac() { | 106 MIDIManagerMac::~MIDIManagerMac() { |
| 103 if (coremidi_input_) | 107 if (coremidi_input_) |
| 104 MIDIPortDispose(coremidi_input_); | 108 MIDIPortDispose(coremidi_input_); |
| 105 if (coremidi_output_) | 109 if (coremidi_output_) |
| 106 MIDIPortDispose(coremidi_output_); | 110 MIDIPortDispose(coremidi_output_); |
| 107 } | 111 } |
| 108 | 112 |
| 109 void MIDIManagerMac::ReadMidiDispatch(const MIDIPacketList* packet_list, | 113 void MIDIManagerMac::ReadMidiDispatch(const MIDIPacketList* packet_list, |
| 110 void* read_proc_refcon, | 114 void* read_proc_refcon, |
| 111 void* src_conn_refcon) { | 115 void* src_conn_refcon) { |
| 112 MIDIManagerMac* manager = static_cast<MIDIManagerMac*>(read_proc_refcon); | 116 MIDIManagerMac* manager = static_cast<MIDIManagerMac*>(read_proc_refcon); |
| 117 #if __LP64__ |
| 118 MIDIEndpointRef source = reinterpret_cast<uintptr_t>(src_conn_refcon); |
| 119 #else |
| 113 MIDIEndpointRef source = static_cast<MIDIEndpointRef>(src_conn_refcon); | 120 MIDIEndpointRef source = static_cast<MIDIEndpointRef>(src_conn_refcon); |
| 121 #endif |
| 114 | 122 |
| 115 // Dispatch to class method. | 123 // Dispatch to class method. |
| 116 manager->ReadMidi(source, packet_list); | 124 manager->ReadMidi(source, packet_list); |
| 117 } | 125 } |
| 118 | 126 |
| 119 void MIDIManagerMac::ReadMidi(MIDIEndpointRef source, | 127 void MIDIManagerMac::ReadMidi(MIDIEndpointRef source, |
| 120 const MIDIPacketList* packet_list) { | 128 const MIDIPacketList* packet_list) { |
| 121 // Lookup the port index based on the source. | 129 // Lookup the port index based on the source. |
| 122 SourceMap::iterator j = source_map_.find(source); | 130 SourceMap::iterator j = source_map_.find(source); |
| 123 if (j == source_map_.end()) | 131 if (j == source_map_.end()) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); | 204 UInt64 nanoseconds = AudioConvertHostTimeToNanos(timestamp); |
| 197 return static_cast<double>(nanoseconds) / 1.0e9; | 205 return static_cast<double>(nanoseconds) / 1.0e9; |
| 198 } | 206 } |
| 199 | 207 |
| 200 MIDITimeStamp MIDIManagerMac::SecondsToMIDITimeStamp(double seconds) { | 208 MIDITimeStamp MIDIManagerMac::SecondsToMIDITimeStamp(double seconds) { |
| 201 UInt64 nanos = UInt64(seconds * 1.0e9); | 209 UInt64 nanos = UInt64(seconds * 1.0e9); |
| 202 return AudioConvertNanosToHostTime(nanos); | 210 return AudioConvertNanosToHostTime(nanos); |
| 203 } | 211 } |
| 204 | 212 |
| 205 } // namespace media | 213 } // namespace media |
| OLD | NEW |