Audio and video transfer process between the YUV format video stream, can not solve the problem at the same time calling Camera

Law enforcement instrument camera video streaming protocol specification

Note:

  • Instrument of law enforcement equipment mentioned in the article can be understood as Android smartphones
  • Law enforcement software mentioned in the article, can be understood in third-party software for Android phones

After reading the article you can learn

  • AIDL technology
  • MemoryFile inter-process memory sharing technology
  • Camera video capture and playback
  • BRIEF suspension frame interface technology

Briefing note

Due to project requirements, we need law enforcement instrument local video when law enforcement software can normal use of the device itself Camera resources. Android system itself does not allow multiple simultaneous use Camera software resources, so the development of a shared memory sub-stream transmission protocol, when the enforcement of software requires a video stream, requested to be written into the YUV format video stream MemoryFile law enforcement equipment instrument law enforcement software from time to time of the cycle to the specified memory read YUV video stream.

Complete code entry

flow chart

Renderings

  • Local camera is not turned on

  • Local camera is turned on

BRIEF DESCRIPTION process and sample code

  1. Law enforcement instrument server receives the client's instructions to open a video stream written to memory

    	//处理客服端发送过来的需要子码流的数据
        private void onHandleAction(Context context, Intent intent) {
            switch (intent.getAction()) {
                /**
                 * 需要子码流
                 */
                case Constants.ACTION_CAMERE_CORE_SHOW:
                    //如果正在发送视频流,就不需要执行后面代码了
                    if (!MemoryFileServiceManager.getInsta(context).isSendVideoFrame())
                        MemoryFileServiceManager.getInsta(context).setSendVideoFrame(true, intent);
                    break;
            }
        }
    复制代码
  2. Service clients to obtain conditions of open video

        private void sendVideoFrame(Intent intent) {
            if (intent != null && intent.getExtras() != null) {
                Bundle extras = intent.getExtras();
                //获取需要预览的宽
                Constants.PREVIEWHEIGHT = extras.getInt(Constants.Config.PREVIEW_WIDTH, 1280);
                //获取需要预览的高
                Constants.PREVIEWHEIGHT = extras.getInt(Constants.Config.PREVIEW_HEIGHT, 720);
                //需要绑定对方服务的进程
                Constants.BIND_OTHER_SERVICE_PCK = extras.getString(Constants.Config.BIND_OTHER_SERVICE_PCK, "");
                //需要绑定对方服务的全路径
                Constants.BIND_OTHER_SERVICE_CLASS = extras.getString(Constants.Config.BIND_OTHER_SERVICE_CLASS, "");
                //需要开启 Camera ID 的前置还是后置 0:后置 1:前置
                Constants.CAMERA_ID = extras.getInt(Constants.Config.CAMERA_ID, 0);
            }
        }
    复制代码
  3. Server is turn the camera on, if you do not need to open has been opened

            //是否摄像头
            if (mCamera == null)
                openCamera();
    复制代码
  4. Initializing a server memory for writing YUV video stream.

     mMemoryFile = initMemoryFile(Constants.MEMORY_FILE_NAME, Constants.MEMORY_SIZE);
    复制代码
  5. Binding other services, provided the file descriptor number

        /**
         * 绑定对方服务,提供 文件描述符
         */
        private void bindOtherService() {
            try {
                if (TextUtils.isEmpty(Constants.BIND_OTHER_SERVICE_PCK) || TextUtils.isEmpty(Constants.BIND_OTHER_SERVICE_CLASS))
                    throw new NullPointerException("PCK or CLSS is null ?");
                Intent intent = new Intent();
                ComponentName cmp = new ComponentName(Constants.BIND_OTHER_SERVICE_PCK, Constants.BIND_OTHER_SERVICE_CLASS);
                intent.setComponent(cmp);
                context.bindService(intent, mCameraServiceConnection, Context.BIND_AUTO_CREATE);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    复制代码
  6. Binding other service successfully, pay at the file descriptor ParcelFileDescriptor

                mCameraService = ICameraCoreService.Stub.asInterface(binder);
                if (mMemoryFile != null) {
                    try {
                        //反射拿到文件描述符号
                        mParcelFileDescriptor = MemoryFileHelper.getParcelFileDescriptor(mMemoryFile);
                        if (mParcelFileDescriptor != null) {
                            mCameraService.addExportMemoryFile(mParcelFileDescriptor, Constants.PREVIEWWIDTH, Constants.PREVIEWHEIGHT, Constants.MEMORY_SIZE);
    
                        }
    复制代码
  7. Sending data, when the flag bit is byte [0] == 0 YUV representative of the server can be written to memory, == 1, can be read YUV data available customer service representative terminal.

        /**
         * 读标志位 写入视频流
         *
         * @param memoryFile
         */
        public void writeBytes(MemoryFile memoryFile) {
            try {
                if (mYUVQueue.size() > 0) {
                    BufferBean mBufferBean = new BufferBean(Constants.BUFFER_SIZE);
                    //读取标志符号
                    memoryFile.readBytes(mBufferBean.isCanRead, 0, 0, 1);
                    //当第一位为 0 的时候,代表客服端已经读取了,可以正常将视频流写入内存中
                    if (mBufferBean.isCanRead[0] == 0) {
                        //拿到视频流
                        byte[] video = mYUVQueue.poll();
                        if (video != null)
                            //将视频流写入内存中
                            memoryFile.writeBytes(video, 0, 0, video.length);
                        //标志位复位,等待客服端读取视频流
                        mBufferBean.isCanRead[0] = 1;
                        memoryFile.writeBytes(mBufferBean.isCanRead, 0, 0, 1);
                    } else {
                        Log.d(TAG, "readShareBufferMsg isCanRead:" + mBufferBean.isCanRead[0] + ";length:"
                                + mBufferBean.mBuffer.length);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                sendBroadcast(Constants.ACTION_FEEDBACK, e.getMessage());
            }
        }
    复制代码
  8. Open the success or failure of other error messages back to the customer service side

    //返回给客服端   
    public void sendBroadcast(String action,String content) {
            Intent intent = new Intent();
            intent.setAction(action);
            ComponentName componentName = new ComponentName("com.t01.sharevideostream",
                    "com.t01.sharevideostream.revices.FeedBackReceiver");
            intent.setComponent(componentName);
            Bundle extras = new Bundle();
            extras.putString(Constants.ACTION_FEEDBACK_CONTENT, content);
            intent.putExtras(extras);
            context.sendBroadcast(intent);
        }
    复制代码

Reproduced in: https: //juejin.im/post/5cf345ddf265da1b8c19731a

Guess you like

Origin blog.csdn.net/weixin_33694620/article/details/91429568