如何实现Android平台GB28181设备对接Camera2数据

技术背景

在写如何实现Android平台GB28181设备对接Camera2数据说明之前,我在前两年的blog就有针对camera2的RTMP直播推送模块做过技术分享:

在Google 推出Android 5.0的时候, Android Camera API 版本升级到了API2(android.hardware.camera2), 之前使用的API1(android.hardware.camera)就被标为 Deprecated 了。

Camera API2相较于API1有很大不同, 并且API2是为了配合HAL3进行使用的, API2有很多API1不支持的特性, 比如:

  1. 更先进的API架构;
  2. 可以获取更多的帧(预览/拍照)信息以及手动控制每一帧的参数;
  3. 对Camera的控制更加完全(比如支持调整focus distance, 剪裁预览/拍照图片);
  4. 支持更多图片格式(yuv/raw)以及高速连拍等。

Camera2 API调用基础流程

  1. 通过context.getSystemService(Context.CAMERA_SERVICE) 获取CameraManager;
  2. 调用CameraManager .open()方法在回调中得到CameraDevice;
  3. 通过CameraDevice.createCaptureSession() 在回调中获取CameraCaptureSession;
  4. 构建CaptureRequest, 有三种模式可选 预览/拍照/录像.;
  5. 通过 CameraCaptureSession发送CaptureRequest, capture表示只发一次请求, setRepeatingRequest表示不断发送请求;
  6. 拍照数据可以在ImageReader.OnImageAvailableListener回调中获取, CaptureCallback中则可获取拍照实际的参数和Camera当前状态。

本次更新,系在Android平台camera2 RTMP推送的基础上,继续支持Android平台GB28181设备和语音广播接入,此外,添加了基于层结构设计的动态水印(动态水印的场景应用特别实在传统行业,重要性不言而喻。包含实时文字水印、图片水印),camera2的技术优越性不再赘述,无图无真相:

新的demo增加了动态水印设置、轻量级RTSP服务、实时录像、快照等。

技术实现

先说camera2的数据采集:

private class OnImageAvailableListenerImpl implements ImageReader.OnImageAvailableListener {@Overridepublic void onImageAvailable(ImageReader reader) {Image image = reader.acquireLatestImage();if ( image != null ){if ( camera2Listener != null ){camera2Listener.onCameraImageData(image);}image.close();}}
}

获取到的数据,投递到SmartPublisher Jni层:

@Override
public void onCameraImageData(Image image) {Rect crop_rect = image.getCropRect();if (isPushingRtmp || isRTSPPublisherRunning || isGB28181StreamRunning || isRecording) {if (libPublisher != null) {Image.Plane[] planes = image.getPlanes();int w = image.getWidth(), h = image.getHeight();int y_offset = 0, u_offset = 0, v_offset = 0;if (!crop_rect.isEmpty()) {w = crop_rect.width();h = crop_rect.height();y_offset += crop_rect.top * planes[0].getRowStride() + crop_rect.left * planes[0].getPixelStride();u_offset += (crop_rect.top / 2) * planes[1].getRowStride() + (crop_rect.left / 2) * planes[1].getPixelStride();v_offset += (crop_rect.top / 2) * planes[2].getRowStride() + (crop_rect.left / 2) * planes[2].getPixelStride();;// Log.i(TAG, "crop w:" + w + " h:" + h + " y_offset:"+ y_offset + " u_offset:" + u_offset + " v_offset:" + v_offset);}int scale_w = 0, scale_h = 0, scale_filter_mode = 0;scale_filter_mode = 3;libPublisher.PostLayerImageYUV420888ByteBuffer(publisherHandle, 0, 0, 0,planes[0].getBuffer(), y_offset, planes[0].getRowStride(),planes[1].getBuffer(), u_offset, planes[1].getRowStride(),planes[2].getBuffer(), v_offset, planes[2].getRowStride(), planes[1].getPixelStride(),w, h, 0, 0,scale_w, scale_h, scale_filter_mode, cameraImageRotationDegree_);}}
}

PostLayerImageYUV420888ByteBuffer()接口设计如下:

/*** 投递层YUV420888图像, 专门为android.media.Image的android.graphics.ImageFormat.YUV_420_888格式提供的接口** @param index: 层索引, 必须大于等于0** @param left: 层叠加的左上角坐标, 对于第0层的话传0** @param top: 层叠加的左上角坐标, 对于第0层的话传0** @param y_plane: 对应android.media.Image.Plane[0].getBuffer()** @param y_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0** @param y_row_stride: 对应android.media.Image.Plane[0].getRowStride()** @param u_plane: android.media.Image.Plane[1].getBuffer()** @param u_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0** @param u_row_stride: android.media.Image.Plane[1].getRowStride()** @param v_plane: 对应android.media.Image.Plane[2].getBuffer()** @param v_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0** @param v_row_stride: 对应android.media.Image.Plane[2].getRowStride()** @param uv_pixel_stride: 对应android.media.Image.Plane[1].getPixelStride()** @param width: width, 必须大于1, 且必须是偶数** @param height: height, 必须大于1, 且必须是偶数** @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转** @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转** @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放** @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放** @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢** @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序** @return {0} if successful*/public native int PostLayerImageYUV420888ByteBuffer(long handle, int index, int left, int top,ByteBuffer y_plane, int y_offset, int y_row_stride,ByteBuffer u_plane, int u_offset, int u_row_stride,ByteBuffer v_plane, int v_offset, int v_row_stride, int uv_pixel_stride,int width, int height, int is_vertical_flip,  int is_horizontal_flip,int scale_width,  int scale_height, int scale_filter_mode,int rotation_degree);

动态水印-文字水印:

private int postText1Layer(int index, int left, int top, int video_w, int video_h) {if (video_w < 1 || video_h < 1)return 0;Bitmap text_bitmap = makeTextBitmap("文本水印一", getFontSize(video_w)+8,Color.argb(255, 200, 250, 0),false, 0,false);if (null == text_bitmap)return 0;ByteBuffer buffer = ByteBuffer.allocateDirect(text_bitmap.getByteCount());text_bitmap.copyPixelsToBuffer(buffer);libPublisher.PostLayerImageRGBA8888ByteBuffer(handle_, index, left, top, buffer, 0,text_bitmap.getRowBytes(), text_bitmap.getWidth(), text_bitmap.getHeight(),0, 0, 0, 0, 0,0);int ret = text_bitmap.getHeight();text_bitmap.recycle();return ret;
}

动态水印-图片水印:

private Bitmap getAssetsBitmap() {Bitmap bitmap = null;try {InputStream s = getAssets().open("tca.png");bitmap = BitmapFactory.decodeStream(s);s.close();} catch (Exception e) {e.printStackTrace();}return bitmap;
}private int postPictureLayer(int index, int left, int top, int video_w, int video_h) {if (video_w < 1 || video_h < 1)return  0;Bitmap bitmap = getAssetsBitmap();if (null == bitmap) {Log.e(TAG, "postPitcureLayer getAssetsBitmap is null");return 0;}if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {Log.e(TAG, "postPitcureLayer config is not ARGB_8888, config:" + Bitmap.Config.ARGB_8888);return 0;}ByteBuffer buffer = ByteBuffer.allocateDirect(bitmap.getByteCount());bitmap.copyPixelsToBuffer(buffer);final int w = bitmap.getWidth();final int h = bitmap.getHeight();if ( w < 2 || h < 2 )return 0;int scale_w = 0, scale_h = 0, scale_filter_mode = 0;final float r_w = video_w - left; // 有可能负数final float r_h = video_h - top; // 有可能负数if (w > r_w || h > r_h) {float s_w = w;float s_h = h;// 0.85的10次方是0.19687, 缩放到0.2倍差不多了for ( int i = 0; i < 10; ++i)  {s_w *= 0.85f;s_h *= 0.85f;if (s_w < r_w && s_h < r_h )break;}if (s_w > r_w || s_h > r_h)return 0;// 如果小于16就算了,太小看也看不见if (s_w < 16.0f || s_h < 16.0f)return  0;scale_w = align((int)(s_w + 0.5f), 2);scale_h = align( (int)(s_h + 0.5f), 2);scale_filter_mode = 3;}/*if ( scale_w > 0 && scale_h > 0)Log.i(TAG, "postTextLayer scale_w:" + scale_w + ", scale_h:" + scale_h + " w:" + w + ", h:" + h) ; */libPublisher.PostLayerImageRGBA8888ByteBuffer(handle_, index, left, top, buffer, 0, bitmap.getRowBytes(), w, h,0, 0, scale_w, scale_h, scale_filter_mode,0);int ret = scale_h > 0 ? scale_h : bitmap.getHeight();bitmap.recycle();return ret;
}

动态水印控制:

public void startPost(long handle, int w, int h, boolean is_text, boolean is_pitcure) {this.is_exit_ = false;this.handle_ = handle;updateVideoSize(w, h);is_text_ = is_text;is_picture_ = is_pitcure;Log.i(TAG, "LayerPostThread.startPost w:" + w + ", h:" + h + ", is_text:" + is_text_ + ", is_pitcure:" + is_picture_);try {this.start();} catch (Exception e) {e.printStackTrace();}
}public void enableText(boolean is_text) {is_text_ = is_text;clear_flag_ = true;if (handle_ != 0) {libPublisher.EnableLayer(handle_, timestamp_index_, is_text_?1:0);libPublisher.EnableLayer(handle_, text1_index_, is_text_?1:0);libPublisher.EnableLayer(handle_, text2_index_, is_text_?1:0);}
}public void enablePicture(boolean is_picture) {is_picture_ = is_picture;clear_flag_ = true;if (handle_ != 0) {libPublisher.EnableLayer(handle_, picture_index_, is_picture_?1:0);}
}public void stopPost() {this.is_exit_ = true;try {this.join(1000);}catch (Exception e) {e.printStackTrace();}handle_ = 0;
}
private LayerPostThread layer_post_thread_ = null;private void startLayerPostThread() {if (null == layer_post_thread_) {layer_post_thread_ = new LayerPostThread();int degree = cameraImageRotationDegree_;if (90 == degree || 270 == degree)layer_post_thread_.startPost(publisherHandle, video_height_, video_width_, isHasTextWatermark(), isHasPictureWatermark());elselayer_post_thread_.startPost(publisherHandle, video_width_, video_height_, isHasTextWatermark(), isHasPictureWatermark());}
}private void stopLayerPostThread() {if (layer_post_thread_ != null) {layer_post_thread_.stopPost();layer_post_thread_ = null;}
}

实时录像、快照之类不再赘述,gb28181的,其实和camera的部分一样:

private boolean initGB28181Agent() {if ( gb28181_agent_ != null )return  true;getLocation(context_);String local_ip_addr = IPAddrUtils.getIpAddress(context_);Log.i(TAG, "initGB28181Agent local ip addr: " + local_ip_addr);if ( local_ip_addr == null || local_ip_addr.isEmpty() ) {Log.e(TAG, "initGB28181Agent local ip is empty");return  false;}gb28181_agent_ = GBSIPAgentFactory.getInstance().create();if ( gb28181_agent_ == null ) {Log.e(TAG, "initGB28181Agent create agent failed");return false;}gb28181_agent_.addListener(this);gb28181_agent_.addPlayListener(this);gb28181_agent_.addAudioBroadcastListener(this);gb28181_agent_.addDeviceControlListener(this);// 必填信息gb28181_agent_.setLocalAddress(local_ip_addr);gb28181_agent_.setServerParameter(gb28181_sip_server_addr_, gb28181_sip_server_port_, gb28181_sip_server_id_, gb28181_sip_domain_);gb28181_agent_.setUserInfo(gb28181_sip_username_, gb28181_sip_password_);// 可选参数gb28181_agent_.setUserAgent(gb28181_sip_user_agent_filed_);gb28181_agent_.setTransportProtocol(gb28181_sip_trans_protocol_==0?"UDP":"TCP");// GB28181配置gb28181_agent_.config(gb28181_reg_expired_, gb28181_heartbeat_interval_, gb28181_heartbeat_count_);com.gb.ntsignalling.Device gb_device = new com.gb.ntsignalling.Device("34020000001380000001", "安卓测试设备", Build.MANUFACTURER, Build.MODEL,"宇宙","火星1","火星", true);if (mLongitude != null && mLatitude != null) {com.gb.ntsignalling.DevicePosition device_pos = new com.gb.ntsignalling.DevicePosition();device_pos.setTime(mLocationTime);device_pos.setLongitude(mLongitude);device_pos.setLatitude(mLatitude);gb_device.setPosition(device_pos);gb_device.setSupportMobilePosition(true); // 设置支持移动位置上报}gb28181_agent_.addDevice(gb_device);/*com.gb28181.ntsignalling.Device gb_device1 = new com.gb28181.ntsignalling.Device("34020000001380000002", "安卓测试设备2", Build.MANUFACTURER, Build.MODEL,"宇宙","火星1","火星", true);if (mLongitude != null && mLatitude != null) {com.gb28181.ntsignalling.DevicePosition device_pos = new com.gb28181.ntsignalling.DevicePosition();device_pos.setTime(mLocationTime);device_pos.setLongitude(mLongitude);device_pos.setLatitude(mLatitude);gb_device1.setPosition(device_pos);gb_device1.setSupportMobilePosition(true);}gb28181_agent_.addDevice(gb_device1);*/if (!gb28181_agent_.createSipStack()) {gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.createSipStack failed.");return  false;}boolean is_bind_local_port_ok = false;// 最多尝试5000个端口int try_end_port = gb28181_sip_local_port_base_ + 5000;try_end_port = try_end_port > 65536 ?65536: try_end_port;for (int i = gb28181_sip_local_port_base_; i < try_end_port; ++i) {if (gb28181_agent_.bindLocalPort(i)) {is_bind_local_port_ok = true;break;}}if (!is_bind_local_port_ok) {gb28181_agent_.releaseSipStack();gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.bindLocalPort failed.");return  false;}if (!gb28181_agent_.initialize()) {gb28181_agent_.unBindLocalPort();gb28181_agent_.releaseSipStack();gb28181_agent_ = null;Log.e(TAG, "initGB28181Agent gb28181_agent_.initialize failed.");return  false;}return true;
}@Override
public void ntsRegisterOK(String dateString) {Log.i(TAG, "ntsRegisterOK Date: " + (dateString!= null? dateString : ""));
}@Override
public void ntsRegisterTimeout() {Log.e(TAG, "ntsRegisterTimeout");
}@Override
public void ntsRegisterTransportError(String errorInfo) {Log.e(TAG, "ntsRegisterTransportError error:" + (errorInfo != null?errorInfo :""));
}@Override
public void ntsOnHeartBeatException(int exceptionCount,  String lastExceptionInfo) {Log.e(TAG, "ntsOnHeartBeatException heart beat timeout count reached, count:" + exceptionCount+", exception info:" + (lastExceptionInfo!=null?lastExceptionInfo:""));// 停止信令, 然后重启handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "gb28281_heart_beart_timeout");stopAudioPlayer();destoryRTPReceiver();if (gb_broadcast_source_id_ != null && gb_broadcast_target_id_ != null && gb28181_agent_ != null)gb28181_agent_.byeAudioBroadcast(gb_broadcast_source_id_, gb_broadcast_target_id_);gb_broadcast_source_id_ = null;gb_broadcast_target_id_ = null;btnGB28181AudioBroadcast.setText("GB28181语音广播");btnGB28181AudioBroadcast.setEnabled(false);stopGB28181Stream();destoryRTPSender();if (gb28181_agent_ != null) {gb28181_agent_.terminateAllPlays(true);Log.i(TAG, "gb28281_heart_beart_timeout sip stop");gb28181_agent_.stop();String local_ip_addr = IPAddrUtils.getIpAddress(context_);if (local_ip_addr != null && !local_ip_addr.isEmpty() ) {Log.i(TAG, "gb28281_heart_beart_timeout get local ip addr: " + local_ip_addr);gb28181_agent_.setLocalAddress(local_ip_addr);}Log.i(TAG, "gb28281_heart_beart_timeout sip start");gb28181_agent_.start();}}},0);
}@Override
public void ntsOnInvitePlay(String deviceId, PlaySessionDescription session_des) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {MediaSessionDescription video_des = session_des_.getVideoDescription();SDPRtpMapAttribute ps_rtpmap_attr = video_des.getPSRtpMapAttribute();Log.i(TAG,"ntsInviteReceived, device_id:" +device_id_+", is_tcp:" + video_des.isRTPOverTCP()+ " rtp_port:" + video_des.getPort() + " ssrc:" + video_des.getSSRC()+ " address_type:" + video_des.getAddressType() + " address:" + video_des.getAddress());// 可以先给信令服务器发送临时振铃响应//sip_stack_android.respondPlayInvite(180, device_id_);long rtp_sender_handle = libPublisher.CreateRTPSender(0);if ( rtp_sender_handle == 0 ) {gb28181_agent_.respondPlayInvite(488, device_id_);Log.i(TAG, "ntsInviteReceived CreateRTPSender failed, response 488, device_id:" + device_id_);return;}gb28181_rtp_payload_type_  = ps_rtpmap_attr.getPayloadType();gb28181_rtp_encoding_name_ =  ps_rtpmap_attr.getEncodingName();libPublisher.SetRTPSenderTransportProtocol(rtp_sender_handle, video_des.isRTPOverUDP()?0:1);libPublisher.SetRTPSenderIPAddressType(rtp_sender_handle, video_des.isIPv4()?0:1);libPublisher.SetRTPSenderLocalPort(rtp_sender_handle, 0);libPublisher.SetRTPSenderSSRC(rtp_sender_handle, video_des.getSSRC());libPublisher.SetRTPSenderSocketSendBuffer(rtp_sender_handle, 2*1024*1024); // 设置到2MlibPublisher.SetRTPSenderClockRate(rtp_sender_handle, ps_rtpmap_attr.getClockRate());libPublisher.SetRTPSenderDestination(rtp_sender_handle, video_des.getAddress(), video_des.getPort());if ( libPublisher.InitRTPSender(rtp_sender_handle) != 0 ) {gb28181_agent_.respondPlayInvite(488, device_id_);libPublisher.DestoryRTPSender(rtp_sender_handle);return;}int local_port = libPublisher.GetRTPSenderLocalPort(rtp_sender_handle);if (local_port == 0) {gb28181_agent_.respondPlayInvite(488, device_id_);libPublisher.DestoryRTPSender(rtp_sender_handle);return;}Log.i(TAG,"get local_port:" + local_port);String local_ip_addr = IPAddrUtils.getIpAddress(context_);gb28181_agent_.respondPlayInviteOK(device_id_,local_ip_addr, local_port);gb28181_rtp_sender_handle_ = rtp_sender_handle;}private String device_id_;private PlaySessionDescription session_des_;public Runnable set(String device_id, PlaySessionDescription session_des) {this.device_id_ = device_id;this.session_des_ = session_des;return this;}}.set(deviceId, session_des),0);
}@Override
public void ntsOnCancelPlay(String deviceId) {// 这里取消Play会话handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnCancelPlay, deviceId=" + device_id_);destoryRTPSender();}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnAckPlay(String deviceId) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG,"ntsOnACKPlay, device_id:" +device_id_);if (!isRTSPPublisherRunning && !isPushingRtmp && !isRecording) {InitAndSetConfig();}libPublisher.SetGB28181RTPSender(publisherHandle, gb28181_rtp_sender_handle_, gb28181_rtp_payload_type_, gb28181_rtp_encoding_name_);int startRet = libPublisher.StartGB28181MediaStream(publisherHandle);if (startRet != 0) {if (!isRTSPPublisherRunning && !isPushingRtmp  && !isRecording) {if (publisherHandle != 0) {libPublisher.SmartPublisherClose(publisherHandle);publisherHandle = 0;}}destoryRTPSender();Log.e(TAG, "Failed to start GB28181 service..");return;}if (!isRTSPPublisherRunning && !isPushingRtmp && !isRecording) {CheckInitAudioRecorder();}startLayerPostThread();isGB28181StreamRunning = true;}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnPlayInviteResponseException(String deviceId, int statusCode, String errorInfo) {// 这里要释放掉响应的资源Log.i(TAG, "ntsOnPlayInviteResponseException, deviceId=" + deviceId + " statusCode=" +statusCode+ " errorInfo:" + errorInfo);handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnPlayInviteResponseException, deviceId=" + device_id_);destoryRTPSender();}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnByePlay(String deviceId) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnByePlay, stop GB28181 media stream, deviceId=" + device_id_);stopGB28181Stream();destoryRTPSender();}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnTerminatePlay(String deviceId) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnTerminatePlay, stop GB28181 media stream, deviceId=" + device_id_);stopGB28181Stream();destoryRTPSender();}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnPlayDialogTerminated(String deviceId) {/*Play会话对应的对话终止, 一般不会出发这个回调,目前只有在响应了200K, 但在64*T1时间后还没收到ACK,才可能会出发收到这个请做相关清理处理*/handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnPlayDialogTerminated, deviceId=" + device_id_);stopGB28181Stream();destoryRTPSender();}private String device_id_;public Runnable set(String device_id) {this.device_id_ = device_id;return this;}}.set(deviceId),0);
}@Override
public void ntsOnDevicePositionRequest(String deviceId, int interval) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {getLocation(context_);Log.v(TAG, "ntsOnDevicePositionRequest, deviceId:" + this.device_id_ + ", Longitude:" + mLongitude+ ", Latitude:" + mLatitude + ", Time:" + mLocationTime);if (mLongitude != null && mLatitude != null) {com.gb.ntsignalling.DevicePosition device_pos = new com.gb.ntsignalling.DevicePosition();device_pos.setTime(mLocationTime);device_pos.setLongitude(mLongitude);device_pos.setLatitude(mLatitude);if (gb28181_agent_ != null ) {gb28181_agent_.updateDevicePosition(device_id_, device_pos);}}}private String device_id_;private int interval_;public Runnable set(String device_id, int interval) {this.device_id_ = device_id;this.interval_ = interval;return this;}}.set(deviceId, interval),0);
}@Override
public void ntsOnNotifyBroadcastCommand(String fromUserName, String fromUserNameAtDomain, String sn, String sourceID, String targetID) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnNotifyBroadcastCommand, fromUserName:"+ from_user_name_ + ", fromUserNameAtDomain:"+ from_user_name_at_domain_+ ", SN:" + sn_ + ", sourceID:" + source_id_ + ", targetID:" + target_id_);if (gb28181_agent_ != null ) {gb28181_agent_.respondBroadcastCommand(from_user_name_, from_user_name_at_domain_,sn_,source_id_, target_id_, true);btnGB28181AudioBroadcast.setText("收到GB28181语音广播通知");}}private String from_user_name_;private String from_user_name_at_domain_;private String sn_;private String source_id_;private String target_id_;public Runnable set(String from_user_name, String from_user_name_at_domain, String sn, String source_id, String target_id) {this.from_user_name_ = from_user_name;this.from_user_name_at_domain_ = from_user_name_at_domain;this.sn_ = sn;this.source_id_ = source_id;this.target_id_ = target_id;return this;}}.set(fromUserName, fromUserNameAtDomain, sn, sourceID, targetID),0);
}

语音广播相关:

@Override
public void ntsOnAudioBroadcast(String commandFromUserName, String commandFromUserNameAtDomain, String sourceID, String targetID) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnAudioBroadcastPlay, fromFromUserName:" + command_from_user_name_+ " FromUserNameAtDomain:" + command_from_user_name_at_domain_+ " sourceID:" + source_id_ + ", targetID:" + target_id_);stopAudioPlayer();destoryRTPReceiver();if (gb28181_agent_ != null ) {String local_ip_addr = IPAddrUtils.getIpAddress(context_);boolean is_tcp = true; // 考虑到跨网段, 默认用TCP传输rtp包rtp_receiver_handle_ = lib_player_.CreateRTPReceiver(0);if (rtp_receiver_handle_ != 0 ) {lib_player_.SetRTPReceiverTransportProtocol(rtp_receiver_handle_, is_tcp?1:0);lib_player_.SetRTPReceiverIPAddressType(rtp_receiver_handle_, 0);if (0 == lib_player_.CreateRTPReceiverSession(rtp_receiver_handle_, 0) ) {int local_port = lib_player_.GetRTPReceiverLocalPort(rtp_receiver_handle_);boolean ret = gb28181_agent_.inviteAudioBroadcast(command_from_user_name_,command_from_user_name_at_domain_,source_id_, target_id_, "IP4", local_ip_addr, local_port, is_tcp?"TCP/RTP/AVP":"RTP/AVP");if (!ret ) {destoryRTPReceiver();btnGB28181AudioBroadcast.setText("GB28181语音广播");}else {btnGB28181AudioBroadcast.setText("GB28181语音广播呼叫中");}} else {destoryRTPReceiver();btnGB28181AudioBroadcast.setText("GB28181语音广播");}}}}private String command_from_user_name_;private String command_from_user_name_at_domain_;private String source_id_;private String target_id_;public Runnable set(String command_from_user_name, String command_from_user_name_at_domain, String source_id, String target_id) {this.command_from_user_name_ = command_from_user_name;this.command_from_user_name_at_domain_ = command_from_user_name_at_domain;this.source_id_ = source_id;this.target_id_ = target_id;return this;}}.set(commandFromUserName, commandFromUserNameAtDomain, sourceID, targetID),0);
}@Override
public void ntsOnInviteAudioBroadcastException(String sourceID, String targetID, String errorInfo) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnInviteAudioBroadcastException, sourceID:" + source_id_ + ", targetID:" + target_id_);destoryRTPReceiver();btnGB28181AudioBroadcast.setText("GB28181语音广播");}private String source_id_;private String target_id_;public Runnable set(String source_id, String target_id) {this.source_id_ = source_id;this.target_id_ = target_id;return this;}}.set(sourceID, targetID),0);
}@Override
public void ntsOnInviteAudioBroadcastTimeout(String sourceID, String targetID) {handler_.postDelayed(new Runnable() {@Overridepublic void run() {Log.i(TAG, "ntsOnInviteAudioBroadcastTimeout, sourceID:" + source_id_ + ", targetID:" + target_id_);destoryRTPReceiver();btnGB28181AudioBroadcast.setText("GB28181语音广播");}private String source_id_;private String target_id_;public Runnable set(String source_id, String target_id) {this.source_id_ = source_id;this.target_id_ = target_id;return this;}}.set(sourceID, targetID),0);
}

我们demo实现的横竖屏切换时,自动切换分辨率,有些国标平台,对分辨率切换支持的并不友好,这点可以根据实际情况调整,比如固定横竖屏。

总的来说,camera2对焦等各个方面,确实优于camera,替换升级也是大势所趋。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/552956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android平台GB28181设备接入端本地SIP端口被占用或屏蔽怎么办?

好多开发者或厂商&#xff0c;对Android平台GB28181接入模块的定位&#xff0c;大多是IPC国标流程打通模拟&#xff0c;基于这个目的&#xff0c;很难按照标准SPEC规范实现Android平台GB28181设备接入&#xff0c;我们在跟第三方国标平台厂商对接时发现&#xff0c;部分公司&am…

Android平台GB28181设备接入端如何实现本地录像?

实现Android平台GB28181设备接入的时候&#xff0c;有个功能点不可避免&#xff0c;那就是本地录像&#xff0c;实际上&#xff0c;在实现GB28181设备接入模块之前&#xff0c;我们前些年做RTMP推送和轻量级RTSP服务的时候&#xff0c;早已经实现了本地录像功能。 本地录像功能…

国网B接口注册(REGISTER)接口描述和消息示例

技术背景 电网视频监控系统是智能电网的一个重要组成部分&#xff0c;广泛应用于电网的建设、生产、运行、经营等方面。由于视频监控系统在不同的建设时期选用了不同的技术和不同厂家的产品&#xff0c;导致了标准不统一、技术路线不一致。目前国家电网公司智能电网建设&#…

国网B接口资源上报(Push_Resourse)接口描述和消息示例

上篇blog&#xff0c;梳理了国网B接口的REGISTER接口描述和消息示例&#xff0c;前端系统加电启动并初次注册成功后&#xff0c;向平台上报前端系统的设备资源信息&#xff08;包括&#xff1a;视频服务器、DVR/DVS、摄像机、告警设备、环境量采集设备等模拟或数字信号采集设备…

Android平台GB28181设备接入端语音广播如何实现实时音量调节

Android平台GB28181设备接入&#xff0c;语音广播功能非常重要&#xff0c;本文要介绍的&#xff0c;不是语音广播的流程&#xff0c;语音广播流程&#xff0c;之前的blog也有非常详细的分享&#xff0c;感兴趣的可以参考官方规范书的交互流程&#xff1a; 语音广播这块&#x…

GB28181基于TCP协议的视音频媒体传输探究及实现

我们先看看官方规范针对TCP协议的视音频传输描述&#xff1a; 实时视频点播、历史视频回放与下载的 TCP媒体传输应支持基于RTP封装的视音频PS流&#xff0c;封装格式参照IETFRFC4571。 流媒体服务器宜同时支持作为TCP媒体流传输服务端和客户端。默认情况下&#xff0c;前端设…

Android平台GB28181接入端如何对接UVC摄像头?

我们在对接Android平台GB28181接入的时候&#xff0c;有公司提出这样的需求&#xff0c;除了采集执法记录仪摄像头自带的数据外&#xff0c;还想通过执法记录仪采集外接UVC摄像头。 实际上&#xff0c;这块对我们来说有点炒冷饭了&#xff0c;不算新的诉求。​大牛直播SDK​在2…

Android平台实现系统内录(捕获播放的音频)并推送RTMP服务技术方案探究

几年来&#xff0c;我们在做无纸化同屏或在线教育相关场景的时候&#xff0c;总是被一件事情困扰&#xff1a;如何实现Android平台的系统内录&#xff0c;并推送到其他播放端&#xff0c;常用的场景比如做无纸化会议或教育的时候&#xff0c;主讲人或老师需要放一个视频&#x…

Android平台GB28181设备接入端PTZ对接详解

PTZCmd实现背景 上一篇blog“Android平台GB28181设备接入模块之球机/云台控制探究”谈到&#xff0c;Android平台做国标GB28181设备接入端的时候&#xff0c;PTZ控制要不要处理&#xff1f;如果处理&#xff0c;难度大不大&#xff1f; 首先说要不要处理&#xff1a;如果只是…

Android平台GB28181设备接入模块摄像头采集方向不对怎么办?

背景 我们在做Android平台GB28181设备接入模块的时候&#xff0c;有开发者提到这样的诉求&#xff1a;他们的智能头盔、执法记录仪等设备&#xff0c;采集到的图像&#xff0c;是旋转了90、180甚至270的&#xff0c;设备本身无法针对图像做翻转或者旋转操作&#xff0c;问我们…

Android平台GB28181设备接入模块分辨率发生变化怎么办?

技术背景 我们在做Android平台gb28181设备接入模块的时候&#xff0c;遇到这样的情况&#xff0c;比如横竖屏分辨率不锁定&#xff0c;采集摄像头的时候&#xff0c;可以实现&#xff0c;横屏状态采集横屏&#xff0c;竖屏状态采集竖屏&#xff0c;简单来说&#xff0c;横屏状…

RTSP、RTMP播放器拉到的视频图像角度不对怎么办?

我们在做RTSP、RTMP播放器的时候&#xff0c;遇到这样的诉求&#xff1a;特别是RTSP&#xff0c;有些摄像头安装可能倒置或者旋转了90亦或270&#xff0c;拉取到图像&#xff0c;势必需要对视频图像做一定的处理&#xff0c;确保显示正常。 为此&#xff0c;我们提供了以下接口…

Android平台GB28181设备接入端如何调节实时音量?

我们在对接Android平台GB28181设备接入端的时候&#xff0c;有开发者提出这样的疑惑&#xff0c;如何调整设备接入端的实时音量&#xff1f; 实际上&#xff0c;这块我们前几年在做RTMP直播推送模块的时候&#xff0c;已经发布了相关的接口&#xff0c;这里再回顾下&#xff1…

【技术分享】如何实现功能完备性能优异的RTMP、RTSP播放器?

技术背景 这几年&#xff0c;我们对接了太多有RTSP或RTMP直播播放器诉求的开发者&#xff0c;他们当中除了寻求完整的解决方案的&#xff0c;还有些是技术探讨&#xff0c;希望能借鉴我们播放端的开发思路或功能特性&#xff0c;完善自己的产品。 忙里偷闲&#xff0c;今天我…

我正在参加2022年度博客之星评选,大家帮我点个五星好评

大家好&#xff0c;我是音视频牛哥&#xff0c;致力于跨平台的实时RTMP推流、转发、RTMP/RTSP直播播放、GB28181设备接入。 有幸参加2022年度博客之星评选&#xff0c;欢迎大家帮点五星好评。如果我的博客能给开发者带来哪怕一丝启发&#xff0c;对博主来说&#xff0c;也甚感…

rtmp/rtsp/hls公网真正可用的测试地址

相信大家在调试播放器的时候&#xff0c;都有这样的困惑&#xff0c;很难找到合适的公有测试源&#xff0c;以下是大牛直播SDK&#xff08;GitHub地址&#xff09;整理的真正可用的直播地址源。 其中&#xff0c;rtmp和rtsp的url&#xff0c;用我们播放器验证通过。 1. rtmp:…

公网可用的RTMP、RTSP测试地址(更新于2021年3月)

好多博客提到的公网可测试的RTSP和RTMP URL大多都不用了&#xff0c;以下是大牛直播SDK(Github)于2021年3月亲测可用的几个URL&#xff0c;有其他可用的URL&#xff0c;也欢迎大家在评论区回复。 RTMP流地址 湖南卫视&#xff1a;rtmp://58.200.131.2:1935/livetv/hunantv (7…

Unity环境下RTMP推流+RTMP播放低延迟解决方案

在本文之前&#xff0c;我们发布了Unity环境下的RTMP推流&#xff08;Windows平台Android平台&#xff09;和RTMP|RTSP拉流&#xff08;Windows平台Android平台iOS平台&#xff09;低延迟的解决方案&#xff0c;今天做个整体汇总&#xff0c;权当抛砖引玉。 1. Unity环境下RTM…

麒麟操作系统|Linux下低延时RTMP|RTSP直播播放实现

背景 国产操作系统多为以Linux为基础二次开发的操作系统。2014年4月8日起&#xff0c;美国微软公司停止了对Windows XP SP3操作系统提供服务支持&#xff0c;这引起了社会和广大用户的广泛关注和对信息安全的担忧。而2020年对Windows7服务支持的终止再一次推动了国产系统的发展…

基于RTMP实现Linux|麒麟操作系统下屏幕|系统声音采集推送

背景 Windows操作系统自问世以来&#xff0c;以其简单易用的图形化界面操作受到大众追捧&#xff0c;为计算机的普及、科技的发展做出了不可磨灭的功绩&#xff0c;也慢慢的成为人们最依赖的操作系统。在中国&#xff0c;90&#xff05;以上的办公环境都是Windows&#xff0c;…