This commit is contained in:
lxmou666 2021-01-05 17:46:03 +08:00
parent 290493f133
commit 6fdfc0db76
6 changed files with 233 additions and 21 deletions

View File

@ -105,7 +105,7 @@ namespace JianGongYun.TRTC.Components
mLocalView = false; mLocalView = false;
mFirstFrame = false; mFirstFrame = false;
mRenderMode = TRTCVideoFillMode.TRTCVideoFillMode_Fit; mRenderMode = TRTCVideoFillMode.TRTCVideoFillMode_Fit;
this.OnViewRemove?.Invoke(); //this.OnViewRemove?.Invoke();
this.InvalidateVisual(); this.InvalidateVisual();
} }
@ -233,7 +233,7 @@ namespace JianGongYun.TRTC.Components
/// 渲染回调byte[] data,int width, int height /// 渲染回调byte[] data,int width, int height
/// </summary> /// </summary>
public event Action<byte[], int, int> OnRenderVideoFrameHandler; public event Action<byte[], int, int> OnRenderVideoFrameHandler;
public event Action OnViewRemove; //public event Action OnViewRemove;
private void RenderFillMode(DrawingContext dc, byte[] data, int width, int height, int rotation) private void RenderFillMode(DrawingContext dc, byte[] data, int width, int height, int rotation)
{ {

View File

@ -172,7 +172,10 @@ namespace JianGongYun.TRTC
//sw.Restart(); //sw.Restart();
lock (MainFrame) lock (MainFrame)
{ {
MainFrame.Create(h, w, MatType.CV_8UC4); if (MainFrame.Cols != w || MainFrame.Rows != h)
{
MainFrame.Create(h, w, MatType.CV_8UC4);
}
Marshal.Copy(data, 0, MainFrame.Data, data.Length); Marshal.Copy(data, 0, MainFrame.Data, data.Length);
} }
//sw.Stop(); //sw.Stop();
@ -214,18 +217,21 @@ namespace JianGongYun.TRTC
var view = AddCustomVideoView(parent, CurrentClassroomEntity.TeacherId, TRTCVideoStreamType.TRTCVideoStreamTypeSub, true); var view = AddCustomVideoView(parent, CurrentClassroomEntity.TeacherId, TRTCVideoStreamType.TRTCVideoStreamTypeSub, true);
if (liveWinMode.IsLive) if (liveWinMode.IsLive)
{ {
Stopwatch sw = new Stopwatch(); //Stopwatch sw = new Stopwatch();
//VideoRecordTask(view, TRTCVideoStreamType.TRTCVideoStreamTypeSub); //VideoRecordTask(view, TRTCVideoStreamType.TRTCVideoStreamTypeSub);
view.OnRenderVideoFrameHandler += (data, w, h) => view.OnRenderVideoFrameHandler += (data, w, h) =>
{ {
sw.Restart(); //sw.Restart();
lock (SubFrame) lock (SubFrame)
{ {
SubFrame.Create(h, w, MatType.CV_8UC4); if (SubFrame.Cols != w || SubFrame.Rows != h)
{
SubFrame.Create(h, w, MatType.CV_8UC4);
}
Marshal.Copy(data, 0, SubFrame.Data, data.Length); Marshal.Copy(data, 0, SubFrame.Data, data.Length);
} }
sw.Stop(); //sw.Stop();
Debug.Print("sub" + sw.ElapsedMilliseconds.ToString()); //Debug.Print("sub" + sw.ElapsedMilliseconds.ToString());
}; };
} }
return view; return view;
@ -314,9 +320,141 @@ namespace JianGongYun.TRTC
BackgroundFrame?.Dispose(); BackgroundFrame?.Dispose();
BackgroundFrame = null; BackgroundFrame = null;
}; };
var resolution = settingWindowViewModel.MainEncParams.videoResolution.ToString().Split('_');//屏幕分辨率 var backColor = Scalar.FromRgb(0x20, 0x20, 0x20);
var fps = settingWindowViewModel.LiveFps; var resolution = settingWindowViewModel.SubEncParams.videoResolution.ToString().Split('_');//屏幕分辨率,本地储存分辨率以屏幕分享分辨率为基准
BackgroundFrame = new Mat(int.Parse(resolution[1]), int.Parse(resolution[2]), MatType.CV_8UC3, Scalar.FromRgb(0x20, 0x20, 0x20)); var fps = settingWindowViewModel.LiveFps;//视频采集的fps
BackgroundFrame = new Mat(int.Parse(resolution[2]), int.Parse(resolution[1]), MatType.CV_8UC3, backColor);//合成双路视频的背景
var delay = 1000 / (int)fps;//每帧时间
var delayEqualize = 0;//每帧时间补偿在性能和其他因素影响下delay的时间不一定充足
Task.Factory.StartNew(() =>
{
Stopwatch stopwatch = new Stopwatch();//计时器
bool onlyCameraInit = false;//只有摄像头的话背景填充为backColor变量标记只需设置一次
bool noImgInit = false;//没有画面背景也填充backColor变量标记只需设置一次
var _recoderDir = RecoderDir;
var videoFile = Path.Combine(_recoderDir, $"{Util.TimeStr()}.avi");
VideoWriter vw = new VideoWriter(videoFile, FourCC.H264, fps, BackgroundFrame.Size());
var backHeight = BackgroundFrame.Rows;//画面高度
var backWidth = BackgroundFrame.Cols;//画面宽度
//屏幕分享画面
var screenRoi = BackgroundFrame[new OpenCvSharp.Rect(0, 0, backWidth, backHeight)];
var screenMask = new Mat(backHeight, backWidth, MatType.CV_8UC1, new Scalar(1));
//摄像头小画面定位
var cameraMargin = 20;//右下角偏移量
var smallCameraSize = 150;
var smallX = backWidth - smallCameraSize - cameraMargin;
var smallY = backHeight - smallCameraSize - cameraMargin;
var smallRoi = BackgroundFrame[new OpenCvSharp.Rect(smallX, smallY, smallCameraSize, smallCameraSize)];
var smallMask = new Mat(smallCameraSize, smallCameraSize, MatType.CV_8UC1, new Scalar(1));
//摄像头大画面位置
var bigRoi = BackgroundFrame[new OpenCvSharp.Rect((backWidth - backHeight) / 2, 0, backHeight, backHeight)];
var bigMask = new Mat(backHeight, backHeight, MatType.CV_8UC1, new Scalar(1));
while (!end)
{
stopwatch.Restart();
if (!liveWinMode.ScreenRunning && !liveWinMode.CameraRunning)//有画面的中途关闭画面,清空上一帧
{
if (!noImgInit)
{
noImgInit = true;
BackgroundFrame.SetTo(backColor);
}
goto Skip2;
}
else
{
if (noImgInit)
{
noImgInit = false;
}
}
//BackgroundFrame.SetTo(backColor);
if (liveWinMode.ScreenRunning)//屏幕分享中
{
if (onlyCameraInit)
{
onlyCameraInit = false;
}
lock (SubFrame)
{
if (SubFrame.Cols != BackgroundFrame.Cols || SubFrame.Rows != BackgroundFrame.Rows)
{
goto Skip1;//图像数据不正常直接跳过
}
SubFrame.CopyTo(screenRoi, screenMask);//屏幕分享和背景一样大,直接覆盖上去
}
}
else
{
if (!onlyCameraInit)//双画面的中途关闭屏幕画面,清空上一帧
{
onlyCameraInit = true;
BackgroundFrame.SetTo(backColor);
}
}
Skip1:
if (liveWinMode.CameraRunning)//摄像头分享中
{
lock (MainFrame)
{
if (MainFrame.Cols <= 0 || MainFrame.Rows <= 0)
{
goto Skip2;//图像数据不正常直接跳过
}
var mainHeight = MainFrame.Rows;//摄像头画面高度
var mainWidth = MainFrame.Cols;//摄像头画面宽度
var dst = MainFrame[new OpenCvSharp.Rect((mainWidth - mainHeight) / 2, 0, mainHeight, mainHeight)];//采集大小为原图高的居中正方形,程序分辨率只实现了横屏,因此宽大于高
if (liveWinMode.ScreenRunning)//屏幕分享中摄像头150正方形大小并悬浮再右下角
{
dst = dst.Resize(new OpenCvSharp.Size(smallCameraSize, smallCameraSize), interpolation: InterpolationFlags.Area);
dst.CopyTo(smallRoi, smallMask);
}
else//只有摄像头,摄像头画面全屏正方形
{
dst = dst.Resize(new OpenCvSharp.Size(backHeight, backHeight), interpolation: InterpolationFlags.Linear);
var roi = new Mat(BackgroundFrame, new OpenCvSharp.Rect(0, 0, dst.Width, dst.Height));
dst.CopyTo(roi);
Cv2.ImShow("123", BackgroundFrame);
Cv2.WaitKey(2);
}
dst.Dispose();
}
}
Skip2:
stopwatch.Stop();
Debug.Print($"video frame run {stopwatch.ElapsedMilliseconds}");
var sleep = delay - (int)stopwatch.ElapsedMilliseconds;//每帧时间减去每帧处理时间为sleep时间
if (sleep < 0)//如果处理时间超过了每帧时间,记录下来
{
delayEqualize += sleep;
}
sleep += delayEqualize;//理论休眠时间再去掉补偿时间
if (sleep > 0)
{
Thread.Sleep(sleep);
}
}
}, TaskCreationOptions.LongRunning);//新开线程
//Stopwatch sw1 = new Stopwatch(); //Stopwatch sw1 = new Stopwatch();
//Stopwatch sw2 = new Stopwatch(); //Stopwatch sw2 = new Stopwatch();

View File

@ -235,6 +235,7 @@ namespace JianGongYun.TRTC.ViewModels
} }
} }
string[] excluded = new string[] { "Notifiaction" };
private SIZE thumbSize = new SIZE { cx = 300, cy = 200 }; private SIZE thumbSize = new SIZE { cx = 300, cy = 200 };
private SIZE iconSize = new SIZE { cx = 50, cy = 50 }; private SIZE iconSize = new SIZE { cx = 50, cy = 50 };
/// <summary> /// <summary>
@ -253,6 +254,10 @@ namespace JianGongYun.TRTC.ViewModels
for (uint i = 0; i < count; i++) for (uint i = 0; i < count; i++)
{ {
var info = temp.getSourceInfo(i); var info = temp.getSourceInfo(i);
if (string.IsNullOrWhiteSpace(info.sourceName) || excluded.Contains(info.sourceName))
{
continue;
}
//var icon = Util.ToWriteableBitmap(ref info.iconBGRA); //var icon = Util.ToWriteableBitmap(ref info.iconBGRA);
//Console.WriteLine($"get LiveScreens {i} {info.sourceId}"); //Console.WriteLine($"get LiveScreens {i} {info.sourceId}");
var thumb = info.thumbBGRA.ToWriteableBitmap(); var thumb = info.thumbBGRA.ToWriteableBitmap();
@ -329,7 +334,7 @@ namespace JianGongYun.TRTC.ViewModels
get get
{ {
var style = new Style(typeof(Border)); var style = new Style(typeof(Border));
if (CameraRunning &&ScreenRunning) if (CameraRunning && ScreenRunning)
{ {
style.Setters.Add(new Setter { Property = FrameworkElement.HorizontalAlignmentProperty, Value = HorizontalAlignment.Right }); style.Setters.Add(new Setter { Property = FrameworkElement.HorizontalAlignmentProperty, Value = HorizontalAlignment.Right });
style.Setters.Add(new Setter { Property = FrameworkElement.VerticalAlignmentProperty, Value = VerticalAlignment.Bottom }); style.Setters.Add(new Setter { Property = FrameworkElement.VerticalAlignmentProperty, Value = VerticalAlignment.Bottom });

View File

@ -82,16 +82,16 @@
</Button> </Button>
<StackPanel Visibility="{Binding IsLive, Converter={StaticResource VisibilityOfBool}}" Orientation="Horizontal" Canvas.Right="27" Canvas.Top="10" Height="50"> <StackPanel Visibility="{Binding IsLive, Converter={StaticResource VisibilityOfBool}}" Orientation="Horizontal" Canvas.Right="27" Canvas.Top="10" Height="50">
<Canvas HorizontalAlignment="Right" Height="50" Width="50"> <Canvas HorizontalAlignment="Right" Height="50" Width="50">
<Metro:AduSysButton ToolTip="禁用/启用麦克风静音" Foreground="#aaaaaa" IconHeight="25" IconWidth="18" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Mic}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用麦克风静音" Click="SetMute_Click" Foreground="#aaaaaa" IconHeight="25" IconWidth="18" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Mic}" Content="" />
<Metro:AduSysButton ToolTip="禁用/启用麦克风静音" Visibility="{Binding MicMute, Converter={StaticResource VisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用麦克风静音" Click="SetMute_Click" Visibility="{Binding MicMute, Converter={StaticResource VisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" />
</Canvas> </Canvas>
<Canvas HorizontalAlignment="Right" Height="50" Width="50"> <Canvas HorizontalAlignment="Right" Height="50" Width="50">
<Metro:AduSysButton ToolTip="禁用/启用摄像头" Foreground="#aaaaaa" IconHeight="25" IconWidth="20" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Camera}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用摄像头" Click="SetCamera_Click" Foreground="#aaaaaa" IconHeight="25" IconWidth="20" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Camera}" Content="" />
<Metro:AduSysButton ToolTip="禁用/启用摄像头" Visibility="{Binding CameraRunning, Converter={StaticResource UnVisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用摄像头" Click="SetCamera_Click" Visibility="{Binding CameraRunning, Converter={StaticResource UnVisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" />
</Canvas> </Canvas>
<Canvas HorizontalAlignment="Right" Height="50" Width="50"> <Canvas HorizontalAlignment="Right" Height="50" Width="50">
<Metro:AduSysButton ToolTip="禁用/启用桌面分享" Foreground="#aaaaaa" IconHeight="25" IconWidth="24" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Screen}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用桌面分享" Click="SetScreen_Click" Foreground="#aaaaaa" IconHeight="25" IconWidth="24" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Screen}" Content="" />
<Metro:AduSysButton ToolTip="禁用/启用桌面分享" Visibility="{Binding ScreenRunning, Converter={StaticResource UnVisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" /> <Metro:AduSysButton ToolTip="禁用/启用桌面分享" Click="SetScreen_Click" Visibility="{Binding ScreenRunning, Converter={StaticResource UnVisibilityOfBool}}" Foreground="#aaaaaa" IconHeight="25" IconWidth="25" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_Ban}" Content="" />
</Canvas> </Canvas>
<Canvas HorizontalAlignment="Right" Height="50" Width="50"> <Canvas HorizontalAlignment="Right" Height="50" Width="50">
<Metro:AduSysButton Click="ChangeWin_Click" ToolTip="切换分享窗口" Foreground="#aaaaaa" IconHeight="25" IconWidth="24" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_ChangeScreen}" Content="" /> <Metro:AduSysButton Click="ChangeWin_Click" ToolTip="切换分享窗口" Foreground="#aaaaaa" IconHeight="25" IconWidth="24" CornerRadius="6" Height="50" Width="50" Icon="{StaticResource Icon_ChangeScreen}" Content="" />
@ -167,6 +167,12 @@
<!--直播中--> <!--直播中-->
<Grid Canvas.Top="0" Canvas.Left="0" Height="{Binding ElementName=CusContent,Path=ActualHeight}" Width="{Binding ElementName=CusContent,Path=ActualWidth}" Visibility="{Binding IsLive, Converter={StaticResource VisibilityOfBool}}"> <Grid Canvas.Top="0" Canvas.Left="0" Height="{Binding ElementName=CusContent,Path=ActualHeight}" Width="{Binding ElementName=CusContent,Path=ActualWidth}" Visibility="{Binding IsLive, Converter={StaticResource VisibilityOfBool}}">
<Canvas x:Name="AfterLivePanel"> <Canvas x:Name="AfterLivePanel">
<Grid Visibility="{Binding MicMute, Converter={StaticResource UnVisibilityOfBool}}" Width="{Binding ElementName=AfterLivePanel,Path=ActualWidth}" Height="{Binding ElementName=AfterLivePanel,Path=ActualHeight}" Canvas.Top="0" Canvas.Left="0">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#aaaaaa" FontSize="14" >仅开启麦克风</TextBlock>
</Grid>
<Grid Visibility="{Binding MicMute, Converter={StaticResource VisibilityOfBool}}" Width="{Binding ElementName=AfterLivePanel,Path=ActualWidth}" Height="{Binding ElementName=AfterLivePanel,Path=ActualHeight}" Canvas.Top="0" Canvas.Left="0">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#aaaaaa" FontSize="14" >所有设备被关闭</TextBlock>
</Grid>
<!--屏幕预览容器--> <!--屏幕预览容器-->
<Grid x:Name="AfterLiveSubViewWrap" Width="{Binding ElementName=AfterLivePanel,Path=ActualWidth}" Height="{Binding ElementName=AfterLivePanel,Path=ActualHeight}" Canvas.Top="0" Canvas.Left="0"></Grid> <Grid x:Name="AfterLiveSubViewWrap" Width="{Binding ElementName=AfterLivePanel,Path=ActualWidth}" Height="{Binding ElementName=AfterLivePanel,Path=ActualHeight}" Canvas.Top="0" Canvas.Left="0"></Grid>
<!--摄像头预览容器--> <!--摄像头预览容器-->

View File

@ -32,6 +32,7 @@ namespace JianGongYun.TRTC.Windows
public LiveWindow() public LiveWindow()
{ {
InitializeComponent(); InitializeComponent();
NoticeManager.Initialize();
//BeforeLiveSubViewWrap.SizeChanged += BeforeLiveSubViewWrap_SizeChanged; //BeforeLiveSubViewWrap.SizeChanged += BeforeLiveSubViewWrap_SizeChanged;
//AfterLiveSubViewWrap.SizeChanged += AfterLiveSubViewWrap_SizeChanged; //AfterLiveSubViewWrap.SizeChanged += AfterLiveSubViewWrap_SizeChanged;
AttachConsole(-1);//把进程挂在控制台,通过命令行启动程序可以看到控制台输出 AttachConsole(-1);//把进程挂在控制台,通过命令行启动程序可以看到控制台输出
@ -271,11 +272,25 @@ namespace JianGongYun.TRTC.Windows
} }
else else
{ {
LiveClassroom.StartVideoSub(BeforeLiveSubViewWrap); if (LiveWindowViewModel.IsLive)//预览和直播是不同的容器
{
LiveClassroom.StartVideoSub(AfterLiveSubViewWrap);
}
else
{
LiveClassroom.StartVideoSub(BeforeLiveSubViewWrap);
}
} }
if (LiveWindowViewModel.LiveType == Models.LiveTypeEnum.CameraAndScreen) if (LiveWindowViewModel.LiveType == Models.LiveTypeEnum.CameraAndScreen)
{ {
LiveClassroom.StartVideoMain(BeforeLiveViewWrap); if (LiveWindowViewModel.IsLive)//预览和直播是不同的容器
{
LiveClassroom.StartVideoMain(AfterLiveViewWrap);
}
else
{
LiveClassroom.StartVideoMain(BeforeLiveViewWrap);
}
} }
} }
@ -284,5 +299,53 @@ namespace JianGongYun.TRTC.Windows
LiveWindowViewModel.ShowShareScreenList = true; LiveWindowViewModel.ShowShareScreenList = true;
this.Dispatcher.Invoke(new Action(() => LiveWindowViewModel.LoadAllScreen())); this.Dispatcher.Invoke(new Action(() => LiveWindowViewModel.LoadAllScreen()));
} }
private void SetMute_Click(object sender, RoutedEventArgs e)
{
LiveClassroom.SetMicMute(null);
NoticeManager.NotifiactionShow.AddNotifiaction(new NotifiactionModel()
{
Title = "提醒",
Content = $"{(LiveWindowViewModel.MicMute ? "" : "")}静音成功"
});
}
private void SetCamera_Click(object sender, RoutedEventArgs e)
{
if (LiveWindowViewModel.CameraRunning)
{
LiveClassroom.StopVideoMain(AfterLiveViewWrap);
}
else
{
LiveClassroom.StartVideoMain(AfterLiveViewWrap);
}
NoticeManager.NotifiactionShow.AddNotifiaction(new NotifiactionModel()
{
Title = "提醒",
Content = $"{(LiveWindowViewModel.CameraRunning ? "" : "")}摄像头成功"
});
}
private void SetScreen_Click(object sender, RoutedEventArgs e)
{
if (LiveWindowViewModel.ScreenRunning)
{
LiveClassroom.StopVideoSub(AfterLiveSubViewWrap);
}
else
{
if (LiveWindowViewModel.CurrentShareScreen == null)//没选择过窗口需要选一次
{
LiveWindowViewModel.ShowShareScreenList = true;
this.Dispatcher.Invoke(new Action(() => LiveWindowViewModel.LoadAllScreen()));
}
LiveClassroom.StartVideoSub(AfterLiveSubViewWrap);
}
NoticeManager.NotifiactionShow.AddNotifiaction(new NotifiactionModel()
{
Title = "提醒",
Content = $"{(LiveWindowViewModel.ScreenRunning ? "" : "")}屏幕分享成功"
});
}
} }
} }

View File

@ -30,7 +30,7 @@
<StackPanel> <StackPanel>
<DockPanel Style="{StaticResource SettingItem}"> <DockPanel Style="{StaticResource SettingItem}">
<TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 摄像头</TextBlock> <TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 摄像头</TextBlock>
<Metro:AduComboBox ItemsSource="{Binding CameraList,Mode=OneWay}" DisplayMemberPath="Text" SelectedValuePath="Id" SelectedValue="{Binding CurrentMic,Mode=TwoWay}" ComBoxItemPanelBackground="{StaticResource ComboBoxBrush}"></Metro:AduComboBox> <Metro:AduComboBox ItemsSource="{Binding CameraList,Mode=OneWay}" DisplayMemberPath="Text" SelectedValuePath="Id" SelectedValue="{Binding CurrentCamera,Mode=TwoWay}" ComBoxItemPanelBackground="{StaticResource ComboBoxBrush}"></Metro:AduComboBox>
</DockPanel> </DockPanel>
<DockPanel Style="{StaticResource SettingItem}"> <DockPanel Style="{StaticResource SettingItem}">
<TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 麦克风</TextBlock> <TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 麦克风</TextBlock>