/// <summary> /// a new connection was created /// </summary> void OnSessionConnectionCreated(MiracastReceiverSession sender, MiracastReceiverConnectionCreatedEventArgs args) { //configure connection CurrentConnection = args.Connection; CurrentConnection.InputDevices.Keyboard.TransmitInput = false; //send pin event if (!string.IsNullOrWhiteSpace(args.Pin)) { PinAvailable?.Invoke(CurrentConnection.Transmitter.Name, args.Pin); } //start timeout task currentConnectionTimeoutCancellation = new CancellationTokenSource(); if (DeadConnectionTimeout > 0) { Task.Run(async() => { try { //wait until timeout await Task.Delay(DeadConnectionTimeout, currentConnectionTimeoutCancellation.Token); //check if a media player was created if (currentPlayer == null) { //no media source was created, kill connection Log($"no media player was created, killing connection to {CurrentConnection?.Transmitter?.MacAddress}"); Invoke(() => { StartNewSession(); }); return; } //connection looks fine to me Log("connection looks healthy, timeout task will now shut down."); } catch (TaskCanceledException) { //timeout was cancelled Log("connection timeout was cancelled"); } }); } //log details Log($"new connection created with {CurrentConnection.Transmitter.Name} ({CurrentConnection.Transmitter.MacAddress}). Auth pin is {(string.IsNullOrWhiteSpace(args.Pin) ? "No Pin" : args.Pin)}"); }
/// <summary> /// end the current cast session, and call CastEnd event /// </summary> /// <param name="sendEvent">should CastEnd be called?</param> void EndCurrentSession(bool sendEvent) { currentPlayer?.Pause(); currentPlayer?.Dispose(); currentPlayer = null; CurrentConnection?.Disconnect(MiracastReceiverDisconnectReason.Finished); CurrentConnection = null; currentSession?.Dispose(); currentSession = null; if (sendEvent) { CastEnd?.Invoke(); } }