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