| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/midi/usb_midi_device_android.h" | 5 #include "media/midi/usb_midi_device_android.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/android/jni_array.h" | 9 #include "base/android/jni_array.h" |
| 10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "jni/UsbMidiDeviceAndroid_jni.h" | 13 #include "jni/UsbMidiDeviceAndroid_jni.h" |
| 14 #include "media/midi/usb_midi_descriptor_parser.h" | 14 #include "media/midi/usb_midi_descriptor_parser.h" |
| 15 | 15 |
| 16 using base::android::JavaParamRef; | 16 using base::android::JavaParamRef; |
| 17 using base::android::ScopedJavaLocalRef; | 17 using base::android::ScopedJavaLocalRef; |
| 18 | 18 |
| 19 namespace media { | 19 namespace media { |
| 20 namespace midi { | 20 namespace midi { |
| 21 | 21 |
| 22 UsbMidiDeviceAndroid::UsbMidiDeviceAndroid( | 22 UsbMidiDeviceAndroid::UsbMidiDeviceAndroid( |
| 23 const base::android::JavaRef<jobject>& raw_device, | 23 const base::android::JavaRef<jobject>& raw_device, |
| 24 UsbMidiDeviceDelegate* delegate) | 24 UsbMidiDeviceDelegate* delegate) |
| 25 : raw_device_(raw_device), delegate_(delegate) { | 25 : raw_device_(raw_device), delegate_(delegate) { |
| 26 JNIEnv* env = base::android::AttachCurrentThread(); | 26 JNIEnv* env = base::android::AttachCurrentThread(); |
| 27 Java_UsbMidiDeviceAndroid_registerSelf(env, raw_device_.obj(), | 27 Java_UsbMidiDeviceAndroid_registerSelf(env, raw_device_, |
| 28 reinterpret_cast<jlong>(this)); | 28 reinterpret_cast<jlong>(this)); |
| 29 | 29 |
| 30 GetDescriptorsInternal(); | 30 GetDescriptorsInternal(); |
| 31 InitDeviceInfo(); | 31 InitDeviceInfo(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 UsbMidiDeviceAndroid::~UsbMidiDeviceAndroid() { | 34 UsbMidiDeviceAndroid::~UsbMidiDeviceAndroid() { |
| 35 JNIEnv* env = base::android::AttachCurrentThread(); | 35 JNIEnv* env = base::android::AttachCurrentThread(); |
| 36 Java_UsbMidiDeviceAndroid_close(env, raw_device_.obj()); | 36 Java_UsbMidiDeviceAndroid_close(env, raw_device_); |
| 37 } | 37 } |
| 38 | 38 |
| 39 std::vector<uint8_t> UsbMidiDeviceAndroid::GetDescriptors() { | 39 std::vector<uint8_t> UsbMidiDeviceAndroid::GetDescriptors() { |
| 40 return descriptors_; | 40 return descriptors_; |
| 41 } | 41 } |
| 42 | 42 |
| 43 std::string UsbMidiDeviceAndroid::GetManufacturer() { | 43 std::string UsbMidiDeviceAndroid::GetManufacturer() { |
| 44 return manufacturer_; | 44 return manufacturer_; |
| 45 } | 45 } |
| 46 | 46 |
| 47 std::string UsbMidiDeviceAndroid::GetProductName() { | 47 std::string UsbMidiDeviceAndroid::GetProductName() { |
| 48 return product_; | 48 return product_; |
| 49 } | 49 } |
| 50 | 50 |
| 51 std::string UsbMidiDeviceAndroid::GetDeviceVersion() { | 51 std::string UsbMidiDeviceAndroid::GetDeviceVersion() { |
| 52 return device_version_; | 52 return device_version_; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void UsbMidiDeviceAndroid::Send(int endpoint_number, | 55 void UsbMidiDeviceAndroid::Send(int endpoint_number, |
| 56 const std::vector<uint8_t>& data) { | 56 const std::vector<uint8_t>& data) { |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); | 57 JNIEnv* env = base::android::AttachCurrentThread(); |
| 58 const uint8_t* head = data.size() ? &data[0] : NULL; | 58 const uint8_t* head = data.size() ? &data[0] : NULL; |
| 59 ScopedJavaLocalRef<jbyteArray> data_to_pass = | 59 ScopedJavaLocalRef<jbyteArray> data_to_pass = |
| 60 base::android::ToJavaByteArray(env, head, data.size()); | 60 base::android::ToJavaByteArray(env, head, data.size()); |
| 61 | 61 |
| 62 Java_UsbMidiDeviceAndroid_send(env, raw_device_.obj(), endpoint_number, | 62 Java_UsbMidiDeviceAndroid_send(env, raw_device_, endpoint_number, |
| 63 data_to_pass.obj()); | 63 data_to_pass); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void UsbMidiDeviceAndroid::OnData(JNIEnv* env, | 66 void UsbMidiDeviceAndroid::OnData(JNIEnv* env, |
| 67 const JavaParamRef<jobject>& caller, | 67 const JavaParamRef<jobject>& caller, |
| 68 jint endpoint_number, | 68 jint endpoint_number, |
| 69 const JavaParamRef<jbyteArray>& data) { | 69 const JavaParamRef<jbyteArray>& data) { |
| 70 std::vector<uint8_t> bytes; | 70 std::vector<uint8_t> bytes; |
| 71 base::android::JavaByteArrayToByteVector(env, data, &bytes); | 71 base::android::JavaByteArrayToByteVector(env, data, &bytes); |
| 72 | 72 |
| 73 const uint8_t* head = bytes.size() ? &bytes[0] : NULL; | 73 const uint8_t* head = bytes.size() ? &bytes[0] : NULL; |
| 74 delegate_->ReceiveUsbMidiData(this, endpoint_number, head, bytes.size(), | 74 delegate_->ReceiveUsbMidiData(this, endpoint_number, head, bytes.size(), |
| 75 base::TimeTicks::Now()); | 75 base::TimeTicks::Now()); |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool UsbMidiDeviceAndroid::RegisterUsbMidiDevice(JNIEnv* env) { | 78 bool UsbMidiDeviceAndroid::RegisterUsbMidiDevice(JNIEnv* env) { |
| 79 return RegisterNativesImpl(env); | 79 return RegisterNativesImpl(env); |
| 80 } | 80 } |
| 81 | 81 |
| 82 void UsbMidiDeviceAndroid::GetDescriptorsInternal() { | 82 void UsbMidiDeviceAndroid::GetDescriptorsInternal() { |
| 83 JNIEnv* env = base::android::AttachCurrentThread(); | 83 JNIEnv* env = base::android::AttachCurrentThread(); |
| 84 base::android::ScopedJavaLocalRef<jbyteArray> descriptors = | 84 base::android::ScopedJavaLocalRef<jbyteArray> descriptors = |
| 85 Java_UsbMidiDeviceAndroid_getDescriptors(env, raw_device_.obj()); | 85 Java_UsbMidiDeviceAndroid_getDescriptors(env, raw_device_); |
| 86 | 86 |
| 87 base::android::JavaByteArrayToByteVector(env, descriptors.obj(), | 87 base::android::JavaByteArrayToByteVector(env, descriptors.obj(), |
| 88 &descriptors_); | 88 &descriptors_); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void UsbMidiDeviceAndroid::InitDeviceInfo() { | 91 void UsbMidiDeviceAndroid::InitDeviceInfo() { |
| 92 UsbMidiDescriptorParser parser; | 92 UsbMidiDescriptorParser parser; |
| 93 UsbMidiDescriptorParser::DeviceInfo info; | 93 UsbMidiDescriptorParser::DeviceInfo info; |
| 94 | 94 |
| 95 const uint8_t* data = descriptors_.size() > 0 ? &descriptors_[0] : nullptr; | 95 const uint8_t* data = descriptors_.size() > 0 ? &descriptors_[0] : nullptr; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 base::StringPrintf("(vendor id = 0x%04x)", info.vendor_id)); | 108 base::StringPrintf("(vendor id = 0x%04x)", info.vendor_id)); |
| 109 product_ = | 109 product_ = |
| 110 GetString(info.product_index, | 110 GetString(info.product_index, |
| 111 base::StringPrintf("(product id = 0x%04x)", info.product_id)); | 111 base::StringPrintf("(product id = 0x%04x)", info.product_id)); |
| 112 device_version_ = info.BcdVersionToString(info.bcd_device_version); | 112 device_version_ = info.BcdVersionToString(info.bcd_device_version); |
| 113 } | 113 } |
| 114 | 114 |
| 115 std::vector<uint8_t> UsbMidiDeviceAndroid::GetStringDescriptor(int index) { | 115 std::vector<uint8_t> UsbMidiDeviceAndroid::GetStringDescriptor(int index) { |
| 116 JNIEnv* env = base::android::AttachCurrentThread(); | 116 JNIEnv* env = base::android::AttachCurrentThread(); |
| 117 base::android::ScopedJavaLocalRef<jbyteArray> descriptors = | 117 base::android::ScopedJavaLocalRef<jbyteArray> descriptors = |
| 118 Java_UsbMidiDeviceAndroid_getStringDescriptor(env, raw_device_.obj(), | 118 Java_UsbMidiDeviceAndroid_getStringDescriptor(env, raw_device_, index); |
| 119 index); | |
| 120 | 119 |
| 121 std::vector<uint8_t> ret; | 120 std::vector<uint8_t> ret; |
| 122 base::android::JavaByteArrayToByteVector(env, descriptors.obj(), &ret); | 121 base::android::JavaByteArrayToByteVector(env, descriptors.obj(), &ret); |
| 123 return ret; | 122 return ret; |
| 124 } | 123 } |
| 125 | 124 |
| 126 std::string UsbMidiDeviceAndroid::GetString(int index, | 125 std::string UsbMidiDeviceAndroid::GetString(int index, |
| 127 const std::string& backup) { | 126 const std::string& backup) { |
| 128 const uint8_t DESCRIPTOR_TYPE_STRING = 3; | 127 const uint8_t DESCRIPTOR_TYPE_STRING = 3; |
| 129 | 128 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 141 std::string encoded(reinterpret_cast<char*>(&descriptor[0]) + 2, size - 2); | 140 std::string encoded(reinterpret_cast<char*>(&descriptor[0]) + 2, size - 2); |
| 142 std::string result; | 141 std::string result; |
| 143 // Unicode ECN specifies that the string is encoded in UTF-16LE. | 142 // Unicode ECN specifies that the string is encoded in UTF-16LE. |
| 144 if (!base::ConvertToUtf8AndNormalize(encoded, "utf-16le", &result)) | 143 if (!base::ConvertToUtf8AndNormalize(encoded, "utf-16le", &result)) |
| 145 return backup; | 144 return backup; |
| 146 return result; | 145 return result; |
| 147 } | 146 } |
| 148 | 147 |
| 149 } // namespace midi | 148 } // namespace midi |
| 150 } // namespace media | 149 } // namespace media |
| OLD | NEW |