Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Commit 59c425e

Browse files
committed
[TestAppUWP] Handle remote video size changes
Ensure the remote video media source is resized when a remote video frame size change is detected, to avoid playback artifacts when stopping and restarting on the remote peer the local video with a different resolution.
1 parent f5bf1d9 commit 59c425e

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

examples/TestAppUwp/MainPage.xaml.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public sealed partial class MainPage : Page
7676
private MediaSource remoteMediaSource = null;
7777
private MediaPlayer remoteVideoPlayer = new MediaPlayer();
7878
private bool _isRemoteVideoPlaying = false;
79+
private uint _remoteVideoWidth = 0;
80+
private uint _remoteVideoHeight = 0;
7981
private object _isRemoteVideoPlayingLock = new object();
8082

8183
private uint _remoteAudioChannelCount = 0;
@@ -464,6 +466,8 @@ private async void OnLoaded(object sender, RoutedEventArgs e)
464466
remoteVideoPlayer.MediaOpened += OnMediaOpened;
465467
remoteVideoPlayer.MediaFailed += OnMediaFailed;
466468
remoteVideoPlayer.MediaEnded += OnMediaEnded;
469+
remoteVideoPlayer.RealTimePlayback = true;
470+
remoteVideoPlayer.AutoPlay = false;
467471

468472
// Bind the XAML UI control (localVideo) to the MediaFoundation rendering pipeline (localVideoPlayer)
469473
// so that the former can render in the UI the video frames produced in the background by the later.
@@ -821,7 +825,12 @@ private void OnMediaOpened(MediaPlayer sender, object args)
821825
{
822826
RunOnMainThread(() => {
823827
localVideo.MediaPlayer.Play();
824-
//startLocalMedia.IsEnabled = true;
828+
});
829+
}
830+
else if (sender == remoteVideoPlayer)
831+
{
832+
RunOnMainThread(() => {
833+
remoteVideo.MediaPlayer.Play();
825834
});
826835
}
827836
}
@@ -873,9 +882,12 @@ private void StopLocalVideo()
873882
if (_isLocalVideoPlaying)
874883
{
875884
localVideo.MediaPlayer.Pause();
885+
localVideo.MediaPlayer.Source = null;
876886
localVideo.SetMediaPlayer(null);
887+
localVideoSource.NotifyError(MediaStreamSourceErrorStatus.Other);
877888
localVideoSource = null;
878-
//localMediaSource.Reset();
889+
localMediaSource.Dispose();
890+
localMediaSource = null;
879891
_isLocalVideoPlaying = false;
880892

881893
// Avoid deadlock in audio processing stack, as this call is delegated to the WebRTC
@@ -968,21 +980,38 @@ private void Peer_RemoteI420FrameReady(I420AVideoFrame frame)
968980
// will be sending some video track.
969981
//< TODO - See if we can add an API to enumerate the remote channels,
970982
// or an On(Audio|Video|Data)Channel(Added|Removed) event?
983+
bool needNewSource = false;
984+
uint width = frame.width;
985+
uint height = frame.height;
971986
lock (_isRemoteVideoPlayingLock)
972987
{
973988
if (!_isRemoteVideoPlaying)
974989
{
975990
_isRemoteVideoPlaying = true;
976-
uint width = frame.width;
977-
uint height = frame.height;
978-
// We don't know the remote video framerate yet, so use a default.
979-
uint framerate = 30;
980-
RunOnMainThread(() => {
981-
remoteVideoSource = CreateVideoStreamSource(width, height, framerate);
982-
remoteVideoPlayer.Source = MediaSource.CreateFromMediaStreamSource(remoteVideoSource);
983-
remoteVideoPlayer.Play();
984-
});
991+
needNewSource = true;
985992
}
993+
else if ((width != _remoteVideoWidth) || (height != _remoteVideoHeight))
994+
{
995+
_remoteVideoWidth = width;
996+
_remoteVideoHeight = height;
997+
needNewSource = true;
998+
}
999+
}
1000+
if (needNewSource)
1001+
{
1002+
// We don't know the remote video framerate yet, so use a default.
1003+
uint framerate = 30;
1004+
RunOnMainThread(() => {
1005+
remoteVideoPlayer.Pause();
1006+
remoteVideoPlayer.Source = null;
1007+
remoteVideo.SetMediaPlayer(null);
1008+
remoteVideoSource?.NotifyError(MediaStreamSourceErrorStatus.Other);
1009+
remoteMediaSource?.Dispose();
1010+
remoteVideoSource = CreateVideoStreamSource(width, height, framerate);
1011+
remoteMediaSource = MediaSource.CreateFromMediaStreamSource(remoteVideoSource);
1012+
remoteVideoPlayer.Source = remoteMediaSource;
1013+
remoteVideo.SetMediaPlayer(remoteVideoPlayer);
1014+
});
9861015
}
9871016

9881017
remoteVideoBridge.HandleIncomingVideoFrame(frame);
@@ -1128,9 +1157,9 @@ private async void StartLocalMediaClicked(object sender, RoutedEventArgs e)
11281157
}
11291158

11301159
localVideoPlayer.Source = null;
1131-
localMediaSource?.Reset();
1160+
localVideoSource?.NotifyError(MediaStreamSourceErrorStatus.Other);
1161+
localMediaSource?.Dispose();
11321162
localVideo.SetMediaPlayer(null);
1133-
localVideoSource = null;
11341163
localVideoSource = CreateVideoStreamSource(width, height, (uint)framerate);
11351164
localMediaSource = MediaSource.CreateFromMediaStreamSource(localVideoSource);
11361165
localVideoPlayer.Source = localMediaSource;

0 commit comments

Comments
 (0)