当前位置:   article > 正文

Unity3D平台实现全景实时RTMP|RTSP流渲染_unity音视频采集渲染

unity音视频采集渲染

好多开发者的使用场景,需要在Windows特别是Android平台实现Unity3D的全景实时视频渲染,本文以Windows平台为例,简单介绍下具体实现:

如果是RTSP或RTMP流数据,实际上难点,主要在于拉取RTSP或RTMP流,解析解码,然后把解码后的YUV数据,回调到Unity层,Unity创建个Sphere,创建个材质球(Material),并把材质球挂在到Sphere即可。

本文以Windows推送端采集全景视频,编码推送到RTMP服务器,播放端拉流回调数据并在Unity渲染为例(左侧是Unity播放端,滑动鼠标,可以实现全景内容切换):

废话不多说,大概流程如下:

本文以调用我们写的RTSP、RTMP直播播放模块为例,首先是初始化模块,然后设置拉流的参数信息:

  1. public void Play(int sel)
  2. {
  3. if (videoctrl[sel].is_running)
  4. {
  5. Debug.Log("已经在播放..");
  6. return;
  7. }
  8. lock (videoctrl[sel].frame_lock_)
  9. {
  10. videoctrl[sel].cur_video_frame_ = null;
  11. }
  12. OpenPlayer(sel);
  13. if (videoctrl[sel].player_handle_ == IntPtr.Zero)
  14. return;
  15. //设置播放URL
  16. NTSmartPlayerSDK.NT_SP_SetURL(videoctrl[sel].player_handle_, videoctrl[sel].videoUrl);
  17. /* ++ 播放前参数配置可加在此处 ++ */
  18. int play_buffer_time_ = 0;
  19. NTSmartPlayerSDK.NT_SP_SetBuffer(videoctrl[sel].player_handle_, play_buffer_time_); //设置buffer time
  20. int is_using_tcp = 0; //TCP模式
  21. NTSmartPlayerSDK.NT_SP_SetRTSPTcpMode(videoctrl[sel].player_handle_, is_using_tcp);
  22. int timeout = 10;
  23. NTSmartPlayerSDK.NT_SP_SetRtspTimeout(videoctrl[sel].player_handle_, timeout);
  24. int is_auto_switch_tcp_udp = 1;
  25. NTSmartPlayerSDK.NT_SP_SetRtspAutoSwitchTcpUdp(videoctrl[sel].player_handle_, is_auto_switch_tcp_udp);
  26. Boolean is_mute_ = false;
  27. NTSmartPlayerSDK.NT_SP_SetMute(videoctrl[sel].player_handle_, is_mute_ ? 1 : 0); //是否启动播放的时候静音
  28. int is_fast_startup = 1;
  29. NTSmartPlayerSDK.NT_SP_SetFastStartup(videoctrl[sel].player_handle_, is_fast_startup); //设置快速启动模式
  30. Boolean is_low_latency_ = false;
  31. NTSmartPlayerSDK.NT_SP_SetLowLatencyMode(videoctrl[sel].player_handle_, is_low_latency_ ? 1 : 0); //设置是否启用低延迟模式
  32. //设置旋转角度(设置0, 90, 180, 270度有效,其他值无效)
  33. int rotate_degrees = 0;
  34. NTSmartPlayerSDK.NT_SP_SetRotation(videoctrl[sel].player_handle_, rotate_degrees);
  35. int volume = 100;
  36. NTSmartPlayerSDK.NT_SP_SetAudioVolume(videoctrl[sel].player_handle_, volume); //设置播放音量, 范围是[0, 100], 0是静音,100是最大音量, 默认是100
  37. // 设置上传下载报速度
  38. int is_report = 0;
  39. int report_interval = 1;
  40. NTSmartPlayerSDK.NT_SP_SetReportDownloadSpeed(videoctrl[sel].player_handle_, is_report, report_interval);
  41. /* -- 播放前参数配置可加在此处 -- */
  42. //video frame callback (YUV/RGB)
  43. videoctrl[sel].video_frame_call_back_ = new SP_SDKVideoFrameCallBack(NT_SP_SetVideoFrameCallBack);
  44. NTSmartPlayerSDK.NT_SP_SetVideoFrameCallBack(videoctrl[sel].player_handle_, (Int32)NT.NTSmartPlayerDefine.NT_SP_E_VIDEO_FRAME_FORMAT.NT_SP_E_VIDEO_FRAME_FROMAT_I420, window_handle_, videoctrl[sel].video_frame_call_back_);
  45. UInt32 flag = NTSmartPlayerSDK.NT_SP_StartPlay(videoctrl[sel].player_handle_);
  46. if (flag == DANIULIVE_RETURN_OK)
  47. {
  48. videoctrl[sel].is_need_get_frame_ = true;
  49. Debug.Log("播放成功");
  50. }
  51. else
  52. {
  53. videoctrl[sel].is_need_get_frame_ = false;
  54. Debug.LogError("播放失败");
  55. }
  56. videoctrl[sel].is_running = true;
  57. }

具体OpenPlayer()实现:

  1. private void OpenPlayer(int sel)
  2. {
  3. window_handle_ = IntPtr.Zero;
  4. if (videoctrl[sel].player_handle_ == IntPtr.Zero)
  5. {
  6. videoctrl[sel].player_handle_ = new IntPtr();
  7. UInt32 ret_open = NTSmartPlayerSDK.NT_SP_Open(out videoctrl[sel].player_handle_, window_handle_, 0, IntPtr.Zero);
  8. if (ret_open != 0)
  9. {
  10. videoctrl[sel].player_handle_ = IntPtr.Zero;
  11. Debug.LogError("调用NT_SP_Open失败..");
  12. return;
  13. }
  14. }
  15. videoctrl[sel].event_call_back_ = new SP_SDKEventCallBack(NT_SP_SDKEventCallBack);
  16. NTSmartPlayerSDK.NT_SP_SetEventCallBack(videoctrl[sel].player_handle_, window_handle_, videoctrl[sel].event_call_back_);
  17. videoctrl[sel].sdk_video_frame_call_back_ = new VideoControl.SetVideoFrameCallBack(SDKVideoFrameCallBack);
  18. videoctrl[sel].sdk_event_call_back_ = new VideoControl.SetEventCallBack(SDKEventCallBack);
  19. }

数据回调到Unity层:

  1. private void SDKVideoFrameCallBack(UInt32 status, IntPtr frame, int sel)
  2. {
  3. //这里拿到回调frame,进行相关操作
  4. NT_SP_VideoFrame video_frame = (NT_SP_VideoFrame)Marshal.PtrToStructure(frame, typeof(NT_SP_VideoFrame));
  5. VideoFrame u3d_frame = new VideoFrame();
  6. u3d_frame.width_ = video_frame.width_;
  7. u3d_frame.height_ = video_frame.height_;
  8. u3d_frame.timestamp_ = (UInt64)video_frame.timestamp_;
  9. int d_y_stride = video_frame.width_;
  10. int d_u_stride = (video_frame.width_ + 1) / 2;
  11. int d_v_stride = d_u_stride;
  12. int d_y_size = d_y_stride * video_frame.height_;
  13. int d_u_size = d_u_stride * ((video_frame.height_ + 1) / 2);
  14. int d_v_size = d_u_size;
  15. int u_v_height = ((u3d_frame.height_ + 1) / 2);
  16. u3d_frame.y_stride_ = d_y_stride;
  17. u3d_frame.u_stride_ = d_u_stride;
  18. u3d_frame.v_stride_ = d_v_stride;
  19. u3d_frame.y_data_ = new byte[d_y_size];
  20. u3d_frame.u_data_ = new byte[d_u_size];
  21. u3d_frame.v_data_ = new byte[d_v_size];
  22. CopyFramePlane(u3d_frame.y_data_, d_y_stride,
  23. video_frame.plane0_, video_frame.stride0_, u3d_frame.height_);
  24. CopyFramePlane(u3d_frame.u_data_, d_u_stride,
  25. video_frame.plane1_, video_frame.stride1_, u_v_height);
  26. CopyFramePlane(u3d_frame.v_data_, d_v_stride,
  27. video_frame.plane2_, video_frame.stride2_, u_v_height);
  28. lock (videoctrl[sel].frame_lock_ )
  29. {
  30. videoctrl[sel].cur_video_frame_ = u3d_frame;
  31. //Debug.LogError("sel: " + sel + " w:" + u3d_frame.width_ + "h:" + u3d_frame.height_);
  32. }
  33. }

Unity刷新Texture:

  1. private void UpdateYUVTexture(VideoFrame video_frame, int sel)
  2. {
  3. if (video_frame.y_data_ == null || video_frame.u_data_ == null || video_frame.v_data_ == null)
  4. {
  5. Debug.Log("video frame with null..");
  6. return;
  7. }
  8. if (videoctrl[sel].yTexture_ != null)
  9. {
  10. videoctrl[sel].yTexture_.LoadRawTextureData(video_frame.y_data_);
  11. videoctrl[sel].yTexture_.Apply();
  12. }
  13. if (videoctrl[sel].uTexture_ != null)
  14. {
  15. videoctrl[sel].uTexture_.LoadRawTextureData(video_frame.u_data_);
  16. videoctrl[sel].uTexture_.Apply();
  17. }
  18. if (videoctrl[sel].vTexture_ != null)
  19. {
  20. videoctrl[sel].vTexture_.LoadRawTextureData(video_frame.v_data_);
  21. videoctrl[sel].vTexture_.Apply();
  22. }
  23. }

全景播放,还需要考虑到鼠标或屏幕滑动,这块实现比较多,放个通用的代码参考:

  1. void Update () {
  2. if (Input.GetMouseButton(0))
  3. {
  4. if (PreMouseLPos.x <= 0)
  5. {
  6. PreMouseLPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
  7. }
  8. else
  9. {
  10. Vector3 CurMouseLPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
  11. Vector3 offset = CurMouseLPos - PreMouseLPos;
  12. Quaternion tt = Quaternion.Euler(offset);
  13. float rotationX = transform.localEulerAngles.y - tt.x * 20;
  14. rotationY += tt.y * 20;
  15. rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
  16. transform.localEulerAngles = new Vector3(rotationY, rotationX, 0);
  17. PreMouseLPos = CurMouseLPos;
  18. }
  19. }
  20. else
  21. {
  22. PreMouseLPos = new Vector3(0.0f, 0.0f, 0.0f);
  23. }
  24. }

感兴趣的开发者,可尝试看看。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/89652
推荐阅读
相关标签
  

闽ICP备14008679号