示例#1
0
        // Start is called before the first frame update
        void Start()
        {
            if (m_SourceTexture == null)
            {
                return;
            }

            try
            {
                m_Server  = new RtspServer(m_Port, m_Username, m_Password);
                m_Encoder = new H264Encoder();
                NativeList <byte> spsNalu = new NativeList <byte>(Allocator.TempJob);
                NativeList <byte> ppsNalu = new NativeList <byte>(Allocator.TempJob);
                var startJob = m_Encoder.Start((uint)m_SourceTexture.width, (uint)m_SourceTexture.height,
                                               (uint)m_Framerate, ref spsNalu, ref ppsNalu);
                m_JobQueue.Enqueue(new JobItem {
                    job = startJob, spsNalu = spsNalu, ppsNalu = ppsNalu
                });

                if (String.IsNullOrEmpty(m_Username) != String.IsNullOrEmpty(m_Password))
                {
                    Debug.LogError("Both username and password should be specified, not just one of them.");
                }
                else if (!String.IsNullOrEmpty(m_Username) && !String.IsNullOrEmpty(m_Password))
                {
                    Debug.Log($"Watch the output on rtsp://{m_Username}:{m_Password}@{GetMyIPAddress()}:{m_Port}");
                }
                else
                {
                    Debug.Log($"Watch the output on rtsp://{GetMyIPAddress()}:{m_Port}");
                }

                var shaderName      = "Video/RGBToNV12";
                var rgbToNV12Shader = Shader.Find(shaderName);
                if (rgbToNV12Shader == null)
                {
                    Debug.LogError("Could not find shader " + shaderName);
                }
                m_RGBToNV12Material = new Material(rgbToNV12Shader);
                m_NV12Texture       = new RenderTexture(m_SourceTexture.width, m_SourceTexture.height * 3 / 2, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear);
                m_NV12Texture.Create();
            }
            catch (Exception e)
            {
                Debug.LogError("Error: Could not start server: " + e);
            }
        }
示例#2
0
        void OnDestroy()
        {
            m_Server?.StopListen();
            m_Server?.Dispose();
            while (m_JobQueue.Count > 0)
            {
                CompleteJobs();
            }
            m_Server = null;
            m_Encoder.Dispose();
            if (m_NV12Texture != null)
            {
                Destroy(m_NV12Texture);
                m_NV12Texture = null;
            }

            if (m_RGBToNV12Material != null)
            {
                Destroy(m_RGBToNV12Material);
                m_RGBToNV12Material = null;
            }
        }