Skip to content

Commit f203a00

Browse files
Merge pull request #504 from TakayukiHoshi1984/modify_host_camera_front_or_back
HostのCameraの排他制御
2 parents 3769176 + 70412c3 commit f203a00

11 files changed

Lines changed: 136 additions & 86 deletions

File tree

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/camera/CameraWrapper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ public synchronized void stopRecording() throws CameraWrapperException {
551551
close();
552552
if (mIsPreview) {
553553
startPreview(mPreviewSurface, true);
554+
} else if (mIsTouchOn) {
555+
mIsTouchOn = false;
556+
turnOnTorch();
554557
}
555558
notifyCameraEvent(CameraEvent.STOPPED_VIDEO_RECORDING);
556559
}
@@ -623,17 +626,21 @@ public void onCaptureBufferLost(@NonNull CameraCaptureSession session, @NonNull
623626
}
624627
resumeRepeatingRequest();
625628
throw new CameraWrapperException(e);
629+
} finally {
630+
mIsTakingStillImage = false;
626631
}
627632
}
628633

629634
private void resumeRepeatingRequest() {
630-
mIsTakingStillImage = false;
631635

632636
try {
633637
if (mIsRecording) {
634638
startRecording(mRecordingSurface, true);
635639
} else if (mIsPreview) {
636640
startPreview(mPreviewSurface, true);
641+
} else if (mIsTouchOn) {
642+
mIsTouchOn = false;
643+
turnOnTorch();
637644
} else {
638645
close();
639646
}
@@ -794,7 +801,12 @@ public synchronized void turnOnTorch(final @Nullable TorchOnListener listener,
794801
if (mTargetSurface != null) {
795802
requestBuilder.addTarget(mTargetSurface);
796803
}
797-
} else {
804+
}
805+
if (mIsRecording) {
806+
requestBuilder.addTarget(mRecordingSurface);
807+
}
808+
809+
if (!mIsPreview && !mIsRecording) {
798810
requestBuilder.addTarget(mDummyPreviewReader.getSurface());
799811
}
800812
requestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
@@ -848,6 +860,8 @@ public synchronized void turnOffTorch(final @Nullable TorchOffListener listener,
848860
throw new IllegalArgumentException(e);
849861
} catch (CameraWrapperException e) {
850862
throw new IllegalArgumentException(e);
863+
} finally {
864+
resumeRepeatingRequest();
851865
}
852866
notifyTorchOffEvent(listener, handler);
853867
}

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostLightProfile.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ public boolean onRequest(final Intent request, final Intent response) {
8989
new PermissionUtility.PermissionRequestCallback() {
9090
@Override
9191
public void onSuccess() {
92-
if (!(isCameraAvailable())) {
93-
MessageUtils.setIllegalDeviceStateError(response, "Camera device is already running.");
94-
sendResponse(response);
95-
return;
96-
}
97-
9892
Bundle lightParam = new Bundle();
9993
setName(lightParam, HOST_DEFAULT_LIGHT_NAME);
10094
setConfig(lightParam, "");
@@ -144,12 +138,6 @@ public boolean onRequest(final Intent request, final Intent response) {
144138
new PermissionUtility.PermissionRequestCallback() {
145139
@Override
146140
public void onSuccess() {
147-
if (!(isCameraAvailable())) {
148-
MessageUtils.setIllegalDeviceStateError(response, "Camera device is already running.");
149-
sendResponse(response);
150-
return;
151-
}
152-
153141
if (flashing != null) {
154142
flashing(HOST_LIGHT_ID, flashing);
155143
setResult(response, DConnectMessage.RESULT_OK);
@@ -213,12 +201,6 @@ public boolean onRequest(final Intent request, final Intent response) {
213201
new PermissionUtility.PermissionRequestCallback() {
214202
@Override
215203
public void onSuccess() {
216-
if (!(isCameraAvailable())) {
217-
MessageUtils.setIllegalDeviceStateError(response, "Camera device is already running.");
218-
sendResponse(response);
219-
return;
220-
}
221-
222204
mPhotoRec.turnOffFlashLight(new HostDevicePhotoRecorder.TurnOffFlashLightListener() {
223205
@Override
224206
public void onRequested() {
@@ -294,18 +276,6 @@ private Handler createHandler(final String name) {
294276
return new Handler(thread.getLooper());
295277
}
296278

297-
/**
298-
* カメラが使用可能どうかをチェックする.
299-
*
300-
* @return 使用可能の場合は true, そうでない場合は false.
301-
*/
302-
private boolean isCameraAvailable() {
303-
if (((HostMediaRecorder) mPhotoRec).getState() != HostMediaRecorder.RecorderState.INACTTIVE) {
304-
return false;
305-
}
306-
return true;
307-
}
308-
309279
/**
310280
* 点滅制御.
311281
*

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostLiveStreamingProfile.java

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class HostLiveStreamingProfile extends DConnectProfile implements LiveStr
4848
private static final String VIDEO_URI_FALSE = "false";
4949
private static final String VIDEO_URI_CAMERA_FRONT = "camera-front";
5050
private static final String VIDEO_URI_CAMERA_BACK = "camera-back";
51+
private static final String VIDEO_URI_CAMERA_0 = "camera_0";
52+
private static final String VIDEO_URI_CAMERA_1 = "camera_1";
5153
private static final String AUDIO_URI_TRUE = "true";
5254
private static final String AUDIO_URI_FALSE = "false";
5355
private static final int CAMERA_TYPE_FRONT = 0;
@@ -108,6 +110,8 @@ public boolean onRequest(final Intent request, final Intent response) {
108110
case VIDEO_URI_FALSE:
109111
case VIDEO_URI_CAMERA_FRONT:
110112
case VIDEO_URI_CAMERA_BACK:
113+
case VIDEO_URI_CAMERA_0:
114+
case VIDEO_URI_CAMERA_1:
111115
break;
112116
default:
113117
MessageUtils.setInvalidRequestParameterError(response, "video parameter illegal");
@@ -416,35 +420,42 @@ public boolean onRequest(final Intent request, final Intent response) {
416420
});
417421
}
418422

419-
private HostDeviceLiveStreamRecorder getHostDeviceLiveStreamRecorder() {
423+
private HostDeviceLiveStreamRecorder getHostDeviceLiveStreamRecorder() {
420424
if (DEBUG) {
421425
Log.d(TAG, "getHostDeviceLiveStreamRecorder()");
422426
Log.d(TAG, "mVideoURI : " + mVideoURI);
423427
}
424428
switch (mVideoURI) {
425-
case VIDEO_URI_TRUE: {
426-
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(null);
427-
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
428-
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
429-
}
430-
break;
431-
}
429+
case VIDEO_URI_TRUE:
432430
case VIDEO_URI_FALSE: {
433431
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(null);
432+
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
433+
throw new RuntimeException("Another target in using.");
434+
}
435+
434436
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
435437
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
436438
}
437439
break;
438440
}
439-
case VIDEO_URI_CAMERA_FRONT: {
440-
HostMediaRecorder hostMediaRecorder = getRecorder(CAMERA_TYPE_FRONT);
441+
case VIDEO_URI_CAMERA_FRONT:
442+
case VIDEO_URI_CAMERA_1: {
443+
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(VIDEO_URI_CAMERA_1);
444+
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
445+
throw new RuntimeException("Another target in using.");
446+
}
441447
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
442448
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
443449
}
444450
break;
445451
}
446-
case VIDEO_URI_CAMERA_BACK: {
447-
HostMediaRecorder hostMediaRecorder = getRecorder(CAMERA_TYPE_BACK);
452+
case VIDEO_URI_CAMERA_BACK:
453+
case VIDEO_URI_CAMERA_0: {
454+
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(VIDEO_URI_CAMERA_0);
455+
if (mHostMediaRecorderManager.usingPreviewOrStreamingRecorder(hostMediaRecorder.getId())) {
456+
throw new RuntimeException("Another target in using.");
457+
}
458+
448459
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
449460
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
450461
}
@@ -455,31 +466,6 @@ private HostDeviceLiveStreamRecorder getHostDeviceLiveStreamRecorder() {
455466
throw new RuntimeException("recorder not found");
456467
}
457468

458-
459-
private HostMediaRecorder getRecorder(int type) {
460-
if (DEBUG) {
461-
Log.d(TAG, "getRecorder()");
462-
Log.d(TAG, "type" + type);
463-
}
464-
for(HostMediaRecorder hostMediaRecorder : mHostMediaRecorderManager.getRecorders()) {
465-
if (DEBUG) {
466-
Log.d(TAG, "name : " + hostMediaRecorder.getName());
467-
}
468-
if (hostMediaRecorder.getName().matches("^Camera [0-9] \\((Front|Back)\\)")) {
469-
if (type == CAMERA_TYPE_FRONT) {
470-
if (hostMediaRecorder.getName().contains("Front")) {
471-
return hostMediaRecorder;
472-
}
473-
} else if (type == CAMERA_TYPE_BACK) {
474-
if (hostMediaRecorder.getName().contains("Back")) {
475-
return hostMediaRecorder;
476-
}
477-
}
478-
}
479-
}
480-
return null;
481-
}
482-
483469
@Override
484470
public void onStart() {
485471
if (DEBUG) {

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/profile/HostMediaStreamingRecordingProfile.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ private void setOptions(final Intent request, final Intent response) {
218218
return;
219219
}
220220

221-
if (recorder.getState() != HostMediaRecorder.RecorderState.INACTTIVE) {
221+
if (recorder.getState() != HostMediaRecorder.RecorderState.INACTIVE
222+
&& recorder.getState() != HostMediaRecorder.RecorderState.PREVIEW) {
222223
MessageUtils.setInvalidRequestParameterError(response, "settings of active target cannot be changed.");
223224
return;
224225
}
@@ -431,6 +432,10 @@ public boolean onRequest(final Intent request, final Intent response) {
431432
MessageUtils.setInvalidRequestParameterError(response, "target is invalid.");
432433
return true;
433434
}
435+
if (mRecorderMgr.usingPreviewOrStreamingRecorder(recorder.getId())) {
436+
MessageUtils.setInvalidRequestParameterError(response, "Another target in using.");
437+
return true;
438+
}
434439

435440
if (!(recorder instanceof HostDevicePhotoRecorder)) {
436441
MessageUtils.setNotSupportAttributeError(response,
@@ -501,7 +506,10 @@ public boolean onRequest(final Intent request, final Intent response) {
501506
MessageUtils.setInvalidRequestParameterError(response, "target is invalid.");
502507
return true;
503508
}
504-
509+
if (mRecorderMgr.usingPreviewOrStreamingRecorder(recorder.getId())) {
510+
MessageUtils.setInvalidRequestParameterError(response, "Another target in using.");
511+
return true;
512+
}
505513
recorder.requestPermission(new HostMediaRecorder.PermissionCallback() {
506514
@Override
507515
public void onAllowed() {
@@ -559,7 +567,6 @@ public boolean onRequest(final Intent request, final Intent response) {
559567
MessageUtils.setInvalidRequestParameterError(response, "target is invalid.");
560568
return true;
561569
}
562-
563570
recorder.requestPermission(new HostMediaRecorder.PermissionCallback() {
564571
@Override
565572
public void onAllowed() {
@@ -678,14 +685,19 @@ public boolean onRequest(final Intent request, final Intent response) {
678685
MessageUtils.setInvalidRequestParameterError(response, "target is invalid.");
679686
return true;
680687
}
688+
if (mRecorderMgr.usingPreviewOrStreamingRecorder(recorder.getId())) {
689+
MessageUtils.setInvalidRequestParameterError(response, "Another target in using.");
690+
return true;
691+
}
681692

682693
if (!(recorder instanceof HostDeviceStreamRecorder)) {
683694
MessageUtils.setNotSupportAttributeError(response,
684695
"target does not support stream recording.");
685696
return true;
686697
}
687698

688-
if (recorder.getState() != HostMediaRecorder.RecorderState.INACTTIVE) {
699+
if (recorder.getState() != HostMediaRecorder.RecorderState.INACTIVE
700+
&& recorder.getState() != HostMediaRecorder.RecorderState.PREVIEW) {
689701
MessageUtils.setIllegalDeviceStateError(response,
690702
recorder.getName() + " is already running.");
691703
return true;
@@ -744,14 +756,13 @@ public boolean onRequest(final Intent request, final Intent response) {
744756
MessageUtils.setInvalidRequestParameterError(response, "target is invalid.");
745757
return true;
746758
}
747-
748759
if (!(recorder instanceof HostDeviceStreamRecorder)) {
749760
MessageUtils.setNotSupportAttributeError(response,
750761
"target does not support stream recording.");
751762
return true;
752763
}
753764

754-
if (recorder.getState() == HostMediaRecorder.RecorderState.INACTTIVE) {
765+
if (recorder.getState() == HostMediaRecorder.RecorderState.INACTIVE) {
755766
MessageUtils.setIllegalDeviceStateError(response, "recorder is stopped already.");
756767
return true;
757768
}
@@ -907,7 +918,6 @@ public void onDisallowed() {
907918
public HostMediaStreamingRecordingProfile(final HostMediaRecorderManager mgr, final FileManager fileMgr) {
908919
mRecorderMgr = mgr;
909920
mFileManager = fileMgr;
910-
911921
addApi(mGetMediaRecorderApi);
912922
addApi(mGetOptionsApi);
913923
addApi(mPutOptionsApi);

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/recorder/AbstractPreviewServerProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public abstract class AbstractPreviewServerProvider implements PreviewServerProv
5252
*/
5353
private HostMediaRecorder mRecorder;
5454

55+
private boolean mIsShownNotification;
5556
/**
5657
* コンストラクタ.
5758
* @param context コンテキスト
@@ -60,6 +61,7 @@ public AbstractPreviewServerProvider(final Context context, final HostMediaRecor
6061
mContext = context;
6162
mRecorder = recorder;
6263
mNotificationId = notificationId;
64+
mIsShownNotification = false;
6365
}
6466

6567
// PreviewServerProvider
@@ -118,6 +120,7 @@ public void onFail() {
118120
// TODO タイムアウト処理
119121
} else {
120122
sendNotification(mRecorder.getId(), mRecorder.getName());
123+
mIsShownNotification = true;
121124
}
122125
} catch (InterruptedException e) {
123126
// ignore.
@@ -171,6 +174,7 @@ private void hideNotification(String id) {
171174
.getSystemService(Service.NOTIFICATION_SERVICE);
172175
if (manager != null) {
173176
manager.cancel(id, getNotificationId());
177+
mIsShownNotification = false;
174178
}
175179
}
176180

@@ -252,4 +256,9 @@ private PendingIntent createPendingIntent(String id) {
252256
intent.putExtra(EXTRA_CAMERA_ID, id);
253257
return PendingIntent.getBroadcast(mContext, getNotificationId(), intent, 0);
254258
}
259+
260+
@Override
261+
public boolean isShownCameraNotification() {
262+
return mIsShownNotification;
263+
}
255264
}

dConnectDevicePlugin/dConnectDeviceHost/app/src/main/java/org/deviceconnect/android/deviceplugin/host/recorder/HostMediaRecorder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ enum RecorderState {
287287
/**
288288
* 動作していない.
289289
*/
290-
INACTTIVE,
290+
INACTIVE,
291291

292292
/**
293293
* 録画が一時停止中の状態.
@@ -299,6 +299,11 @@ enum RecorderState {
299299
*/
300300
RECORDING,
301301

302+
/**
303+
* プレビューが表示されている状態.
304+
*/
305+
PREVIEW,
306+
302307
/**
303308
* エラーで停止している状態.
304309
*/

0 commit comments

Comments
 (0)