//利用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(); } } } }