/*
         * 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.");
        }
Example #2
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);
        }
        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);
        }