Windows平台下如何实现Unity3D下的RTMP推送
生活随笔
收集整理的這篇文章主要介紹了
Windows平台下如何实现Unity3D下的RTMP推送
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
好多開發(fā)者苦于很難在unity3d下實(shí)現(xiàn)RTMP直播推送,本次以大牛直播SDK(Github)的Windows平臺(tái)RTMP推送模塊(以推攝像頭為例,如需推屏幕數(shù)據(jù),設(shè)置相關(guān)參數(shù)即可)為例,介紹下unity3d的RTMP推送集成。
簡(jiǎn)單來(lái)說(shuō),Unity3D環(huán)境下,可以直接調(diào)用C#的接口封裝,針對(duì)此,我們先做了一層封裝?(nt_publisher_wrapper.cs),核心代碼如下:
初始化和基礎(chǔ)參數(shù)設(shè)置:
private bool InitSDK(){if (!is_pusher_sdk_init_){// 設(shè)置日志路徑(請(qǐng)確保目錄存在)String log_path = "D:\\pulisherlog";NTSmartLog.NT_SL_SetPath(log_path);UInt32 isInited = NTSmartPublisherSDK.NT_PB_Init(0, IntPtr.Zero);if (isInited != 0){Debug.Log("調(diào)用NT_PB_Init失敗..");return false;}is_pusher_sdk_init_ = true;}return true;} public bool OpenPublisherHandle(uint video_option, uint audio_option){if (publisher_handle_ != IntPtr.Zero){return true;}publisher_handle_count_ = 0;if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_Open(out publisher_handle_,video_option, audio_option, 0, IntPtr.Zero)){return false;}if (publisher_handle_ != IntPtr.Zero){pb_event_call_back_ = new NT_PB_SDKEventCallBack(PbEventCallBack);NTSmartPublisherSDK.NT_PB_SetEventCallBack(publisher_handle_, IntPtr.Zero, pb_event_call_back_);return true;}else{return false;}} private void SetCommonOptionToPublisherSDK(){if (!IsPublisherHandleAvailable()){Debug.Log("SetCommonOptionToPublisherSDK, publisher handle with null..");return;}CameraInfo camera = cameras_[cur_sel_camera_index_];NT_PB_VideoCaptureCapability cap = camera.capabilities_[cur_sel_camera_resolutions_index_];SetVideoCaptureDeviceBaseParameter(camera.id_.ToString(), (UInt32)cap.width_, (UInt32)cap.height_);SetFrameRate((UInt32)CalBitRate(edit_key_frame_, cap.width_, cap.height_));SetVideoEncoderType(is_h264_encoder ? 1 : 2);SetVideoQualityV2(CalVideoQuality(cap.width_, cap.height_, is_h264_encoder));SetVideoMaxBitRate((CalMaxKBitRate(edit_key_frame_, cap.width_, cap.height_, false)));SetVideoKeyFrameInterval((edit_key_frame_));if (is_h264_encoder){SetVideoEncoderProfile(1);}SetVideoEncoderSpeed(CalVideoEncoderSpeed(cap.width_, cap.height_, is_h264_encoder));// 音頻相關(guān)設(shè)置SetAuidoInputDeviceId(0);SetPublisherAudioCodecType(1);SetPublisherMute(is_mute);SetInputAudioVolume(Convert.ToSingle(edit_audio_input_volume_));}預(yù)覽、停止預(yù)覽:
public bool StartPreview(){if(CheckPublisherHandleAvailable() == false)return false;video_preview_image_callback_ = new NT_PB_SDKVideoPreviewImageCallBack(SDKVideoPreviewImageCallBack);NTSmartPublisherSDK.NT_PB_SetVideoPreviewImageCallBack(publisher_handle_, (int)NTSmartPublisherDefine.NT_PB_E_IMAGE_FORMAT.NT_PB_E_IMAGE_FORMAT_RGB32, IntPtr.Zero, video_preview_image_callback_);if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_StartPreview(publisher_handle_, 0, IntPtr.Zero)){if (0 == publisher_handle_count_){NTSmartPublisherSDK.NT_PB_Close(publisher_handle_);publisher_handle_ = IntPtr.Zero;}return false;}publisher_handle_count_++;is_previewing_ = true;return true;}public void StopPreview(){if (is_previewing_ == false) return;is_previewing_ = false;publisher_handle_count_--;NTSmartPublisherSDK.NT_PB_StopPreview(publisher_handle_);if (0 == publisher_handle_count_){NTSmartPublisherSDK.NT_PB_Close(publisher_handle_);publisher_handle_ = IntPtr.Zero;}}開始推送、停止推送:
public bool StartPublisher(String url){if (CheckPublisherHandleAvailable() == false) return false;if (publisher_handle_ == IntPtr.Zero){return false;}if (!String.IsNullOrEmpty(url)){NTSmartPublisherSDK.NT_PB_SetURL(publisher_handle_, url, IntPtr.Zero);}if (NTBaseCodeDefine.NT_ERC_OK != NTSmartPublisherSDK.NT_PB_StartPublisher(publisher_handle_, IntPtr.Zero)){if (0 == publisher_handle_count_){NTSmartPublisherSDK.NT_PB_Close(publisher_handle_);publisher_handle_ = IntPtr.Zero;}is_publishing_ = false;return false;}publisher_handle_count_++;is_publishing_ = true;return true;}public void StopPublisher(){if (is_publishing_ == false) return;publisher_handle_count_--;NTSmartPublisherSDK.NT_PB_StopPublisher(publisher_handle_);if (0 == publisher_handle_count_){NTSmartPublisherSDK.NT_PB_Close(publisher_handle_);publisher_handle_ = IntPtr.Zero;}is_publishing_ = false;}相關(guān)event事件回調(diào):
private void PbEventCallBack(IntPtr handle, IntPtr user_data, UInt32 event_id,Int64 param1,Int64 param2,UInt64 param3,UInt64 param4,[MarshalAs(UnmanagedType.LPStr)] String param5,[MarshalAs(UnmanagedType.LPStr)] String param6,IntPtr param7){String event_log = "";switch (event_id){case (uint)NTSmartPublisherDefine.NT_PB_E_EVENT_ID.NT_PB_E_EVENT_ID_CONNECTING:event_log = "連接中";if (!String.IsNullOrEmpty(param5)){event_log = event_log + " url:" + param5;}break;case (uint)NTSmartPublisherDefine.NT_PB_E_EVENT_ID.NT_PB_E_EVENT_ID_CONNECTION_FAILED:event_log = "連接失敗";if (!String.IsNullOrEmpty(param5)){event_log = event_log + " url:" + param5;}break;case (uint)NTSmartPublisherDefine.NT_PB_E_EVENT_ID.NT_PB_E_EVENT_ID_CONNECTED:event_log = "已連接";if (!String.IsNullOrEmpty(param5)){event_log = event_log + " url:" + param5;}break;case (uint)NTSmartPublisherDefine.NT_PB_E_EVENT_ID.NT_PB_E_EVENT_ID_DISCONNECTED:event_log = "斷開連接";if (!String.IsNullOrEmpty(param5)){event_log = event_log + " url:" + param5;}break;default:break;}if(OnLogEventMsg != null) OnLogEventMsg.Invoke(event_id, event_log);}SmartPublishWinMono.cs 調(diào)用上述封裝的代碼即可,本地預(yù)覽的話,拿到回調(diào)的RGB數(shù)據(jù),在unity3d上層刷下即可,如下圖:
經(jīng)測(cè)試,unity3d下,RTMP推送,配合RTMP播放端,依然可以實(shí)現(xiàn)毫秒級(jí)延遲的推拉流體驗(yàn)。
總結(jié)
以上是生活随笔為你收集整理的Windows平台下如何实现Unity3D下的RTMP推送的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 花了140万留学美国,回国工资仅4500
- 下一篇: 微软推行 Windows 10 更新政策