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