Beispiel #1
0
        /// <summary>
        /// If the user is logged in and ready to broadcast this will kick off an asynchronous test of bandwidth to all ingest servers.
        /// This will in turn fill in the bitrate fields in the ingest list.
        /// </summary>
        /// <returns>The IngestTester instance that is valid during the test.</returns>
        public virtual IngestTester StartIngestTest()
        {
            if (!this.IsReadyToBroadcast || m_IngestList == null)
            {
                return(null);
            }

            if (this.IsIngestTesting)
            {
                return(null);
            }

            m_IngestTester = new IngestTester(m_Stream, m_IngestList);
            m_IngestTester.Start();

            SetBroadcastState(BroadcastState.IngestTesting);

            return(m_IngestTester);
        }
Beispiel #2
0
        /// <summary>
        /// Updates the internals of the controller.  This should be called at least as frequently as the desired broadcast framerate.
        /// Asynchronous results will be fired from inside this function.
        /// </summary>
        public virtual void Update()
        {
            // for stress testing to make sure memory is being passed around properly
            //GC.Collect();

            if (m_Stream == null || !m_SdkInitialized)
            {
                return;
            }

            ErrorCode ret = m_Stream.PollTasks();

            CheckError(ret);

            // update the ingest tester
            if (this.IsIngestTesting)
            {
                m_IngestTester.Update();

                // all done testing
                if (m_IngestTester.IsDone)
                {
                    m_IngestTester = null;
                    SetBroadcastState(BroadcastState.ReadyToBroadcast);
                }
            }

            switch (m_BroadcastState)
            {
            // Kick off an authentication request
            case BroadcastState.Authenticated:
            {
                SetBroadcastState(BroadcastState.LoggingIn);

                ret = m_Stream.Login(m_AuthToken);
                if (Error.Failed(ret))
                {
                    string err = Error.GetString(ret);
                    ReportError(string.Format("Error in TTV_Login: {0}\n", err));
                }
                break;
            }

            // Login
            case BroadcastState.LoggedIn:
            {
                SetBroadcastState(BroadcastState.FindingIngestServer);

                ret = m_Stream.GetIngestServers(m_AuthToken);
                if (Error.Failed(ret))
                {
                    SetBroadcastState(BroadcastState.LoggedIn);

                    string err = Error.GetString(ret);
                    ReportError(string.Format("Error in TTV_GetIngestServers: {0}\n", err));
                }
                break;
            }

            // Ready to stream
            case BroadcastState.ReceivedIngestServers:
            {
                SetBroadcastState(BroadcastState.ReadyToBroadcast);

                // Kick off requests for the user and stream information that aren't 100% essential to be ready before streaming starts
                ret = m_Stream.GetUserInfo(m_AuthToken);
                if (Error.Failed(ret))
                {
                    string err = Error.GetString(ret);
                    ReportError(string.Format("Error in TTV_GetUserInfo: {0}\n", err));
                }

                UpdateStreamInfo();

                ret = m_Stream.GetArchivingState(m_AuthToken);
                if (Error.Failed(ret))
                {
                    string err = Error.GetString(ret);
                    ReportError(string.Format("Error in TTV_GetArchivingState: {0}\n", err));
                }

                break;
            }

            // Waiting for the start/stop callback
            case BroadcastState.Starting:
            case BroadcastState.Stopping:
            {
                break;
            }

            // No action required
            case BroadcastState.FindingIngestServer:
            case BroadcastState.Authenticating:
            case BroadcastState.Initialized:
            case BroadcastState.Uninitialized:
            case BroadcastState.IngestTesting:
            {
                break;
            }

            case BroadcastState.Broadcasting:
            case BroadcastState.Paused:
            {
                UpdateStreamInfo();
                break;
            }

            default:
            {
                break;
            }
            }
        }
        /// <summary>
        /// If the user is logged in and ready to broadcast this will kick off an asynchronous test of bandwidth to all ingest servers.
        /// This will in turn fill in the bitrate fields in the ingest list.
        /// </summary>
        /// <returns>The IngestTester instance that is valid during the test.</returns>
        public virtual IngestTester StartIngestTest()
        {
            if (!this.IsReadyToBroadcast || m_IngestList == null)
            {
                return null;
            }

            if (this.IsIngestTesting)
            {
                return null;
            }

            m_IngestTester = new IngestTester(m_Stream, m_IngestList);
            m_IngestTester.Start();

            SetBroadcastState(BroadcastState.IngestTesting);

            return m_IngestTester;
        }
        /// <summary>
        /// Updates the internals of the controller.  This should be called at least as frequently as the desired broadcast framerate.
        /// Asynchronous results will be fired from inside this function.
        /// </summary>
        public virtual void Update()
        {
            // for stress testing to make sure memory is being passed around properly
            //GC.Collect();

            if (m_Stream == null || !m_SdkInitialized)
            {
                return;
            }

            ErrorCode ret = m_Stream.PollTasks();
            CheckError(ret);

            // update the ingest tester
            if (this.IsIngestTesting)
            {
                m_IngestTester.Update();

                // all done testing
                if (m_IngestTester.IsDone)
                {
                    m_IngestTester = null;
                    SetBroadcastState(BroadcastState.ReadyToBroadcast);
                }
            }

            switch (m_BroadcastState)
            {
                // Kick off an authentication request
                case BroadcastState.Authenticated:
                {
                    SetBroadcastState(BroadcastState.LoggingIn);

                    ret = m_Stream.Login(m_AuthToken);
                    if (Error.Failed(ret))
                    {
                        string err = Error.GetString(ret);
                        ReportError(string.Format("Error in TTV_Login: {0}\n", err));
                    }
                    break;
                }
                // Login
                case BroadcastState.LoggedIn:
                {
                    SetBroadcastState(BroadcastState.FindingIngestServer);

                    ret = m_Stream.GetIngestServers(m_AuthToken);
                    if (Error.Failed(ret))
                    {
                        SetBroadcastState(BroadcastState.LoggedIn);

                        string err = Error.GetString(ret);
                        ReportError(string.Format("Error in TTV_GetIngestServers: {0}\n", err));
                    }
                    break;
                }
                // Ready to stream
                case BroadcastState.ReceivedIngestServers:
                {
                    SetBroadcastState(BroadcastState.ReadyToBroadcast);

                    // Kick off requests for the user and stream information that aren't 100% essential to be ready before streaming starts
                    ret = m_Stream.GetUserInfo(m_AuthToken);
                    if (Error.Failed(ret))
                    {
                        string err = Error.GetString(ret);
                        ReportError(string.Format("Error in TTV_GetUserInfo: {0}\n", err));
                    }

                    UpdateStreamInfo();

                    ret = m_Stream.GetArchivingState(m_AuthToken);
                    if (Error.Failed(ret))
                    {
                        string err = Error.GetString(ret);
                        ReportError(string.Format("Error in TTV_GetArchivingState: {0}\n", err));
                    }

                    break;
                }
                // Waiting for the start/stop callback
                case BroadcastState.Starting:
                case BroadcastState.Stopping:
                {
                    break;
                }
                // No action required
                case BroadcastState.FindingIngestServer:
                case BroadcastState.Authenticating:
                case BroadcastState.Initialized:
                case BroadcastState.Uninitialized:
                case BroadcastState.IngestTesting:
                {
                    break;
                }
                case BroadcastState.Broadcasting:
                case BroadcastState.Paused:
                {
                    UpdateStreamInfo();
                    break;
                }
                default:
                {
                    break;
                }
            }
        }
Beispiel #5
0
    protected void OnIngestTesterStateChanged(IngestTester source, IngestTester.TestState state)
    {
        switch (state)
        {
            case IngestTester.TestState.Finished:
            case IngestTester.TestState.Cancelled:
                {
                    m_IngestTester.OnTestStateChanged -= OnIngestTesterStateChanged;

                    // use the best server based on kbps
                    m_BroadcastController.IngestServer = m_BroadcastController.IngestList.BestServer;

                    if (m_BroadcastController.IngestServer != null)
                    {
                        DebugOverlay.Instance.AddViewportText("Selected Best Ingest Server: " + m_BroadcastController.IngestServer.ServerName, 2);
                    }

                    break;
                }
        }
    }
Beispiel #6
0
    void OnGUI()
    {
        if (m_AutoBroadcast)
        {
            return;
        }

        int width = 150;
        int height = 30;
        int top = 70;
        int i = 0;

        bool init = false;
        bool shutdown = false;
        bool requestAuthToken = false;
        bool setAuthToken = false;
        bool logOut = false;
        bool start = false;
        bool stop = false;
        bool pause = false;
        bool resume = false;
        bool runCommercial = false;
        bool startIngestTest = false;
        bool skipIngestServer = false;
        bool cancelIngestTest = false;
        bool gamePause = false;
        bool gameResume = false;
        bool loadNextScene = false;
        bool enterFullScreen = false;
        bool exitFullScreen = false;
        bool toggleSound = false;

        if (m_BroadcastController.IsInitialized)
        {
            if (m_BroadcastController.IsBroadcasting)
            {
                runCommercial = GUI.Button(new Rect(10, top + height * i++, width, height), "Run Commercial");

                if (m_BroadcastController.IsPaused)
                {
                    resume = GUI.Button(new Rect(10, top + height * i++, width, height), "Resume");
                }
                else
                {
                    pause = GUI.Button(new Rect(10, top + height * i++, width, height), "Pause");
                }

                stop = GUI.Button(new Rect(10, top + height * i++, width, height), "Stop");
            }
            else
            {
                if (m_BroadcastController.IsLoggedIn)
                {
                    if (m_BroadcastController.IsReadyToBroadcast)
                    {
                        start = GUI.Button(new Rect(10, top + height * i++, width, height), "Start");
                        startIngestTest = GUI.Button(new Rect(10, top + height * i++, width, height), "Start Ingest Test");
                        logOut = GUI.Button(new Rect(10, top + height * i++, width, height), "Log Out");
                    }
                    else if (m_BroadcastController.IsIngestTesting)
                    {
                        skipIngestServer = GUI.Button(new Rect(10, top + height * i++, width, height), "Skip Server");
                        cancelIngestTest = GUI.Button(new Rect(10, top + height * i++, width, height), "Cancel Ingest Test");
                    }
                }
                else
                {
                    requestAuthToken = GUI.Button(new Rect(10, top + height * i++, width, height), "Request Auth Token");
                    setAuthToken = GUI.Button(new Rect(10, top + height * i++, width, height), "Use Existing Auth Token");
                }

                if (!m_BroadcastController.IsIngestTesting)
                {
                    shutdown = GUI.Button(new Rect(10, top + height * i++, width, height), "Shutdown");
                }
            }
        }
        else
        {
            init = GUI.Button(new Rect(10, top + height * i++, width, height), "Init");
        }

        if (m_GamePaused)
        {
            gameResume = GUI.Button(new Rect(10, top + height * i++, width, height), "Resume Game");
        }
        else
        {
            gamePause = GUI.Button(new Rect(10, top + height * i++, width, height), "Pause Game");
        }

        loadNextScene = GUI.Button(new Rect(10, top + height * i++, width, height), "Load Next Scene");

        #if !UNITY_IPHONE
        if (Screen.fullScreen)
        {
            exitFullScreen = GUI.Button(new Rect(10, top + height * i++, width, height), "Exit Full Screen");
        }
        else
        {
            enterFullScreen = GUI.Button(new Rect(10, top + height * i++, width, height), "Go Full Screen");
        }
        #endif

        // Find an AudioSource
        AudioSource audioSource = null;
        UnityEngine.Object[] arr = GameObject.FindObjectsOfType(typeof(AudioSource));
        if (arr != null && arr.Length > 0)
        {
            audioSource = arr[0] as AudioSource;
        }

        if (audioSource != null)
        {
            if (audioSource.isPlaying)
            {
                toggleSound = GUI.Button(new Rect(10, top + height * i++, width, height), "Stop Sound");
            }
            else
            {
                toggleSound = GUI.Button(new Rect(10, top + height * i++, width, height), "Play Sound");
            }
        }

        if (init)
        {
            if (!m_BroadcastController.Initialize())
            {
                DebugOverlay.Instance.AddViewportText("Error initializing Twitch", 2);
            }
        }
        else if (shutdown)
        {
            m_BroadcastController.Shutdown();
        }
        else if (start)
        {
            StartBroadcasting();
        }
        else if (requestAuthToken)
        {
            m_BroadcastController.RequestAuthToken(m_UserName, m_Password);
        }
        else if (setAuthToken)
        {
            string token = PlayerPrefs.GetString(s_TwitchAuthTokenKey, null);
            string username = PlayerPrefs.GetString(s_TwitchUserNameKey, null);

            if (token != null && username != null)
            {
                m_BroadcastController.SetAuthToken(username, new AuthToken(token));
            }
        }
        else if (logOut)
        {
            m_BroadcastController.Logout();
        }
        else if (stop)
        {
            m_BroadcastController.StopBroadcasting();
        }
        else if (pause)
        {
            m_BroadcastController.PauseBroadcasting();
        }
        else if (resume)
        {
            m_BroadcastController.ResumeBroadcasting();
        }
        else if (runCommercial)
        {
            m_BroadcastController.RunCommercial();
        }
        else if (startIngestTest)
        {
            m_IngestTester = m_BroadcastController.StartIngestTest();
            if (m_IngestTester != null)
            {
                m_IngestTester.OnTestStateChanged += OnIngestTesterStateChanged;
            }
        }
        else if (skipIngestServer)
        {
            m_BroadcastController.IngestTester.SkipCurrentServer();
        }
        else if (cancelIngestTest)
        {
            m_BroadcastController.IngestTester.Cancel();
        }

        if (gamePause)
        {
            m_GamePaused = true;
            Time.timeScale = 0;
        }
        else if (gameResume)
        {
            m_GamePaused = false;
            Time.timeScale = 1;
        }
        else if (loadNextScene)
        {
            string scene = "SampleScene";
            if (Application.loadedLevelName == "SampleScene")
            {
                scene = "SampleScene2";
            }
            Application.LoadLevel(scene);
        }
        else if (enterFullScreen)
        {
            Screen.SetResolution(1080, 1024, true);
        }
        else if (exitFullScreen)
        {
            Screen.SetResolution(1080, 1024, false);
        }
        else if (toggleSound)
        {
            if (audioSource.isPlaying)
            {
                audioSource.Stop();
            }
            else
            {
                audioSource.Play();
            }
        }

        DebugOverlay.Instance.AddViewportText("Audio sample rate: " + AudioSettings.outputSampleRate, 0);
    }
Beispiel #7
0
    void OnGUI()
    {
        int width = 150;
        int height = 30;
        int top = 70;
        int i = 0;

        bool init = false;
        bool shutdown = false;
        bool requestAuthToken = false;
        bool setAuthToken = false;
        bool logOut = false;
        bool start = false;
        bool stop = false;
        bool pause = false;
        bool resume = false;
        bool runCommercial = false;
        bool startIngestTest = false;
        bool skipIngestServer = false;
        bool cancelIngestTest = false;
        bool gamePause = false;
        bool gameResume = false;
        bool loadNextScene = false;
        bool enterFullScreen = false;
        bool exitFullScreen = false;

        if (m_BroadcastController.IsInitialized)
        {
            if (m_BroadcastController.IsBroadcasting)
            {
                runCommercial = GUI.Button(new Rect(10,top+height*i++,width,height), "Run Commercial");

                if (m_BroadcastController.IsPaused)
                {
                    resume = GUI.Button(new Rect(10,top+height*i++,width,height), "Resume");
                }
                else
                {
                    pause = GUI.Button(new Rect(10,top+height*i++,width,height), "Pause");
                }

                stop = GUI.Button(new Rect(10,top+height*i++,width,height), "Stop");
            }
            else
            {
                if (m_BroadcastController.IsLoggedIn)
                {
                    if (m_BroadcastController.IsReadyToBroadcast)
                    {
                        start = GUI.Button(new Rect(10, top + height * i++, width, height), "Start");
                        startIngestTest = GUI.Button(new Rect(10, top + height * i++, width, height), "Start Ingest Test");
                        logOut = GUI.Button(new Rect(10,top+height*i++,width,height), "Log Out");
                    }
                    else if (m_BroadcastController.IsIngestTesting)
                    {
                        skipIngestServer = GUI.Button(new Rect(10, top + height * i++, width, height), "Skip Server");
                        cancelIngestTest = GUI.Button(new Rect(10, top + height * i++, width, height), "Cancel Ingest Test");
                    }
                }
                else
                {
                    requestAuthToken = GUI.Button(new Rect(10,top+height*i++,width,height), "Request Auth Token");
                    setAuthToken = GUI.Button(new Rect(10,top+height*i++,width,height), "Use Existing Auth Token");
                }

                if (!m_BroadcastController.IsIngestTesting)
                {
                    shutdown = GUI.Button(new Rect(10, top + height * i++, width, height), "Shutdown");
                }
            }
        }
        else
        {
            init = GUI.Button(new Rect(10,top+height*i++,width,height), "Init");
        }

        if (m_GamePaused)
        {
            gameResume = GUI.Button(new Rect(10, top + height * i++, width, height), "Resume Game");
        }
        else
        {
            gamePause = GUI.Button(new Rect(10, top + height * i++, width, height), "Pause Game");
        }

        loadNextScene = GUI.Button(new Rect(10, top + height * i++, width, height), "Load Next Scene");

        if (Screen.fullScreen)
        {
            exitFullScreen = GUI.Button(new Rect(10, top + height * i++, width, height), "Exit Full Screen");
        }
        else
        {
            enterFullScreen = GUI.Button(new Rect(10, top + height * i++, width, height), "Go Full Screen");
        }

        if (init)
        {
            if (!m_BroadcastController.InitializeTwitch())
            {
                DebugOverlay.Instance.AddViewportText("Error initializing Twitch", 2);
            }
        }
        else if (shutdown)
        {
            m_BroadcastController.ShutdownTwitch();
        }
        else if (start)
        {
            VideoParams videoParams = null;

            if (m_CalculateParamsFromBitrate)
            {
                videoParams = m_BroadcastController.GetRecommendedVideoParams((uint)m_TargetBitrate, (uint)m_BroadcastFramesPerSecond, m_BroadcastBitsPerPixel, m_BroadcastAspectRatio);
            }
            else
            {
                videoParams = m_BroadcastController.GetRecommendedVideoParams((uint)m_BroadcastWidth, (uint)m_BroadcastHeight, (uint)m_BroadcastFramesPerSecond);
            }

            if (!m_BroadcastController.StartBroadcasting(videoParams))
            {
                return;
            }
        }
        else if (requestAuthToken)
        {
            m_BroadcastController.RequestAuthToken(m_UserName, m_Password);
        }
        else if (setAuthToken)
        {
            string token = PlayerPrefs.GetString(s_TwitchAuthTokenKey, null);
            string username = PlayerPrefs.GetString(s_TwitchUserNameKey, null);

            if (token != null && username != null)
            {
                m_BroadcastController.SetAuthToken(username, new AuthToken(token));
            }
        }
        else if (logOut)
        {
            m_BroadcastController.Logout();
        }
        else if (stop)
        {
            m_BroadcastController.StopBroadcasting();
        }
        else if (pause)
        {
            m_BroadcastController.PauseBroadcasting();
        }
        else if (resume)
        {
            m_BroadcastController.ResumeBroadcasting();
        }
        else if (runCommercial)
        {
            m_BroadcastController.RunCommercial();
        }
        else if (startIngestTest)
        {
            m_IngestTester = m_BroadcastController.StartIngestTest();
            if (m_IngestTester != null)
            {
                m_IngestTester.OnTestStateChanged += OnIngestTesterStateChanged;
            }
        }
        else if (skipIngestServer)
        {
            m_BroadcastController.IngestTester.SkipCurrentServer();
        }
        else if (cancelIngestTest)
        {
            m_BroadcastController.IngestTester.Cancel();
        }

        if (gamePause)
        {
            m_GamePaused = true;
            Time.timeScale = 0;
        }
        else if (gameResume)
        {
            m_GamePaused = false;
            Time.timeScale = 1;
        }
        else if (loadNextScene)
        {
            string scene = "SampleScene";
            if (Application.loadedLevelName == "SampleScene")
            {
                scene = "SampleScene2";
            }
            Application.LoadLevel(scene);
        }
        else if (enterFullScreen)
        {
            Screen.SetResolution(1080, 1024, true);
        }
        else if (exitFullScreen)
        {
            Screen.SetResolution(1080, 1024, false);
        }
    }
Beispiel #8
0
 public StatCallbackListener(IngestTester tester)
     : base(tester)
 {
 }
Beispiel #9
0
 protected IngestTesterAccess(IngestTester tester)
 {
     m_IngestTester = tester;
 }
Beispiel #10
0
 public StreamCallbackListener(IngestTester tester)
     : base(tester)
 {
 }
Beispiel #11
0
 protected IngestTesterAccess(IngestTester tester)
 {
     m_IngestTester = tester;
 }