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