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