|
|
@@ -1,19 +1,195 @@
|
|
|
package com.izouma.mobilecybergames;
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.media.projection.MediaProjectionManager;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import com.alivc.live.pusher.AlivcAudioAACProfileEnum;
|
|
|
+import com.alivc.live.pusher.AlivcFpsEnum;
|
|
|
+import com.alivc.live.pusher.AlivcLivePushConfig;
|
|
|
+import com.alivc.live.pusher.AlivcLivePushInfoListener;
|
|
|
+import com.alivc.live.pusher.AlivcLivePusher;
|
|
|
+import com.alivc.live.pusher.AlivcPreviewOrientationEnum;
|
|
|
+import com.alivc.live.pusher.AlivcResolutionEnum;
|
|
|
+
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
+import io.flutter.plugin.common.PluginRegistry;
|
|
|
+
|
|
|
+public class ScreenStreamPlugin implements MethodChannel.MethodCallHandler, PluginRegistry.ActivityResultListener {
|
|
|
+ public static final int CAPTURE_PERMISSION_REQUEST_CODE = 0x1123;
|
|
|
+
|
|
|
+ private final PluginRegistry.Registrar registrar;
|
|
|
+
|
|
|
+ private AlivcLivePushConfig mAlivcLivePushConfig;
|
|
|
+ private AlivcLivePusher mAlivcLivePusher = null;
|
|
|
+ private boolean started = false;
|
|
|
+ private String url;
|
|
|
+ private MethodChannel.Result result;
|
|
|
+
|
|
|
+ public static void registerWith(PluginRegistry.Registrar registrar) {
|
|
|
+ final MethodChannel channel = new MethodChannel(registrar.messenger(), "screen_stream");
|
|
|
+ MethodChannel.MethodCallHandler methodCallHandler = new ScreenStreamPlugin(registrar);
|
|
|
+ registrar.addActivityResultListener((PluginRegistry.ActivityResultListener) methodCallHandler);
|
|
|
+ channel.setMethodCallHandler(methodCallHandler);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScreenStreamPlugin(PluginRegistry.Registrar registrar) {
|
|
|
+ this.registrar = registrar;
|
|
|
+ AlivcLivePushConfig.setMediaProjectionPermissionResultData(null);
|
|
|
+ mAlivcLivePushConfig = new AlivcLivePushConfig();//初始化推流配置类
|
|
|
+ mAlivcLivePushConfig.setResolution(AlivcResolutionEnum.RESOLUTION_720P);//分辨率540P,最大支持720P
|
|
|
+ mAlivcLivePushConfig.setFps(AlivcFpsEnum.FPS_10); //建议用户使用20fps
|
|
|
+ mAlivcLivePushConfig.setEnableBitrateControl(true); // 打开码率自适应,默认为true
|
|
|
+ mAlivcLivePushConfig.setPreviewOrientation(AlivcPreviewOrientationEnum.ORIENTATION_LANDSCAPE_HOME_RIGHT); // 默认为竖屏,可设置home键向左或向右横屏。
|
|
|
+ mAlivcLivePushConfig.setAudioProfile(AlivcAudioAACProfileEnum.AAC_LC);//设置音频编码模式
|
|
|
+ mAlivcLivePushConfig.setEnableBitrateControl(true);// 打开码率自适应,默认为true
|
|
|
+ }
|
|
|
|
|
|
-public class ScreenStreamPlugin implements MethodChannel.MethodCallHandler {
|
|
|
@Override
|
|
|
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
|
|
|
-
|
|
|
+ if ("start".equals(methodCall.method)) {
|
|
|
+ url = methodCall.argument("url");
|
|
|
+ if (!TextUtils.isEmpty(url)) {
|
|
|
+ if (Pattern.matches("^(?i)rtmp:\\\\/\\\\/", url)) {
|
|
|
+ if (!start()) {
|
|
|
+ result.error("start fail", "need android 5.0", null);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.error("wrong url", url, null);
|
|
|
+ } else if ("stop".equals(methodCall.method)) {
|
|
|
+ stop();
|
|
|
+ result.success("success");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public boolean start(String url) {
|
|
|
+ public boolean start() {
|
|
|
+ if (started) {
|
|
|
+ mAlivcLivePusher.stopPush();
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) registrar.activeContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
|
|
|
+ registrar.activity().startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), CAPTURE_PERMISSION_REQUEST_CODE);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
public boolean stop() {
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ public void startPushWithoutSurface() {
|
|
|
+ mAlivcLivePusher = new AlivcLivePusher();
|
|
|
+ try {
|
|
|
+ mAlivcLivePusher.init(registrar.context(), mAlivcLivePushConfig);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalStateException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ mAlivcLivePusher.setLivePushInfoListener(new AlivcLivePushInfoListener() {
|
|
|
+ @Override
|
|
|
+ public void onPreviewStarted(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPreviewStoped(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPushStarted(AlivcLivePusher pusher) {
|
|
|
+ started = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFirstAVFramePushed(AlivcLivePusher alivcLivePusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPushPauesed(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPushResumed(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPushStoped(AlivcLivePusher pusher) {
|
|
|
+ started = false;
|
|
|
+ mAlivcLivePusher = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onPushRestarted(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFirstFramePreviewed(AlivcLivePusher pusher) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDropFrame(AlivcLivePusher pusher, int countBef, int countAft) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAdjustBitRate(AlivcLivePusher pusher, int curBr, int targetBr) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAdjustFps(AlivcLivePusher pusher, int curFps, int targetFps) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ mAlivcLivePusher.startPreview(null);
|
|
|
+ mAlivcLivePusher.startPush(url);
|
|
|
+ mAlivcLivePusher.setCaptureVolume(50);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ switch (requestCode) {
|
|
|
+ case CAPTURE_PERMISSION_REQUEST_CODE: {
|
|
|
+ if (resultCode == Activity.RESULT_OK) {
|
|
|
+ AlivcLivePushConfig.setMediaProjectionPermissionResultData(data);
|
|
|
+ if (mAlivcLivePushConfig.getMediaProjectionPermissionResultData() != null) {
|
|
|
+ if (mAlivcLivePusher == null) {
|
|
|
+ startPushWithoutSurface();
|
|
|
+ if (result != null) {
|
|
|
+ result.success("success");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (result != null) {
|
|
|
+ result.error("needs permission", "permission not granted", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|