Skip to content

Commit be3571c

Browse files
committed
Use SurfaceTexture to keep the video running even if main Activity is paused or stopped. Fixes #6
1 parent 0bb7a56 commit be3571c

File tree

11 files changed

+185
-134
lines changed

11 files changed

+185
-134
lines changed

sensorhub-android-app/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<uses-permission android:name="android.permission.CAMERA" />
1212
<uses-permission android:name="android.permission.INTERNET" />
1313
<uses-permission android:name="android.permission.BLUETOOTH" />
14-
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
14+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
1515

1616
<uses-sdk
1717
android:minSdkVersion="21"

sensorhub-android-app/res/layout/activity_main.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
android:paddingTop="@dimen/activity_vertical_margin"
1010
tools:context=".MainActivity" >
1111

12-
<SurfaceView
13-
android:id="@+id/textureView1"
12+
<TextureView
13+
android:id="@+id/video"
1414
android:layout_width="match_parent"
1515
android:layout_height="match_parent"
1616
android:layout_gravity="center"/>
17-
17+
1818
<TextView
1919
android:id="@+id/text"
2020
android:layout_width="match_parent"
2121
android:layout_height="wrap_content"
2222
android:padding="10dp"
2323
android:background="#FFFFFF"
2424
android:text="@string/hello_world"/>
25-
25+
2626
</RelativeLayout>

sensorhub-android-app/src/org/sensorhub/android/MainActivity.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import android.content.pm.PackageInfo;
1818
import android.content.pm.PackageManager;
19+
import android.graphics.PixelFormat;
20+
import android.graphics.SurfaceTexture;
1921
import android.util.Log;
2022
import java.net.MalformedURLException;
2123
import java.net.URL;
@@ -71,16 +73,17 @@
7173
import android.widget.TextView;
7274
import org.vast.util.DateTimeFormat;
7375

76+
import javax.microedition.khronos.egl.EGL10;
7477

75-
public class MainActivity extends Activity implements SurfaceHolder.Callback, IEventListener
78+
79+
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener, IEventListener
7680
{
7781
TextView textArea;
7882
SensorHubService boundService;
7983
IModuleConfigRepository sensorhubConfig;
8084
Handler displayHandler;
8185
Runnable displayCallback;
8286
StringBuffer displayText = new StringBuffer();
83-
SurfaceHolder camPreviewSurfaceHolder;
8487
boolean oshStarted = false;
8588
ArrayList<SOSTClient> sostClients = new ArrayList<SOSTClient>();
8689
URL sosUrl = null;
@@ -144,7 +147,7 @@ protected void updateConfig(SharedPreferences prefs, String runName)
144147
showVideo = true;
145148
sensorsConfig.videoCodec = prefs.getString("video_codec", AndroidSensorsConfig.JPEG_CODEC);
146149
sensorsConfig.androidContext = this.getApplicationContext();
147-
sensorsConfig.camPreviewSurfaceHolder = this.camPreviewSurfaceHolder;
150+
sensorsConfig.camPreviewTexture = boundService.getVideoTexture();
148151
sensorsConfig.runName = runName;
149152
sensorhubConfig.add(sensorsConfig);
150153
addSosTConfig(sensorsConfig, sosUser, sosPwd);
@@ -201,7 +204,7 @@ protected void updateConfig(SharedPreferences prefs, String runName)
201204
flironeConfig.name = "FLIR One Camera [" + deviceName + "]";
202205
flironeConfig.autoStart = true;
203206
flironeConfig.androidContext = this.getApplicationContext();
204-
flironeConfig.camPreviewSurfaceHolder = this.camPreviewSurfaceHolder;
207+
flironeConfig.camPreviewTexture = boundService.getVideoTexture();
205208
sensorhubConfig.add(flironeConfig);
206209
addSosTConfig(flironeConfig, sosUser, sosPwd);
207210
}
@@ -238,8 +241,6 @@ protected void onCreate(Bundle savedInstanceState)
238241
super.onCreate(savedInstanceState);
239242
setContentView(R.layout.activity_main);
240243
textArea = (TextView) findViewById(R.id.text);
241-
SurfaceView camPreview = (SurfaceView) findViewById(R.id.textureView1);
242-
camPreview.getHolder().addCallback(this);
243244

244245
// bind to SensorHub service
245246
Intent intent = new Intent(this, SensorHubService.class);
@@ -318,12 +319,17 @@ public void onClick(DialogInterface dialog, int whichButton)
318319
{
319320
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
320321
String runName = input.getText().toString();
321-
updateConfig(PreferenceManager.getDefaultSharedPreferences(MainActivity.this), runName);
322322
newStatusMessage("Starting SensorHub...");
323+
324+
updateConfig(PreferenceManager.getDefaultSharedPreferences(MainActivity.this), runName);
323325
sostClients.clear();
324326
boundService.startSensorHub(sensorhubConfig, MainActivity.this);
327+
325328
if (showVideo)
329+
{
330+
showVideo();
326331
textArea.setBackgroundColor(0x80FFFFFF);
332+
}
327333
}
328334
});
329335

@@ -350,7 +356,7 @@ protected void showAboutPopup()
350356
}
351357

352358
String message = "A software platform for building smart sensor networks and the Internet of Things\n\n";
353-
message += "Version: " + version;
359+
message += "Version: " + version + "\n";
354360

355361
AlertDialog.Builder alert = new AlertDialog.Builder(this);
356362
alert.setTitle("OpenSensorHub");
@@ -399,7 +405,6 @@ public void run()
399405
{
400406
displayStatus();
401407
textArea.setText(Html.fromHtml(displayText.toString()));
402-
//textArea.setText(new DateTimeFormat().formatIso(System.currentTimeMillis()/1000., 0));
403408
displayHandler.postDelayed(this, 1000);
404409
}
405410
};
@@ -524,6 +529,25 @@ protected void stopListeningForEvents()
524529
}
525530

526531

532+
protected void showVideo()
533+
{
534+
if (boundService.getVideoTexture() != null)
535+
{
536+
TextureView textureView = (TextureView) findViewById(R.id.video);
537+
if (textureView.getSurfaceTexture() != boundService.getVideoTexture())
538+
{
539+
textureView.setSurfaceTexture(boundService.getVideoTexture());
540+
textureView.setSurfaceTextureListener(this);
541+
}
542+
}
543+
}
544+
545+
546+
protected void hideVideo()
547+
{
548+
}
549+
550+
527551
@Override
528552
protected void onStart()
529553
{
@@ -540,6 +564,7 @@ protected void onResume()
540564
{
541565
startListeningForEvents();
542566
startRefreshingStatus();
567+
showVideo();
543568
}
544569
}
545570

@@ -549,6 +574,7 @@ protected void onPause()
549574
{
550575
stopListeningForEvents();
551576
stopRefreshingStatus();
577+
hideVideo();
552578
super.onPause();
553579
}
554580

@@ -571,20 +597,25 @@ protected void onDestroy()
571597

572598

573599
@Override
574-
public void surfaceCreated(SurfaceHolder holder)
600+
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1)
575601
{
576-
this.camPreviewSurfaceHolder = holder;
577602
}
578603

579604

580605
@Override
581-
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
606+
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1)
582607
{
583608
}
584609

585610

586611
@Override
587-
public void surfaceDestroyed(SurfaceHolder holder)
612+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture)
613+
{
614+
return false;
615+
}
616+
617+
@Override
618+
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture)
588619
{
589-
}
620+
}
590621
}

sensorhub-android-app/src/org/sensorhub/android/SensorHubService.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616

1717
import javax.xml.parsers.DocumentBuilderFactory;
1818

19+
import android.content.Context;
20+
import android.graphics.PixelFormat;
21+
import android.graphics.SurfaceTexture;
1922
import android.os.*;
2023
import android.os.Process;
2124
import android.util.Log;
25+
import android.view.Gravity;
26+
import android.view.SurfaceView;
27+
import android.view.WindowManager;
2228
import org.sensorhub.api.common.IEventListener;
2329
import org.sensorhub.api.module.IModuleConfigRepository;
2430
import org.sensorhub.api.module.ModuleConfig;
@@ -45,8 +51,9 @@ public class SensorHubService extends Service
4551
private HandlerThread msgThread;
4652
private Handler msgHandler;
4753
SensorHub sensorhub;
48-
49-
54+
SurfaceTexture videoTex;
55+
56+
5057
public class LocalBinder extends Binder {
5158
SensorHubService getService() {
5259
return SensorHubService.this;
@@ -71,6 +78,10 @@ public void onCreate() {
7178
dbf.setNamespaceAware(true);
7279
XMLImplFinder.setDOMImplementation(dbf.newDocumentBuilder().getDOMImplementation());
7380

81+
// create video surface texture here so it's not destroyed when pausing the app
82+
videoTex = new SurfaceTexture(1);
83+
videoTex.detachFromGLContext();
84+
7485
// start handler thread
7586
msgThread = new HandlerThread("SensorHubService", Process.THREAD_PRIORITY_BACKGROUND);
7687
msgThread.start();
@@ -129,6 +140,7 @@ public void onDestroy()
129140
{
130141
stopSensorHub();
131142
msgThread.quitSafely();
143+
videoTex.release();
132144
}
133145

134146

@@ -139,12 +151,15 @@ public IBinder onBind(Intent intent)
139151
}
140152

141153

142-
/**
143-
* @return the SensorHub instance
144-
*/
145154
public SensorHub getSensorHub()
146155
{
147156
return sensorhub;
148157
}
158+
159+
160+
public SurfaceTexture getVideoTexture()
161+
{
162+
return videoTex;
163+
}
149164

150165
}

sensorhub-android-flirone/src/org/sensorhub/impl/driver/flir/FlirOneCameraConfig.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212
1313
******************************* END LICENSE BLOCK ***************************/
1414

15-
package org.sensorhub.impl.driver.flir;
16-
17-
import org.sensorhub.api.sensor.SensorConfig;
15+
package org.sensorhub.impl.driver.flir;
16+
17+
import android.graphics.SurfaceTexture;
18+
import org.sensorhub.api.sensor.SensorConfig;
1819
import android.content.Context;
19-
import android.view.SurfaceHolder;
20-
21-
22-
/**
23-
* <p>
24-
* Configuration class for the FLIR One thermal camera driver
25-
* </p>
26-
*
27-
* @author Alex Robin <[email protected]>
28-
* @since Mar 5, 2016
29-
*/
30-
public class FlirOneCameraConfig extends SensorConfig
20+
21+
22+
/**
23+
* <p>
24+
* Configuration class for the FLIR One thermal camera driver
25+
* </p>
26+
*
27+
* @author Alex Robin <[email protected]>
28+
* @since Mar 5, 2016
29+
*/
30+
public class FlirOneCameraConfig extends SensorConfig
3131
{
3232
public boolean enableVisOutput = false;
3333
public boolean enableStatusOutput = false;
@@ -36,12 +36,12 @@ public class FlirOneCameraConfig extends SensorConfig
3636
public String runDescription;
3737

3838
public transient Context androidContext;
39-
public transient SurfaceHolder camPreviewSurfaceHolder;
39+
public transient SurfaceTexture camPreviewTexture;
4040

4141

4242

4343
public FlirOneCameraConfig()
4444
{
4545
this.moduleClass = FlirOneCameraDriver.class.getCanonicalName();
46-
}
47-
}
46+
}
47+
}

0 commit comments

Comments
 (0)