yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 5 | #include "media/midi/midi_manager_android.h" |
| 6 | |
| 7 | #include "base/android/build_info.h" |
torne | 31cc92a | 2015-11-25 10:25:50 -0800 | [diff] [blame] | 8 | #include "base/android/context_utils.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 9 | #include "base/command_line.h" |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 11 | #include "base/strings/stringprintf.h" |
| 12 | #include "jni/MidiManagerAndroid_jni.h" |
| 13 | #include "media/midi/midi_device_android.h" |
yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 14 | #include "media/midi/midi_manager_usb.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 15 | #include "media/midi/midi_output_port_android.h" |
| 16 | #include "media/midi/midi_switches.h" |
yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 17 | #include "media/midi/usb_midi_device_factory_android.h" |
| 18 | |
torne | caf5d7f | 2016-08-04 08:59:04 -0700 | [diff] [blame] | 19 | using base::android::JavaParamRef; |
| 20 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 21 | namespace midi { |
yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 22 | |
| 23 | MidiManager* MidiManager::Create() { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 24 | auto sdk_version = base::android::BuildInfo::GetInstance()->sdk_int(); |
| 25 | if (sdk_version <= base::android::SDK_VERSION_LOLLIPOP_MR1 || |
| 26 | !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 27 | switches::kUseAndroidMidiApi)) { |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 28 | return new MidiManagerUsb(std::unique_ptr<UsbMidiDevice::Factory>( |
| 29 | new UsbMidiDeviceFactoryAndroid)); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | return new MidiManagerAndroid(); |
| 33 | } |
| 34 | |
| 35 | MidiManagerAndroid::MidiManagerAndroid() {} |
| 36 | |
shaochuan | 4e37635 | 2016-08-23 22:02:45 -0700 | [diff] [blame] | 37 | MidiManagerAndroid::~MidiManagerAndroid() { |
| 38 | base::AutoLock auto_lock(scheduler_lock_); |
| 39 | CHECK(!scheduler_); |
| 40 | } |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 41 | |
| 42 | void MidiManagerAndroid::StartInitialization() { |
| 43 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 | |
| 45 | uintptr_t pointer = reinterpret_cast<uintptr_t>(this); |
| 46 | raw_manager_.Reset(Java_MidiManagerAndroid_create( |
| 47 | env, base::android::GetApplicationContext(), pointer)); |
shaochuan | 4e37635 | 2016-08-23 22:02:45 -0700 | [diff] [blame] | 48 | |
| 49 | { |
| 50 | base::AutoLock auto_lock(scheduler_lock_); |
| 51 | scheduler_.reset(new MidiScheduler(this)); |
| 52 | } |
| 53 | |
torne | 94f8184 | 2016-08-16 08:10:44 -0700 | [diff] [blame] | 54 | Java_MidiManagerAndroid_initialize(env, raw_manager_); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
shaochuan | 4e37635 | 2016-08-23 22:02:45 -0700 | [diff] [blame] | 57 | void MidiManagerAndroid::Finalize() { |
| 58 | // Destruct MidiScheduler on Chrome_IOThread. |
| 59 | base::AutoLock auto_lock(scheduler_lock_); |
| 60 | scheduler_.reset(); |
| 61 | } |
| 62 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 63 | void MidiManagerAndroid::DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 64 | uint32_t port_index, |
| 65 | const std::vector<uint8_t>& data, |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 66 | double timestamp) { |
| 67 | if (port_index >= all_output_ports_.size()) { |
| 68 | // |port_index| is provided by a renderer so we can't believe that it is |
| 69 | // in the valid range. |
| 70 | return; |
| 71 | } |
| 72 | DCHECK_EQ(output_ports().size(), all_output_ports_.size()); |
| 73 | if (output_ports()[port_index].state == MIDI_PORT_CONNECTED) { |
| 74 | // We treat send call as implicit open. |
| 75 | // TODO(yhirano): Implement explicit open operation from the renderer. |
| 76 | if (all_output_ports_[port_index]->Open()) { |
| 77 | SetOutputPortState(port_index, MIDI_PORT_OPENED); |
| 78 | } else { |
| 79 | // We cannot open the port. It's useless to send data to such a port. |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. |
| 85 | // The task posted to the MidiScheduler will be disposed safely on deleting |
| 86 | // the scheduler. |
| 87 | scheduler_->PostSendDataTask( |
| 88 | client, data.size(), timestamp, |
| 89 | base::Bind(&MidiOutputPortAndroid::Send, |
| 90 | base::Unretained(all_output_ports_[port_index]), data)); |
| 91 | } |
| 92 | |
| 93 | void MidiManagerAndroid::OnReceivedData(MidiInputPortAndroid* port, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 94 | const uint8_t* data, |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 95 | size_t size, |
| 96 | base::TimeTicks timestamp) { |
| 97 | const auto i = input_port_to_index_.find(port); |
| 98 | DCHECK(input_port_to_index_.end() != i); |
| 99 | ReceiveMidiData(i->second, data, size, timestamp); |
| 100 | } |
| 101 | |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 102 | void MidiManagerAndroid::OnInitialized( |
| 103 | JNIEnv* env, |
| 104 | const JavaParamRef<jobject>& caller, |
| 105 | const JavaParamRef<jobjectArray>& devices) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 106 | jsize length = env->GetArrayLength(devices); |
| 107 | |
| 108 | for (jsize i = 0; i < length; ++i) { |
| 109 | jobject raw_device = env->GetObjectArrayElement(devices, i); |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 110 | AddDevice(base::MakeUnique<MidiDeviceAndroid>(env, raw_device, this)); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 111 | } |
| 112 | CompleteInitialization(Result::OK); |
| 113 | } |
| 114 | |
yhirano | 5949203 | 2016-10-05 18:26:42 -0700 | [diff] [blame] | 115 | void MidiManagerAndroid::OnInitializationFailed( |
| 116 | JNIEnv* env, |
| 117 | const JavaParamRef<jobject>& caller) { |
| 118 | CompleteInitialization(Result::INITIALIZATION_ERROR); |
| 119 | } |
| 120 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 121 | void MidiManagerAndroid::OnAttached(JNIEnv* env, |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 122 | const JavaParamRef<jobject>& caller, |
| 123 | const JavaParamRef<jobject>& raw_device) { |
ricea | 37e4576 | 2016-08-25 02:43:39 -0700 | [diff] [blame] | 124 | AddDevice(base::MakeUnique<MidiDeviceAndroid>(env, raw_device, this)); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void MidiManagerAndroid::OnDetached(JNIEnv* env, |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 128 | const JavaParamRef<jobject>& caller, |
| 129 | const JavaParamRef<jobject>& raw_device) { |
vmpstr | 0142111 | 2016-07-25 20:34:10 -0700 | [diff] [blame] | 130 | for (auto* device : devices_) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 131 | if (device->HasRawDevice(env, raw_device)) { |
vmpstr | 0142111 | 2016-07-25 20:34:10 -0700 | [diff] [blame] | 132 | for (auto* port : device->input_ports()) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 133 | DCHECK(input_port_to_index_.end() != input_port_to_index_.find(port)); |
| 134 | size_t index = input_port_to_index_[port]; |
| 135 | SetInputPortState(index, MIDI_PORT_DISCONNECTED); |
| 136 | } |
vmpstr | 0142111 | 2016-07-25 20:34:10 -0700 | [diff] [blame] | 137 | for (auto* port : device->output_ports()) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 138 | DCHECK(output_port_to_index_.end() != output_port_to_index_.find(port)); |
| 139 | size_t index = output_port_to_index_[port]; |
| 140 | SetOutputPortState(index, MIDI_PORT_DISCONNECTED); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 146 | void MidiManagerAndroid::AddDevice(std::unique_ptr<MidiDeviceAndroid> device) { |
vmpstr | 0142111 | 2016-07-25 20:34:10 -0700 | [diff] [blame] | 147 | for (auto* port : device->input_ports()) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 148 | // We implicitly open input ports here, because there are no signal |
| 149 | // from the renderer when to open. |
| 150 | // TODO(yhirano): Implement open operation in Blink. |
| 151 | MidiPortState state = port->Open() ? MIDI_PORT_OPENED : MIDI_PORT_CONNECTED; |
| 152 | |
| 153 | const size_t index = all_input_ports_.size(); |
| 154 | all_input_ports_.push_back(port); |
| 155 | // Port ID must be unique in a MIDI manager. This ID setting is |
| 156 | // sufficiently unique although there is no user-friendly meaning. |
| 157 | // TODO(yhirano): Use a hashed string as ID. |
| 158 | const std::string id( |
| 159 | base::StringPrintf("native:port-in-%ld", static_cast<long>(index))); |
| 160 | |
| 161 | input_port_to_index_.insert(std::make_pair(port, index)); |
| 162 | AddInputPort(MidiPortInfo(id, device->GetManufacturer(), |
| 163 | device->GetProductName(), |
| 164 | device->GetDeviceVersion(), state)); |
| 165 | } |
vmpstr | 0142111 | 2016-07-25 20:34:10 -0700 | [diff] [blame] | 166 | for (auto* port : device->output_ports()) { |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 167 | const size_t index = all_output_ports_.size(); |
| 168 | all_output_ports_.push_back(port); |
| 169 | |
| 170 | // Port ID must be unique in a MIDI manager. This ID setting is |
| 171 | // sufficiently unique although there is no user-friendly meaning. |
| 172 | // TODO(yhirano): Use a hashed string as ID. |
| 173 | const std::string id( |
| 174 | base::StringPrintf("native:port-out-%ld", static_cast<long>(index))); |
| 175 | |
| 176 | output_port_to_index_.insert(std::make_pair(port, index)); |
| 177 | AddOutputPort( |
| 178 | MidiPortInfo(id, device->GetManufacturer(), device->GetProductName(), |
| 179 | device->GetDeviceVersion(), MIDI_PORT_CONNECTED)); |
| 180 | } |
| 181 | devices_.push_back(device.release()); |
| 182 | } |
| 183 | |
| 184 | bool MidiManagerAndroid::Register(JNIEnv* env) { |
| 185 | return RegisterNativesImpl(env); |
yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 186 | } |
| 187 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 188 | } // namespace midi |