yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
| 5 | #ifndef MEDIA_MIDI_MIDI_MANAGER_ANDROID_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_ANDROID_H_ |
| 7 | |
| 8 | #include <jni.h> |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 11 | |
| 12 | #include <memory> |
Takuto Ikuta | fd795cb | 2019-01-05 00:32:48 +0000 | [diff] [blame] | 13 | #include <unordered_map> |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | #include "base/android/scoped_java_ref.h" |
shaochuan | 4e37635 | 2016-08-23 22:02:45 -0700 | [diff] [blame] | 17 | #include "base/synchronization/lock.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 18 | #include "base/time/time.h" |
| 19 | #include "media/midi/midi_input_port_android.h" |
| 20 | #include "media/midi/midi_manager.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 21 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 22 | namespace midi { |
| 23 | |
| 24 | class MidiDeviceAndroid; |
| 25 | class MidiOutputPortAndroid; |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 26 | class MidiService; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 27 | |
| 28 | // MidiManagerAndroid is a MidiManager subclass for Android M or newer. For |
| 29 | // older android OSes, we use MidiManagerUsb. |
| 30 | class MidiManagerAndroid final : public MidiManager, |
| 31 | public MidiInputPortAndroid::Delegate { |
| 32 | public: |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 33 | explicit MidiManagerAndroid(MidiService* service); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 34 | ~MidiManagerAndroid() override; |
| 35 | |
| 36 | // MidiManager implementation. |
| 37 | void StartInitialization() override; |
| 38 | void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 39 | uint32_t port_index, |
| 40 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 41 | base::TimeTicks timestamp) override; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 42 | |
| 43 | // MidiInputPortAndroid::Delegate implementation. |
| 44 | void OnReceivedData(MidiInputPortAndroid*, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 45 | const uint8_t* data, |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 46 | size_t size, |
| 47 | base::TimeTicks timestamp) override; |
| 48 | |
| 49 | // Called from the Java world. |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 50 | void OnInitialized(JNIEnv* env, |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 51 | const base::android::JavaParamRef<jobjectArray>& devices); |
Eric Stevenson | 10e457c | 2019-07-26 21:26:06 +0000 | [diff] [blame] | 52 | void OnInitializationFailed(JNIEnv* env); |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 53 | void OnAttached(JNIEnv* env, |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 54 | const base::android::JavaParamRef<jobject>& device); |
| 55 | void OnDetached(JNIEnv* env, |
torne | db299dd | 2015-11-25 06:17:43 -0800 | [diff] [blame] | 56 | const base::android::JavaParamRef<jobject>& device); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 57 | |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 58 | private: |
danakj | 75afea0 | 2016-04-25 20:36:04 -0700 | [diff] [blame] | 59 | void AddDevice(std::unique_ptr<MidiDeviceAndroid> device); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 60 | void AddInputPortAndroid(MidiInputPortAndroid* port, |
| 61 | MidiDeviceAndroid* device); |
| 62 | void AddOutputPortAndroid(MidiOutputPortAndroid* port, |
| 63 | MidiDeviceAndroid* device); |
| 64 | |
Takashi Toyoshima | 596f72b | 2017-09-25 06:47:48 +0000 | [diff] [blame] | 65 | // TODO(toyoshim): Remove |lock_| once dynamic instantiation mode is enabled |
| 66 | // by default. This protects objects allocated on the I/O thread from doubly |
| 67 | // released on the main thread. |
| 68 | base::Lock lock_; |
| 69 | |
xiaofeng.zhang | 06152d8 | 2017-05-21 04:39:34 -0700 | [diff] [blame] | 70 | std::vector<std::unique_ptr<MidiDeviceAndroid>> devices_; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 71 | // All ports held in |devices_|. Each device has ownership of ports, but we |
| 72 | // can store pointers here because a device will keep its ports while it is |
| 73 | // alive. |
| 74 | std::vector<MidiInputPortAndroid*> all_input_ports_; |
| 75 | // A dictionary from a port to its index. |
| 76 | // input_port_to_index_[all_input_ports_[i]] == i for each valid |i|. |
Takuto Ikuta | fd795cb | 2019-01-05 00:32:48 +0000 | [diff] [blame] | 77 | std::unordered_map<MidiInputPortAndroid*, size_t> input_port_to_index_; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 78 | |
| 79 | // Ditto for output ports. |
| 80 | std::vector<MidiOutputPortAndroid*> all_output_ports_; |
Takuto Ikuta | fd795cb | 2019-01-05 00:32:48 +0000 | [diff] [blame] | 81 | std::unordered_map<MidiOutputPortAndroid*, size_t> output_port_to_index_; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 82 | |
| 83 | base::android::ScopedJavaGlobalRef<jobject> raw_manager_; |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | } // namespace midi |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 87 | |
| 88 | #endif // MEDIA_MIDI_MIDI_MANAGER_ANDROID_H_ |