Skip to content

Commit 72aefde

Browse files
ScreenCastを配信する機能の追加。
1 parent 9ecbc22 commit 72aefde

2 files changed

Lines changed: 245 additions & 60 deletions

File tree

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

Lines changed: 100 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.deviceconnect.android.deviceplugin.host.recorder.HostDeviceLiveStreamRecorder;
1212
import org.deviceconnect.android.deviceplugin.host.recorder.HostMediaRecorder;
1313
import org.deviceconnect.android.deviceplugin.host.recorder.HostMediaRecorderManager;
14+
import org.deviceconnect.android.deviceplugin.host.recorder.screen.ScreenCastRecorder;
1415
import org.deviceconnect.android.event.Event;
1516
import org.deviceconnect.android.event.EventError;
1617
import org.deviceconnect.android.event.EventManager;
@@ -50,6 +51,7 @@ public class HostLiveStreamingProfile extends DConnectProfile implements LiveStr
5051
private static final String VIDEO_URI_CAMERA_BACK = "camera-back";
5152
private static final String VIDEO_URI_CAMERA_0 = "camera_0";
5253
private static final String VIDEO_URI_CAMERA_1 = "camera_1";
54+
private static final String VIDEO_URI_SCREEN = "screen";
5355
private static final String AUDIO_URI_TRUE = "true";
5456
private static final String AUDIO_URI_FALSE = "false";
5557
private static final int CAMERA_TYPE_FRONT = 0;
@@ -100,6 +102,7 @@ public boolean onRequest(final Intent request, final Intent response) {
100102
Log.d(TAG, "broadcastURI : " + broadcastURI);
101103
}
102104

105+
boolean isScreenCast = false;
103106
//映像リソースURIの取得
104107
mVideoURI = (String) extras.get(PARAM_KEY_VIDEO);
105108
if (mVideoURI == null) {
@@ -113,6 +116,9 @@ public boolean onRequest(final Intent request, final Intent response) {
113116
case VIDEO_URI_CAMERA_0:
114117
case VIDEO_URI_CAMERA_1:
115118
break;
119+
case VIDEO_URI_SCREEN:
120+
isScreenCast = true;
121+
break;
116122
default:
117123
MessageUtils.setInvalidRequestParameterError(response, "video parameter illegal");
118124
return true;
@@ -121,7 +127,6 @@ public boolean onRequest(final Intent request, final Intent response) {
121127
if (DEBUG) {
122128
Log.d(TAG, "mVideoURI : " + mVideoURI);
123129
}
124-
125130
//映像リソースURIからレコーダーを取得する
126131
try {
127132
mHostDeviceLiveStreamRecorder = getHostDeviceLiveStreamRecorder();
@@ -137,51 +142,24 @@ public boolean onRequest(final Intent request, final Intent response) {
137142
return true;
138143
}
139144

140-
//クライアントの生成
141-
mHostDeviceLiveStreamRecorder.createLiveStreamingClient(broadcastURI, eventListener);
142-
143-
//映像無し以外の場合はエンコーダーとパラメーターをセット
144-
if (!mVideoURI.equals("false")) {
145-
Integer width = parseInteger(request, PARAM_KEY_WIDTH);
146-
Integer height = parseInteger(request, PARAM_KEY_HEIGHT);
147-
Integer bitrate = parseInteger(request, PARAM_KEY_BITRATE);
148-
Integer frameRate = parseInteger(request, PARAM_KEY_FRAME_RATE);
149-
if (DEBUG) {
150-
Log.d(TAG, "width : " + width);
151-
Log.d(TAG, "height : " + height);
152-
Log.d(TAG, "bitrate : " + bitrate);
153-
Log.d(TAG, "frameRate : " + frameRate);
154-
}
155-
mHostDeviceLiveStreamRecorder.setVideoEncoder(width, height, bitrate, frameRate);
156-
}
157-
158-
//音声リソースURIの取得
159-
mAudioURI = (String) extras.get(PARAM_KEY_AUDIO);
160-
if (mAudioURI == null) {
161-
mAudioURI = "false";
145+
if (isScreenCast) {
146+
((ScreenCastRecorder) mHostDeviceLiveStreamRecorder).requestPermission(new HostMediaRecorder.PermissionCallback() {
147+
@Override
148+
public void onAllowed() {
149+
startLiveStreaming(request, response, extras, broadcastURI, eventListener);
150+
sendResponse(response);
151+
}
152+
153+
@Override
154+
public void onDisallowed() {
155+
MessageUtils.setUnknownError(response, "Permission for screencast is not granted.");
156+
sendResponse(response);
157+
}
158+
});
159+
return false;
162160
} else {
163-
switch (mAudioURI) {
164-
case AUDIO_URI_TRUE:
165-
case AUDIO_URI_FALSE:
166-
break;
167-
default:
168-
MessageUtils.setInvalidRequestParameterError(response, "audio parameter illegal");
169-
return true;
170-
}
171-
}
172-
if (DEBUG) {
173-
Log.d(TAG, "audioUri : " + mAudioURI);
161+
startLiveStreaming(request, response, extras, broadcastURI, eventListener);
174162
}
175-
176-
//音声無し以外の場合はエンコーダーをセット
177-
if (!mAudioURI.equals("false")) {
178-
mHostDeviceLiveStreamRecorder.setAudioEncoder();
179-
}
180-
181-
//ストリーミング開始
182-
mHostDeviceLiveStreamRecorder.startLiveStreaming();
183-
184-
setResult(response, DConnectMessage.RESULT_OK);
185163
} else {
186164
MessageUtils.setInvalidRequestParameterError(response, "parameter not available");
187165
}
@@ -357,6 +335,54 @@ public boolean onRequest(final Intent request, final Intent response) {
357335
});
358336
}
359337

338+
private void startLiveStreaming(Intent request, Intent response, Bundle extras, String broadcastURI, LiveStreamingClient.EventListener eventListener) {
339+
//クライアントの生成
340+
mHostDeviceLiveStreamRecorder.createLiveStreamingClient(broadcastURI, eventListener);
341+
342+
//映像無し以外の場合はエンコーダーとパラメーターをセット
343+
if (!mVideoURI.equals("false")) {
344+
Integer width = parseInteger(request, PARAM_KEY_WIDTH);
345+
Integer height = parseInteger(request, PARAM_KEY_HEIGHT);
346+
Integer bitrate = parseInteger(request, PARAM_KEY_BITRATE);
347+
Integer frameRate = parseInteger(request, PARAM_KEY_FRAME_RATE);
348+
if (DEBUG) {
349+
Log.d(TAG, "width : " + width);
350+
Log.d(TAG, "height : " + height);
351+
Log.d(TAG, "bitrate : " + bitrate);
352+
Log.d(TAG, "frameRate : " + frameRate);
353+
}
354+
mHostDeviceLiveStreamRecorder.setVideoEncoder(width, height, bitrate, frameRate);
355+
}
356+
357+
//音声リソースURIの取得
358+
mAudioURI = (String) extras.get(PARAM_KEY_AUDIO);
359+
if (mAudioURI == null) {
360+
mAudioURI = "false";
361+
} else {
362+
switch (mAudioURI) {
363+
case AUDIO_URI_TRUE:
364+
case AUDIO_URI_FALSE:
365+
break;
366+
default:
367+
MessageUtils.setInvalidRequestParameterError(response, "audio parameter illegal");
368+
return;
369+
}
370+
}
371+
if (DEBUG) {
372+
Log.d(TAG, "audioUri : " + mAudioURI);
373+
}
374+
375+
//音声無し以外の場合はエンコーダーをセット
376+
if (!mAudioURI.equals("false")) {
377+
mHostDeviceLiveStreamRecorder.setAudioEncoder();
378+
}
379+
380+
//ストリーミング開始
381+
mHostDeviceLiveStreamRecorder.startLiveStreaming();
382+
383+
setResult(response, DConnectMessage.RESULT_OK);
384+
}
385+
360386
private HostDeviceLiveStreamRecorder getHostDeviceLiveStreamRecorder() {
361387
if (DEBUG) {
362388
Log.d(TAG, "getHostDeviceLiveStreamRecorder()");
@@ -366,35 +392,50 @@ private HostDeviceLiveStreamRecorder getHostDeviceLiveStreamRecorder() {
366392
case VIDEO_URI_TRUE:
367393
case VIDEO_URI_FALSE: {
368394
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(null);
369-
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
370-
throw new RuntimeException("Another target in using.");
371-
}
395+
if (hostMediaRecorder != null) {
396+
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
397+
throw new RuntimeException("Another target in using.");
398+
}
372399

373-
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
374-
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
400+
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
401+
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
402+
}
375403
}
376404
break;
377405
}
378406
case VIDEO_URI_CAMERA_FRONT:
379407
case VIDEO_URI_CAMERA_1: {
380408
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(VIDEO_URI_CAMERA_1);
381-
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
382-
throw new RuntimeException("Another target in using.");
383-
}
384-
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
385-
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
409+
if (hostMediaRecorder != null) {
410+
if (mHostMediaRecorderManager.usingStreamingRecorder()) {
411+
throw new RuntimeException("Another target in using.");
412+
}
413+
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
414+
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
415+
}
386416
}
387417
break;
388418
}
389419
case VIDEO_URI_CAMERA_BACK:
390420
case VIDEO_URI_CAMERA_0: {
391421
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(VIDEO_URI_CAMERA_0);
392-
if (mHostMediaRecorderManager.usingPreviewOrStreamingRecorder(hostMediaRecorder.getId())) {
393-
throw new RuntimeException("Another target in using.");
394-
}
422+
if (hostMediaRecorder != null) {
423+
if (mHostMediaRecorderManager.usingPreviewOrStreamingRecorder(hostMediaRecorder.getId())) {
424+
throw new RuntimeException("Another target in using.");
425+
}
395426

396-
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
397-
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
427+
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
428+
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
429+
}
430+
}
431+
break;
432+
}
433+
case VIDEO_URI_SCREEN: {
434+
HostMediaRecorder hostMediaRecorder = mHostMediaRecorderManager.getRecorder(VIDEO_URI_SCREEN);
435+
if (hostMediaRecorder != null) {
436+
if (hostMediaRecorder instanceof HostDeviceLiveStreamRecorder) {
437+
return (HostDeviceLiveStreamRecorder) hostMediaRecorder;
438+
}
398439
}
399440
break;
400441
}

0 commit comments

Comments
 (0)