Android實(shí)現(xiàn)Unity3D下RTMP推送的示例
關(guān)于屏幕采集,有兩種方案:
1. 直接封裝Android原生的屏幕采集工程,在unity提供接口,拿到屏幕權(quán)限后,獲取屏幕數(shù)據(jù)并推送;
2. 如果只需要拿到Unity的窗體或攝像機(jī)數(shù)據(jù)推出去,可在Unity下獲取到需要推送的原始數(shù)據(jù),然后封裝原生的RTMP推流接口,調(diào)用原生SDK實(shí)現(xiàn)數(shù)據(jù)推送,這種做法的好處是,可以自定義需要采集的數(shù)據(jù)內(nèi)容,只要按照原生SDK提供的接口,完成數(shù)據(jù)對(duì)接即可,具體實(shí)現(xiàn)參看本文。
本文以Android平臺(tái)為例,介紹下Unity環(huán)境下的Android平臺(tái)RTMP推流,數(shù)據(jù)采集在Unity完成,數(shù)據(jù)編碼推送,調(diào)用大牛直播SDK(官方)Android平臺(tái)RTMP直播推送SDK原生庫(kù)對(duì)外二次封裝的接口,高效率的實(shí)現(xiàn)RTMP推送。廢話(huà)多說(shuō),先上圖看效果。
下圖系A(chǔ)ndroid平臺(tái)Unity環(huán)境下采集屏幕,編碼推送到RTMP服務(wù)器,然后Windows平臺(tái)播放器拉取RTMP流播放,為了方便看到延遲效果,特地在Android端的Unity窗口顯示了當(dāng)前時(shí)間,可以看到,整體延遲在毫秒級(jí):
數(shù)據(jù)采集推送
unity數(shù)據(jù)采集相對(duì)簡(jiǎn)單,可以很輕松的拿到RGB24的數(shù)據(jù):
texture_ = new Texture2D(video_width_, video_height_, TextureFormat.RGB24, false); texture_.ReadPixels(new Rect(0, 0, video_width_, video_height_), 0, 0, false); texture_.Apply();
然后通過(guò)調(diào)用texture_.GetRawTextureData(); 獲取到數(shù)據(jù)即可。
拿到數(shù)據(jù)后,調(diào)用原生SDK封裝的NT_PB_U3D_OnCaptureVideoRGB24PtrData()接口,完成數(shù)據(jù)投遞。
簡(jiǎn)單調(diào)用流程
private void Start() { game_object_ = this.gameObject.name; AndroidJavaClass android_class = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); java_obj_cur_activity_ = android_class.GetStatic<AndroidJavaObject>("currentActivity"); pusher_obj_ = new AndroidJavaObject("com.daniulive.smartpublisher.SmartPublisherUnity3d"); NT_PB_U3D_Init(); //NT_U3D_SetSDKClientKey("", "", 0); btn_encode_mode_.onClick.AddListener(OnEncodeModeBtnClicked); btn_pusher_.onClick.AddListener(OnPusherBtnClicked); btn_mute_.onClick.AddListener(OnMuteBtnClicked); }
完成接口初始化后,調(diào)用Push()接口
public void Push() { if (is_running) { Debug.Log("已推送.."); return; } if (texture_ != null) { UnityEngine.Object.Destroy(texture_); texture_ = null; } video_width_ = Screen.width; video_height_ = Screen.height; scale_width_ = (video_width_ + 1) / 2; scale_height_ = (video_height_ + 1) / 2; if (scale_width_ % 2 != 0) { scale_width_ = scale_width_ + 1; } if (scale_height_ % 2 != 0) { scale_height_ = scale_height_ + 1; } texture_ = new Texture2D(video_width_, video_height_, TextureFormat.RGB24, false); //獲取輸入框的url string url = input_url_.text.Trim(); if (!url.StartsWith("rtmp://")) { push_url_ = "rtmp://192.168.0.199:1935/hls/stream1"; } else { push_url_ = url; } OpenPusher(); if (pusher_handle_ == 0) return; NT_PB_U3D_Set_Game_Object(pusher_handle_, game_object_); /* ++ 推送前參數(shù)配置可加在此處 ++ */ InitAndSetConfig(); NT_PB_U3D_SetPushUrl(pusher_handle_, push_url_); /* -- 推送前參數(shù)配置可加在此處 -- */ int flag = NT_PB_U3D_StartPublisher(pusher_handle_); if (flag == DANIULIVE_RETURN_OK) { Debug.Log("推送成功.."); } else { Debug.LogError("推送失敗.."); } is_running = true; }
調(diào)用OpenPusher()
private void OpenPusher() { if ( java_obj_cur_activity_ == null ) { Debug.LogError("getApplicationContext is null"); return; } int audio_opt = 1; int video_opt = 1; pusher_handle_ = NT_PB_U3D_Open(audio_opt, video_opt, video_width_, video_height_); if (pusher_handle_ != 0) Debug.Log("NT_PB_U3D_Open success"); else Debug.LogError("NT_PB_U3D_Open fail"); }
InitAndSetConfig()
private void InitAndSetConfig() { if (is_hw_encode_) { int h264HWKbps = setHardwareEncoderKbps(true, video_width_, video_height_); Debug.Log("h264HWKbps: " + h264HWKbps); int isSupportH264HWEncoder = NT_PB_U3D_SetVideoHWEncoder(pusher_handle_, h264HWKbps); if (isSupportH264HWEncoder == 0) { Debug.Log("Great, it supports h.264 hardware encoder!"); } } else { if (is_sw_vbr_mode_) //H.264 software encoder { int is_enable_vbr = 1; int video_quality = CalVideoQuality(video_width_, video_height_, true); int vbr_max_bitrate = CalVbrMaxKBitRate(video_width_, video_height_); NT_PB_U3D_SetSwVBRMode(pusher_handle_, is_enable_vbr, video_quality, vbr_max_bitrate); //NT_PB_U3D_SetSWVideoEncoderSpeed(pusher_handle_, 2); } } NT_PB_U3D_SetAudioCodecType(pusher_handle_, 1); NT_PB_U3D_SetFPS(pusher_handle_, 25); NT_PB_U3D_SetGopInterval(pusher_handle_, 25*2); //NT_PB_U3D_SetSWVideoBitRate(pusher_handle_, 600, 1200); }
ClosePusher()
private void ClosePusher() { if (texture_ != null) { UnityEngine.Object.Destroy(texture_); texture_ = null; } int flag = NT_PB_U3D_StopPublisher(pusher_handle_); if (flag == DANIULIVE_RETURN_OK) { Debug.Log("停止成功.."); } else { Debug.LogError("停止失敗.."); } flag = NT_PB_U3D_Close(pusher_handle_); if (flag == DANIULIVE_RETURN_OK) { Debug.Log("關(guān)閉成功.."); } else { Debug.LogError("關(guān)閉失敗.."); } pusher_handle_ = 0; NT_PB_U3D_UnInit(); is_running = false; }
為了便于測(cè)試,Update()刷新下當(dāng)前時(shí)間:
private void Update() { //獲取當(dāng)前時(shí)間 hour = DateTime.Now.Hour; minute = DateTime.Now.Minute; millisecond = DateTime.Now.Millisecond; second = DateTime.Now.Second; year = DateTime.Now.Year; month = DateTime.Now.Month; day = DateTime.Now.Day; GameObject.Find("Canvas/Panel/LableText").GetComponent<Text>().text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2} " + "{4:D4}/{5:D2}/{6:D2}", hour, minute, second, millisecond, year, month, day); }
相關(guān)Event處理
public void onNTSmartEvent(string param) { if (!param.Contains(",")) { Debug.Log("[onNTSmartEvent] android傳遞參數(shù)錯(cuò)誤"); return; } string[] strs = param.Split(','); string player_handle =strs[0]; string code = strs[1]; string param1 = strs[2]; string param2 = strs[3]; string param3 = strs[4]; string param4 = strs[5]; Debug.Log("[onNTSmartEvent] code: 0x" + Convert.ToString(Convert.ToInt32(code), 16)); String publisher_event = ""; switch (Convert.ToInt32(code)) { case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_STARTED: publisher_event = "開(kāi)始.."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTING: publisher_event = "連接中.."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTION_FAILED: publisher_event = "連接失敗.."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_CONNECTED: publisher_event = "連接成功.."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_DISCONNECTED: publisher_event = "連接斷開(kāi).."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_STOP: publisher_event = "關(guān)閉.."; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_RECORDER_START_NEW_FILE: publisher_event = "開(kāi)始一個(gè)新的錄像文件 : " + param3; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_ONE_RECORDER_FILE_FINISHED: publisher_event = "已生成一個(gè)錄像文件 : " + param3; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_SEND_DELAY: publisher_event = "發(fā)送時(shí)延: " + param1 + " 幀數(shù):" + param2; break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_CAPTURE_IMAGE: publisher_event = "快照: " + param1 + " 路徑:" + param3; if (Convert.ToInt32(param1) == 0) { publisher_event = publisher_event + "截取快照成功.."; } else { publisher_event = publisher_event + "截取快照失敗.."; } break; case EVENTID.EVENT_DANIULIVE_ERC_PUBLISHER_RTSP_URL: publisher_event = "RTSP服務(wù)URL: " + param3; break; case EVENTID.EVENT_DANIULIVE_ERC_PUSH_RTSP_SERVER_RESPONSE_STATUS_CODE: publisher_event = "RTSP status code received, codeID: " + param1 + ", RTSP URL: " + param3; break; case EVENTID.EVENT_DANIULIVE_ERC_PUSH_RTSP_SERVER_NOT_SUPPORT: publisher_event = "服務(wù)器不支持RTSP推送, 推送的RTSP URL: " + param3; break; } Debug.Log(publisher_event); }
總結(jié)
通過(guò)以上流程,可以實(shí)現(xiàn)Unity環(huán)境下屏幕或攝像機(jī)數(shù)據(jù),毫秒級(jí)體驗(yàn)的RTMP推送和播放,感興趣的開(kāi)發(fā)者可酌情參考。
以上就是Android實(shí)現(xiàn)Unity3D下RTMP推送的示例的詳細(xì)內(nèi)容,更多關(guān)于Android實(shí)現(xiàn)Unity3D下RTMP推送的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android編程基于自定義View實(shí)現(xiàn)絢麗的圓形進(jìn)度條功能示例
這篇文章主要介紹了Android編程基于自定義View實(shí)現(xiàn)絢麗的圓形進(jìn)度條功能,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義view實(shí)現(xiàn)圓形進(jìn)度條的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-01-01android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單進(jìn)度條ProgressBar效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android自定義滑動(dòng)接聽(tīng)電話(huà)控件組實(shí)例
這篇文章主要介紹了Android自定義滑動(dòng)接聽(tīng)電話(huà)控件組,接聽(tīng)電話(huà)可以左右滑動(dòng),感興趣的小伙伴們可以參考一下。2016-10-10Android自定義View繪制貝塞爾曲線(xiàn)的方法
這篇文章主要為大家詳細(xì)介紹了Android自定義View繪制貝塞爾曲線(xiàn)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Android中EditText如何去除邊框添加下劃線(xiàn)
這篇文章主要介紹了Android中EditText如何去除邊框添加下劃線(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-02-02解決EditText不顯示光標(biāo)的三種方法(總結(jié))
下面小編就為大家?guī)?lái)一篇解決EditText不顯示光標(biāo)的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Android App中實(shí)現(xiàn)向右滑動(dòng)銷(xiāo)毀功能的要點(diǎn)解析
這篇文章主要介紹了Android應(yīng)用中實(shí)現(xiàn)向右滑動(dòng)銷(xiāo)毀條目功能的要點(diǎn)解析,有些類(lèi)似于iOS App中的滑動(dòng)頁(yè)面刪除效果,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)加載數(shù)據(jù)的方法,實(shí)例分析了Android通過(guò)滑動(dòng)實(shí)現(xiàn)動(dòng)態(tài)加載數(shù)據(jù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07超簡(jiǎn)單的幾行代碼搞定Android底部導(dǎo)航欄功能
這篇文章主要介紹了超簡(jiǎn)單的幾行代碼搞定Android底部導(dǎo)航欄功能,需要的朋友可以參考下2018-03-03