Esempio n. 1
0
        public override async Task <CommandResult> ExecuteAsync(CancellationToken cancel)
        {
            Console.WriteLine($"Connecting to {Hostname}...");

            try
            {
                using (Client = await SmartGlassClient.ConnectAsync(Hostname))
                {
                    var loop = new Loop(typeof(SessionCommandType));
                    loop.Execute();

                    Console.WriteLine($"Disconnected");
                }

                return(CommandResult.Success);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to connect: {e}");
            }
            finally
            {
                Client = null;
            }

            return(CommandResult.RuntimeFailure);
        }
Esempio n. 2
0
        /// <summary>
        /// Connect to console, request Broadcast Channel and start gamestream
        /// </summary>
        /// <param name="ipAddress">IP address of console</param>
        /// <param name="gamestreamConfig">Desired gamestream configuration</param>
        /// <returns></returns>
        public static async Task <GamestreamSession> ConnectToConsole(string ipAddress, GamestreamConfiguration gamestreamConfig)
        {
            try
            {
                SmartGlassClient client = await SmartGlassClient.ConnectAsync(ipAddress, _userHash, _xToken);

                return(await client.BroadcastChannel.StartGamestreamAsync(gamestreamConfig));
            }
            catch (Exception e)
            {
                Console.WriteLine($"Connection timed out! msg: {e.Message}");
                return(null);
            }
        }
        /*
         * Protocol
         */

        public async Task StartStream(SurfaceTexture surface)
        {
            System.Diagnostics.Debug.WriteLine($"Connecting to console...");

            _smartGlassClient = await SmartGlassClient.ConnectAsync(_hostName);

            // Get general gamestream configuration
            var config = GamestreamConfiguration.GetStandardConfig();

            /* Modify standard config, if desired */

            var broadcastChannel = _smartGlassClient.BroadcastChannel;
            var session          = await broadcastChannel.StartGamestreamAsync(config);

            System.Diagnostics.Debug.WriteLine(
                $"Connecting to Nano, TCP: {session.TcpPort}, UDP: {session.UdpPort}");

            _nanoClient = new NanoClient(_hostName, session);

            // General Handshaking & Opening channels
            await _nanoClient.InitializeProtocolAsync();

            // Audio & Video client handshaking
            // Sets desired AV formats
            Packets.AudioFormat audioFormat = _nanoClient.AudioFormats[0];
            Packets.VideoFormat videoFormat = _nanoClient.VideoFormats[0];
            await _nanoClient.InitializeStreamAsync(audioFormat, videoFormat);

            // Start ChatAudio channel
            Packets.AudioFormat chatAudioFormat = new Packets.AudioFormat(1, 24000, AudioCodec.Opus);
            await _nanoClient.OpenChatAudioChannelAsync(chatAudioFormat);

            _mcConsumer = new MediaCoreConsumer(surface, audioFormat, videoFormat);
            _nanoClient.AddConsumer(_mcConsumer);

            // Tell console to start sending AV frames
            await _nanoClient.StartStreamAsync();

            // Start Controller input channel
            await _nanoClient.OpenInputChannelAsync(1280, 720);

            System.Diagnostics.Debug.WriteLine($"Nano connected and running.");
        }
Esempio n. 4
0
        public override async Task <CommandResult> ExecuteAsync(CancellationToken cancel)
        {
            if (TokenFilePath != null)
            {
                using (FileStream fs = File.Open(TokenFilePath, FileMode.Open))
                {
                    AuthService = AuthenticationService.LoadFromFile(fs);
                    AuthService.Authenticate();
                }

                using (FileStream fs = File.Open(TokenFilePath, FileMode.Create))
                {
                    AuthService.DumpToFile(fs);
                }
            }

            Console.WriteLine($"Connecting to {Hostname}...");

            try
            {
                using (Client = await SmartGlassClient.ConnectAsync(Hostname,
                                                                    AuthService == null ? null : AuthService.XToken.UserInformation.Userhash,
                                                                    AuthService == null ? null : AuthService.XToken.Jwt))
                {
                    var loop = new Loop(typeof(SessionCommandType));
                    loop.Execute();

                    Console.WriteLine($"Disconnected");
                }

                return(CommandResult.Success);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to connect: {e}");
            }
            finally
            {
                Client = null;
            }

            return(CommandResult.RuntimeFailure);
        }
Esempio n. 5
0
        public override async Task <CommandResult> ExecuteAsync(CancellationToken cancel)
        {
            Console.WriteLine($"Connecting to {Hostname}...");

            try
            {
                using (Client = await SmartGlassClient.ConnectAsync(Hostname))
                {
                    var broadcastChannel = Client.BroadcastChannel;
                    // TODO: Wait for BroadcastMessages here...

                    var result = await broadcastChannel.StartGamestreamAsync();

                    Console.WriteLine($"Connecting to Nano, TCP: {result.TcpPort}, UDP: {result.UdpPort}");
                    var nano = new NanoClient(Hostname, result.TcpPort, result.UdpPort, new Guid());
                    await nano.Initialize();

                    FileConsumer consumer = new FileConsumer("nanostream");
                    nano.AddConsumer(consumer);

                    await nano.StartStream();

                    var loop = new Loop(typeof(SessionCommandType));
                    loop.Execute();

                    Console.WriteLine($"Disconnected");
                }

                return(CommandResult.Success);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to connect: {e}");
            }
            finally
            {
                Client = null;
            }

            return(CommandResult.RuntimeFailure);
        }
Esempio n. 6
0
 static GamestreamSession ConnectToConsole(string ipAddress, GamestreamConfiguration gamestreamConfig)
 {
     try
     {
         // 如果Task失败了GetAwaiter()会直接抛出异常,而Task.Wait()会抛出AggregateException
         SmartGlassClient client = SmartGlassClient.ConnectAsync(ipAddress, _userHash, _xToken)
                                   .GetAwaiter().GetResult();
         return(client.BroadcastChannel.StartGamestreamAsync(gamestreamConfig)
                .GetAwaiter().GetResult());
     }
     catch (SmartGlassException e)
     {
         throw Shell.Log("Failed to connect: {0}", e, e.Message);
     }
     catch (TimeoutException e)
     {
         throw Shell.Log("Timeout while connecting: {0}", e, e.Message);
     }
     catch (Exception e)
     {
         throw Shell.Log(e);
     }
 }
        public override async Task <CommandResult> ExecuteAsync(CancellationToken cancel)
        {
            if (TokenFilePath != null)
            {
                using (FileStream fs = File.Open(TokenFilePath, FileMode.Open))
                {
                    AuthService = await AuthenticationService.LoadFromJsonFileStream(fs);

                    await AuthService.AuthenticateAsync();
                }

                await AuthService.DumpToJsonFileAsync(TokenFilePath);
            }

            Console.WriteLine($"Connecting to {Hostname}...");

            GamestreamSession session = null;
            SmartGlassClient  Client  = null;

            try
            {
                Client = await SmartGlassClient.ConnectAsync(Hostname,
                                                             AuthService == null?null : AuthService.XToken.UserInformation.Userhash,
                                                             AuthService == null?null : AuthService.XToken.Jwt);
            }
            catch (SmartGlassException e)
            {
                Console.WriteLine($"Failed to connect: {e.Message}");
                return(CommandResult.RuntimeFailure);
            }
            catch (TimeoutException)
            {
                Console.WriteLine($"Timeout while connecting");
                return(CommandResult.RuntimeFailure);
            }

            var broadcastChannel = Client.BroadcastChannel;

            var config = GamestreamConfiguration.GetStandardConfig();

            try
            {
                session = await broadcastChannel.StartGamestreamAsync(config);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to send StartGamestream: {e.Message}");
                return(CommandResult.RuntimeFailure);
            }

            Console.WriteLine($"Connecting to Nano, TCP: {session.TcpPort}, UDP: {session.UdpPort}");
            var nano = new NanoClient(Hostname, session);

            try
            {
                Console.WriteLine($"Running protocol init...");
                await nano.InitializeProtocolAsync();

                await nano.OpenInputChannelAsync(1280, 720);

                await nano.OpenChatAudioChannelAsync(
                    new Nano.Packets.AudioFormat(1, 24000, AudioCodec.Opus));

                Console.WriteLine("Adding FileConsumer");
                FileConsumer consumer = new FileConsumer("nanostream");
                nano.AddConsumer(consumer);

                Console.WriteLine("Initializing AV stream (handshaking)...");
                await nano.InitializeStreamAsync(nano.AudioFormats[0],
                                                 nano.VideoFormats[0]);

                Console.WriteLine("Starting stream...");
                await nano.StartStreamAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine($"Failed to initialize gamestream: {e}");
                return(CommandResult.RuntimeFailure);
            }

            Console.WriteLine("Stream is running");

            var loop = new Loop(typeof(SessionCommandType));

            loop.Execute();

            return(CommandResult.Success);
        }
Esempio n. 8
0
        static void Main()
        {
            AllocConsole();

            Console.Write("tokenFilePath: ");
            string tokenFilePath = Console.ReadLine();

            AuthenticationService auth;

            if (!File.Exists(tokenFilePath))
            {
                Shell.WriteLine("Warning: '{0}' file not found.\n", tokenFilePath);

                string reqURL = AuthenticationService.GetWindowsLiveAuthenticationUrl();

                Console.WriteLine("1) Open following URL in your WebBrowser:\n\n{0}\n\n" +
                                  "2) Authenticate with your Microsoft Account\n" +
                                  "3) Paste returned URL from addressbar: \n", reqURL);

                // Call requestUrl via WebWidget or manually and authenticate

                try
                {
                    string url = Console.ReadLine();
                    WindowsLiveResponse rep = AuthenticationService.ParseWindowsLiveResponse(url);
                    auth = new AuthenticationService(rep);

                    auth.Authenticate();
                }
                catch (Exception e)
                {
                    Shell.WriteLine($"Error: Authentication failed, error: {e.Message}");
                    Shell.PressAnyKeyToContinue();
                    return;
                }

                Console.WriteLine(auth.XToken);
                Console.WriteLine(auth.UserInformation);

                // Save token to JSON

                FileStream tokenOutputFile = null;
                try
                {
                    tokenOutputFile = new FileStream(tokenFilePath, FileMode.Create);
                }
                catch (Exception e)
                {
                    Shell.WriteLine("Error: Failed to open token outputfile \'{0}\', error: {1}",
                                    tokenOutputFile, e.Message);
                    Shell.PressAnyKeyToContinue();
                    return;
                }
                auth.DumpToFile(tokenOutputFile);
                tokenOutputFile.Close();

                Console.WriteLine("Storing tokens to file \'{0}\' on successful auth",
                                  tokenOutputFile.Name);
            }
            else
            {
                // Load token from JSON

                FileStream fs = new FileStream(tokenFilePath, FileMode.Open);
                auth = AuthenticationService.LoadFromFile(fs);
                try
                {
                    auth.Authenticate();
                }
                catch (Exception e)
                {
                    Shell.WriteLine($"Error: Failed to refresh XBL tokens, error: {e.Message}");
                    Shell.PressAnyKeyToContinue();
                    return;
                }
                fs.Close();
            }

            UserHash = auth.XToken.UserInformation.Userhash;
            XToken   = auth.XToken.Jwt;

            Discover().Wait();

            Console.Write("Input IP Address or hostname: ");
            string addressOrHostname = Console.ReadLine();

            Console.WriteLine($"Connecting to {addressOrHostname}...");
            SmartGlassClient client;

            try
            {
                Task <SmartGlassClient> connect = SmartGlassClient.ConnectAsync(
                    addressOrHostname, UserHash, XToken);

                // 如果Task失败了GetAwaiter()会直接抛出异常,而Task.Wait()会抛出AggregateException
                client = connect.GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                if (e is SmartGlassException)
                {
                    Shell.WriteLine($"Error: Failed to connect: {e.Message}");
                }
                else if (e is TimeoutException)
                {
                    Shell.WriteLine($"Error: Timeout while connecting: {e.Message}");
                }
                else
                {
                    Shell.WriteLine($"Error: {e}");
                }

                Shell.PressAnyKeyToContinue();
                return;
            }

            // Get general gamestream configuration
            GamestreamConfiguration config = GamestreamConfiguration.GetStandardConfig();
            // Modify standard config, if desired

            GamestreamSession session = client.BroadcastChannel.StartGamestreamAsync(config)
                                        .GetAwaiter().GetResult();

            Console.WriteLine($"Connecting to NANO // TCP: {session.TcpPort}, UDP: {session.UdpPort}");

            Console.WriteLine($"Running protocol init...");
            Nano = new NanoClient(addressOrHostname, session);
            try
            {
                // General Handshaking & Opening channels
                Nano.InitializeProtocolAsync().Wait();

                // Start Controller input channel
                Nano.OpenInputChannelAsync(1280, 720).Wait();

                //IConsumer consumer = /* initialize consumer */;
                //nano.AddConsumer(consumer);

                // Start consumer, if necessary
                //consumer.Start();

                // Audio & Video client handshaking
                // Sets desired AV formats
                Console.WriteLine("Initializing AV stream (handshaking)...");

                AudioFormat = Nano.AudioFormats[0];
                VideoFormat = Nano.VideoFormats[0];

                Nano.InitializeStreamAsync(AudioFormat, VideoFormat).Wait();

                // Start ChatAudio channel
                // TODO: Send opus audio chat samples to console
                ChatAudioFormat = new AudioFormat(1, 24000, AudioCodec.Opus);
                Nano.OpenChatAudioChannelAsync(ChatAudioFormat).Wait();

                // Tell console to start sending AV frames
                Console.WriteLine("Starting stream...");

                Nano.StartStreamAsync().Wait();

                Shell.WriteLine("Note: Stream is running");
            }
            catch (Exception e)
            {
                Shell.WriteLine($"Error: Failed to init Nano, error: {e}");
                Shell.PressAnyKeyToContinue();
                return;
            }

            // Run a mainloop, to gather controller input events or similar

            FreeConsole();

            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Xstream());

            // finally (dirty)
            Process.GetCurrentProcess().Kill();
        }