unity3D技术分享

[Windows]大牛直播SDK基于unity3d平台的rtmp

2018-06-04  本文已影响660人  3f270e490cb7

demo说明

Ø SmartU3dWinPlayer: 大牛直播SDK Unity3D Windows RTMP/RTSP直播播放端工程。

Demo界面如下:

<figure style="margin: 1em 0px;"> image

</figure>

集成说明

Ø Unity3D接口和调用demo,参见:SmartPlayerWindowsMono.cs

Ø SmartU3dAndroidPlayer\Assets\Plugins\x86和SmartU3dAndroidPlayer\Assets\Plugins\x86_64下相关库到工程:

<figure style="margin: 1em 0px;"> image

</figure>

n nt_base_code_define.cs加入到工程;

n smart_player_define.cs 加入工程;

n smart_player_sdk.cs加入工程。

Ø 如需集成到自己系统测试,请用大牛直播SDK的app name, windows测试app name:SmartPlayer .

调用时序(V2)

Windows调用时序,可参照Windows C#调用SDK调用说明(参见“视沃科技-Windows-SDK集成说明.pdf”),此处不再赘述。

调用流程:

1. 开始播放的时候,调用OpenPlayer(),完成PlayerInit和PlayerOpen操作,并设置eventcallback和videoframecallback,并完成相关参数配置,设置YUV回调输出,调用PlayerStart,完成播放端的启动:

    public void Play()
    {
        if (is_running)
        {
            Debug.Log("已经在播放。。");
            return;
        }

        lock (frame_lock_)
        {
            cur_video_frame_ = null;
        }

        //获取输入框的url
        string url = input_url_.text.Trim();

        if (!url.StartsWith("rtmp://") && !url.StartsWith("rtsp://"))
        {
            videoUrl = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
        }
        else
        {
            videoUrl = url;
        }

        OpenPlayer();

        if (player_handle_ == IntPtr.Zero)
            return;

        //设置播放URL
        NTSmartPlayerSDK.NT_SP_SetURL(player_handle_, videoUrl);

        /* ++ 播放前参数配置可加在此处 ++ */

        NTSmartPlayerSDK.NT_SP_SetBuffer(player_handle_, play_buffer_time_);        //设置buffer time

        int is_using_tcp = 1;        //TCP模式
        NTSmartPlayerSDK.NT_SP_SetRTSPTcpMode(player_handle_, is_using_tcp);

        NTSmartPlayerSDK.NT_SP_SetMute(player_handle_, is_mute_ ? 1 : 0);                    //是否启动播放的时候静音

        int is_fast_startup = 1;
        NTSmartPlayerSDK.NT_SP_SetFastStartup(player_handle_, is_fast_startup);              //设置快速启动模式

        NTSmartPlayerSDK.NT_SP_SetLowLatencyMode(player_handle_, is_low_latency_ ? 1 : 0);    //设置是否启用低延迟模式

        //设置旋转角度(设置0, 90, 180, 270度有效,其他值无效)
        NTSmartPlayerSDK.NT_SP_SetRotation(player_handle_, rotate_degrees);

        // 设置上传下载报速度
        int is_report = 0;
        int report_interval = 1;
        NTSmartPlayerSDK.NT_SP_SetReportDownloadSpeed(player_handle_, is_report, report_interval);
        /* -- 播放前参数配置可加在此处 -- */

        //video frame callback (YUV/RGB)
        video_frame_call_back_ = new SP_SDKVideoFrameCallBack(NT_SP_SetVideoFrameCallBack);
        NTSmartPlayerSDK.NT_SP_SetVideoFrameCallBack(player_handle_, (Int32)NT.NTSmartPlayerDefine.NT_SP_E_VIDEO_FRAME_FORMAT.NT_SP_E_VIDEO_FRAME_FROMAT_I420, window_handle_, video_frame_call_back_);

        UInt32 flag = NTSmartPlayerSDK.NT_SP_StartPlay(player_handle_);

        if (flag == DANIULIVE_RETURN_OK)
        {
            is_need_get_frame_ = true;
            Debug.Log("播放成功");
        }
        else
        {
            is_need_get_frame_ = false;
            Debug.LogError("播放失败");
        }

        is_running = true;
    }

    private void OpenPlayer()
    {
        window_handle_ = IntPtr.Zero;

        UInt32 isInited = NT.NTSmartPlayerSDK.NT_SP_Init(0, IntPtr.Zero);

        if (isInited != 0)
        {
            Debug.LogError("调用NT_SP_Init失败.." + isInited.ToString());
            return;
        }

        if (player_handle_ == IntPtr.Zero)
        {
            player_handle_ = new IntPtr();
            UInt32 ret_open = NTSmartPlayerSDK.NT_SP_Open(out player_handle_, window_handle_, 0, IntPtr.Zero);
            if (ret_open != 0)
            {
                player_handle_ = IntPtr.Zero;
                Debug.LogError("调用NT_SP_Open失败..");
                return;
            }
        }

        event_call_back_ = new SP_SDKEventCallBack(NT_SP_SDKEventCallBack);
        NTSmartPlayerSDK.NT_SP_SetEventCallBack(player_handle_, window_handle_, event_call_back_);

        // 异步回调功能
        sdk_video_frame_call_back_ = new SetVideoFrameCallBack(SDKVideoFrameCallBack);
        sdk_event_call_back_ = new SetEventCallBack(SDKEventCallBack);
    }

2. Video frame实时处理并绘制:

开始播放后,daniulive直播播放端SDK回调yuv数据及相关信息,unity3d获取到数据信息后,调用InitYUVTexture(),完成初始化工作,调用UpdateYUVTexture()实现数据实时刷新,当数据信息发生变化时,会二次调用InitYUVTexture(),完成初始化。

3. 停止播放:

    private void ClosePlayer()
    {
        is_need_get_frame_ = false;
        is_need_init_texture_ = false;

        if (player_handle_ == IntPtr.Zero)
        {
            return;
        }

        UInt32 flag = NTSmartPlayerSDK.NT_SP_StopPlay(player_handle_);
        if (flag == DANIULIVE_RETURN_OK)
        {
            Debug.Log("停止成功");
        }
        else
        {
            Debug.LogError("停止失败");
        }

        NTSmartPlayerSDK.NT_SP_UnInit();

        player_handle_ = IntPtr.Zero;

        is_running = false;
    }

Event回调

    private void SDKEventCallBack(UInt32 event_id,
        Int64 param1,
        Int64 param2,
        UInt64 param3,
        [MarshalAs(UnmanagedType.LPStr)] String param4,
        [MarshalAs(UnmanagedType.LPStr)] String param5,
        IntPtr param6)
    {
        if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_PLAYBACK_REACH_EOS == event_id)
        {
            //本地flv文件播放之用
            return;
        }
        else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_RECORDER_REACH_EOS == event_id)
        {
            NTSmartPlayerSDK.NT_SP_StopRecorder(player_handle_);
            return;
        }
        else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_PULLSTREAM_REACH_EOS == event_id)
        {
            if (player_handle_ != IntPtr.Zero)
            {
                NTSmartPlayerSDK.NT_SP_StopPullStream(player_handle_);
            }
            return;
        }
        if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTING == event_id
            || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED == event_id
            || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTED == event_id
            || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DISCONNECTED == event_id)
        {
            connection_status_ = event_id;
        }
        if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_START_BUFFERING == event_id
            || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == event_id
            || (UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_STOP_BUFFERING == event_id)
        {
            buffer_status_ = event_id;
            if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == event_id)
            {
                buffer_percent_ = (Int32)param1;
            }
        }
        if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DOWNLOAD_SPEED == event_id)
        {
            download_speed_ = (Int32)param1;
        }

        String t_show_str = "";
        if (connection_status_ != 0)
        {
            t_show_str += "链接状态: ";
            if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTING == connection_status_)
            {
                t_show_str += "链接中";
            }
            else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTION_FAILED == connection_status_)
            {
                t_show_str += "链接失败";
            }
            else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_CONNECTED == connection_status_)
            {
                t_show_str += "链接成功";
            }
            else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_DISCONNECTED == connection_status_)
            {
                t_show_str += "链接断开";
            }
        }

        if (download_speed_ != -1)
        {
            String ss = "  下载速度: " + (download_speed_ * 8 / 1000).ToString() + "kbps " + (download_speed_ / 1024).ToString() + "KB/s";
            t_show_str += ss;
        }

        if (buffer_status_ != 0)
        {
            t_show_str += "  缓冲状态: ";
            if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_START_BUFFERING == buffer_status_)
            {
                t_show_str += "开始缓冲";
            }
            else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_BUFFERING == buffer_status_)
            {
                String ss = "缓冲中 " + buffer_percent_.ToString() + "%";
                t_show_str += ss;
            }
            else if ((UInt32)NTSmartPlayerDefine.NT_SP_E_EVENT_ID.NT_SP_E_EVENT_ID_STOP_BUFFERING == buffer_status_)
            {
                t_show_str += "结束缓冲";
            }
        }
        Debug.LogError(t_show_str);
    }

SDK调用说明:

http://web1712221406366.gz01.bdysite.com/wp-content/uploads/2018/06/%E8%A7%86%E6%B2%83%E7%A7%91%E6%8A%80-%E5%A4%A7%E7%89%9B%E7%9B%B4%E6%92%ADSDKV2Unity3D%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E1.1.pdf​web1712221406366.gz01.bdysite.com

Windows播放端app下载:

http://web1712221406366.gz01.bdysite.com/wp-content/uploads/2018/06/Daniulive-Windows-Unity3dPlayer-2018-06-04.zip​web1712221406366.gz01.bdysite.com

上一篇 下一篇

猜你喜欢

热点阅读