Streamer.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.os.Handler;
6 import android.util.Log;
7 
23 
24 import java.util.List;
25 import java.util.Map;
26 
27 public class Streamer {
28  private static final String TAG = "librs camera streamer";
29  private static final int DEFAULT_TIMEOUT = 3000;
30  private static final int L500_TIMEOUT = 15000;
31 
32  interface Listener{
33  void config(Config config);
34  void onFrameset(FrameSet frameSet);
35  }
36 
37  private final Context mContext;
38  private final boolean mLoadConfig;
39 
40  private boolean mIsStreaming = false;
41  private final Handler mHandler = new Handler();
42  private final Listener mListener;
43 
45 
46  public Streamer(Context context, boolean loadConfig, Listener listener){
47  mContext = context;
48  mListener = listener;
49  mLoadConfig = loadConfig;
50  }
51 
52  private Runnable mStreaming = new Runnable() {
53  @Override
54  public void run() {
55  try {
56  try(FrameSet frames = mPipeline.waitForFrames()) {
57  mListener.onFrameset(frames);
58  }
59  mHandler.post(mStreaming);
60  }
61  catch (Exception e) {
62  Log.e(TAG, "streaming, error: " + e.getMessage());
63  }
64  }
65  };
66 
67  private int getFirstFrameTimeout() {
68  RsContext ctx = new RsContext();
69  try(DeviceList devices = ctx.queryDevices()) {
70  if (devices.getDeviceCount() == 0) {
71  return DEFAULT_TIMEOUT;
72  }
73  try (Device device = devices.createDevice(0)) {
74  if(device == null)
75  return DEFAULT_TIMEOUT;
76  ProductLine pl = ProductLine.valueOf(device.getInfo(CameraInfo.PRODUCT_LINE));
77  return pl == ProductLine.L500 ? L500_TIMEOUT : DEFAULT_TIMEOUT;
78  }
79  }
80  }
81 
82  private void configStream(Config config){
83  config.disableAllStreams();
84  RsContext ctx = new RsContext();
85  String pid;
86  Map<Integer, List<StreamProfile>> profilesMap;
87  try(DeviceList devices = ctx.queryDevices()) {
88  if (devices.getDeviceCount() == 0) {
89  return;
90  }
91  try (Device device = devices.createDevice(0)) {
92  if(device == null){
93  Log.e(TAG, "failed to create device");
94  return;
95  }
96  pid = device.getInfo(CameraInfo.PRODUCT_ID);
98 
99  SharedPreferences sharedPref = mContext.getSharedPreferences(mContext.getString(R.string.app_settings), Context.MODE_PRIVATE);
100 
101  for(Map.Entry e : profilesMap.entrySet()){
102  List<StreamProfile> profiles = (List<StreamProfile>) e.getValue();
103  StreamProfile p = profiles.get(0);
104  if(!sharedPref.getBoolean(SettingsActivity.getEnabledDeviceConfigString(pid, p.getType(), p.getIndex()), false))
105  continue;
106  int index = sharedPref.getInt(SettingsActivity.getIndexdDeviceConfigString(pid, p.getType(), p.getIndex()), 0);
107  if(index == -1 || index >= profiles.size())
108  throw new IllegalArgumentException("Failed to resolve config");
109  StreamProfile sp = profiles.get(index);
110  if(p.is(Extension.VIDEO_PROFILE)){
112  config.enableStream(vsp.getType(), vsp.getIndex(), vsp.getWidth(), vsp.getHeight(), vsp.getFormat(), vsp.getFrameRate());
113  }
114  if(p.is(Extension.MOTION_PROFILE)){
116  config.enableStream(msp.getType(), msp.getIndex(), 0, 0, msp.getFormat(), msp.getFrameRate());
117  }
118  }
119  }
120  }
121  }
122 
123  void configAndStart() throws Exception {
124  try(Config config = new Config()){
125  if(mLoadConfig)
127  if(mListener != null)
128  mListener.config(config);
129  // try statement needed here to release resources allocated by the Pipeline:start() method
130  try (PipelineProfile pp = mPipeline.start(config)){}
131  }
132  }
133 
134  public synchronized void start() throws Exception {
135  if(mIsStreaming)
136  return;
137  try{
138  mPipeline = new Pipeline();
139  Log.d(TAG, "try start streaming");
140  configAndStart();
141  int firstFrameTimeOut = getFirstFrameTimeout();
142  try(FrameSet frames = mPipeline.waitForFrames(firstFrameTimeOut)){} // w/a for l500
143  mIsStreaming = true;
144  mHandler.post(mStreaming);
145  Log.d(TAG, "streaming started successfully");
146  } catch (Exception e) {
147  Log.e(TAG, "failed to start streaming: " + e.getMessage());
148  mPipeline.close();
149  throw e;
150  }
151  }
152 
153  public synchronized void stop() {
154  if(!mIsStreaming)
155  return;
156  try {
157  Log.d(TAG, "try stop streaming");
158  mIsStreaming = false;
159  mHandler.removeCallbacks(mStreaming);
160  mPipeline.stop();
161  mPipeline.close();
162  Log.d(TAG, "streaming stopped successfully");
163  } catch (Exception e) {
164  Log.w(TAG, "failed to stop streaming");
165  mPipeline = null;
166  }
167  }
168 }
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
GLfloat GLfloat p
Definition: glext.h:12687
static String getIndexdDeviceConfigString(String pid, StreamType streamType, int streamIndex)
e
Definition: rmse.py:177
static Map< Integer, List< StreamProfile > > createProfilesMap(Device device)
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
GLuint index
Streamer(Context context, boolean loadConfig, Listener listener)
Definition: Streamer.java:46
devices
Definition: test-fg.py:9
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
void enableStream(StreamType type)
Definition: Config.java:9
static String getEnabledDeviceConfigString(String pid, StreamType streamType, int streamIndex)
auto device
Definition: pyrs_net.cpp:17
void configStream(Config config)
Definition: Streamer.java:82


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11