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