Skip to content

Commit ac61072

Browse files
レコーダの同期を取るための処理を追加
1 parent 57622de commit ac61072

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

dConnectDevicePlugin/dConnectDeviceUVC/app/src/main/java/org/deviceconnect/android/deviceplugin/uvc/profile/UVCMediaStreamRecordingProfile.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.deviceconnect.android.deviceplugin.uvc.profile.utils.H265Profile;
1818
import org.deviceconnect.android.deviceplugin.uvc.recorder.Broadcaster;
1919
import org.deviceconnect.android.deviceplugin.uvc.recorder.MediaRecorder;
20+
import org.deviceconnect.android.deviceplugin.uvc.recorder.MediaRecorderManager;
2021
import org.deviceconnect.android.deviceplugin.uvc.recorder.PreviewServer;
2122
import org.deviceconnect.android.deviceplugin.uvc.recorder.uvc.UvcRecorder;
2223
import org.deviceconnect.android.deviceplugin.uvc.service.UVCService;
@@ -370,6 +371,13 @@ public boolean onRequest(final Intent request, final Intent response) {
370371
return true;
371372
}
372373

374+
MediaRecorderManager mrm = service.getMediaRecorderManager();
375+
if (!mrm.canUseRecorder(recorder)) {
376+
// 他のカメラが使用中の場合はエラーを返却
377+
MessageUtils.setIllegalDeviceStateError(response, "Other recorder are being used.");
378+
return true;
379+
}
380+
373381
recorder.requestPermission(new MediaRecorder.PermissionCallback() {
374382
@Override
375383
public void onAllowed() {
@@ -484,9 +492,22 @@ public boolean onRequest(final Intent request, final Intent response) {
484492
return true;
485493
}
486494

495+
MediaRecorderManager mrm = service.getMediaRecorderManager();
496+
if (!mrm.canUseRecorder(recorder)) {
497+
// 他のカメラが使用中の場合はエラーを返却
498+
MessageUtils.setIllegalDeviceStateError(response, "Other recorder are being used.");
499+
return true;
500+
}
501+
487502
recorder.requestPermission(new MediaRecorder.PermissionCallback() {
488503
@Override
489504
public void onAllowed() {
505+
// TODO 排他的に処理を行うようにします。
506+
if (recorder instanceof UvcRecorder) {
507+
// 使用していない場合は停止する
508+
mrm.stopCameraRecorder(recorder);
509+
}
510+
490511
Broadcaster b = recorder.startBroadcaster(broadcastURI);
491512
if (b != null) {
492513
setResult(response, DConnectMessage.RESULT_OK);

dConnectDevicePlugin/dConnectDeviceUVC/app/src/main/java/org/deviceconnect/android/deviceplugin/uvc/recorder/MediaRecorderManager.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.deviceconnect.android.deviceplugin.uvc.recorder.mjpeg.UvcMjpgRecorder;
1111
import org.deviceconnect.android.deviceplugin.uvc.recorder.uncompressed.UvcUncompressedRecorder;
1212
import org.deviceconnect.android.deviceplugin.uvc.recorder.uvc.UvcRecorder;
13+
import org.deviceconnect.android.libmedia.streaming.gles.EGLSurfaceDrawingThread;
1314
import org.deviceconnect.android.libmedia.streaming.util.WeakReferenceList;
1415
import org.deviceconnect.android.libuvc.Parameter;
1516
import org.deviceconnect.android.libuvc.UVCCamera;
@@ -106,6 +107,59 @@ public void destroy() {
106107
mUvcRecorderList.clear();
107108
}
108109

110+
/**
111+
* レコーダが使用できるか確認します.
112+
*
113+
* @param recorder レコーダ
114+
* @return 使用できる場合はtrue、それ以外はfalse
115+
*/
116+
public boolean canUseRecorder(UvcRecorder recorder) {
117+
for (UvcRecorder uvcRecorder : getUvcRecorderList()) {
118+
if (uvcRecorder == recorder) {
119+
continue;
120+
}
121+
if (uvcRecorder.isPreviewRunning() || uvcRecorder.isBroadcasterRunning() ||
122+
uvcRecorder.getState() == MediaRecorder.State.RECORDING) {
123+
// カメラが使用中
124+
return false;
125+
}
126+
}
127+
return true;
128+
}
129+
130+
/**
131+
* 使用するレコーダ以外のレコーダを停止します。
132+
*
133+
* プレビュー配信中などの停止できない場合には、停止しません。
134+
* その場合には、カメラを使用できないので注意が必要になります。
135+
*
136+
* @param useRecorder 使用するレコーダ
137+
*/
138+
public void stopCameraRecorder(MediaRecorder useRecorder) {
139+
for (MediaRecorder recorder : getUvcRecorderList()) {
140+
if (recorder == useRecorder) {
141+
continue;
142+
}
143+
144+
if (recorder instanceof UvcRecorder) {
145+
if (!recorder.isPreviewRunning() && !recorder.isBroadcasterRunning() &&
146+
recorder.getState() != MediaRecorder.State.RECORDING) {
147+
// 強制的にカメラを停止
148+
EGLSurfaceDrawingThread drawingThread = recorder.getSurfaceDrawingThread();
149+
drawingThread.stop(true);
150+
151+
// カメラの処理は別スレッドで行われているので、ここで少し待機します
152+
try {
153+
Thread.sleep(200);
154+
} catch (InterruptedException e) {
155+
// ignore.
156+
}
157+
}
158+
}
159+
}
160+
}
161+
162+
109163
/**
110164
* レコーダのリストを取得します.
111165
*

dConnectDevicePlugin/dConnectDeviceUVC/app/src/main/java/org/deviceconnect/android/deviceplugin/uvc/recorder/uvc/UvcSurfaceDrawingThread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected void onStopped() {
6565
stopCamera();
6666
}
6767

68-
private void startCamera(SurfaceTexture surfaceTexture) {
68+
private synchronized void startCamera(SurfaceTexture surfaceTexture) {
6969
try {
7070
UvcRecorder.UvcSettings settings = (UvcRecorder.UvcSettings) mRecorder.getSettings();
7171

@@ -100,7 +100,7 @@ public void onError(UVCPlayerException e) {
100100
}
101101
}
102102

103-
private void stopCamera() {
103+
private synchronized void stopCamera() {
104104
if (mPlayer != null) {
105105
try {
106106
mPlayer.stop();

0 commit comments

Comments
 (0)