Skip to content

Commit

Permalink
Revert "Enabled video resolution picker"
Browse files Browse the repository at this point in the history
This reverts commit 6ebfc2d.
  • Loading branch information
yassirbisteni committed Apr 24, 2024
1 parent fd4804b commit 52ce431
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 214 deletions.
141 changes: 22 additions & 119 deletions RawVideo/CameraCaptureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,59 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Graphics;
using Windows.Graphics.Imaging;
using Windows.Media.Capture;
using Windows.Media.Capture.Frames;

namespace RawVideo
{
internal class MediaFrameSourceBundle
{
public MediaFrameSourceGroup Group { get; set; }
public MediaFrameSourceInfo Info { get; set; }
public MediaFrameFormat Format { get; set; }
}

internal class VideoFormatBundle
{
public SizeInt32 Size { get; set; }
public MediaFrameFormat Format { get; set; }
}

internal class VideoFormatBundleComparer : IEqualityComparer<VideoFormatBundle>
{
public bool Equals(VideoFormatBundle s1, VideoFormatBundle s2)
{
return s1.Size.Width == s2.Size.Width && s1.Size.Height == s2.Size.Height;
}

public int GetHashCode(VideoFormatBundle obj)
{
return obj.Size.Width.GetHashCode() ^ obj.Size.Height.GetHashCode();
}
}

internal class CameraCaptureService : CaptureService
{
private readonly MediaFrameSourceBundle bundle;
private MediaCapture mediaCapture;
private MediaFrameReader mediaFrameReader;
private MediaFrameSourceGroup sourceGroup;
private MediaFrameSourceInfo sourceInfo;

public CameraCaptureService(RawOutgoingVideoStream rawOutgoingVideoStream,
RawVideoFrameKind rawVideoFrameKind, MediaFrameSourceBundle bundle) :
RawVideoFrameKind rawVideoFrameKind,
Tuple<MediaFrameSourceGroup, MediaFrameSourceInfo> mediaFrameSource) :
base(rawOutgoingVideoStream, rawVideoFrameKind)
{
this.bundle = bundle;
sourceGroup = mediaFrameSource.Item1;
sourceInfo = mediaFrameSource.Item2;
}

public async Task StartAsync()
{
var settings = new MediaCaptureInitializationSettings
{
SourceGroup = bundle.Group,
SharingMode = MediaCaptureSharingMode.ExclusiveControl,
SourceGroup = sourceGroup,
SharingMode = MediaCaptureSharingMode.SharedReadOnly,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu
};

mediaCapture = new MediaCapture();
MediaFrameReaderStartStatus mediaFrameReaderStatus =
MediaFrameReaderStartStatus.UnknownFailure;
MediaFrameReaderStartStatus mediaFrameReaderStatus;

try
{
await mediaCapture.InitializeAsync(settings);
MediaFrameSource source = mediaCapture.FrameSources[bundle.Info.Id];

await source.SetFormatAsync(bundle.Format);

mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(source);
mediaFrameReader.FrameArrived += FrameArrived;
MediaFrameSource selectedSource = mediaCapture.FrameSources[sourceInfo.Id];
mediaFrameReader = await mediaCapture.CreateFrameReaderAsync(selectedSource);
mediaFrameReaderStatus = await mediaFrameReader.StartAsync();
}
catch (Exception ex)
{
mediaFrameReaderStatus = MediaFrameReaderStartStatus.UnknownFailure;

Console.WriteLine(ex.Message);
}

if (mediaFrameReaderStatus == MediaFrameReaderStartStatus.Success)
{
mediaFrameReader.FrameArrived += FrameArrived;
}
}

public async Task StopAsync()
Expand Down Expand Up @@ -110,98 +88,23 @@ public async Task StopAsync()
}
}

public static async Task<List<MediaFrameSourceBundle>> GetCameraListAsync()
public static async Task<List<Tuple<MediaFrameSourceGroup, MediaFrameSourceInfo>>> GetCameraListAsync()
{
IReadOnlyList<MediaFrameSourceGroup> groups = await MediaFrameSourceGroup.FindAllAsync();
var cameraList = new List<MediaFrameSourceBundle>();
var cameraList = new List<Tuple<MediaFrameSourceGroup, MediaFrameSourceInfo>>();

foreach (MediaFrameSourceGroup sourceGroup in groups)
{
foreach (MediaFrameSourceInfo sourceInfo in sourceGroup.SourceInfos)
{
if (sourceInfo.SourceKind == MediaFrameSourceKind.Color)
{
cameraList.Add(new MediaFrameSourceBundle()
{
Group = sourceGroup,
Info = sourceInfo
});
cameraList.Add(new Tuple<MediaFrameSourceGroup, MediaFrameSourceInfo>(sourceGroup, sourceInfo));
}
}
}

return cameraList
.OrderBy(x => x.Info.DeviceInformation.Name)
.ToList();
}

public static async Task<List<VideoFormatBundle>> GetSupportedVideoFormats(MediaFrameSourceBundle bundle)
{
var settings = new MediaCaptureInitializationSettings
{
SourceGroup = bundle.Group,
SharingMode = MediaCaptureSharingMode.SharedReadOnly,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu
};

IReadOnlyList<MediaFrameFormat> sourceFormatList = null;
var videoFormatList= new List<VideoFormatBundle>();

using (var mediaCapture = new MediaCapture())
{
try
{
await mediaCapture.InitializeAsync(settings);
MediaFrameSource source = mediaCapture.FrameSources[bundle.Info.Id];
sourceFormatList = source.SupportedFormats;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

if (sourceFormatList != null)
{
var resolutionList = Enum.GetValues(typeof(VideoStreamResolution))
.Cast<VideoStreamResolution>()
.ToList();

var acsFormatList = resolutionList
.Select(x =>
{
var format = new VideoStreamFormat
{
Resolution = x
};

return new SizeInt32()
{
Width = format.Width,
Height = format.Height
};
})
.Distinct()
.ToList();

videoFormatList = sourceFormatList
.Select(x => new VideoFormatBundle()
{
Size = new SizeInt32()
{
Width = (int) x.VideoFormat.Width,
Height = (int) x.VideoFormat.Height
},
Format = x
})
.Distinct(new VideoFormatBundleComparer())
.Where(x => acsFormatList.Contains(x.Size))
.OrderByDescending(x => x.Size.Width)
.ToList();
}

return videoFormatList;
return cameraList.OrderBy(item => item.Item2.DeviceInformation.Name).ToList();
}
}
}
28 changes: 13 additions & 15 deletions RawVideo/CaptureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@ namespace RawVideo
internal abstract class CaptureService
{
public event EventHandler<RawVideoFrame> FrameArrived;
protected readonly RawOutgoingVideoStream stream;
protected readonly RawOutgoingVideoStream rawOutgoingVideoStream;
private readonly RawVideoFrameKind rawVideoFrameKind;

protected CaptureService(RawOutgoingVideoStream stream, RawVideoFrameKind rawVideoFrameKind)
protected CaptureService(RawOutgoingVideoStream rawOutgoingVideoStream, RawVideoFrameKind rawVideoFrameKind)
{
this.stream = stream;
this.rawOutgoingVideoStream = rawOutgoingVideoStream;
this.rawVideoFrameKind = rawVideoFrameKind;
}

protected async Task SendRawVideoFrame(SoftwareBitmap bitmap)
{
var format = stream.Format;
if (bitmap != null && format != null && CanSendRawVideoFrames())
if (bitmap != null && CanSendRawVideoFrames())
{
try
{
RawVideoFrame frame = ConvertSoftwareBitmapToRawVideoFrame(bitmap, format);
RawVideoFrame frame = ConvertSoftwareBitmapToRawVideoFrame(bitmap);

await stream.SendRawVideoFrameAsync(frame);
await rawOutgoingVideoStream.SendRawVideoFrameAsync(frame);

FrameArrived?.Invoke(this, frame);
}
Expand All @@ -40,9 +39,7 @@ protected async Task SendRawVideoFrame(SoftwareBitmap bitmap)
}
}

private unsafe RawVideoFrame ConvertSoftwareBitmapToRawVideoFrame(
SoftwareBitmap bitmap,
VideoStreamFormat format)
private unsafe RawVideoFrame ConvertSoftwareBitmapToRawVideoFrame(SoftwareBitmap bitmap)
{
bitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Rgba8);

Expand All @@ -62,22 +59,21 @@ private unsafe RawVideoFrame ConvertSoftwareBitmapToRawVideoFrame(
{
case RawVideoFrameKind.Buffer:
MemoryBuffer buffer = Buffer.CreateMemoryBufferOverIBuffer(bitmapBuffer);

frame = new RawVideoFrameBuffer()
{
Buffers = new MemoryBuffer[] { buffer },
StreamFormat = format
StreamFormat = rawOutgoingVideoStream.Format
};

break;
case RawVideoFrameKind.Texture:
var timeSpan = new TimeSpan(stream.TimestampInTicks);
var timeSpan = new TimeSpan(rawOutgoingVideoStream.TimestampInTicks);
var sample = MediaStreamSample.CreateFromBuffer(bitmapBuffer, timeSpan);

frame = new RawVideoFrameTexture()
{
Texture = sample,
StreamFormat = stream.Format
StreamFormat = rawOutgoingVideoStream.Format
};

break;
Expand All @@ -88,7 +84,9 @@ private unsafe RawVideoFrame ConvertSoftwareBitmapToRawVideoFrame(

private bool CanSendRawVideoFrames()
{
return stream != null && stream.State == VideoStreamState.Started;
return rawOutgoingVideoStream != null &&
rawOutgoingVideoStream.Format != null &&
rawOutgoingVideoStream.State == VideoStreamState.Started;
}
}
}
41 changes: 13 additions & 28 deletions RawVideo/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter
Width="Auto"
Width="500"
Height="50"
FontSize="25"
Background="White"
Foreground="Black"
Padding="20,0,20,0"
/>
</Grid>
</ControlTemplate>
Expand Down Expand Up @@ -216,8 +215,9 @@
</ComboBox>
</StackPanel>


<StackPanel
Width="Auto"
Width="500"
Orientation="Horizontal"
>
<ComboBox
Expand Down Expand Up @@ -250,22 +250,6 @@
<SolidColorBrush x:Key="ComboBoxBorderBrushPointerOver" Color="Black"/>
</ComboBox.Resources>
</ComboBox>

<ComboBox
Width="200"
x:Name="outgoingVideoFormatComboBox"
PlaceholderText="OutgoingVideoFormat"
SelectionChanged="OutgoingVideoFormatSelected"
Style="{StaticResource ComboBoxStyle}"
ItemContainerStyle="{StaticResource ComboBoxItemStyle}"
Margin="10,10,0,0"
>
<ComboBox.Resources>
<SolidColorBrush x:Key="ComboBoxBackgroundPointerOver" Color="white"/>
<SolidColorBrush x:Key="ComboBoxForegroundPointerOver" Color="Black"/>
<SolidColorBrush x:Key="ComboBoxBorderBrushPointerOver" Color="Black"/>
</ComboBox.Resources>
</ComboBox>
</StackPanel>

<ComboBox
Expand Down Expand Up @@ -314,33 +298,34 @@
</ComboBox>
</StackPanel>

<StackPanel
<RelativePanel
x:Name="videoContainer"
Width="640"
Height="360"
Margin="0,10,0,0"
Visibility="Collapsed"
Orientation="Vertical"
>
<RelativePanel
x:Name="incomingVideoContainer"
Width="480"
Height="270"
Width="640"
Height="360"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="8"
Margin="0,0,0,10"
>
</RelativePanel>

<RelativePanel
x:Name="outgoingVideoContainer"
Width="480"
Height="270"
Width="224"
Height="108"
Margin="10,10,0,0"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="8"
>
</RelativePanel>
</StackPanel>
</RelativePanel>

<StackPanel
Width="Auto"
Expand All @@ -349,7 +334,7 @@
Spacing="10"
Background="White"
HorizontalAlignment="Center"
Margin="0,10,0,0"
Margin="0,10,0,0"
>
<Button
x:Name="startCallButton"
Expand Down
Loading

0 comments on commit 52ce431

Please sign in to comment.