Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android - backend bug fixes and camera app new features #3366

Merged
merged 3 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/android/jni/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,27 @@ Java_com_intel_realsense_librealsense_Config_nEnableRecordToFile(JNIEnv *env, jc

(*env)->ReleaseStringUTFChars(env, filePath_, filePath);
}

JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_Config_nDisableStream(JNIEnv *env, jclass type, jlong handle,
jint streamType) {
rs2_error *e = NULL;
rs2_config_disable_stream((rs2_config *) handle, streamType, &e);
handle_error(env, e);
}

JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_Config_nEnableAllStreams(JNIEnv *env, jclass type,
jlong handle) {
rs2_error *e = NULL;
rs2_config_enable_all_stream((rs2_config *) handle, &e);
handle_error(env, e);
}

JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_Config_nDisableAllStreams(JNIEnv *env, jclass type,
jlong handle) {
rs2_error *e = NULL;
rs2_config_disable_all_streams((rs2_config *) handle, &e);
handle_error(env, e);
}
19 changes: 15 additions & 4 deletions src/android/usb_host/device_watcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace librealsense;
using namespace librealsense::usb_host;

static std::vector<std::shared_ptr<device>> _devices;
static librealsense::platform::device_changed_callback _callback;
static librealsense::platform::device_changed_callback _callback = nullptr;

std::vector<std::shared_ptr<device>> device_watcher::get_device_list()
{
Expand All @@ -25,7 +25,7 @@ void device_watcher::start(librealsense::platform::device_changed_callback callb

void device_watcher::stop()
{

_callback = nullptr;
}

std::vector<platform::uvc_device_info> device_watcher::query_uvc_devices() {
Expand Down Expand Up @@ -55,7 +55,10 @@ Java_com_intel_realsense_librealsense_DeviceWatcher_nAddUsbDevice(JNIEnv *env, j
jint fileDescriptor) {
platform::backend_device_group prev;
prev.uvc_devices = device_watcher::query_uvc_devices();
LOG_DEBUG("AddUsbDevice, previous device count: " << prev.uvc_devices.size());
const char *deviceName = env->GetStringUTFChars(deviceName_, 0);
LOG_DEBUG("AddUsbDevice, adding device: " << deviceName << ", descriptor: " << fileDescriptor);

auto handle = usb_device_new(deviceName, fileDescriptor);
env->ReleaseStringUTFChars(deviceName_, deviceName);

Expand All @@ -69,22 +72,30 @@ Java_com_intel_realsense_librealsense_DeviceWatcher_nAddUsbDevice(JNIEnv *env, j

if(_callback)
_callback(prev, curr);

LOG_DEBUG("AddUsbDevice, current device count: " << curr.uvc_devices.size());
}

extern "C" JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_DeviceWatcher_nRemoveUsbDevice(JNIEnv *env, jclass type,
jint fileDescriptor) {
platform::backend_device_group prev;
prev.uvc_devices = device_watcher::query_uvc_devices();
LOG_DEBUG("RemoveUsbDevice, previous device count: " << prev.uvc_devices.size());

_devices.erase(std::remove_if(_devices.begin(), _devices.end(), [fileDescriptor](std::shared_ptr<device> d)
{
if(fileDescriptor == d->get_file_descriptor())
if(fileDescriptor == d->get_file_descriptor()){
d->release();
LOG_DEBUG("RemoveUsbDevice, removing device: " << d->get_name().c_str() << ", descriptor: " << fileDescriptor);
return true;
}
}), _devices.end());

platform::backend_device_group curr;
curr.uvc_devices = device_watcher::query_uvc_devices();

_callback(prev, curr);
if(_callback)
_callback(prev, curr);
LOG_DEBUG("RemoveUsbDevice, current device count: " << curr.uvc_devices.size());
}
26 changes: 25 additions & 1 deletion src/android/usb_host/usb_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ device::device(usb_device *device) :
_desc_length = usb_device_get_descriptors_length(_handle);
build_tree();
start();
start_interrupt_listener();
}
}

Expand All @@ -30,9 +31,16 @@ void device::claim_interface(int interface)
usb_device_claim_interface(_handle, interface);
}

device::~device() {
device::~device()
{
release();
}

void device::release()
{
stop();
_pipes.clear();
LOG_DEBUG("usb device: " << _name << ", released");
}

bool device::add_configuration(usb_configuration &usbConfiguration) {
Expand All @@ -48,6 +56,22 @@ void device::add_pipe(uint8_t ep_address, std::shared_ptr<usb_pipe> pipe) {
}
}

void device::start_interrupt_listener()
{
foreach_interface([&](const usb_interface& interface)
{
for(int i = 0; i < interface.get_Endpoint_count(); ++i)
{
auto ep = interface.get_endpoint(i);
auto attr = ep.get_descriptor()->bmAttributes;
if(attr == USB_ENDPOINT_XFER_INT){
claim_interface(interface.get_descriptor().bInterfaceNumber);
_pipes[ep.get_endpoint_address()]->listen_to_interrupts();
}
}
});
}

void device::start()
{
_pull_requests = true;
Expand Down
4 changes: 4 additions & 0 deletions src/android/usb_host/usb_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace librealsense
device(usb_device *device);
~device();

void release();

void claim_interface(int interface);

usb_device* get_handle() const { return _handle; }
Expand Down Expand Up @@ -80,6 +82,8 @@ namespace librealsense
void stop();
void build_tree();
void add_pipe(uint8_t ep_address, std::shared_ptr<usb_pipe> pipe);
void start_interrupt_listener();

int get_configuration_count() { return _configurations.size(); }
const usb_configuration &get_configuration(int index) { return _configurations.at(index); }
std::mutex m;
Expand Down
29 changes: 19 additions & 10 deletions src/android/usb_host/usb_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ usb_pipe::usb_pipe(usb_device *usb_device, usb_endpoint endpoint) :
_device(usb_device)
{
_request = std::shared_ptr<usb_request>(usb_request_new(_device, _endpoint.get_descriptor()), [](usb_request* req) {usb_request_free(req);});
auto attr = _endpoint.get_descriptor()->bmAttributes;
if(attr == USB_ENDPOINT_XFER_INT)
{
_interrupt_buffer = std::vector<uint8_t>(INTERRUPT_BUFFER_SIZE);
queue_interrupt_request();
}
}

usb_pipe::~usb_pipe()
Expand All @@ -36,10 +30,15 @@ usb_pipe::~usb_pipe()

bool usb_pipe::reset()
{
return usb_device_control_transfer(_device,
0x02, //UVC_FEATURE,
0x01, //CLEAR_FEATURE
0, _endpoint.get_endpoint_address(), NULL, 0, 10) == UVC_SUCCESS;
bool rv = usb_device_control_transfer(_device,
0x02, //UVC_FEATURE,
0x01, //CLEAR_FEATURE
0, _endpoint.get_endpoint_address(), NULL, 0, 10) == UVC_SUCCESS;
if(rv)
LOG_DEBUG("USB pipe " << (int)_endpoint.get_endpoint_address() << " reset successfully");
else
LOG_DEBUG("Failed to reset the USB pipe " << (int)_endpoint.get_endpoint_address());
return rv;
}

size_t usb_pipe::read_pipe(uint8_t *buffer, size_t buffer_len, unsigned int timeout_ms) {
Expand Down Expand Up @@ -87,4 +86,14 @@ void usb_pipe::queue_interrupt_request()
{
std::lock_guard<std::mutex> lock(_mutex);
submit_request(_interrupt_buffer.data(), _interrupt_buffer.size());
}

void usb_pipe::listen_to_interrupts()
{
auto attr = _endpoint.get_descriptor()->bmAttributes;
if(attr == USB_ENDPOINT_XFER_INT)
{
_interrupt_buffer = std::vector<uint8_t>(INTERRUPT_BUFFER_SIZE);
queue_interrupt_request();
}
}
1 change: 1 addition & 0 deletions src/android/usb_host/usb_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace librealsense
size_t read_pipe(uint8_t *buffer, size_t buffer_len, unsigned int timeout_ms = 100);
void queue_finished_request(usb_request* response);
bool reset();
void listen_to_interrupts();

private:
void submit_request(uint8_t *buffer, size_t buffer_len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />
</activity>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />
</activity>
</application>

Expand Down
11 changes: 1 addition & 10 deletions wrappers/android/examples/java_example/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,14 @@ Once the gradle changes are done and the gradle sync was executed, you should be
It might be required to invalidate caches in case your app is failing to import librealsense (File->Invalidate Caches->Invalidate and restart)

### Application Manifest
Two modifications are required in the [application manifest file](app/src/main/AndroidManifest.xml).
A modifications is required in the [application manifest file](app/src/main/AndroidManifest.xml).

#### Camera Permissions
This permission is required by Androis >= 9, for apps that target lower Android version this is not required.
>```xml
> <uses-permission android:name="android.permission.CAMERA"/>
>```

#### USB Event Filter
The intent-filter should be added to one of the application's activities.
This addition will allow Android to "remember" that the user allowed your app to use the RealSense USB device and prevent from the USB permission pop-up to appear on every run.
>```xml
> <intent-filter>
> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
> </intent-filter>
> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />

## Example Code
Let's look at the only source code file in this example, the [MainActivity](app/src/main/java/com/example/realsense_java_example/MainActivity.java).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />
</activity>
</application>

Expand Down
11 changes: 1 addition & 10 deletions wrappers/android/examples/native_example/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,14 @@ Your native app need to link with 'librealsense.so' in order to do that you will
>```

### Application Manifest
Two modifications are required in the [application manifest file](app/src/main/AndroidManifest.xml).
A modifications is required in the [application manifest file](app/src/main/AndroidManifest.xml).

#### Camera Permissions
This permission is required by Androis >= 9, for apps that target lower Android version this is not required.
>```xml
> <uses-permission android:name="android.permission.CAMERA"/>
>```

#### USB Event Filter
The intent-filter should be added to one of the application's activities.
This addition will allow Android to "remember" that the user allowed your app to use the RealSense USB device and prevent from the USB permission pop-up to appear on every run.
>```xml
> <intent-filter>
> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
> </intent-filter>
> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />

## Example Code
### Java Code
Let's look at the only Java source code file in this example, the [MainActivity](app/src/main/java/com/example/realsense_native_example/MainActivity.java).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/usb_filter" />
</activity>
</application>

Expand Down
14 changes: 13 additions & 1 deletion wrappers/android/librealsense/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.intel.realsense.librealsense">
<application android:allowBackup="true" android:supportsRtl="true"/>
<application android:allowBackup="true" android:supportsRtl="true">
<activity android:name=".DeviceWatcherActivity"
android:theme="@android:style/Theme.NoDisplay"
android:directBootAware="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>

<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/usb_filter" />
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public void enableStream(StreamType type, int index, int width, int height, Stre
nEnableStream(mHandle, type.value(), index, width, height, format.value(), framerate);
}

public void disableStream(StreamType type) { nDisableStream(mHandle, type.value()); }

public void enableAllStreams() { nEnableAllStreams(mHandle); }

public void disableAllStreams() { nDisableAllStreams(mHandle); }

public void enableRecordToFile(String filePath) {
nEnableRecordToFile(mHandle, filePath);
}
Expand All @@ -42,6 +48,9 @@ public void close() {
private static native long nCreate();
private static native void nDelete(long handle);
private static native void nEnableStream(long handle, int type, int index, int width, int height, int format, int framerate);
private static native void nDisableStream(long handle, int type);
private static native void nEnableAllStreams(long handle);
private static native void nDisableAllStreams(long handle);
private static native void nEnableDeviceFromFile(long handle, String filePath);
private static native void nEnableRecordToFile(long handle, String filePath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.intel.realsense.librealsense;

import android.app.Activity;
import android.os.Bundle;

public class DeviceWatcherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ public int getDeviceCount() {
}

public synchronized void setDevicesChangedCallback(DeviceListener listener) {
if(mListener != null)
mDeviceWatcher.removeListener(mListener);
removeDevicesChangedCallback();
mListener = listener;
mDeviceWatcher.addListener(mListener);
}

public synchronized void removeDevicesChangedCallback() {
if(mListener != null)
mDeviceWatcher.removeListener(mListener);
}

public RsContext() {
mHandle = nCreate();
}

@Override
public void close() {
if(mListener != null)
mDeviceWatcher.removeListener(mListener);
removeDevicesChangedCallback();
nDelete(mHandle);
}

Expand Down
Loading