技术背景
实际上,我在年前的blog,已经写过Android平台GB28181后台service模式启动摄像头按需回传数据了,此次版本,是上个demo的迭代版,目的是平台侧如果不发起回传请求的话,摄像头不打开。
后台service模式启动后,仅完成平台上线注册,如果有语音广播过来,自动播放语音广播audio,如果平台侧订阅实时位置,安卓端按照位置订阅间隔,实时上报当前位置,当前端发起回传请求时,打开摄像头,再投递数据到底层模块,完成数据编码打包和回传,关闭回传后,摄像头自动关闭,达到最大限度节约资源占用的目的。
技术实现
懒得截图了,还是用老图吧,新的版本,在任务栏加了notify提醒,下面图片没有。
目前,Android平台GB28181设备接入侧模块,覆盖了以下功能:
- [视频格式]H.264/H.265(Android H.265硬编码);
- [音频格式]G.711 A律、AAC;
- [音量调节]Android平台采集端支持实时音量调节;
- [H.264硬编码]支持H.264特定机型硬编码;
- [H.265硬编码]支持H.265特定机型硬编码;
- [软硬编码参数配置]支持gop间隔、帧率、bit-rate设置;
- [软编码参数配置]支持软编码profile、软编码速度、可变码率设置;
- 支持横屏、竖屏推流;
- Android平台支持后台service推送屏幕(推送屏幕需要5.0+版本);
- 支持纯视频、音视频PS打包传输;
- 支持RTP OVER UDP和RTP OVER TCP被动模式(TCP媒体流传输客户端);
- 支持信令通道网络传输协议TCP/UDP设置;
- 支持注册、注销,支持注册刷新及注册有效期设置;
- 支持设备目录查询应答;
- 支持心跳机制,支持心跳间隔、心跳检测次数设置;
- 支持移动设备位置(MobilePosition)订阅和通知;
- 适用国家标准:GB/T 28181—2016;
- 支持语音广播;
- 支持语音对讲;
- 支持图像抓拍;
- 支持历史视音频文件检索;
- 支持历史视音频文件下载;
- 支持历史视音频文件回放;
- 支持云台控制和预置位查询;
- [实时水印]支持动态文字水印、png水印;
- [镜像]Android平台支持前置摄像头实时镜像功能;
- [实时静音]支持实时静音/取消静音;
- [实时快照]支持实时快照;
- [降噪]支持环境音、手机干扰等引起的噪音降噪处理、自动增益、VAD检测;
- [外部编码前视频数据对接]支持YUV数据对接;
- [外部编码前音频数据对接]支持PCM对接;
- [外部编码后视频数据对接]支持外部H.264数据对接;
- [外部编码后音频数据对接]外部AAC数据对接;
- [扩展录像功能]支持和录像SDK组合使用,录像相关功能。
先说这个提醒代码吧,onCreate()的时候,调用show_notification();即可:
private Notification get_notification() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("NT-GB28181-Service-Demo")
.setContentText("大牛直播SDK-安卓国标28181-Service-测试程序");
if (Build.VERSION.SDK_INT >=26)
builder.setChannelId(notification_id_);
Notification notification = builder.build();
return notification;
}
private void show_notification(){
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if(Build.VERSION.SDK_INT >= 26){
NotificationChannel channel = new NotificationChannel(notification_id_, notification_name_, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
startForeground(313,get_notification());
}
onStart()里面,调用initGB28181Agent()完成国标设备侧到平台侧的register、catalog和keepalive交互,如果平台侧需要订阅位置,Android GB28181设备端可按照订阅时间间隔,实时上报位置信息。
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i(TAG, "onStart..");
video_width_ = intent.getExtras().getInt("CAMERAWIDTH");
video_height_ = intent.getExtras().getInt("CAMERAHEIGHT");
boolean isCameraFaceFront = intent.getExtras().getBoolean("SWITCHCAMERA");
Log.i(TAG, "video_width: " + video_width_ + ", video_height: " + video_height_ + " isCameraFaceFront: " + isCameraFaceFront);
if (isCameraFaceFront)
current_camera_type_ = FRONT;
else
current_camera_type_ = BACK;
is_hardware_encoder = intent.getExtras().getBoolean("HWENCODER");
window_manager_ = (WindowManager) getSystemService(Service.WINDOW_SERVICE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, intent, 0);
bgSurfaceView = new SurfaceView(this);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
1, 1, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
window_manager_.addView(bgSurfaceView, layoutParams);
bgSurfaceView.getHolder().addCallback(this);
if (null == gb28181_agent_ ) {
if(!initGB28181Agent() )
return;
}
if (!gb28181_agent_.isRunning()) {
if ( gb28181_agent_.start() )
Log.i(TAG, "gb28181_agent_.start ok");
else
Log.e(TAG, "gb28181_agent_.start failed");
}
}
GB28181信令交互处理如下,收到平台侧invite请求后,尝试打开摄像头:
// BackgroudService.java
// Author: daniusdk.com
@Override
public void ntsOnInvitePlay(String deviceId, SessionDescription session_des) {
handler_.postDelayed(new Runnable() {
@Override
public void run() {
// 先振铃响应下
gb28181_agent_.respondPlayInvite(180, device_id_);
if (!try_preview_camera()) {
gb28181_agent_.respondPlayInvite(488, device_id_);
Log.i(TAG, "ntsOnInvitePlay try_preview_camera failed, response 488, device_id:" + device_id_);
return;
}
MediaSessionDescription video_des = null;
SDPRtpMapAttribute ps_rtpmap_attr = null;
// 28181 视频使用PS打包
Vector<MediaSessionDescription> video_des_list = session_des_.getVideoPSDescriptions();
if (video_des_list != null && !video_des_list.isEmpty()) {
for(MediaSessionDescription m : video_des_list) {
if (m != null && m.isValidAddressType() && m.isHasAddress() ) {
video_des = m;
ps_rtpmap_attr = video_des.getPSRtpMapAttribute();
break;
}
}
}
if (null == video_des) {
gb28181_agent_.respondPlayInvite(488, device_id_);
Log.i(TAG, "ntsOnInvitePlay get video description is null, response 488, device_id:" + device_id_);
return;
}
if (null == ps_rtpmap_attr ) {
gb28181_agent_.respondPlayInvite(488, device_id_);
Log.i(TAG, "ntsOnInvitePlay get ps rtp map attribute is null, response 488, device_id:" + device_id_);
return;
}
Log.i(TAG,"ntsOnInvitePlay, 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());
long rtp_sender_handle = lib_publisher_.CreateRTPSender(0);
if ( rtp_sender_handle == 0 ) {
gb28181_agent_.respondPlayInvite(488, device_id_);
Log.i(TAG, "ntsOnInvitePlay 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();
lib_publisher_.SetRTPSenderTransportProtocol(rtp_sender_handle, video_des.isRTPOverUDP()?0:1);
lib_publisher_.SetRTPSenderIPAddressType(rtp_sender_handle, video_des.isIPv4()?0:1);
lib_publisher_.SetRTPSenderLocalPort(rtp_sender_handle, 0);
lib_publisher_.SetRTPSenderSSRC(rtp_sender_handle, video_des.getSSRC());
lib_publisher_.SetRTPSenderSocketSendBuffer(rtp_sender_handle, 2*1024*1024); // 设置到2M
lib_publisher_.SetRTPSenderClockRate(rtp_sender_handle, ps_rtpmap_attr.getClockRate());
lib_publisher_.SetRTPSenderDestination(rtp_sender_handle, video_des.getAddress(), video_des.getPort());
if ( lib_publisher_.InitRTPSender(rtp_sender_handle) != 0 ) {
gb28181_agent_.respondPlayInvite(488, device_id_);
lib_publisher_.DestoryRTPSender(rtp_sender_handle);
return;
}
int local_port = lib_publisher_.GetRTPSenderLocalPort(rtp_sender_handle);
if (local_port == 0) {
gb28181_agent_.respondPlayInvite(488, device_id_);
lib_publisher_.DestoryRTPSender(rtp_sender_handle);
return;
}
Log.i(TAG,"get local_port:" + local_port);
String local_ip_addr = IPAddrUtils.getIpAddress(context_);
MediaSessionDescription local_video_des = new MediaSessionDescription(video_des.getType());
local_video_des.addFormat(String.valueOf(ps_rtpmap_attr.getPayloadType()));
local_video_des.addRtpMapAttribute(ps_rtpmap_attr);
local_video_des.setAddressType(video_des.getAddressType());
local_video_des.setAddress(local_ip_addr);
local_video_des.setPort(local_port);
local_video_des.setTransportProtocol(video_des.getTransportProtocol());
local_video_des.setSSRC(video_des.getSSRC());
if (!gb28181_agent_.respondPlayInviteOK(device_id_,local_video_des) ) {
lib_publisher_.DestoryRTPSender(rtp_sender_handle);
Log.e(TAG, "ntsOnInvitePlay call respondPlayInviteOK failed.");
return;
}
gb28181_rtp_sender_handle_ = rtp_sender_handle;
}
private String device_id_;
private SessionDescription session_des_;
public Runnable set(String device_id, SessionDescription session_des) {
this.device_id_ = device_id;
this.session_des_ = session_des;
return this;
}
}.set(deviceId, session_des),0);
}
bye的时候,关闭释放摄像头:
@Override
public void ntsOnByePlay(String deviceId) {
handler_.postDelayed(new Runnable() {
@Override
public void run() {
Log.i(TAG, "ntsOnByePlay, stop GB28181 media stream, deviceId=" + device_id_);
stopGB28181Stream();
destoryRTPSender();
release_camera();
}
private String device_id_;
public Runnable set(String device_id) {
this.device_id_ = device_id;
return this;
}
}.set(deviceId),0);
}
其他部分,和年前的版本基本类似,这里就不再赘述。
总结
后台采集摄像头,如果想再进一步扩展,可以把android平台gb28181的camera2 demo,都移植过来,实现功能更强大的国标设备侧,这里主要是展示,收到国标平台侧的回传请求后,才打开摄像头,才开始编码打包,最大限度的减少资源的占用,感兴趣的开发者可以跟我单独交流。