Android平台如何实现多路低延迟RTSP|RTMP播放?

技术背景

实际上,我们在2015年做Android平台RTSP、RTMP播放模块的时候,第一版就支持了多实例播放,因为SDK设计比较灵活,做个简单的player实例封装即可实现多实例播放(Android Unity的就有多路demo),所以官方一直没有正式demo,本次也是有个开发者提到,希望测试下我们多路播放的效果,自己又不想做封装,索性给做个版本。

技术实现

废话不多说,先上图:

我们针对的功能展示,主要是播放和录像这块,先说播放:

ini 复制代码
/*
 * SmartPlayer.java
 * Author: https://daniusdk.com
 * Created by DaniuLive on 2015/09/26.
 */
class ButtonPlayback1Listener implements View.OnClickListener {
	public void onClick(View v) {
		if (stream_player_1_.is_playing()) {
			Log.i(TAG, "Stop player1..");

			boolean iRet = stream_player_1_.StopPlayer();

			if (!iRet) {
				Log.e(TAG, "Call StopPlayer failed..");
				return;
			}

			stream_player_1_.try_release();
			btn_playback1.setText("开始播放1");
			SetViewVisibility(surface_view_1_);
		} else {
			Log.i(TAG, "Start playback stream1++");

			int play_buffer = 0;
			int is_using_tcp = 0;
			if(!stream_player_1_.OpenPlayerHandle(playback_url_1_, play_buffer, is_using_tcp))
				return;

			stream_player_1_.SetView(surface_view_1_);

			boolean is_mute = false;
			boolean iPlaybackRet = stream_player_1_.StartPlayer(isHardwareDecoder, is_enable_hardware_render_mode, is_mute);
			if (!iPlaybackRet) {
				Log.e(TAG, "Call StartPlayer failed..");
				return;
			}

			btn_playback1.setText("停止播放1");
		}
	}
}

对应的OpenPlayerHandle()实现如下:

ini 复制代码
/*
 * LibPlayerWrapper.java.java
 * Author: https://daniusdk.com
 */
public boolean OpenPlayerHandle(String playback_url, int play_buffer, int is_using_tcp) {

	if (check_native_handle())
		return true;

	if(!isValidRtspOrRtmpUrl(playback_url))
		return false;

	long handle = lib_player_.SmartPlayerOpen(application_context());
	if (0==handle) {
		Log.e(TAG, "sdk open failed!");
		return false;
	}

	lib_player_.SetSmartPlayerEventCallbackV2(handle, new EventHandleV2());

	lib_player_.SmartPlayerSetBuffer(handle, play_buffer);

	// set report download speed(默认2秒一次回调 用户可自行调整report间隔)
	lib_player_.SmartPlayerSetReportDownloadSpeed(handle, 1, 4);

	boolean isFastStartup = true;
	lib_player_.SmartPlayerSetFastStartup(handle, isFastStartup ? 1 : 0);

	//设置RTSP超时时间
	int rtsp_timeout = 10;
	lib_player_.SmartPlayerSetRTSPTimeout(handle, rtsp_timeout);

	//设置RTSP TCP/UDP模式自动切换
	int is_auto_switch_tcp_udp = 1;
	lib_player_.SmartPlayerSetRTSPAutoSwitchTcpUdp(handle, is_auto_switch_tcp_udp);

	lib_player_.SmartPlayerSaveImageFlag(handle, 1);

	// It only used when playback RTSP stream..
	lib_player_.SmartPlayerSetRTSPTcpMode(handle, is_using_tcp);

	lib_player_.DisableEnhancedRTMP(handle, 0);

	lib_player_.SmartPlayerSetUrl(handle, playback_url);

	set(handle);

	return true;
}

对应的开始播放、停止播放设计:

kotlin 复制代码
/*
 * LibPlayerWrapper.java
 * Author: https://daniusdk.com
 */
public boolean StartPlayer(boolean is_hardware_decoder, boolean is_enable_hardware_render_mode, boolean is_mute) {
	if (is_playing()) {
		Log.e(TAG, "already playing, native_handle:" + get());
		return false;
	}

	SetPlayerParam(is_hardware_decoder, is_enable_hardware_render_mode, is_mute);

	int ret = lib_player_.SmartPlayerStartPlay(get());
	if (ret != OK) {
		Log.e(TAG, "call StartPlay failed, native_handle:" + get() + ", ret:" + ret);
		return false;
	}

	write_lock_.lock();
	try {
		this.is_playing_ = true;
	} finally {
		write_lock_.unlock();
	}

	Log.i(TAG, "call StartPlayer OK, native_handle:" + get());
	return true;
}

public boolean StopPlayer() {
	if (!check_native_handle())
		return false;

	if (!is_playing()) {
		Log.w(TAG, "it's not playing, native_handle:" + get());
		return false;
	}

	boolean is_need_call = false;
	write_lock_.lock();
	try {
		if (this.is_playing_) {
			this.is_playing_ = false;
			is_need_call = true;
		}
	} finally {
		write_lock_.unlock();
	}

	if (is_need_call)
		lib_player_.SmartPlayerStopPlay(get());

	return true;
}

录像设计:

ini 复制代码
/*
 * SmartPlayer.java
 * Author: https://daniusdk.com
 */
class ButtonRecorder1Listener implements View.OnClickListener {
	public void onClick(View v) {
		if (stream_player_1_.is_recording()) {
			Log.i(TAG, "Stop recorder1..");

			boolean iRet = stream_player_1_.StopRecorder();

			if (!iRet) {
				Log.e(TAG, "Call StopRecorder failed..");
				return;
			}

			stream_player_1_.try_release();
			btn_recorder1.setText("开始录像1");
		} else {
			Log.i(TAG, "Start recorder stream1++");

			int play_buffer = 0;
			int is_using_tcp = 0;

			if(!stream_player_1_.OpenPlayerHandle(playback_url_1_, play_buffer, is_using_tcp))
				return;

			stream_player_1_.ConfigRecorderParam(recDir, 400, 1, 1, 1);

			boolean iRecRet = stream_player_1_.StartRecorder();
			if (!iRecRet) {
				Log.e(TAG, "Call StartRecorder failed..");
				return;
			}

			btn_recorder1.setText("停止录像1");
		}
	}
}

录像参数配置选项:

csharp 复制代码
/*
 * LibPlayerWrapper.java
 * Author: https://daniusdk.com
 */
public boolean ConfigRecorderParam(String rec_dir, int file_max_size, int is_transcode_aac,
								   int is_record_video, int is_record_audio) {

	if(!check_native_handle())
		return false;

	if (null == rec_dir || rec_dir.isEmpty())
		return false;

	int ret = lib_player_.SmartPlayerCreateFileDirectory(rec_dir);
	if (ret != 0) {
		Log.e(TAG, "Create record dir failed, path:" + rec_dir);
		return false;
	}

	if (lib_player_.SmartPlayerSetRecorderDirectory(get(), rec_dir) != 0) {
		Log.e(TAG, "Set record dir failed , path:" + rec_dir);
		return false;
	}

	if (lib_player_.SmartPlayerSetRecorderFileMaxSize(get(),file_max_size) != 0) {
		Log.e(TAG, "SmartPlayerSetRecorderFileMaxSize failed.");
		return false;
	}

	lib_player_.SmartPlayerSetRecorderAudioTranscodeAAC(get(), is_transcode_aac);

	// 更细粒度控制录像的, 一般情况无需调用
	lib_player_.SmartPlayerSetRecorderVideo(get(), is_record_video);
	lib_player_.SmartPlayerSetRecorderAudio(get(), is_record_audio);
	return true;
}

开始录像、结束录像:

kotlin 复制代码
/*
 * LibPlayerWrapper.java
 * Author: https://daniusdk.com
 */
public boolean StartRecorder() {

	if (is_recording()) {
		Log.e(TAG, "already recording, native_handle:" + get());
		return false;
	}

	int ret = lib_player_.SmartPlayerStartRecorder(get());
	if (ret != OK) {
		Log.e(TAG, "call SmartPlayerStartRecorder failed, native_handle:" + get() + ", ret:" + ret);
		return false;
	}

	write_lock_.lock();
	try {
		this.is_recording_ = true;
	} finally {
		write_lock_.unlock();
	}

	Log.i(TAG, "call SmartPlayerStartRecorder OK, native_handle:" + get());
	return true;
}

public boolean StopRecorder() {
	if (!check_native_handle())
		return false;

	if (!is_recording()) {
		Log.w(TAG, "it's not recording, native_handle:" + get());
		return false;
	}

	boolean is_need_call = false;
	write_lock_.lock();
	try {
		if (this.is_recording_) {
			this.is_recording_ = false;
			is_need_call = true;
		}
	} finally {
		write_lock_.unlock();
	}

	if (is_need_call)
		lib_player_.SmartPlayerStopRecorder(get());

	return true;
}

总结

说了这么多,以RTSP播放为例,大概说下实现的功能:

  • [支持播放协议]高稳定、超低延迟、业内首屈一指的RTSP直播播放器SDK;
  • [多实例播放]支持多实例播放;
  • [事件回调]支持网络状态、buffer状态等回调;
  • [视频格式]支持H.265、H.264,此外,还支持RTSP MJPEG播放;
  • [音频格式]支持AAC/PCMA/PCMU;
  • [H.264/H.265软解码]支持H.264/H.265软解;
  • [H.264硬解码]Windows/Android/iOS支持特定机型H.264硬解;
  • [H.265硬解]Windows/Android/iOS支持特定机型H.265硬解;
  • [H.264/H.265硬解码]Android支持设置Surface模式硬解和普通模式硬解码;
  • [RTSP模式设置]支持RTSP TCP/UDP模式设置;
  • [RTSP TCP/UDP自动切换]支持RTSP TCP、UDP模式自动切换;
  • [RTSP超时设置]支持RTSP超时时间设置,单位:秒;
  • [RTSP 401认证处理]支持上报RTSP 401事件,如URL携带鉴权信息,会自动处理;
  • [缓冲时间设置]支持buffer time设置;
  • [首屏秒开]支持首屏秒开模式;
  • [复杂网络处理]支持断网重连等各种网络环境自动适配;
  • [快速切换URL]支持播放过程中,快速切换其他URL,内容切换更快;
  • [音视频多种render机制]Android平台,视频:surfaceview/OpenGL ES,音频:AudioTrack/OpenSL ES;
  • [实时静音]支持播放过程中,实时静音/取消静音;
  • [实时音量调节]支持播放过程中实时调节音量;
  • [实时快照]支持播放过程中截取当前播放画面;
  • [只播关键帧]Windows平台支持实时设置是否只播放关键帧;
  • [渲染角度]支持0°,90°,180°和270°四个视频画面渲染角度设置;
  • [渲染镜像]支持水平反转、垂直反转模式设置;
  • [等比例缩放]支持图像等比例缩放绘制(Android设置surface模式硬解模式不支持);
  • [实时下载速度更新]支持当前下载速度实时回调(支持设置回调时间间隔);
  • [解码前视频数据回调]支持H.264/H.265数据回调;
  • [解码后视频数据回调]支持解码后YUV/RGB数据回调;
  • [解码前音频数据回调]支持AAC/PCMA/PCMU数据回调;
  • [音视频自适应]支持播放过程中,音视频信息改变后自适应;
  • [扩展录像功能]完美支持和录像SDK组合使用。

上面只是简单的播放、录像的演示,除此之外,大牛直播SDK的RTSP、RTMP播放器海康实现播放缓冲设置、软硬解码设置、实时快照、实时音量调节、实时解码后数据回调等。毫秒级延迟,完全满足对延迟、稳定性要求苛刻的场景下。感兴趣的开发者,可以单独Q我89030985和我沟通。

相关推荐
关键帧Keyframe2 天前
音视频面试题集锦第 7 期
音视频开发·视频编码·客户端
关键帧Keyframe2 天前
音视频面试题集锦第 8 期
ios·音视频开发·客户端
蚝油菜花7 天前
MimicTalk:字节跳动和浙江大学联合推出 15 分钟生成 3D 说话人脸视频的生成模型
人工智能·开源·音视频开发
音视频牛哥9 天前
Android平台RTSP|RTMP播放器高效率如何回调YUV或RGB数据?
音视频开发·视频编码·直播
<Sunny>11 天前
MPP音视频总结
音视频开发·1024程序员节·海思mpp
GoFly开发者12 天前
GoFly快速开发框架已集成了RTSP流媒体服务器(直播、录播)代码插件-开发者集成流媒体服务器更加方便
go·音视频开发
aqi0013 天前
FFmpeg开发笔记(六十)使用国产的ijkplayer播放器观看网络视频
android·ffmpeg·音视频·直播·流媒体
linkedbyte13 天前
常用录屏软件功能比较
音视频·直播·录屏
aqi0016 天前
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
android·ffmpeg·音视频·直播·流媒体