OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_android.h" | 5 #include "media/midi/midi_manager_android.h" |
6 | 6 |
7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
8 #include "base/android/context_utils.h" | 8 #include "base/android/context_utils.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "jni/MidiManagerAndroid_jni.h" | 12 #include "jni/MidiManagerAndroid_jni.h" |
13 #include "media/midi/midi_device_android.h" | 13 #include "media/midi/midi_device_android.h" |
14 #include "media/midi/midi_manager_usb.h" | 14 #include "media/midi/midi_manager_usb.h" |
15 #include "media/midi/midi_output_port_android.h" | 15 #include "media/midi/midi_output_port_android.h" |
16 #include "media/midi/midi_switches.h" | 16 #include "media/midi/midi_switches.h" |
17 #include "media/midi/usb_midi_device_factory_android.h" | 17 #include "media/midi/usb_midi_device_factory_android.h" |
18 | 18 |
19 using base::android::JavaParamRef; | 19 using base::android::JavaParamRef; |
| 20 using midi::mojom::PortState; |
20 using midi::mojom::Result; | 21 using midi::mojom::Result; |
21 | 22 |
22 namespace midi { | 23 namespace midi { |
23 | 24 |
24 MidiManager* MidiManager::Create() { | 25 MidiManager* MidiManager::Create() { |
25 auto sdk_version = base::android::BuildInfo::GetInstance()->sdk_int(); | 26 auto sdk_version = base::android::BuildInfo::GetInstance()->sdk_int(); |
26 if (sdk_version <= base::android::SDK_VERSION_LOLLIPOP_MR1 || | 27 if (sdk_version <= base::android::SDK_VERSION_LOLLIPOP_MR1 || |
27 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 28 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
28 switches::kUseAndroidMidiApi)) { | 29 switches::kUseAndroidMidiApi)) { |
29 return new MidiManagerUsb(std::unique_ptr<UsbMidiDevice::Factory>( | 30 return new MidiManagerUsb(std::unique_ptr<UsbMidiDevice::Factory>( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 void MidiManagerAndroid::DispatchSendMidiData(MidiManagerClient* client, | 65 void MidiManagerAndroid::DispatchSendMidiData(MidiManagerClient* client, |
65 uint32_t port_index, | 66 uint32_t port_index, |
66 const std::vector<uint8_t>& data, | 67 const std::vector<uint8_t>& data, |
67 double timestamp) { | 68 double timestamp) { |
68 if (port_index >= all_output_ports_.size()) { | 69 if (port_index >= all_output_ports_.size()) { |
69 // |port_index| is provided by a renderer so we can't believe that it is | 70 // |port_index| is provided by a renderer so we can't believe that it is |
70 // in the valid range. | 71 // in the valid range. |
71 return; | 72 return; |
72 } | 73 } |
73 DCHECK_EQ(output_ports().size(), all_output_ports_.size()); | 74 DCHECK_EQ(output_ports().size(), all_output_ports_.size()); |
74 if (output_ports()[port_index].state == MIDI_PORT_CONNECTED) { | 75 if (output_ports()[port_index].state == PortState::CONNECTED) { |
75 // We treat send call as implicit open. | 76 // We treat send call as implicit open. |
76 // TODO(yhirano): Implement explicit open operation from the renderer. | 77 // TODO(yhirano): Implement explicit open operation from the renderer. |
77 if (all_output_ports_[port_index]->Open()) { | 78 if (all_output_ports_[port_index]->Open()) { |
78 SetOutputPortState(port_index, MIDI_PORT_OPENED); | 79 SetOutputPortState(port_index, PortState::OPENED); |
79 } else { | 80 } else { |
80 // We cannot open the port. It's useless to send data to such a port. | 81 // We cannot open the port. It's useless to send data to such a port. |
81 return; | 82 return; |
82 } | 83 } |
83 } | 84 } |
84 | 85 |
85 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. | 86 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. |
86 // The task posted to the MidiScheduler will be disposed safely on deleting | 87 // The task posted to the MidiScheduler will be disposed safely on deleting |
87 // the scheduler. | 88 // the scheduler. |
88 scheduler_->PostSendDataTask( | 89 scheduler_->PostSendDataTask( |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 127 } |
127 | 128 |
128 void MidiManagerAndroid::OnDetached(JNIEnv* env, | 129 void MidiManagerAndroid::OnDetached(JNIEnv* env, |
129 const JavaParamRef<jobject>& caller, | 130 const JavaParamRef<jobject>& caller, |
130 const JavaParamRef<jobject>& raw_device) { | 131 const JavaParamRef<jobject>& raw_device) { |
131 for (auto* device : devices_) { | 132 for (auto* device : devices_) { |
132 if (device->HasRawDevice(env, raw_device)) { | 133 if (device->HasRawDevice(env, raw_device)) { |
133 for (auto* port : device->input_ports()) { | 134 for (auto* port : device->input_ports()) { |
134 DCHECK(input_port_to_index_.end() != input_port_to_index_.find(port)); | 135 DCHECK(input_port_to_index_.end() != input_port_to_index_.find(port)); |
135 size_t index = input_port_to_index_[port]; | 136 size_t index = input_port_to_index_[port]; |
136 SetInputPortState(index, MIDI_PORT_DISCONNECTED); | 137 SetInputPortState(index, PortState::DISCONNECTED); |
137 } | 138 } |
138 for (auto* port : device->output_ports()) { | 139 for (auto* port : device->output_ports()) { |
139 DCHECK(output_port_to_index_.end() != output_port_to_index_.find(port)); | 140 DCHECK(output_port_to_index_.end() != output_port_to_index_.find(port)); |
140 size_t index = output_port_to_index_[port]; | 141 size_t index = output_port_to_index_[port]; |
141 SetOutputPortState(index, MIDI_PORT_DISCONNECTED); | 142 SetOutputPortState(index, PortState::DISCONNECTED); |
142 } | 143 } |
143 } | 144 } |
144 } | 145 } |
145 } | 146 } |
146 | 147 |
147 void MidiManagerAndroid::AddDevice(std::unique_ptr<MidiDeviceAndroid> device) { | 148 void MidiManagerAndroid::AddDevice(std::unique_ptr<MidiDeviceAndroid> device) { |
148 for (auto* port : device->input_ports()) { | 149 for (auto* port : device->input_ports()) { |
149 // We implicitly open input ports here, because there are no signal | 150 // We implicitly open input ports here, because there are no signal |
150 // from the renderer when to open. | 151 // from the renderer when to open. |
151 // TODO(yhirano): Implement open operation in Blink. | 152 // TODO(yhirano): Implement open operation in Blink. |
152 MidiPortState state = port->Open() ? MIDI_PORT_OPENED : MIDI_PORT_CONNECTED; | 153 PortState state = port->Open() ? PortState::OPENED : PortState::CONNECTED; |
153 | 154 |
154 const size_t index = all_input_ports_.size(); | 155 const size_t index = all_input_ports_.size(); |
155 all_input_ports_.push_back(port); | 156 all_input_ports_.push_back(port); |
156 // Port ID must be unique in a MIDI manager. This ID setting is | 157 // Port ID must be unique in a MIDI manager. This ID setting is |
157 // sufficiently unique although there is no user-friendly meaning. | 158 // sufficiently unique although there is no user-friendly meaning. |
158 // TODO(yhirano): Use a hashed string as ID. | 159 // TODO(yhirano): Use a hashed string as ID. |
159 const std::string id( | 160 const std::string id( |
160 base::StringPrintf("native:port-in-%ld", static_cast<long>(index))); | 161 base::StringPrintf("native:port-in-%ld", static_cast<long>(index))); |
161 | 162 |
162 input_port_to_index_.insert(std::make_pair(port, index)); | 163 input_port_to_index_.insert(std::make_pair(port, index)); |
163 AddInputPort(MidiPortInfo(id, device->GetManufacturer(), | 164 AddInputPort(MidiPortInfo(id, device->GetManufacturer(), |
164 device->GetProductName(), | 165 device->GetProductName(), |
165 device->GetDeviceVersion(), state)); | 166 device->GetDeviceVersion(), state)); |
166 } | 167 } |
167 for (auto* port : device->output_ports()) { | 168 for (auto* port : device->output_ports()) { |
168 const size_t index = all_output_ports_.size(); | 169 const size_t index = all_output_ports_.size(); |
169 all_output_ports_.push_back(port); | 170 all_output_ports_.push_back(port); |
170 | 171 |
171 // Port ID must be unique in a MIDI manager. This ID setting is | 172 // Port ID must be unique in a MIDI manager. This ID setting is |
172 // sufficiently unique although there is no user-friendly meaning. | 173 // sufficiently unique although there is no user-friendly meaning. |
173 // TODO(yhirano): Use a hashed string as ID. | 174 // TODO(yhirano): Use a hashed string as ID. |
174 const std::string id( | 175 const std::string id( |
175 base::StringPrintf("native:port-out-%ld", static_cast<long>(index))); | 176 base::StringPrintf("native:port-out-%ld", static_cast<long>(index))); |
176 | 177 |
177 output_port_to_index_.insert(std::make_pair(port, index)); | 178 output_port_to_index_.insert(std::make_pair(port, index)); |
178 AddOutputPort( | 179 AddOutputPort( |
179 MidiPortInfo(id, device->GetManufacturer(), device->GetProductName(), | 180 MidiPortInfo(id, device->GetManufacturer(), device->GetProductName(), |
180 device->GetDeviceVersion(), MIDI_PORT_CONNECTED)); | 181 device->GetDeviceVersion(), PortState::CONNECTED)); |
181 } | 182 } |
182 devices_.push_back(device.release()); | 183 devices_.push_back(device.release()); |
183 } | 184 } |
184 | 185 |
185 bool MidiManagerAndroid::Register(JNIEnv* env) { | 186 bool MidiManagerAndroid::Register(JNIEnv* env) { |
186 return RegisterNativesImpl(env); | 187 return RegisterNativesImpl(env); |
187 } | 188 } |
188 | 189 |
189 } // namespace midi | 190 } // namespace midi |
OLD | NEW |