Android 视频流输出

//利用Wifi 将手机屏幕信息分享到桌面应用,Socket 视频传输流.


//首先申请录屏权限

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void requestCapturePermission() {
    Intent intent = new Intent(this, ScreenPermissionActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

private void initScreenManager() {
    L.d("初始化录屏。。。");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mMediaProjectionManager = (MediaProjectionManager)
                getSystemService(Context.MEDIA_PROJECTION_SERVICE);

        mMediaProjection = mMediaProjectionManager.getMediaProjection(Activity.RESULT_OK,
                mCapturePermissionIntent);
    }
    startDisplayManager();
    new Thread(new EncoderWorker()).start();
}

  @TargetApi(19)
    public void startDisplayManager() {
        DisplayManager mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Surface encoderInputSurface = null;
        try {
            encoderInputSurface = createDisplaySurface();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            virtualDisplay = mDisplayManager.createVirtualDisplay("Remote Droid",
                    AccessConstants.PHONE_WIDTH, AccessConstants.PHONE_HEIGHT, 50,
                    encoderInputSurface,
                    DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_SECURE);
        } else {
            if (mMediaProjection != null) {
                virtualDisplay = mMediaProjection.createVirtualDisplay("Remote Droid",
                        AccessConstants.PHONE_WIDTH, AccessConstants.PHONE_HEIGHT, AccessConstants.DPI,
                        DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                        encoderInputSurface, null, null);
            } else {
                Log.e(TAG, "Something went wrong. Please restart the app.");
            }
        }

//        sendData2();
        encoder.start();
    }

//再初始化 socket 


      try {
                    Log.d(TAG, "连接Socket 中...");
                    mSocket = mSS.accept();
                    mIs = mSocket.getInputStream();
                    mOs = mSocket.getOutputStream();
                    mDis = new DataInputStream(mIs);
                    mDos = new DataOutputStream(mOs);
                    Log.d(TAG, "连接Socket 成功... ");
                    mSocketList.add(mSocket);
                } catch (IOException e) {
                    e.printStackTrace();
//                    if (mSocketList.contains(mSocket)) {
//                        mSocketList.remove(mSocket);
//                    }
                    EventBus.getDefault().post(new EventBusConstants.SocketError());
                }

                if (mDos == null) {
                    Log.e(TAG, " mDos is null return...");
                    return;
                }

//                new Thread(new SendData1()).start();

                sendData1();

//发送录屏数据


    private void sendData1() {
        ByteBuffer[] encoderOutputBuffers = encoder.getOutputBuffers();

        boolean encoderDone = false;
        MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
        String infoString;

        while (!encoderDone) {

            if (!isNeedSendData) {
                continue;
            }

            int encoderStatus;

            try {
                encoderStatus = encoder.dequeueOutputBuffer(info, 1000);
            } catch (IllegalStateException e) {
                e.printStackTrace();
                break;
            }

            if (encoderStatus == MediaCodec.INFO_TRY_AGAIN_LATER) {
                // no output available yet
                //Log.d(TAG, "no output from encoder available");
            } else if (encoderStatus == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                // not expected for an encoder
                encoderOutputBuffers = encoder.getOutputBuffers();
                Log.d(TAG, "encoder output buffers changed");
            } else if (encoderStatus == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                // not expected for an encoder
                MediaFormat newFormat = encoder.getOutputFormat();
                Log.d(TAG, "encoder output format changed: " + newFormat);
            } else if (encoderStatus < 0) {
                Log.e(TAG, "encoderStatus < 0");
                continue;
            } else {
                ByteBuffer encodedData = encoderOutputBuffers[encoderStatus];
                if (encodedData == null) {
                    Log.d(TAG, "============It's NULL. BREAK!=============");
                    return;
                }

                final byte[] b = new byte[info.size];
                try {
                    if (info.size != 0) {
                        encodedData.limit(info.offset + info.size);
                        encodedData.position(info.offset);
                        encodedData.get(b, info.offset, info.offset + info.size);

                        try {
                            if (mDos == null) {
                                return;
                            }
                            mDos.write(b);
                            mDos.flush();
//                            Log.d(TAG, "输出 ");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
//                            }
//                        }).start();

                    }

                } catch (BufferUnderflowException e) {
                    e.printStackTrace();
                }

                encoderDone = (info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0;

                try {
                    if (encoder == null) {
                        Log.e("ServerService ", "encoder is null");
                        return;
                    }
                    encoder.releaseOutputBuffer(encoderStatus, false);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }

        }
    }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值