private NetworkMonitor networkMonitor;
private UsbMonitor mUsbMonitor;
private ServiceHealthMonitor mServiceHealthMonitor;
- private static final int INTERVAL = 2 * 60; // seconds
+ private static final int INTERVAL = 3 * 60; // seconds
- boolean mBounded;
+ boolean mBounded = false;
MyService mServer;
Intent mIntent;
if(mUsbMonitor.hasUsbCamera()){
doBindService();
}
-
}
}, this);
gLogger.error("onNotHealthy, in " + INTERVAL + " seconds, the publishing may stopped or have error ");
doUnbindService();
doBindService();
-
+ //restartApplication();
}
});
mServiceHealthMonitor.setInterval(INTERVAL); // 5 minutes
if(mServer.setRtmpUrl(UserInfo.getConfig().toUrl(mMacAddr))){
mServer.startPreview(mHolder);
}
- gLogger.debug("onServiceConnected - start health monitor thread, interval " + INTERVAL);
+
}
};
private void doBindService(){
gLogger.debug("doBindService");
if(!mBounded && canStartService()) {
+ gLogger.debug("Start service --------->");
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
}
}
private void doUnbindService() {
gLogger.debug("doUnbindService");
if(mBounded){
+ gLogger.debug("Stop service <---------");
unbindService(mConnection);
mBounded = false;
}
if(NetworkMonitor.isNetworkAvailable(this) && mUsbMonitor.hasUsbCamera()){
gLogger.error("Current network is available");
doBindService();
-
-
} else {
- gLogger.error("Current network is NOT available");
+ gLogger.error("Current network NOT available or no USB Camera connected");
}
mBtnStart.setText("start");
public void onClick(View view){
gLogger.error("----------> onClick");
saveConfig();
-
- //unbindService(mConnection);
- bindService(mIntent, mConnection, BIND_AUTO_CREATE);
- //doUnbindService();
- //doBindService();
+ doUnbindService();
+ doBindService();
}
});
if(!mServiceHealthMonitor.isAlive()) {
- gLogger.debug("mServiceHealthMonitor start");
+ gLogger.debug("mServiceHealthMonitor start, interval " + INTERVAL);
mServiceHealthMonitor.start();
}
}
@Override
protected void onDestroy(){
super.onDestroy();
+ mUsbMonitor.unregisterReceiver();
unregisterReceiver(networkMonitor);
+ unbindService(mConnection);
gLogger.debug("onDestroy --------->");
}
public void onCameraError(String msg){
gLogger.error("onCameraEvent " + msg);
if(mUsbMonitor.hasUsbCamera()){
- mServer.reopenCamera();
+ //mServer.reopenCamera();
}
}
gLogger.error("onPublishing: " + msg);
mServiceHealthMonitor.record();
}
+
+ private void restartApplication() {
+ final Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(intent);
+ }
}
}
if(frontId != -1) camerId = frontId;
else if(backId != -1) camerId = backId;
- mCamera = Camera.open(camerId);
+ gLogger.debug("openCameraOriginal - camera id " + camerId);
+ mCamera = Camera.open(1);
} catch (Exception e) {
gLogger.error("openCameraOriginal - camera is not available. error: " + e.getMessage());
if(mListener != null) mListener.onCameraError("openCamera - error: " + e.getMessage());
gLogger.error( "onDestroy --------->");
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
if(mCamera != null){
- mCamera.stopPreview();
- mCamera.release();
+ try {
+ mCamera.stopPreview();
+ mCamera.release();
+ mCamera = null;
+ } catch (Exception e){
+ gLogger.error("onDestroy - error " + e.getMessage());
+ e.printStackTrace();
+ }
}
}
}
public void reopenCamera() {
+ gLogger.debug("reopenCamera");
if(mCamera != null){
mCamera.stopPreview();
mCamera.release();
+ mCamera = null;
}
openCameraOriginal();
}
public boolean setRtmpUrl (String url){
//this.rtmpUrl = url;
- if(mCamera == null) return false;
+ if(mCamera == null){
+ gLogger.error("setRtmpUrl, error mCamera is null");
+ return false;
+ }
gLogger.error("setRtmpUrl - size: " + width + "x" + height + ". url: " + url);
int ret = FfmpegHelper.initEncoder(width, height, url);
if(ret != 0){
//mUsbManager.requestPermission(device, mPermissionIntent);
}
+ public void unregisterReceiver (){
+ mContext.unregisterReceiver(mUsbReceiver);
+
+ }
+
private boolean isUvcCamera(UsbDevice device){
return (device.getDeviceProtocol() == 1 && device.getProductName().contains("Camera"));
}
gLogger.error(device.toString());
if(isUvcCamera(device)) {
mUsbCameraList.remove(device);
- mListener.onCameraConnected();
+ mListener.onCameraDisconnected();
}
}
} else if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
gLogger.error(device.toString());
if(isUvcCamera(device)) {
mUsbCameraList.add(device);
- mListener.onCameraDisconnected();
+ mListener.onCameraConnected();
}
}
}