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> |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
| 13 | #include "base/android/scoped_java_ref.h" |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 14 | #include "base/containers/hash_tables.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/memory/scoped_vector.h" |
| 17 | #include "base/time/time.h" |
| 18 | #include "media/midi/midi_input_port_android.h" |
| 19 | #include "media/midi/midi_manager.h" |
| 20 | #include "media/midi/midi_scheduler.h" |
| 21 | |
| 22 | namespace media { |
| 23 | namespace midi { |
| 24 | |
| 25 | class MidiDeviceAndroid; |
| 26 | class MidiOutputPortAndroid; |
| 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: |
| 33 | MidiManagerAndroid(); |
| 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, |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 41 | double timestamp) override; |
| 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, |
| 51 | const base::android::JavaParamRef<jobject>& caller, |
| 52 | const base::android::JavaParamRef<jobjectArray>& devices); |
| 53 | void OnAttached(JNIEnv* env, |
| 54 | const base::android::JavaParamRef<jobject>& caller, |
| 55 | const base::android::JavaParamRef<jobject>& device); |
| 56 | void OnDetached(JNIEnv* env, |
| 57 | const base::android::JavaParamRef<jobject>& caller, |
| 58 | const base::android::JavaParamRef<jobject>& device); |
yhirano | bc742d8 | 2015-09-17 07:41:44 -0700 | [diff] [blame] | 59 | |
| 60 | static bool Register(JNIEnv* env); |
| 61 | |
| 62 | private: |
| 63 | void AddDevice(scoped_ptr<MidiDeviceAndroid> device); |
| 64 | void AddInputPortAndroid(MidiInputPortAndroid* port, |
| 65 | MidiDeviceAndroid* device); |
| 66 | void AddOutputPortAndroid(MidiOutputPortAndroid* port, |
| 67 | MidiDeviceAndroid* device); |
| 68 | |
| 69 | ScopedVector<MidiDeviceAndroid> devices_; |
| 70 | // All ports held in |devices_|. Each device has ownership of ports, but we |
| 71 | // can store pointers here because a device will keep its ports while it is |
| 72 | // alive. |
| 73 | std::vector<MidiInputPortAndroid*> all_input_ports_; |
| 74 | // A dictionary from a port to its index. |
| 75 | // input_port_to_index_[all_input_ports_[i]] == i for each valid |i|. |
| 76 | base::hash_map<MidiInputPortAndroid*, size_t> input_port_to_index_; |
| 77 | |
| 78 | // Ditto for output ports. |
| 79 | std::vector<MidiOutputPortAndroid*> all_output_ports_; |
| 80 | base::hash_map<MidiOutputPortAndroid*, size_t> output_port_to_index_; |
| 81 | |
| 82 | base::android::ScopedJavaGlobalRef<jobject> raw_manager_; |
| 83 | scoped_ptr<MidiScheduler> scheduler_; |
| 84 | }; |
| 85 | |
| 86 | } // namespace midi |
| 87 | } // namespace media |
| 88 | |
| 89 | #endif // MEDIA_MIDI_MIDI_MANAGER_ANDROID_H_ |