Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIDCS 927 - Update CustomVideoRenderer sample to include filter and watermark #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CustomVideoRenderer/CustomVideoRenderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -37,6 +38,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -105,6 +107,19 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Data\vonage-logo-white-456x100.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Data\vonage-logo-white-913x200.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="WriteableBitmapEx">
<Version>1.6.7</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 19 additions & 17 deletions CustomVideoRenderer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomVideoRenderer" x:Class="CustomVideoRenderer.MainWindow"
mc:Ignorable="d"
Title="Screen Sharing Example" Height="350" Width="525" MinWidth="320" MinHeight="240">
<Grid>
<UniformGrid x:Name="SubscriberGrid" Rows="1" Columns="0">
</UniformGrid>
<local:SampleVideoRenderer x:Name="PublisherVideo" HorizontalAlignment="Right" Height="114" VerticalAlignment="Bottom" Width="184" Margin="0,0,10.429,10.143" BorderBrush="#FF5B1919" BorderThickness="1" >
<local:SampleVideoRenderer.Effect>
<DropShadowEffect Opacity="0.6"/>
</local:SampleVideoRenderer.Effect>
<local:SampleVideoRenderer.Background>
<ImageBrush Stretch="UniformToFill">
</ImageBrush>
</local:SampleVideoRenderer.Background>
</local:SampleVideoRenderer>
<Grid HorizontalAlignment="Left" Height="200" VerticalAlignment="Top" Width="100">
<Button x:Name="ConnectDisconnectButton" Content="Connect" HorizontalAlignment="Left" Margin="10,10,0,160" Width="80" Click="Connect_Click"/>
<Button x:Name="FilterButton" Content="Toggle filter" HorizontalAlignment="Left" Margin="10,50,0,120" Width="80" Click="FilterButton_Click"/>
Title="Screen Sharing Example" Height="720" Width="1280
" MinWidth="320" MinHeight="240">
<Grid>
<UniformGrid x:Name="SubscriberGrid" Rows="1" Columns="0">
</UniformGrid>
<local:SampleVideoRenderer x:Name="PublisherVideo" HorizontalAlignment="Right" Height="360" VerticalAlignment="Bottom" Width="640" Margin="0,0,10,10" BorderBrush="#FF5B1919" BorderThickness="1" >
<local:SampleVideoRenderer.Effect>
<DropShadowEffect Opacity="0.6"/>
</local:SampleVideoRenderer.Effect>
<local:SampleVideoRenderer.Background>
<ImageBrush Stretch="UniformToFill">
</ImageBrush>
</local:SampleVideoRenderer.Background>
</local:SampleVideoRenderer>
<Grid HorizontalAlignment="Left" Height="200" VerticalAlignment="Top" Width="100">
<Button x:Name="ConnectDisconnectButton" Content="Connect" HorizontalAlignment="Left" Margin="10,10,-15,160" Width="105" Click="Connect_Click"/>
<Button x:Name="FilterButton" Content="Toggle filter" HorizontalAlignment="Left" Margin="10,45,-15,125" Width="105" Click="FilterButton_Click"/>
<Button x:Name="WatermarkButton" Content="Toggle watermark" HorizontalAlignment="Left" Margin="10,80,-15,90" Width="105" Click="WatermarkButton_Click"/>
</Grid>
</Grid>
</Grid>
</Window>
11 changes: 11 additions & 0 deletions CustomVideoRenderer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public partial class MainWindow : Window
private const string SESSION_ID = "";
private const string TOKEN = "";


VideoCapturer Capturer;
Session Session;
Publisher Publisher;
Expand Down Expand Up @@ -117,6 +118,7 @@ private void Session_StreamReceived(object sender, Session.StreamEventArgs e)

SampleVideoRenderer renderer = new SampleVideoRenderer();
renderer.EnableBlueFilter = PublisherVideo.EnableBlueFilter;
renderer.EnableWatermark = PublisherVideo.EnableWatermark;

SubscriberGrid.Children.Add(renderer);
UpdateGridSize(SubscriberGrid.Children.Count);
Expand Down Expand Up @@ -196,5 +198,14 @@ private void FilterButton_Click(object sender, RoutedEventArgs e)
((SampleVideoRenderer)subscriber.VideoRenderer).EnableBlueFilter = PublisherVideo.EnableBlueFilter;
}
}

private void WatermarkButton_Click(object sender, RoutedEventArgs e)
{
PublisherVideo.EnableWatermark = !PublisherVideo.EnableWatermark;
foreach (var subscriber in SubscriberByStream.Values)
{
((SampleVideoRenderer)subscriber.VideoRenderer).EnableWatermark = PublisherVideo.EnableWatermark;
}
}
}
}
113 changes: 113 additions & 0 deletions CustomVideoRenderer/SampleVideoRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public class SampleVideoRenderer : Control, IVideoRenderer
private WriteableBitmap VideoBitmap;

public bool EnableBlueFilter;
public bool EnableWatermark;
public bool AutoResize;

public WriteableBitmap Watermark
{
get; private set;
}
public float RelativeSize
{
get; set;
}

static SampleVideoRenderer()
{
Expand All @@ -53,6 +64,8 @@ public SampleVideoRenderer()
var brush = new ImageBrush();
brush.Stretch = Stretch.UniformToFill;
Background = brush;

Watermark = CreateFromResource("Data/vonage-logo-white-913x200.png");
}

public void RenderFrame(VideoFrame frame)
Expand All @@ -62,6 +75,9 @@ public void RenderFrame(VideoFrame frame)
{
try
{
WriteableBitmap watermark = Watermark;
Rect position = new Rect(10, 10, Watermark.PixelWidth, Watermark.PixelHeight);

if (frame.Width != FrameWidth || frame.Height != FrameHeight)
{
FrameWidth = frame.Width;
Expand All @@ -77,6 +93,27 @@ public void RenderFrame(VideoFrame frame)
{
throw new Exception("Please use an ImageBrush as background in the SampleVideoRenderer control");
}

if (AutoResize)
{
int w = Watermark.PixelWidth;
int h = Watermark.PixelHeight;

float ratio = (float)w / h;
if (ratio > 1)
{
w = (int)(FrameWidth * RelativeSize);
h = (int)(w / ratio);
}
else
{
h = (int)(FrameHeight * RelativeSize);
w = (int)(h * ratio);
}

watermark = ResizeWritableBitmap(Watermark, w, h);
position = new Rect(10, 10, w, h);
}
}

if (VideoBitmap != null)
Expand All @@ -100,6 +137,11 @@ public void RenderFrame(VideoFrame frame)
p += stride[0] - FrameWidth * 4;
}
}

if (EnableWatermark)
{
VideoBitmap.Blit(position, watermark, new Rect(0, 0, position.Width, position.Height));
}
}
VideoBitmap.AddDirtyRect(new Int32Rect(0, 0, FrameWidth, FrameHeight));
VideoBitmap.Unlock();
Expand All @@ -111,5 +153,76 @@ public void RenderFrame(VideoFrame frame)
}
}));
}

private WriteableBitmap CreateFromResource(string resource)
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(resource, UriKind.RelativeOrAbsolute);
bi.EndInit();
return new WriteableBitmap(bi);
}

private static WriteableBitmap ResizeWritableBitmap(WriteableBitmap wBitmap, int reqWidth, int reqHeight)
{
int Stride = wBitmap.PixelWidth * ((wBitmap.Format.BitsPerPixel + 7) / 8);
int NumPixels = Stride * wBitmap.PixelHeight;
ushort[] ArrayOfPixels = new ushort[NumPixels];


wBitmap.CopyPixels(ArrayOfPixels, Stride, 0);

int OriWidth = (int)wBitmap.PixelWidth;
int OriHeight = (int)wBitmap.PixelHeight;

double nXFactor = (double)OriWidth / (double)reqWidth;
double nYFactor = (double)OriHeight / (double)reqHeight;

double fraction_x, fraction_y, one_minus_x, one_minus_y;
int ceil_x, ceil_y, floor_x, floor_y;

ushort pix1, pix2, pix3, pix4;
int nStride = reqWidth * ((wBitmap.Format.BitsPerPixel + 7) / 8);
int nNumPixels = reqWidth * reqHeight;
ushort[] newArrayOfPixels = new ushort[nNumPixels];

for (int y = 0; y < reqHeight; y++)
{
for (int x = 0; x < reqWidth; x++)
{
// Setup
floor_x = (int)Math.Floor(x * nXFactor);
floor_y = (int)Math.Floor(y * nYFactor);

ceil_x = floor_x + 1;
if (ceil_x >= OriWidth) ceil_x = floor_x;

ceil_y = floor_y + 1;
if (ceil_y >= OriHeight) ceil_y = floor_y;

fraction_x = x * nXFactor - floor_x;
fraction_y = y * nYFactor - floor_y;

one_minus_x = 1.0 - fraction_x;
one_minus_y = 1.0 - fraction_y;

pix1 = ArrayOfPixels[floor_x + floor_y * OriWidth];
pix2 = ArrayOfPixels[ceil_x + floor_y * OriWidth];
pix3 = ArrayOfPixels[floor_x + ceil_y * OriWidth];
pix4 = ArrayOfPixels[ceil_x + ceil_y * OriWidth];

ushort g1 = (ushort)(one_minus_x * pix1 + fraction_x * pix2);
ushort g2 = (ushort)(one_minus_x * pix3 + fraction_x * pix4);
ushort g = (ushort)(one_minus_y * (double)(g1) + fraction_y * (double)(g2));
newArrayOfPixels[y * reqWidth + x] = g;
}
}

WriteableBitmap newWBitmap = new WriteableBitmap(reqWidth, reqHeight, 96, 96, PixelFormats.Gray16, null);
Int32Rect Imagerect = new Int32Rect(0, 0, reqWidth, reqHeight);
int newStride = reqWidth * ((PixelFormats.Gray16.BitsPerPixel + 7) / 8);
newWBitmap.WritePixels(Imagerect, newArrayOfPixels, newStride, 0);
return newWBitmap;
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading