Skip to content
milkyway river edited this page Mar 19, 2018 · 1 revision

常见问题

  • 如何调节清晰度?

    清晰度跟分辨率和码率有关.

    分辨率的调节见MediaStream类里面updateResolution方法,参数分别为宽和高.

   /**
    * 更新分辨率
    */
   public void updateResolution(final int w, final int h) {
       if (mCamera == null) return;
       stopPreview();
       destroyCamera();
       mCameraHandler.post(new Runnable() {
           @Override
           public void run() {
               width = w;
               height = h;
           }
       });
       createCamera();
       startPreview();
   }

码率的调节分两种,对于硬编码,见HWConsumer类里面startMediaCodec函数,bitrate为码率,开发时可以适当调大,调小.

        /**
         * 初始化编码器
         */
        private void startMediaCodec() throws IOException {
            int framerate = 20;
    
            int bitrate = (int) (mWidth * mHeight * 20 * 2 * 0.05f);
            if (mWidth >= 1920 || mHeight >= 1920) bitrate *= 0.3;
            else if (mWidth >= 1280 || mHeight >= 1280) bitrate *= 0.4;
            else if (mWidth >= 720 || mHeight >= 720) bitrate *= 0.6;
            mMediaCodec = MediaCodec.createByCodecName(info.mName);
            MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", mWidth, mHeight);
            mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
            mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, framerate);
            mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, info.mColorFormat);
            mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
            mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
            mMediaCodec.start();
    
            Bundle params = new Bundle();
            params.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mMediaCodec.setParameters(params);
            }
        }

对于软编码,见SWConsumer类里面onVideoStart,其中bitrate为码率.

  @Override
  public void onVideoStart(int width, int height) {
      this.mWidth = width;
      this.mHeight = height;

      x264 = new X264Encoder();
      int bitrate = (int) (mWidth*mHeight*20*2*0.07f);
      x264.create(width, height, 20, bitrate/500);
      mVideoStarted = true;
      start();
  }
  • 为什么有时候延迟比较严重?如何调整?

    有多重因素影响视频延迟.比如网络状况,编码性能,码率大小等等.所以调试的时候一定先判定问题所在,然后针对性修改

    有些客户端本身会做缓存,这同样也会增大延迟.

Clone this wiki locally