技术背景
前些年,我们在完成Android平台RTMP直播推送模块后,遇到这样的技术需求,好多开发者希望在Android平台,实现摄像头和麦克风音视频数据采集编码打包后,对外提供RTSP(Real Time Streaming Protocol)服务。
通常,这样的技术需求,需要通过集成支持RTSP服务功能的库或自己实现轻量级的RTSP服务逻辑。考虑到移动端设备的性能和实际并发诉求,简单来说,只要让Android模块,像IPC(网络摄像头)一样,提供小并发的技术需求就足够了。
技术实现
Android平台实现轻量级的RTSP服务,对外提供RTSP拉流的方式,共享摄像头和麦克风数据,可参考下面的实现逻辑:
- 配置摄像头:首先,你需要访问Android设备上的摄像头并获取视频流。这通常涉及到使用Android的Camera2 API或CameraX库来捕获视频帧,考虑到好的体验和目前市面上的版本,都已经是5.0以后,一般建议使用Camera2采集;
- 视频编码:将捕获到的视频帧编码为适合网络传输的格式,如H.264或H.265,音频的话,采集到的麦克风数据,可以编码成AAC或者PCMA;
- 实现RTSP服务器:自研实现轻量级RTSP服务逻辑,支持设置RTSP服务器的参数,如端口号、流名称等。同时,配置服务器以从摄像头麦克风接收视音频流,并将其封装为RTSP流;
- 启动服务器:启动RTSP服务器,使其开始监听并响应RTSP客户端的请求,发布RTSP流,对外提供RTSP拉流能力;
- 查看RTSP会话数:轻量级RTSP服务,需要有支持查看RTSP会话数的能力。
功能设计
有了上述的技术需求,我们Android平台轻量级RTSP模块能力迭代如下:
- [视频格式]H.264/H.265(Android H.265硬编码);
- [音频格式]G.711 A律、AAC;
- 协议:RTSP;
- [音量调节]Android平台采集端支持实时音量调节;
- [H.264硬编码]支持H.264特定机型硬编码;
- [H.265硬编码]支持H.265特定机型硬编码;
- [音视频]支持纯音频/纯视频/音视频;
- [摄像头]支持采集过程中,前后摄像头实时切换;
- 支持帧率、关键帧间隔(GOP)、码率(bit-rate)设置;
- [实时水印]支持动态文字水印、png水印;
- [实时快照]支持实时快照;
- [降噪]支持环境音、手机干扰等引起的噪音降噪处理、自动增益、VAD检测;
- [外部编码前视频数据对接]支持YUV数据对接;
- [外部编码前音频数据对接]支持PCM对接;
- [外部编码后视频数据对接]支持外部H.264、H.265数据对接;
- [外部编码后音频数据对接]外部AAC数据对接;
- [扩展录像功能]支持和录像SDK组合使用,录像相关功能。
- 支持RTSP端口设置;
- 支持RTSP鉴权用户名、密码设置;
- 支持获取当前RTSP服务会话连接数;
- 支持Android 5.1及以上版本。
接口设计
Android内置轻量级RTSP服务模块接口设计 | ||
---|---|---|
调用描述 | 接口 | 接口描述 |
SmartRTSPServerSDK | ||
初始化RTSP Server | InitRtspServer | Init rtsp server(和UnInitRtspServer配对使用,即便是启动多个RTSP服务,也只需调用一次InitRtspServer,请确保在OpenRtspServer之前调用) |
创建一个rtsp server | OpenRtspServer | 创建一个rtsp server,返回rtsp server句柄 |
设置端口 | SetRtspServerPort | 设置rtsp server 监听端口, 在StartRtspServer之前必须要设置端口 |
设置鉴权用户名、密码 | SetRtspServerUserNamePassword | 设置rtsp server 鉴权用户名和密码, 这个可以不设置,只有需要鉴权的再设置 |
获取rtsp server当前会话数 | GetRtspServerClientSessionNumbers | 获取rtsp server当前的客户会话数, 这个接口必须在StartRtspServer之后再调用 |
启动rtsp server | StartRtspServer | 启动rtsp server |
停止rtsp server | StopRtspServer | 停止rtsp server |
关闭rtsp server | CloseRtspServer | 关闭rtsp server |
UnInit rtsp server | UnInitRtspServer | UnInit rtsp server(和InitRtspServer配对使用,即便是启动多个RTSP服务,也只需调用一次UnInitRtspServer) |
SmartRTSPServerSDK供Publisher调用的接口 | ||
设置rtsp的流名称 | SetRtspStreamName | 设置rtsp的流名称 |
给要发布的rtsp流设置rtsp server | AddRtspStreamServer | 给要发布的rtsp流设置rtsp server, 一个流可以发布到多个rtsp server上,rtsp server的创建启动请参考OpenRtspServer和StartRtspServer接口 |
清除设置的rtsp server | ClearRtspStreamServer | 清除设置的rtsp server |
启动rtsp流 | StartRtspStream | 启动rtsp流 |
停止rtsp流 | StopRtspStream | 停止rtsp流 |
逻辑调用
以Android平台Camera2对接为例,先初始化RTSP Server:
代码语言:java复制/*
* MainActivity.java
* Author: daniusdk.com
* WeChat:xinsheng120
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
context_ = this.getApplicationContext();
libPublisher = new SmartPublisherJniV2();
libPublisher.InitRtspServer(context_); //和UnInitRtspServer配对使用,即便是启动多个RTSP服务,也只需调用一次InitRtspServer,请确保在OpenRtspServer之前调用
}
启动、停止RTSP服务:
代码语言:java复制//启动/停止RTSP服务
class ButtonRtspServiceListener implements View.OnClickListener {
public void onClick(View v) {
if (isRTSPServiceRunning) {
stopRtspService();
btnRtspService.setText("启动RTSP服务");
btnRtspPublisher.setEnabled(false);
isRTSPServiceRunning = false;
return;
}
Log.i(TAG, "onClick start rtsp service..");
rtsp_handle_ = libPublisher.OpenRtspServer(0);
if (rtsp_handle_ == 0) {
Log.e(TAG, "创建rtsp server实例失败! 请检查SDK有效性");
} else {
int port = 8554;
if (libPublisher.SetRtspServerPort(rtsp_handle_, port) != 0) {
libPublisher.CloseRtspServer(rtsp_handle_);
rtsp_handle_ = 0;
Log.e(TAG, "创建rtsp server端口失败! 请检查端口是否重复或者端口不在范围内!");
}
if (libPublisher.StartRtspServer(rtsp_handle_, 0) == 0) {
Log.i(TAG, "启动rtsp server 成功!");
} else {
libPublisher.CloseRtspServer(rtsp_handle_);
rtsp_handle_ = 0;
Log.e(TAG, "启动rtsp server失败! 请检查设置的端口是否被占用!");
}
btnRtspService.setText("停止RTSP服务");
btnRtspPublisher.setEnabled(true);
isRTSPServiceRunning = true;
}
}
}
stopRtspService()实现如下:
代码语言:java复制//停止RTSP服务
private void stopRtspService() {
if(!isRTSPServiceRunning)
{
return;
}
if (libPublisher != null && rtsp_handle_ != 0) {
libPublisher.StopRtspServer(rtsp_handle_);
libPublisher.CloseRtspServer(rtsp_handle_);
rtsp_handle_ = 0;
}
}
发布、停止RTSP流:
代码语言:java复制//发布/停止RTSP流
class ButtonRtspPublisherListener implements View.OnClickListener {
public void onClick(View v) {
if (stream_publisher_.is_rtsp_publishing()) {
stopRtspPublisher();
btnRtspPublisher.setText("发布RTSP流");
btnGetRtspSessionNumbers.setEnabled(false);
btnRtspService.setEnabled(true);
return;
}
Log.i(TAG, "onClick start rtsp publisher..");
InitAndSetConfig();
String rtsp_stream_name = "stream1";
stream_publisher_.SetRtspStreamName(rtsp_stream_name);
stream_publisher_.ClearRtspStreamServer();
stream_publisher_.AddRtspStreamServer(rtsp_handle_);
if (!stream_publisher_.StartRtspStream()) {
stream_publisher_.try_release();
Log.e(TAG, "调用发布rtsp流接口失败!");
return;
}
startAudioRecorder();
startLayerPostThread();
btnRtspPublisher.setText("停止RTSP流");
btnGetRtspSessionNumbers.setEnabled(true);
btnRtspService.setEnabled(false);
}
}
stopRtspPublisher()实现如下:
代码语言:java复制//停止发布RTSP流
private void stopRtspPublisher() {
stream_publisher_.StopRtspStream();
stream_publisher_.try_release();
if (!stream_publisher_.is_publishing())
stopAudioRecorder();
}
其中,InitAndSetConfig()实现如下,通过调研SmartPublisherOpen()接口,生成推送实例句柄。
代码语言:java复制/*
* MainActivity.java
* Author: daniusdk.com
*/
private void InitAndSetConfig() {
if (null == libPublisher)
return;
if (!stream_publisher_.empty())
return;
Log.i(TAG, "InitAndSetConfig video width: " video_width_ ", height" video_height_ " imageRotationDegree:" cameraImageRotationDegree_);
int audio_opt = 1;
long handle = libPublisher.SmartPublisherOpen(context_, audio_opt, 3, video_width_, video_height_);
if (0==handle) {
Log.e(TAG, "sdk open failed!");
return;
}
Log.i(TAG, "publisherHandle=" handle);
int fps = 25;
int gop = fps * 3;
initialize_publisher(libPublisher, handle, video_width_, video_height_, fps, gop);
stream_publisher_.set(libPublisher, handle);
}
对应的initialize_publisher()实现如下,设置软硬编码、帧率、关键帧间隔等。
代码语言:java复制private boolean initialize_publisher(SmartPublisherJniV2 lib_publisher, long handle, int width, int height, int fps, int gop) {
if (null == lib_publisher) {
Log.e(TAG, "initialize_publisher lib_publisher is null");
return false;
}
if (0 == handle) {
Log.e(TAG, "initialize_publisher handle is 0");
return false;
}
if (videoEncodeType == 1) {
int kbps = LibPublisherWrapper.estimate_video_hardware_kbps(width, height, fps, true);
Log.i(TAG, "h264HWKbps: " kbps);
int isSupportH264HWEncoder = lib_publisher.SetSmartPublisherVideoHWEncoder(handle, kbps);
if (isSupportH264HWEncoder == 0) {
lib_publisher.SetNativeMediaNDK(handle, 0);
lib_publisher.SetVideoHWEncoderBitrateMode(handle, 1); // 0:CQ, 1:VBR, 2:CBR
lib_publisher.SetVideoHWEncoderQuality(handle, 39);
lib_publisher.SetAVCHWEncoderProfile(handle, 0x08); // 0x01: Baseline, 0x02: Main, 0x08: High
// lib_publisher.SetAVCHWEncoderLevel(handle, 0x200); // Level 3.1
// lib_publisher.SetAVCHWEncoderLevel(handle, 0x400); // Level 3.2
// lib_publisher.SetAVCHWEncoderLevel(handle, 0x800); // Level 4
lib_publisher.SetAVCHWEncoderLevel(handle, 0x1000); // Level 4.1 多数情况下,这个够用了
//lib_publisher.SetAVCHWEncoderLevel(handle, 0x2000); // Level 4.2
// lib_publisher.SetVideoHWEncoderMaxBitrate(handle, ((long)h264HWKbps)*1300);
Log.i(TAG, "Great, it supports h.264 hardware encoder!");
}
} else if (videoEncodeType == 2) {
int kbps = LibPublisherWrapper.estimate_video_hardware_kbps(width, height, fps, false);
Log.i(TAG, "hevcHWKbps: " kbps);
int isSupportHevcHWEncoder = lib_publisher.SetSmartPublisherVideoHevcHWEncoder(handle, kbps);
if (isSupportHevcHWEncoder == 0) {
lib_publisher.SetNativeMediaNDK(handle, 0);
lib_publisher.SetVideoHWEncoderBitrateMode(handle, 1); // 0:CQ, 1:VBR, 2:CBR
lib_publisher.SetVideoHWEncoderQuality(handle, 39);
// libPublisher.SetVideoHWEncoderMaxBitrate(handle, ((long)hevcHWKbps)*1200);
Log.i(TAG, "Great, it supports hevc hardware encoder!");
}
}
boolean is_sw_vbr_mode = true;
//H.264 software encoder
if (is_sw_vbr_mode) {
int is_enable_vbr = 1;
int video_quality = LibPublisherWrapper.estimate_video_software_quality(width, height, true);
int vbr_max_kbps = LibPublisherWrapper.estimate_video_vbr_max_kbps(width, height, fps);
lib_publisher.SmartPublisherSetSwVBRMode(handle, is_enable_vbr, video_quality, vbr_max_kbps);
}
if (is_pcma_) {
lib_publisher.SmartPublisherSetAudioCodecType(handle, 3);
} else {
lib_publisher.SmartPublisherSetAudioCodecType(handle, 1);
}
lib_publisher.SetSmartPublisherEventCallbackV2(handle, new EventHandlerPublisherV2().set(handler_, record_executor_));
lib_publisher.SmartPublisherSetSWVideoEncoderProfile(handle, 3);
lib_publisher.SmartPublisherSetSWVideoEncoderSpeed(handle, 2);
lib_publisher.SmartPublisherSetGopInterval(handle, gop);
lib_publisher.SmartPublisherSetFPS(handle, fps);
// lib_publisher.SmartPublisherSetSWVideoBitRate(handle, 600, 1200);
boolean is_noise_suppression = true;
lib_publisher.SmartPublisherSetNoiseSuppression(handle, is_noise_suppression ? 1 : 0);
boolean is_agc = false;
lib_publisher.SmartPublisherSetAGC(handle, is_agc ? 1 : 0);
int echo_cancel_delay = 0;
lib_publisher.SmartPublisherSetEchoCancellation(handle, 1, echo_cancel_delay);
return true;
}
发布RTSP流成功后,会回调上来可供拉流的RTSP URL:
代码语言:java复制private static class EventHandlerPublisherV2 implements NTSmartEventCallbackV2 {
@Override
public void onNTSmartEventCallbackV2(long handle, int id, long param1, long param2, String param3, String param4, Object param5) {
switch (id) {
...
case NTSmartEventID.EVENT_DANIULIVE_ERC_PUBLISHER_RTSP_URL:
publisher_event = "RTSP服务URL: " param3;
break;
}
}
}
获取RTSP Session会话数:
代码语言:java复制//获取RTSP会话数
class ButtonGetRtspSessionNumbersListener implements View.OnClickListener {
public void onClick(View v) {
if (libPublisher != null && rtsp_handle_ != 0) {
int session_numbers = libPublisher.GetRtspServerClientSessionNumbers(rtsp_handle_);
Log.i(TAG, "GetRtspSessionNumbers: " session_numbers);
PopRtspSessionNumberDialog(session_numbers);
}
}
}
//当前RTSP会话数弹出框
private void PopRtspSessionNumberDialog(int session_numbers) {
final EditText inputUrlTxt = new EditText(this);
inputUrlTxt.setFocusable(true);
inputUrlTxt.setEnabled(false);
String session_numbers_tag = "RTSP服务当前客户会话数: " session_numbers;
inputUrlTxt.setText(session_numbers_tag);
AlertDialog.Builder builderUrl = new AlertDialog.Builder(this);
builderUrl
.setTitle("内置RTSP服务")
.setView(inputUrlTxt).setNegativeButton("确定", null);
builderUrl.show();
}
数据投递如下(以Camera2采集为例,如果是其他视频格式,也可以正常对接):
代码语言:java复制@Override
public void onCameraImageData(Image image) {
....
for (LibPublisherWrapper i : publisher_array_)
i.PostLayerImageYUV420888ByteBuffer(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, rotation_degree);
}
音频采集投递设计如下:
代码语言:java复制void startAudioRecorder() {
if (audio_recorder_ != null)
return;
audio_recorder_ = new NTAudioRecordV2(this);
Log.i(TAG, "startAudioRecorder call audio_recorder_.start() ...");
audio_recorder_callback_ = new NTAudioRecordV2CallbackImpl(stream_publisher_, null);
audio_recorder_.AddCallback(audio_recorder_callback_);
if (!audio_recorder_.Start(is_pcma_ ? 8000 : 44100, 1) ) {
audio_recorder_.RemoveCallback(audio_recorder_callback_);
audio_recorder_callback_ = null;
audio_recorder_ = null;
Log.e(TAG, "startAudioRecorder start failed.");
}
else {
Log.i(TAG, "startAudioRecorder call audio_recorder_.start() OK---...");
}
}
void stopAudioRecorder() {
if (null == audio_recorder_)
return;
Log.i(TAG, "stopAudioRecorder ");
audio_recorder_.Stop();
if (audio_recorder_callback_ != null) {
audio_recorder_.RemoveCallback(audio_recorder_callback_);
audio_recorder_callback_ = null;
}
audio_recorder_ = null;
Log.i(TAG, "stopAudioRecorder---");
}
回调Audio数据的地方,直接投递出去:
代码语言:java复制private static class NTAudioRecordV2CallbackImpl implements NTAudioRecordV2Callback {
private WeakReference<LibPublisherWrapper> publisher_0_;
private WeakReference<LibPublisherWrapper> publisher_1_;
public NTAudioRecordV2CallbackImpl(LibPublisherWrapper publisher_0) {
if (publisher_0 != null)
publisher_0_ = new WeakReference<>(publisher_0);
}
private final LibPublisherWrapper get_publisher_0() {
if (publisher_0_ !=null)
return publisher_0_.get();
return null;
}
@Override
public void onNTAudioRecordV2Frame(ByteBuffer data, int size, int sampleRate, int channel, int per_channel_sample_number) {
LibPublisherWrapper publisher_0 = get_publisher_0();
if (publisher_0 != null)
publisher_0.OnPCMData(data, size, sampleRate, channel, per_channel_sample_number);
}
}
onDestroy() 的时候,调研UnInitRtspServer()即可:
代码语言:java复制@Override
protected void onDestroy() {
Log.i(TAG, "activity destory!");
stopAudioRecorder();
stopRtspPublisher();
stopRtspService();
isRTSPServiceRunning = false;
stream_publisher_.release();
if (libPublisher != null)
libPublisher.UnInitRtspServer(); //如已启用内置服务功能(InitRtspServer),调用UnInitRtspServer, 注意,即便是启动多个RTSP服务,也只需调用UnInitRtspServer一次
stopLayerPostThread();
if (camera2Helper != null) {
camera2Helper.release();
}
super.onDestroy();
}
总结
Android平台实现内网环境下摄像头麦克风采集共享,其实只要采集数据后编码打包,把Android模块做成个轻量级的网络摄像头(或者IPC)服务即可。如果需要更高层级的逻辑实现,可以增加录像、快照等,感兴趣的开发者,可以单独跟我沟通探讨。