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

close API fixed - openSensor with multiple profiles API added #9320

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
22 changes: 22 additions & 0 deletions src/android/jni/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,28 @@ Java_com_intel_realsense_librealsense_Sensor_nOpen(JNIEnv *env, jclass type, jlo
handle_error(env, e);
}

extern "C"
JNIEXPORT void JNICALL
Java_com_intel_realsense_librealsense_Sensor_nOpenMultiple(JNIEnv *env, jclass type, jlong device_handle,
jlongArray profiles_handle, int num_of_profiles) {
// retrieving profiles from array
jboolean isCopy = 0;
jlong* profiles = env->GetLongArrayElements(profiles_handle, &isCopy);
// building C++ profiles vector
rs2_error* e = nullptr;
std::vector<const rs2_stream_profile*> profs;
profs.reserve(num_of_profiles);
for (int i = 0; i < num_of_profiles; ++i)
{
auto p = reinterpret_cast<const rs2_stream_profile*>(profiles[i]);
profs.push_back(p);
}
// API call
rs2_open_multiple(reinterpret_cast<rs2_sensor *>(device_handle),
profs.data(), static_cast<int>(num_of_profiles), &e);
handle_error(env, e);
}

static frame_callback_data sdata = {NULL, 0, JNI_FALSE, NULL, NULL};

extern "C"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import com.intel.realsense.librealsense.Colorizer;
Expand Down Expand Up @@ -181,37 +183,81 @@ private void configAndStart() throws Exception {
}
}

boolean depth_profile_found = false;
boolean ir_profile_found = false;

if (depth_sensor != null) {
List<StreamProfile> sps = depth_sensor.getStreamProfiles();
StreamProfile sp = sps.get(0);
List<StreamProfile> stream_profiles = depth_sensor.getStreamProfiles();
StreamProfile depth_profile = stream_profiles.get(0);
StreamProfile ir_profile = stream_profiles.get(0);

for (StreamProfile sp2 : sps) {
if (sp2.getType().compareTo(StreamType.DEPTH) == 0) {
for (StreamProfile stream_profile : stream_profiles) {
if (depth_profile_found && ir_profile_found)
break;
if (!depth_profile_found && stream_profile.getType().compareTo(StreamType.DEPTH) == 0) {

if (sp2.is(Extension.VIDEO_PROFILE)) {
VideoStreamProfile video_stream_profile = sp2.as(Extension.VIDEO_PROFILE);
if (stream_profile.is(Extension.VIDEO_PROFILE)) {
VideoStreamProfile video_stream_profile = stream_profile.as(Extension.VIDEO_PROFILE);

// After using the "as" method we can use the new data type
// for additional operations:
StreamFormat sf = video_stream_profile.getFormat();
int index = sp2.getIndex();
StreamType st = sp2.getType();
int index = stream_profile.getIndex();
StreamType st = stream_profile.getType();
int w = video_stream_profile.getWidth();
int h = video_stream_profile.getHeight();
int fps = video_stream_profile.getFrameRate();

if (w == 640 && fps == 30 && (sf.compareTo(StreamFormat.Z16) == 0)) {
if (w == 640 && h == 480 && fps == 30 && (sf.compareTo(StreamFormat.Z16) == 0)) {
Log.d(TAG, "depth stream: " + index + ":" + st.name() + ":" + sf.name() + ":" + w + "x" + h + "@" + fps + "HZ");

sp = sp2;
break;
depth_profile = stream_profile;
depth_profile_found = true;
}
}
}
if (!ir_profile_found && stream_profile.getType().compareTo(StreamType.INFRARED) == 0 && stream_profile.getIndex() == 1 ) {

if (stream_profile.is(Extension.VIDEO_PROFILE)) {
VideoStreamProfile video_stream_profile = stream_profile.as(Extension.VIDEO_PROFILE);

// After using the "as" method we can use the new data type
// for additional operations:
StreamFormat sf = video_stream_profile.getFormat();
int index = stream_profile.getIndex();
StreamType st = stream_profile.getType();
int w = video_stream_profile.getWidth();
int h = video_stream_profile.getHeight();
int fps = video_stream_profile.getFrameRate();

if (w == 640 && h == 480 && fps == 30 && (sf.compareTo(StreamFormat.Y8) == 0)) {
Log.d(TAG, "ir stream: " + index + ":" + st.name() + ":" + sf.name() + ":" + w + "x" + h + "@" + fps + "HZ");

ir_profile = stream_profile;
ir_profile_found = true;
}
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add exception if profiles were not found - or if only one was found, play only this one, and add a toast message on the missing profile

depth_sensor.open(sp);
depth_sensor.start(mDepthFrameHandler);
if (!depth_profile_found && !ir_profile_found) {
Toast.makeText(this, "The requested profiles are not available in this device ", Toast.LENGTH_LONG).show();
}
else {
List<StreamProfile> requested_profiles = new ArrayList<StreamProfile>();
if (depth_profile_found)
requested_profiles.add(depth_profile);
else
Toast.makeText(this, "The depth requested profile is not available in this device ", Toast.LENGTH_LONG).show();

if (ir_profile_found)
requested_profiles.add(ir_profile);
else
Toast.makeText(this, "The infrared requested profile is not available in this device ", Toast.LENGTH_LONG).show();

depth_sensor.openSensor(requested_profiles);
depth_sensor.start(mDepthFrameHandler);
}
}
}

Expand Down Expand Up @@ -241,7 +287,7 @@ private synchronized void stop() {
if (depth_sensor != null) depth_sensor.stop();

if (mColorizer != null) mColorizer.close();
if (depth_sensor != null) {depth_sensor.close();}
if (depth_sensor != null) {depth_sensor.closeSensor();}

if (mDevice != null) mDevice.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void hardwareReset(){
@Override
public void close() {
for (Sensor s : _sensors)
s.delete();
s.close();
if(mOwner)
nRelease(mHandle);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Sensor extends Options {

Sensor(long h) {
mOwner = false;
mHandle = h;
}

Expand Down Expand Up @@ -35,10 +36,21 @@ public boolean is(Extension extension) {
return nIsSensorExtendableTo(mHandle, extension.value());
}

public void open(StreamProfile sp) {
// this method's name is not open() so that the user will call the
// closeSensor to close the sensor (and not the close() method which has another aim)
public void openSensor(StreamProfile sp) {
nOpen(mHandle, sp.getHandle());
}

public void openSensor(List<StreamProfile> sp_list) {
int size = sp_list.size();
long[] profiles_array = new long[size];
for (int i = 0; i < size; ++i) {
profiles_array[i] = sp_list.get(i).getHandle();
}
nOpenMultiple(mHandle, profiles_array, sp_list.size());
}

public void start(FrameCallback cb) {
nStart(mHandle, cb);
}
Expand All @@ -47,20 +59,24 @@ public void stop() {
nStop(mHandle);
}

// release resources - from AutoCloseable interface
@Override
public void close() {
nClose(mHandle);
if (mOwner)
nRelease(mHandle);
}

public void delete() {
if(mOwner)
nRelease(mHandle);
// this method's name is not close() because this is already
// taken by the inherited method from AutoCloseable interface
public void closeSensor(){
nClose(mHandle);
}

private static native long[] nGetStreamProfiles(long handle);
private static native void nRelease(long handle);
private static native boolean nIsSensorExtendableTo(long handle, int extension);
private static native void nOpen(long handle, long sp);
private static native void nOpenMultiple(long handle, long[] sp_list, int num_of_profiles);
private static native void nStart(long handle, FrameCallback callback);
private static native void nStop(long handle);
private static native void nClose(long handle);
Expand Down