导航菜单

页面标题

页面副标题

FanCode v7.24.0 - MirroringService.java 源代码

正在查看: FanCode v7.24.0 应用的 MirroringService.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.connectsdk.service.webos.lgcast.screenmirroring.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.projection.MediaProjection;
import android.os.IBinder;
import com.connectsdk.R;
import com.connectsdk.device.ConnectableDevice;
import com.connectsdk.discovery.DiscoveryManager;
import com.connectsdk.service.webos.lgcast.common.connection.ConnectionManager;
import com.connectsdk.service.webos.lgcast.common.connection.ConnectionManagerError;
import com.connectsdk.service.webos.lgcast.common.connection.ConnectionManagerListener;
import com.connectsdk.service.webos.lgcast.common.connection.MobileDescription;
import com.connectsdk.service.webos.lgcast.common.streaming.RTPStreaming;
import com.connectsdk.service.webos.lgcast.common.utils.AppUtil;
import com.connectsdk.service.webos.lgcast.common.utils.HandlerThreadEx;
import com.connectsdk.service.webos.lgcast.common.utils.IOUtil;
import com.connectsdk.service.webos.lgcast.common.utils.Logger;
import com.connectsdk.service.webos.lgcast.common.utils.StringUtil;
import com.connectsdk.service.webos.lgcast.common.utils.ThreadUtil;
import com.connectsdk.service.webos.lgcast.common.utils.TimerUtil;
import com.connectsdk.service.webos.lgcast.screenmirroring.ScreenMirroringConfig;
import com.connectsdk.service.webos.lgcast.screenmirroring.ScreenMirroringConst;
import com.connectsdk.service.webos.lgcast.screenmirroring.capability.MirroringSinkCapability;
import com.connectsdk.service.webos.lgcast.screenmirroring.capability.MirroringSourceCapability;
import com.connectsdk.service.webos.lgcast.screenmirroring.capability.VideoSizeInfo;
import com.connectsdk.service.webos.lgcast.screenmirroring.service.MirroringService;
import com.connectsdk.service.webos.lgcast.screenmirroring.service.MirroringServiceEvent;
import com.connectsdk.service.webos.lgcast.screenmirroring.uibc.UibcAccessibilityService;
import org.json.JSONObject;

public class MirroringService extends Service {
    private ej.a mAudioCapture;
    private ConnectionManager mConnectionManager;
    private int mCurrentOrientation;
    private int mCurrentScreenWidth;
    private MediaProjection mMediaProjection;
    private MirroringServiceEvent mMirroringServiceEvent;
    private MirroringSinkCapability mMirroringSinkCapability;
    private MirroringSourceCapability mMirroringSourceCapability;
    private MirroringVolume mMirroringVolume;
    private RTPStreaming mRTPStreaming;
    private HandlerThreadEx mServiceHandler;
    private ej.f mVideoCapture;

    class AnonymousClass1 implements ConnectionManagerListener {
        final Intent val$intent;

        AnonymousClass1(Intent intent) {
            this.val$intent = intent;
        }

        public void lambda$onError$0(ConnectionManagerError connectionManagerError) {
            MirroringServiceIF.notifyError(MirroringService.this.getBaseContext(), MirroringServiceIF.toMirroringError(connectionManagerError));
        }

        @Override
        public void onConnectionCompleted(JSONObject jSONObject) {
            Logger.debug("onConnectionCompleted", new Object[0]);
            MirroringService.this.mMirroringSinkCapability = new MirroringSinkCapability(jSONObject);
            MirroringSinkCapability mirroringSinkCapability = MirroringService.this.mMirroringSinkCapability;
            String str = ScreenMirroringConfig.Test.displayOrientation;
            if (str == null) {
                str = MirroringService.this.mMirroringSinkCapability.displayOrientation;
            }
            mirroringSinkCapability.displayOrientation = str;
            MirroringService.this.mMirroringSinkCapability.debug();
            MirroringService mirroringService = MirroringService.this;
            mirroringService.mMirroringSourceCapability = MirroringServiceFunc.createMirroringSourceCapa(mirroringService.getBaseContext(), MirroringService.this.mMirroringSinkCapability);
            MirroringService.this.mMirroringSourceCapability.debug();
            MobileDescription mobileDescription = new MobileDescription(MirroringService.this.getBaseContext());
            mobileDescription.debug();
            MirroringService.this.mConnectionManager.setSourceDeviceCapability(MirroringService.this.mMirroringSourceCapability.toJSONObject(), mobileDescription.toJSONObject());
            UibcAccessibilityService.onDisplayRotated(MirroringService.this.mMirroringSinkCapability.displayOrientation);
        }

        @Override
        public void onConnectionFailed(String str) {
            Logger.error("onConnectionFailed (%s)", str);
            MirroringServiceIF.respondStart(MirroringService.this.getBaseContext(), false, false);
            MirroringService.this.stop();
        }

        @Override
        public void onError(final ConnectionManagerError connectionManagerError, String str) {
            Logger.error("onError: connectionError=%s, errorMessage=%s", connectionManagerError, str);
            TimerUtil.schedule(new TimerUtil.TimerListener() {
                @Override
                public final void onTime() {
                    MirroringService.AnonymousClass1.this.lambda$onError$0(connectionManagerError);
                }
            }, 150L);
            MirroringService.this.stop();
        }

        @Override
        public void onPairingRejected() {
            Logger.error("onPairingRejected", new Object[0]);
            MirroringServiceIF.respondStart(MirroringService.this.getBaseContext(), false, false);
            MirroringService.this.stop();
        }

        @Override
        public void onPairingRequested() {
            Logger.debug("onPairingRequested", new Object[0]);
            MirroringServiceIF.notifyPairing(MirroringService.this.getBaseContext());
        }

        @Override
        public void onReceiveGetParameter(JSONObject jSONObject) {
            Logger.error("onReceiveGetParameter (noop)", new Object[0]);
        }

        @Override
        public void onReceivePlayCommand(JSONObject jSONObject) {
            Logger.debug("onReceivePlayCommand", new Object[0]);
            boolean startCaptureAndStreaming = MirroringService.this.startCaptureAndStreaming(this.val$intent);
            MirroringServiceIF.respondStart(MirroringService.this.getBaseContext(), startCaptureAndStreaming, MirroringServiceFunc.isDualScreen(this.val$intent));
            if (startCaptureAndStreaming) {
                return;
            }
            MirroringService.this.stop();
        }

        @Override
        public void onReceiveSetParameter(JSONObject jSONObject) {
            Logger.debug("onReceiveSetParameter", new Object[0]);
            JSONObject optJSONObject = jSONObject != null ? jSONObject.optJSONObject("mirroring") : null;
            String optString = optJSONObject != null ? optJSONObject.optString("displayOrientation") : null;
            if (optString == null) {
                return;
            }
            Logger.debug("onDisplayRotated (displayOrientation=%s, phoneOrientation=%s)", optString, Integer.valueOf(AppUtil.getOrientation(MirroringService.this.getBaseContext())));
            UibcAccessibilityService.onDisplayRotated(optString);
            if (MirroringService.this.mMirroringSinkCapability == null || !MirroringService.this.mMirroringSinkCapability.isSupportPortraitMode()) {
                Logger.error("TV does not support PORTRAIT mode", new Object[0]);
            } else {
                MirroringService.this.updateVideoSizeInfo(optString);
                MirroringService.this.mVideoCapture.a(MirroringService.this.mMirroringSourceCapability.videoWidth, MirroringService.this.mMirroringSourceCapability.videoHeight, MirroringService.this.mMirroringSourceCapability.videoBitrate);
            }
        }

        @Override
        public void onReceiveStopCommand(JSONObject jSONObject) {
            Logger.error("onReceiveStopCommand (noop)", new Object[0]);
        }
    }

    private void closeTvConnection() {
        Logger.print("closeTvConnection", new Object[0]);
        ConnectionManager connectionManager = this.mConnectionManager;
        if (connectionManager != null) {
            connectionManager.closeConnection();
        }
        this.mConnectionManager = null;
    }

    private void executeStart(Intent intent) {
        AnonymousClass1 anonymousClass1 = new AnonymousClass1(intent);
        Logger.print("executeStart", new Object[0]);
        start(intent, anonymousClass1);
    }

    private void executeStop() {
        Logger.print("executeStop", new Object[0]);
        stop();
        MirroringServiceIF.respondStop(this, true);
    }

    private void executeStopByNotification() {
        Logger.print("executeStopByNotification", new Object[0]);
        stop();
        MirroringServiceIF.notifyError(this, MirroringServiceError.ERROR_STOPPED_BY_NOTIFICATION);
    }

    @SuppressLint({"NewApi"})
    private void initializeService() {
        Logger.print("initializeService (SDK version=%s)", IOUtil.readRawResourceText(this, R.raw.lgcast_version));
        startForeground(4096, MirroringServiceFunc.createNotification(this), 32);
        startService(new Intent(this, (Class<?>) UibcAccessibilityService.class).setAction(UibcAccessibilityService.START_SERVICE));
        MirroringServiceEvent mirroringServiceEvent = new MirroringServiceEvent(this);
        this.mMirroringServiceEvent = mirroringServiceEvent;
        mirroringServiceEvent.startScreenOnOffReceiver(new MirroringServiceEvent.ScreenOnOffListener() {
            @Override
            public final void onScreenOnOffChanged(boolean z9) {
                MirroringService.this.lambda$initializeService$1(z9);
            }
        });
        this.mMirroringServiceEvent.startAccessibilitySettingObserver(new MirroringServiceEvent.AccessibilitySettingListener() {
            @Override
            public final void onAccessibilitySettingChanged(boolean z9) {
                MirroringService.this.lambda$initializeService$2(z9);
            }
        });
        MirroringVolume mirroringVolume = new MirroringVolume(this);
        this.mMirroringVolume = mirroringVolume;
        mirroringVolume.startMute();
    }

    public void lambda$initializeService$1(boolean z9) {
        ConnectionManager connectionManager = this.mConnectionManager;
        if (connectionManager != null) {
            connectionManager.notifyScreenOnOff(z9);
        }
    }

    public void lambda$initializeService$2(boolean z9) {
        JSONObject createUibcInfo = MirroringServiceFunc.createUibcInfo(getBaseContext());
        ConnectionManager connectionManager = this.mConnectionManager;
        if (connectionManager != null) {
            connectionManager.updateSourceDeviceCapability(createUibcInfo);
        }
    }

    public void lambda$onStartCommand$0(String str, Intent intent) {
        if (MirroringServiceIF.ACTION_START_REQUEST.equals(str)) {
            executeStart(intent);
        } else if (MirroringServiceIF.ACTION_STOP_REQUEST.equals(str)) {
            executeStop();
        } else if (MirroringServiceIF.ACTION_STOP_BY_NOTIFICATION.equals(str)) {
            executeStopByNotification();
        }
    }

    private void lambda$startCaptureAndStreaming$3() {
        Logger.error("### TEST MASTER KEY UPDATE ###", new Object[0]);
        RTPStreaming rTPStreaming = this.mRTPStreaming;
        if (rTPStreaming != null) {
            rTPStreaming.updateMasterKey();
        }
    }

    private void lambda$startCaptureAndStreaming$4() {
        Logger.error("### TEST ORIENTATION CHANGE ###", new Object[0]);
        MirroringSourceCapability mirroringSourceCapability = this.mMirroringSourceCapability;
        if (mirroringSourceCapability.videoWidth == 1920 && mirroringSourceCapability.videoHeight == 1080) {
            Logger.error("Change to PORTRAIT mode", new Object[0]);
            MirroringSourceCapability mirroringSourceCapability2 = this.mMirroringSourceCapability;
            mirroringSourceCapability2.videoWidth = ScreenMirroringConfig.Video.DEFAULT_HEIGHT;
            mirroringSourceCapability2.videoHeight = ScreenMirroringConfig.Video.DEFAULT_WIDTH;
        } else {
            Logger.error("Change to LANDSCAPE mode", new Object[0]);
            MirroringSourceCapability mirroringSourceCapability3 = this.mMirroringSourceCapability;
            mirroringSourceCapability3.videoWidth = ScreenMirroringConfig.Video.DEFAULT_WIDTH;
            mirroringSourceCapability3.videoHeight = ScreenMirroringConfig.Video.DEFAULT_HEIGHT;
        }
        this.mVideoCapture.d();
        ej.f fVar = this.mVideoCapture;
        MirroringSourceCapability mirroringSourceCapability4 = this.mMirroringSourceCapability;
        fVar.c(mirroringSourceCapability4.videoWidth, mirroringSourceCapability4.videoHeight, mirroringSourceCapability4.videoBitrate, this.mMediaProjection, this.mRTPStreaming.getVideoStreamHandler());
    }

    private void openTvConnection(Intent intent, ConnectionManagerListener connectionManagerListener) {
        Logger.print("openTvConnection", new Object[0]);
        ConnectableDevice deviceByIpAddress = DiscoveryManager.getInstance().getDeviceByIpAddress(MirroringServiceFunc.getDeviceIpAddress(intent));
        ConnectionManager connectionManager = new ConnectionManager("mirroring");
        this.mConnectionManager = connectionManager;
        connectionManager.openConnection(deviceByIpAddress, connectionManagerListener);
    }

    private void start(Intent intent, ConnectionManagerListener connectionManagerListener) {
        Logger.print("stop", new Object[0]);
        initializeService();
        openTvConnection(intent, connectionManagerListener);
    }

    public boolean startCaptureAndStreaming(Intent intent) {
        Logger.print("startCaptureAndStreaming", new Object[0]);
        try {
            MediaProjection mediaProjection = MirroringServiceFunc.getMediaProjection(this, intent);
            this.mMediaProjection = mediaProjection;
            if (mediaProjection == null) {
                throw new Exception("Invalid projection");
            }
            mediaProjection.registerCallback(new MediaProjection.Callback() {
                @Override
                public void onStop() {
                    super.onStop();
                    MirroringService.this.stopCaptureAndStreaming();
                }
            }, null);
            RTPStreaming rTPStreaming = new RTPStreaming();
            this.mRTPStreaming = rTPStreaming;
            rTPStreaming.setStreamingConfig(MirroringServiceFunc.createRtpVideoConfig(this.mMirroringSourceCapability.videoBitrate), MirroringServiceFunc.createRtpAudioConfig(), MirroringServiceFunc.createRtpSecurityConfig(this.mMirroringSourceCapability.masterKeys));
            RTPStreaming rTPStreaming2 = this.mRTPStreaming;
            MirroringSinkCapability mirroringSinkCapability = this.mMirroringSinkCapability;
            rTPStreaming2.open(this, 1356955624L, mirroringSinkCapability.ipAddress, mirroringSinkCapability.videoUdpPort, mirroringSinkCapability.audioUdpPort);
            ej.a aVar = new ej.a();
            this.mAudioCapture = aVar;
            aVar.a(new ej.b() {
                public final void onError() {
                    MirroringService.this.stop();
                }
            });
            this.mAudioCapture.b(ScreenMirroringConfig.Audio.SAMPLING_RATE, 2, this.mMediaProjection, this.mRTPStreaming.getAudioStreamHandler());
            ej.f fVar = new ej.f();
            this.mVideoCapture = fVar;
            fVar.b(new ej.b() {
                public final void onError() {
                    MirroringService.this.stop();
                }
            });
            ej.f fVar2 = this.mVideoCapture;
            MirroringSourceCapability mirroringSourceCapability = this.mMirroringSourceCapability;
            fVar2.c(mirroringSourceCapability.videoWidth, mirroringSourceCapability.videoHeight, mirroringSourceCapability.videoBitrate, this.mMediaProjection, this.mRTPStreaming.getVideoStreamHandler());
            return true;
        } catch (Exception e9) {
            Logger.error(e9);
            return false;
        }
    }

    public void stop() {
        Logger.print("stop", new Object[0]);
        stopCaptureAndStreaming();
        closeTvConnection();
        terminateService();
    }

    public void stopCaptureAndStreaming() {
        Logger.print("stopCaptureAndStreaming", new Object[0]);
        ej.f fVar = this.mVideoCapture;
        if (fVar != null) {
            fVar.d();
        }
        this.mVideoCapture = null;
        ej.a aVar = this.mAudioCapture;
        if (aVar != null) {
            aVar.c();
        }
        this.mAudioCapture = null;
        RTPStreaming rTPStreaming = this.mRTPStreaming;
        if (rTPStreaming != null) {
            rTPStreaming.close();
        }
        this.mRTPStreaming = null;
        MediaProjection mediaProjection = this.mMediaProjection;
        if (mediaProjection != null) {
            mediaProjection.stop();
        }
        this.mMediaProjection = null;
    }

    private void terminateService() {
        Logger.print("terminateService", new Object[0]);
        MirroringVolume mirroringVolume = this.mMirroringVolume;
        if (mirroringVolume != null) {
            mirroringVolume.stopMute();
        }
        this.mMirroringVolume = null;
        MirroringServiceEvent mirroringServiceEvent = this.mMirroringServiceEvent;
        if (mirroringServiceEvent != null) {
            mirroringServiceEvent.quit();
        }
        this.mMirroringServiceEvent = null;
        startService(new Intent(this, (Class<?>) UibcAccessibilityService.class).setAction(UibcAccessibilityService.STOP_SERVICE));
        stopForeground(true);
        ThreadUtil.runOnMainLooper(new Runnable() {
            @Override
            public final void run() {
                MirroringService.this.stopSelf();
            }
        }, 150L);
    }

    public void updateVideoSizeInfo(String str) {
        VideoSizeInfo createVideoSizeInfo = MirroringServiceFunc.createVideoSizeInfo(this, ScreenMirroringConst.LANDSCAPE.equals(str));
        createVideoSizeInfo.debug();
        MirroringSourceCapability mirroringSourceCapability = this.mMirroringSourceCapability;
        mirroringSourceCapability.videoWidth = createVideoSizeInfo.videoWidth;
        mirroringSourceCapability.videoHeight = createVideoSizeInfo.videoHeight;
        mirroringSourceCapability.videoActiveWidth = createVideoSizeInfo.videoActiveWidth;
        mirroringSourceCapability.videoActiveHeight = createVideoSizeInfo.videoActiveHeight;
        mirroringSourceCapability.videoOrientation = createVideoSizeInfo.videoOrientation;
        this.mMirroringSinkCapability.displayOrientation = str;
        JSONObject jSONObject = createVideoSizeInfo.toJSONObject(this);
        ConnectionManager connectionManager = this.mConnectionManager;
        if (connectionManager != null) {
            connectionManager.updateSourceDeviceCapability(jSONObject);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        if (this.mConnectionManager == null || this.mMirroringSinkCapability == null || this.mMirroringSourceCapability == null) {
            return;
        }
        int i9 = this.mCurrentOrientation;
        int i10 = configuration.orientation;
        String str = ScreenMirroringConst.LANDSCAPE;
        if (i9 != i10) {
            Logger.debug("Orientation changed: old=%d, new=%d", Integer.valueOf(i9), Integer.valueOf(configuration.orientation));
            int i11 = configuration.orientation;
            this.mCurrentOrientation = i11;
            updateVideoSizeInfo(i11 == 2 ? ScreenMirroringConst.LANDSCAPE : ScreenMirroringConst.PORTRAIT);
        }
        int i12 = this.mCurrentScreenWidth;
        if (i12 != configuration.smallestScreenWidthDp) {
            Logger.debug("Screen width changed: old=%d, new=%d", Integer.valueOf(i12), Integer.valueOf(configuration.smallestScreenWidthDp));
            this.mCurrentScreenWidth = configuration.smallestScreenWidthDp;
            if (this.mCurrentOrientation != 2) {
                str = ScreenMirroringConst.PORTRAIT;
            }
            updateVideoSizeInfo(str);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.showDebug(false);
        HandlerThreadEx handlerThreadEx = new HandlerThreadEx("MirroringService Handler");
        this.mServiceHandler = handlerThreadEx;
        handlerThreadEx.start();
        this.mCurrentOrientation = AppUtil.getOrientation(this);
        this.mCurrentScreenWidth = getResources().getConfiguration().smallestScreenWidthDp;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mServiceHandler.quit();
        this.mServiceHandler = null;
    }

    @Override
    public int onStartCommand(final Intent intent, int i9, int i10) {
        Logger.print("onStartCommand: " + StringUtil.toString(intent), new Object[0]);
        final String action = intent != null ? intent.getAction() : null;
        this.mServiceHandler.post(new Runnable() {
            @Override
            public final void run() {
                MirroringService.this.lambda$onStartCommand$0(action, intent);
            }
        });
        return 1;
    }
}