技术背景
我们在做内网环境的一对一音视频互动的时候,遇到这样的技术诉求:如智能硬件场景下(比如操控智能硬件),纯内网环境,如何不要单独部署RTMP或类似流媒体服务,实现一对一音视频互动。
目前大多数场景,是走RTMP或WebRTC,无一例外的需要部署流媒体服务,如果纯内网环境下,实际上是考虑,两个终端同时开启轻量级RTSP服务,然后相互拉取对方回调上来的RTSP URL,通过回音消除等,实现智能化场景的一对一音视频互动。
技术实现
为此,我们在大牛直播SDK之前一对一互动demo基础上,添加了轻量级RTSP服务模块,上面系播放端,下面是轻量级RTSP服务。如果需要一对一互动,只要先点击启动RTSP服务,然后再发布RTSP流即可回调上来可以拉流的RTSP URL,回上来的URL,可以通过其他技术逻辑,通知给对方终端。
双方获取到对方的RTSP URL后,开始播放即可。
对应的代码如下:
//Author: daniusdk.com //启动/停止RTSP服务 class ButtonRtspServiceListener implements 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端口失败! 请检查端口是否重复或者端口不在范围内!"); } //String user_name = "admin"; //String password = "12345"; //libPublisher.SetRtspServerUserNamePassword(rtsp_handle_, user_name, password); 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; } } }
发布RTSP流:
//发布/停止RTSP流 class ButtonRtspPublisherListener implements OnClickListener { public void onClick(View v) { if (isRTSPPublisherRunning) { stopRtspPublisher(); if (!isPushingRtmp) { ConfigControlEnable(true); } btnRtspPublisher.setText("发布RTSP流"); btnGetRtspSessionNumbers.setEnabled(false); btnRtspService.setEnabled(true); isRTSPPublisherRunning = false; return; } Log.i(TAG, "onClick start rtsp publisher.."); if (!isPushingRtmp) { InitPusherAndSetConfig(); } if (publisherHandle == 0) { Log.e(TAG, "Start rtsp publisher, publisherHandle is null.."); return; } String rtsp_stream_name = "stream1"; libPublisher.SetRtspStreamName(publisherHandle, rtsp_stream_name); libPublisher.ClearRtspStreamServer(publisherHandle); libPublisher.AddRtspStreamServer(publisherHandle, rtsp_handle_, 0); if (libPublisher.StartRtspStream(publisherHandle, 0) != 0) { Log.e(TAG, "调用发布rtsp流接口失败!"); return; } if (!isPushingRtmp) { if (pushType == 0 || pushType == 1) { CheckInitAudioRecorder(); //enable pure video publisher.. } ConfigControlEnable(false); } startLayerPostThread(); btnRtspPublisher.setText("停止RTSP流"); btnGetRtspSessionNumbers.setEnabled(true); btnRtspService.setEnabled(false); isRTSPPublisherRunning = true; } }
获取RTSP流会话链接数:
//当前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(); } //获取RTSP会话数 class ButtonGetRtspSessionNumbersListener implements 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); } } }
相关Event状态回调设计如下:
class EventHandlePublisherV2 implements NTSmartEventCallbackV2 { @Override public void onNTSmartEventCallbackV2(long handle, int id, long param1, long param2, String param3, String param4, Object param5) { Log.i(TAG, "EventHandlePublisherV2: 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: 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); Message message = new Message(); message.what = PUBLISHER_EVENT_MSG; message.obj = publisher_event; handler_.sendMessage(message); } }
如果需要播放对方的RTSP URL:
btnPlaybackStartStopPlayback.setOnClickListener(new Button.OnClickListener() { // @Override public void onClick(View v) { if (isPlaybackViewStarted) { Log.i(PLAY_TAG, "Stop playback stream++"); btnPlaybackStartStopPlayback.setText("开始播放 "); //btnPopInputText.setEnabled(true); btnPlaybackPopInputUrl.setEnabled(true); btnPlaybackHardwareDecoder.setEnabled(true); btnPlaybackSetPlayBuffer.setEnabled(true); btnPlaybackFastStartup.setEnabled(true); if (playerHandle != 0) { libPlayer.SmartPlayerStopPlay(playerHandle); } releasePlayerHandle(); isPlaybackViewStarted = false; Log.i(PLAY_TAG, "Stop playback stream--"); } else { Log.i(PLAY_TAG, "Start playback stream++"); playerHandle = libPlayer.SmartPlayerOpen(curContext); if (playerHandle == 0) { Log.e(PLAY_TAG, "surfaceHandle with nil.."); return; } libPlayer.SetSmartPlayerEventCallbackV2(playerHandle, new EventHandlePlayerV2()); libPlayer.SmartPlayerSetSurface(playerHandle, playerSurfaceView); //if set the second param with null, it means it will playback audio only.. // libPlayer.SmartPlayerSetSurface(playerHandle, null); libPlayer.SmartPlayerSetRenderScaleMode(playerHandle, 1); // External Render test //libPlayer.SmartPlayerSetExternalRender(playerHandle, new RGBAExternalRender()); //libPlayer.SmartPlayerSetExternalRender(playerHandle, new I420ExternalRender()); libPlayer.SmartPlayerSetExternalAudioOutput(playerHandle, new PlayerExternalPcmOutput()); libPlayer.SmartPlayerSetAudioOutputType(playerHandle, 1); libPlayer.SmartPlayerSetBuffer(playerHandle, playbackBuffer); libPlayer.SmartPlayerSetFastStartup(playerHandle, isPlaybackFastStartup ? 1 : 0); if (isPlaybackMute) { libPlayer.SmartPlayerSetMute(playerHandle, isPlaybackMute ? 1 : 0); } if (isPlaybackHardwareDecoder) { int isSupportHevcHwDecoder = libPlayer.SetSmartPlayerVideoHevcHWDecoder(playerHandle, 1); int isSupportH264HwDecoder = libPlayer .SetSmartPlayerVideoHWDecoder(playerHandle, 1); Log.i(TAG, "isSupportH264HwDecoder: " + isSupportH264HwDecoder + ", isSupportHevcHwDecoder: " + isSupportHevcHwDecoder); } ; if (playbackUrl == null) { Log.e(PLAY_TAG, "playback URL with NULL..."); return; } libPlayer.SmartPlayerSetAudioVolume(playerHandle, curAudioVolume); libPlayer.SmartPlayerSetUrl(playerHandle, playbackUrl); int iPlaybackRet = libPlayer.SmartPlayerStartPlay(playerHandle); if (iPlaybackRet != 0) { releasePlayerHandle(); Log.e(PLAY_TAG, "StartPlayback stream failed.."); return; } btnPlaybackStartStopPlayback.setText("停止播放"); btnPlaybackPopInputUrl.setEnabled(false); btnPlaybackHardwareDecoder.setEnabled(false); btnPlaybackSetPlayBuffer.setEnabled(false); btnPlaybackFastStartup.setEnabled(false); isPlaybackViewStarted = true; Log.i(PLAY_TAG, "Start playback stream--"); } } });
技术总结
Android平台一对一互动,纯内网环境下,不部署单独的流媒体服务器,走轻量级RTSP服务真的非常方便,感兴趣的开发者可以尝试看看。