GB28181设备接入侧录像查询和录像下载技术探究之实时录像

简介: 我们在对接GB28181设备接入侧的时候,除了常规实时音视频按需上传外,还有个重要的功能,就是本地实时录像,录像后的数据,在执法记录仪等前端设备留底,然后,到工作站拷贝到专门的平台。

技术背景

我们在对接GB28181设备接入侧的时候,除了常规实时音视频按需上传外,还有个重要的功能,就是本地实时录像,录像后的数据,在执法记录仪等前端设备留底,然后,到工作站拷贝到专门的平台。


本文探讨的是,基于GB28181设备接入更进一步的处理:录像查询和录像下载,本文以我们Android平台开发的GB28181设备接入为例,做个简单的分析。

本地录像存储

eb8cd3a5d58742d996502ab267a021af.jpg

GB28181设备接入侧,非常重要的功能属性就是实时录像,我们在做实时录像的时候,设计如下:


先说录像参数设置:

/**
   * SmartPublisherJniV2.java
   * Author: daniusdk.com
   * Created on 2015/09/20.
   */
/**
   * 音频录制开关, 目的是为了更细粒度的去控制录像, 一般不需要调用这个接口, 这个接口使用场景比如同时推送音视频,但只想录制视频,可以调用这个接口关闭音频录制
   *
   * @param is_recoder: 0: do not recorder; 1: recorder; sdk默认是1
   *
   * @return {0} if successful
   */
public native int SmartPublisherSetRecorderAudio(long handle, int is_recoder);
/**
   * 视频录制开关, 目的是为了更细粒度的去控制录像, 一般不需要调用这个接口, 这个接口使用场景比如同时推送音视频,但只想录制音频,可以调用这个接口关闭视频录制
   *
   * @param is_recoder: 0: do not recorder; 1: recorder; sdk默认是1
   *
   * @return {0} if successful
   */
public native int SmartPublisherSetRecorderVideo(long handle, int is_recoder);
/**
     * Create file directory(创建录像存放目录)
     * 
     * @param path,  E.g: /sdcard/daniulive/rec
     * 
     * <pre> The interface is only used for recording the stream data to local side. </pre> 
     * 
     * @return {0} if successful
     */
public native int SmartPublisherCreateFileDirectory(String path);
/**
     * Set recorder directory(设置录像存放目录)
     * 
     * @param path: the directory of recorder file.
     * 
     * <pre> NOTE: make sure the path should be existed, or else the setting failed. </pre>
     * 
     * @return {0} if successful
     */
public native int SmartPublisherSetRecorderDirectory(long handle, String path);
/**
     * Set the size of every recorded file(设置单个录像文件大小,如超过最大文件大小,自动切换到下个文件录制)
     * 
     * @param size: (MB), (5M~500M), if not in this range, set default size with 200MB.
     * 
     * @return {0} if successful
     */
public native int SmartPublisherSetRecorderFileMaxSize(long handle, int size);


录像控制:

/**
  * Start recorder(开始录像)
  *
  * @return {0} if successful
  */
public native int SmartPublisherStartRecorder(long handle);
/**
   * Pause recorder(暂停/恢复录像)
   *
   * is_pause: 1表示暂停, 0表示恢复录像, 输入其他值将调用失败
   *
   * @return {0} if successful
   */
public native int SmartPublisherPauseRecorder(long handle, int is_pause);
/**
    * Stop recorder(停止录像)
    *
    * @return {0} if successful
    */
public native int SmartPublisherStopRecorder(long handle);


录像状态回调

private static class EventHandlerPublisherV2 implements NTSmartEventCallbackV2 {
  @Override
  public void onNTSmartEventCallbackV2(long handle, int id, long param1, long param2, String param3, String param4, Object param5) {
    Log.i(TAG, "EventHandeV2: handle=" + handle + " id:" + id);
    String publisher_event = "";
    switch (id) {
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_STARTED:
        publisher_event = "开始..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTING:
        publisher_event = "连接中..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTION_FAILED:
        publisher_event = "连接失败..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTED:
        publisher_event = "连接成功..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_DISCONNECTED:
        publisher_event = "连接断开..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_STOP:
        publisher_event = "关闭..";
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_RECORDER_START_NEW_FILE:
        publisher_event = "开始一个新的录像文件 : " + param3;
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_ONE_RECORDER_FILE_FINISHED:
        if (record_executor_ != null) {
          RecordExecutorService executor = record_executor_.get();
          if (executor != null)
            executor.execute(new RecordFileFinishedHandler().set(handle, param3, param1));
        }
        publisher_event = "已生成一个录像文件 : " + param3;
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_SEND_DELAY:
        publisher_event = "发送时延: " + param1 + " 帧数:" + param2;
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_CAPTURE_IMAGE:
        publisher_event = "快照: " + param1 + " 路径:" + param3;
        if (param1 == 0) {
          publisher_event = publisher_event + "截取快照成功..";
        } else {
          publisher_event = publisher_event + "截取快照失败..";
        }
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_RTSP_URL:
        publisher_event = "RTSP服务URL: " + param3;
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUSH_RTSP_SERVER_RESPONSE_STATUS_CODE:
        publisher_event ="RTSP status code received, codeID: " + param1 + ", RTSP URL: " + param3;
        break;
      case NTSmartEventID.EVENT_DANIULIVE_ERC_PUSH_RTSP_SERVER_NOT_SUPPORT:
        publisher_event ="服务器不支持RTSP推送, 推送的RTSP URL: " + param3;
        break;
    }
    String str = "当前回调状态:" + publisher_event;
    Log.i(TAG, str);
    if (handler_ != null) {
      android.os.Handler handler = handler_.get();
      if (handler != null) {
        Message message = new Message();
        message.what = PUBLISHER_EVENT_MSG;
        message.obj = publisher_event;
        handler.sendMessage(message);
      }
    }
  }
  public NTSmartEventCallbackV2 set(android.os.Handler handler, RecordExecutorService record_executor) {
    this.handler_ = new WeakReference<>(handler);
    this.record_executor_ = new WeakReference<>(record_executor);
    return this;
  }
  private WeakReference<android.os.Handler> handler_;
  private WeakReference<RecordExecutorService> record_executor_;
}


为适配GB28181的录像查询和处理,我们会把录像的文件,文件名做一定的处理,比如加上开始、结束时间还有duration和file size。


录像调用逻辑如下:

void ConfigRecorderParam() {
  if (libPublisher != null && publisherHandle != 0) {
    if (recDir != null && !recDir.isEmpty()) {
      int ret = libPublisher.SmartPublisherCreateFileDirectory(recDir);
      if (0 == ret) {
        if (0 != libPublisher.SmartPublisherSetRecorderDirectory(publisherHandle, recDir)) {
          Log.e(TAG, "Set record dir failed , path:" + recDir);
          return;
        }
        // 更细粒度控制录像的, 一般情况无需调用
        //libPublisher.SmartPublisherSetRecorderAudio(publisherHandle, 0);
        // libPublisher.SmartPublisherSetRecorderVideo(publisherHandle, 0);
        if (0 != libPublisher.SmartPublisherSetRecorderFileMaxSize(publisherHandle, 200)) {
          Log.e(TAG, "SmartPublisherSetRecorderFileMaxSize failed.");
          return;
        }
      } else {
        Log.e(TAG, "Create record dir failed, path:" + recDir);
      }
    }
  }
}
class ButtonStartRecorderListener implements View.OnClickListener {
  public void onClick(View v) {
    if (isRecording) {
      stopRecorder();
      if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
        ConfigControlEnable(true);
      }
      btnStartRecorder.setText("实时录像");
      btnPauseRecorder.setText("暂停录像");
      btnPauseRecorder.setEnabled(false);
      isPauseRecording = true;
      return;
    }
    Log.i(TAG, "onClick start recorder..");
    if (libPublisher == null)
      return;
    if (!isPushingRtmp && !isRTSPPublisherRunning&& !isGB28181StreamRunning) {
      InitAndSetConfig();
    }
    ConfigRecorderParam();
    int startRet = libPublisher.SmartPublisherStartRecorder(publisherHandle);
    if (startRet != 0) {
      if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
        if (publisherHandle != 0) {
          long handle = publisherHandle;
          publisherHandle = 0;
          libPublisher.SmartPublisherClose(handle);
        }
      }
      Log.e(TAG, "Failed to start recorder.");
      return;
    }
    if (!isPushingRtmp && !isRTSPPublisherRunning && !isGB28181StreamRunning) {
      CheckInitAudioRecorder();
      ConfigControlEnable(false);
    }
    startLayerPostThread();
    btnStartRecorder.setText("停止录像");
    isRecording = true;
    btnPauseRecorder.setEnabled(true);
    isPauseRecording = true;
  }
}
class ButtonPauseRecorderListener implements View.OnClickListener {
  public void onClick(View v) {
    if (isRecording) {
      if(isPauseRecording)
      {
        int ret = libPublisher.SmartPublisherPauseRecorder(publisherHandle, 1);
        if (ret == 0)
        {
          isPauseRecording = false;
          btnPauseRecorder.setText("恢复录像");
        }
        else if(ret == 3)
        {
          Log.e(TAG, "Pause recorder failed, please re-try again..");
        }
        else
        {
          Log.e(TAG, "Pause recorder failed..");
        }
      }
      else
      {
        int ret = libPublisher.SmartPublisherPauseRecorder(publisherHandle, 0);
        if (ret == 0)
        {
          isPauseRecording = true;
          btnPauseRecorder.setText("暂停录像");
        }
        else if(ret == 3)
        {
          Log.e(TAG, "Resume recorder failed, please re-try again..");
        }
        else
        {
          Log.e(TAG, "Resume recorder failed..");
        }
      }
    }
  }
}

总结

如果需要实现GB28181平台的录像查询和录像下载,实时录像的处理必不可少。下一章节,我们将根据GB28181规范探讨录像查询和录像下载。

相关文章
|
8月前
|
编解码 监控 API
Android平台GB28181设备接入侧音频采集推送示例
GB/T28181是广泛应用于视频监控行业的标准协议规范,可以在不同设备之间实现互联互通。今天我们主要探讨Android平台的Audio采集部分。
|
8月前
|
数据采集 前端开发 Android开发
Android平台RTMP推送或GB28181设备接入端如何实现采集audio音量放大?
我们在做Android平台RTMP推送和GB28181设备对接的时候,遇到这样的问题,有的设备,麦克风采集出来的audio,音量过高或过低,特别是有些设备,采集到的麦克风声音过低,导致播放端听不清前端采集的audio,这时候,就需要针对采集到的audio,做音量放大处理。
|
8月前
|
存储 Android开发 开发者
Android平台GB28181设备接入端实现实时快照
Android平台GB28181设计开发的时候,有个功能必不可少的:实时快照,特别是用于执法记录仪等场景下,用于图像留底或分析等考量。
|
8月前
|
Android开发 开发者
Android平台GB28181设备接入端如何实现本地录像?
实现Android平台GB28181设备接入的时候,有个功能点不可避免,那就是本地录像,实际上,在实现GB28181设备接入模块之前,我们前些年做RTMP推送和轻量级RTSP服务的时候,早已经实现了本地录像功能。
|
8月前
|
编解码 监控 网络协议
Android平台GB28181设备接入侧如何实现按需打开视音频采集传输
Android平台GB28181设备接入侧如何实现按需打开视音频采集传输
121 2
|
8月前
|
编解码 Android开发 数据安全/隐私保护
GB28181设备接入侧如何对接外部编码后音视频数据并实现预览播放
我们在对接GB28181设备接入模块的时候,遇到这样的技术诉求,好多开发者期望能提供编码后(H.264/H.265、AAC/PCMA)数据对接,确保外部采集设备,比如无人机类似回调过来的数据,直接通过模块,对接到GB28181平台侧,此外,还期望不支持或者内网没有外部网络权限的RTSP设备,也能间接接入到国标平台。
|
8月前
|
Android开发 开发者
Android平台GB28181设备接入端语音广播如何实现实时音量调节
Android平台GB28181设备接入,语音广播功能非常重要,本文要介绍的,不是语音广播的流程,语音广播流程,之前的blog也有非常详细的分享,感兴趣的可以参考官方规范书的交互流程:
|
8月前
|
编解码 Android开发 开发者
Android平台GB28181设备接入模块如何实现实时视频和本地录像双码流编码
我们在做Android平台GB28181设备接入模块的时候,遇到这样的场景,比如执法记录仪或智慧工地等场景下,由于GB28181设备接入模块,注册到国标平台后,平时只是心跳保持,或还有实时位置订阅,查看视频的时候,是按需看,而且有时候,网络环境并不是太好,所以,催生了这样一个诉求:部分开发者希望能本地录像的时候,录制高分辨率(比如1920*1080),国标平台侧发起实时视频查看请求的时候,上传低分辨率(如1280*720)数据,有点类似于IPC的主码流和子码流。
|
8月前
|
前端开发 定位技术 Android开发
Android平台GB28181设备接入端如何实时更新经纬度实现国标平台侧电子地图位置标注
我们在做GB28181设备接入端的时候,其中有个功能,不难但非常重要:那就是GB28181实时位置的订阅(mobileposition subscribe)和上报(notify)。
121 0
|
8月前
|
编解码 Android开发 数据安全/隐私保护
Android平台GB28181设备接入端对接编码前后音视频源类型浅析
今天主要对Android平台GB28181设备接入模块支持的接入数据类型,做个简单的汇总: 1. 编码前数据(目前支持的有YV12/NV21/NV12/I420/RGB24/RGBA32/RGB565等数据类型),其中,Android平台前后摄像头数据,或者屏幕数据,或者Unity拿到的数据,均属编码前数据; 2. 编码后数据(如无人机等264/HEVC数据,或者本地解析的MP4音视频数据); 3. 拉取RTSP或RTMP流并接入至GB28181平台(比如其他IPC的RTSP流,可通过Android平台GB28181接入到国标平台)。