// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "media/midi/usb_midi_device_android.h" #include #include #include "base/android/jni_array.h" #include "base/time/time.h" #include "jni/UsbMidiDeviceAndroid_jni.h" namespace media { UsbMidiDeviceAndroid::UsbMidiDeviceAndroid(ObjectRef raw_device, UsbMidiDeviceDelegate* delegate) : raw_device_(raw_device), delegate_(delegate) { JNIEnv* env = base::android::AttachCurrentThread(); Java_UsbMidiDeviceAndroid_registerSelf( env, raw_device_.obj(), reinterpret_cast(this)); } UsbMidiDeviceAndroid::~UsbMidiDeviceAndroid() { JNIEnv* env = base::android::AttachCurrentThread(); Java_UsbMidiDeviceAndroid_close(env, raw_device_.obj()); } std::vector UsbMidiDeviceAndroid::GetDescriptor() { JNIEnv* env = base::android::AttachCurrentThread(); base::android::ScopedJavaLocalRef descriptors = Java_UsbMidiDeviceAndroid_getDescriptors(env, raw_device_.obj()); std::vector ret; base::android::JavaByteArrayToByteVector(env, descriptors.obj(), &ret); return ret; } void UsbMidiDeviceAndroid::Send(int endpoint_number, const std::vector& data) { JNIEnv* env = base::android::AttachCurrentThread(); const uint8* head = data.size() ? &data[0] : NULL; ScopedJavaLocalRef data_to_pass = base::android::ToJavaByteArray(env, head, data.size()); Java_UsbMidiDeviceAndroid_send( env, raw_device_.obj(), endpoint_number, data_to_pass.obj()); } void UsbMidiDeviceAndroid::OnData(JNIEnv* env, jobject caller, jint endpoint_number, jbyteArray data) { std::vector bytes; base::android::JavaByteArrayToByteVector(env, data, &bytes); const uint8* head = bytes.size() ? &bytes[0] : NULL; delegate_->ReceiveUsbMidiData(this, endpoint_number, head, bytes.size(), base::TimeTicks::HighResNow()); } bool UsbMidiDeviceAndroid::RegisterUsbMidiDevice(JNIEnv* env) { return RegisterNativesImpl(env); } } // namespace media