This commit is contained in:
lxmou666 2021-01-06 17:02:59 +08:00
parent cdd02b0c05
commit fb28b4cc89
7 changed files with 119 additions and 66 deletions

View File

@ -56,6 +56,6 @@
</PackageReference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="set Platform=Win64&#xD;&#xA;SETLOCAL ENABLEDELAYEDEXPANSION&#xD;&#xA;if $(PlatformName)==x86 ( &#xD;&#xA;set Platform=Win32&#xD;&#xA;)&#xD;&#xA;copy /Y &quot;$(ProjectDir)TRTC_SDK\!Platform!\lib\*.dll&quot; &quot;$(ProjectDir)$(OutDir)&quot;&#xD;&#xA;ENDLOCAL" />
<Exec Command="set Platform=Win64&#xD;&#xA;SETLOCAL ENABLEDELAYEDEXPANSION&#xD;&#xA;if $(PlatformName)==x86 ( &#xD;&#xA;set Platform=Win32&#xD;&#xA;)&#xD;&#xA;copy /Y &quot;$(ProjectDir)TRTC_SDK\!Platform!\lib\*.dll&quot; &quot;$(ProjectDir)$(OutDir)&quot;&#xD;&#xA;copy /Y &quot;$(ProjectDir)TRTC_SDK\!Platform!\lib\*.exe&quot; &quot;$(ProjectDir)$(OutDir)&quot;&#xD;&#xA;ENDLOCAL" />
</Target>
</Project>

View File

@ -163,6 +163,9 @@ namespace JianGongYun.TRTC.Components
{
if (!mFirstFrame)
mFirstFrame = true;
OnRenderVideoFrameHandler?.Invoke(data, width, height);
if (mPause)
return false;
if (data == null || data.Length <= 0)
@ -237,7 +240,6 @@ namespace JianGongYun.TRTC.Components
private void RenderFillMode(DrawingContext dc, byte[] data, int width, int height, int rotation)
{
OnRenderVideoFrameHandler?.Invoke(data, width, height);
int viewWidth = (int)this.ActualWidth, viewHeight = (int)this.ActualHeight;
PixelFormat pixelFormat = PixelFormats.Pbgra32;
int bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;
@ -271,7 +273,6 @@ namespace JianGongYun.TRTC.Components
private void RenderFitMode(DrawingContext dc, byte[] data, int width, int height, int rotation)
{
OnRenderVideoFrameHandler?.Invoke(data, width, height);
int viewWidth = (int)this.ActualWidth, viewHeight = (int)this.ActualHeight;
PixelFormat pixelFormat = PixelFormats.Pbgra32;
int bytesPerPixel = (pixelFormat.BitsPerPixel + 7) / 8;

View File

@ -99,7 +99,6 @@ namespace JianGongYun.TRTC
if (!string.IsNullOrEmpty(currentMic))
{
var res = lTXDeviceManager.setCurrentDevice(TRTCDeviceType.TXMediaDeviceTypeMic, currentMic);
Console.WriteLine($"设置麦克风:{res}");
}
lTXDeviceManager.setCurrentDeviceVolume(TRTCDeviceType.TXMediaDeviceTypeMic, settingWindowViewModel.MicVolume);//麦克风采集音量
lTRTCCloud.setSystemAudioLoopbackVolume(settingWindowViewModel.SytemGatherVolume);//系统声音采集音量
@ -165,11 +164,8 @@ namespace JianGongYun.TRTC
var view = AddCustomVideoView(parent, CurrentClassroomEntity.TeacherId, TRTCVideoStreamType.TRTCVideoStreamTypeBig, true);
if (liveWinMode.IsLive)
{
//Stopwatch sw = new Stopwatch();
//VideoRecordTask(view, TRTCVideoStreamType.TRTCVideoStreamTypeBig);
view.OnRenderVideoFrameHandler += (data, w, h) =>
{
//sw.Restart();
lock (MainFrame)
{
if (MainFrame.Cols != w || MainFrame.Rows != h)
@ -178,8 +174,6 @@ namespace JianGongYun.TRTC
}
Marshal.Copy(data, 0, MainFrame.Data, data.Length);
}
//sw.Stop();
//Debug.Print("main" + sw.ElapsedMilliseconds.ToString());
};
}
return view;
@ -221,11 +215,8 @@ namespace JianGongYun.TRTC
var view = AddCustomVideoView(parent, CurrentClassroomEntity.TeacherId, TRTCVideoStreamType.TRTCVideoStreamTypeSub, true);
if (liveWinMode.IsLive)
{
//Stopwatch sw = new Stopwatch();
//VideoRecordTask(view, TRTCVideoStreamType.TRTCVideoStreamTypeSub);
view.OnRenderVideoFrameHandler += (data, w, h) =>
{
//sw.Restart();
lock (SubFrame)
{
if (SubFrame.Cols != w || SubFrame.Rows != h)
@ -234,8 +225,6 @@ namespace JianGongYun.TRTC
}
Marshal.Copy(data, 0, SubFrame.Data, data.Length);
}
//sw.Stop();
//Debug.Print("sub" + sw.ElapsedMilliseconds.ToString());
};
}
return view;
@ -280,25 +269,27 @@ namespace JianGongYun.TRTC
{
liveWinMode.MicRunning = true;
lTRTCCloud.startLocalAudio(settingWindowViewModel.LiveAudioLevel);
//if (liveWinMode.IsLive && !liveWinMode.AudioRecordRunning)
//{
// liveWinMode.AudioRecordRunning = true;
// //var time = Util.TimeStr();
// var pars = new TRTCAudioRecordingParams { filePath = Path.Combine(RecoderDir, $"temp_audio.aac") };
// var res = lTRTCCloud.startAudioRecording(ref pars);
// //Console.WriteLine(res);
//}
StartRecordAudio();
}
}
/// <summary>
/// 录音
/// </summary>
public static void StartRecordAudio()
{
if (liveWinMode.IsLive && !liveWinMode.AudioRecordRunning)
{
liveWinMode.AudioRecordRunning = true;
//var time = Util.TimeStr();
var pars = new TRTCAudioRecordingParams { filePath = Path.Combine(RecoderDir, $"temp_audio.aac") };
var res = lTRTCCloud.startAudioRecording(ref pars);
//Console.WriteLine(res);
lTRTCCloud.startAudioRecording(ref pars);
}
}
public static void StopRecordAudio()
{
if (liveWinMode.AudioRecordRunning)
{
liveWinMode.AudioRecordRunning = false;
lTRTCCloud.stopAudioRecording();
}
}
/// <summary>
@ -309,11 +300,7 @@ namespace JianGongYun.TRTC
if (liveWinMode.MicRunning)
{
liveWinMode.MicRunning = false;
if (liveWinMode.AudioRecordRunning)
{
liveWinMode.AudioRecordRunning = false;
lTRTCCloud.stopAudioRecording();
}
StopRecordAudio();
lTRTCCloud.stopLocalAudio();
}
}
@ -337,31 +324,41 @@ namespace JianGongYun.TRTC
var fps = settingWindowViewModel.LiveFps;//视频采集的fps
BackgroundFrame = new Mat(int.Parse(resolution[2]), int.Parse(resolution[1]), MatType.CV_8UC4, backColor);//合成双路视频的背景
var delay = 1000 / (int)fps;//每帧时间
var delayEqualize = 0;//每帧时间补偿在性能和其他因素影响下delay的时间不一定充足
var _recoderDir = RecoderDir;
var videoFile = Path.Combine(_recoderDir, $"temp_video.avi");
//var videoFrameTemp = Path.Combine(_recoderDir, $"videoFrameTemp.bmp");
var backHeight = BackgroundFrame.Rows;//画面高度
var backWidth = BackgroundFrame.Cols;//画面宽度
VideoWriter vw = new VideoWriter(videoFile, FourCC.H264, fps, BackgroundFrame.Size());
var end = false;
ConcurrentQueue<Mat> mats = new ConcurrentQueue<Mat>();
//int runFps = 0;//实时帧
//Timer timer = new Timer((a) =>
//{
// Debug.Print($"runFps {runFps}. leavingsMat {mats.Count}.");
// Interlocked.Exchange(ref runFps, 0);
//}, null, 0, 1000);
onEnd = () =>
{
end = true;
//timer.Dispose();
};
//帧合并线程
Task.Factory.StartNew(() =>
{
Stopwatch stopwatch = new Stopwatch();//计时器
Stopwatch stopwatch = new Stopwatch();//计时器
bool onlyCameraInit = false;//只有摄像头的话背景填充为backColor变量标记只需设置一次
bool noImgInit = false;//没有画面背景也填充backColor变量标记只需设置一次
var delayEqualize = 0;//每帧时间补偿在性能和其他因素影响下delay的时间不一定充足
//屏幕分享画面
var screenRoi = BackgroundFrame[new OpenCvSharp.Rect(0, 0, backWidth, backHeight)];
@ -376,7 +373,10 @@ namespace JianGongYun.TRTC
//摄像头大画面位置
var bigRoi = BackgroundFrame[new OpenCvSharp.Rect((backWidth - backHeight) / 2, 0, backHeight, backHeight)];
StartRecordAudio();
Debug.Print("开始处理帧线程");
while (!end)
{
stopwatch.Restart();
@ -452,38 +452,89 @@ namespace JianGongYun.TRTC
Skip2:
var temp = BackgroundFrame.CvtColor(ColorConversionCodes.BGRA2BGR);
vw.Write(temp);
temp.Dispose();
mats.Enqueue(BackgroundFrame.CvtColor(ColorConversionCodes.BGRA2BGR));
//Interlocked.Increment(ref runFps);
stopwatch.Stop();
var aa = $"video frame run {stopwatch.ElapsedMilliseconds}";
//Debug.Print(aa);
Console.WriteLine(aa);
var sleep = delay - (int)stopwatch.ElapsedMilliseconds;//每帧时间减去每帧处理时间为sleep时间
if (sleep < 0)//如果处理时间超过了每帧时间,记录下来
//if (sleep < 0)//如果处理时间超过了每帧时间,记录下来
//{
// delayEqualize += sleep;
//}
//if (delayEqualize < 0 && sleep > 0)
//{
// delayEqualize += sleep;
// if (delayEqualize > 0)
// {
// delayEqualize = 0;
// }
//}
//var lastsleep = sleep + delayEqualize;//理论休眠时间再去掉补偿时间
//Debug.Print($"video frame run {stopwatch.ElapsedMilliseconds}. lastsleep {lastsleep}. sum {(stopwatch.ElapsedMilliseconds + lastsleep)}");
//if (lastsleep > 0)
//{
// Thread.Sleep(lastsleep);
//}
if (liveWinMode.IsLive && sleep > 0)
{
delayEqualize += sleep;
}
if (delayEqualize < 0 && sleep > 0)
{
delayEqualize += sleep;
if (delayEqualize > 0)
{
delayEqualize = 0;
}
}
var lastsleep = sleep + delayEqualize;//理论休眠时间再去掉补偿时间
if (lastsleep > 0)
{
Thread.Sleep(lastsleep);
Thread.Sleep(sleep);
}
}
BackgroundFrame?.Dispose();
BackgroundFrame = null;
vw.Dispose();
Debug.Print("录制结束");
Debug.Print("结束处理帧线程");
}, TaskCreationOptions.LongRunning);//新开线程
//新开线程写文件,减少帧处理时间
Task.Factory.StartNew(() =>
{
var delayEqualize = 0;//每帧时间补偿在性能和其他因素影响下delay的时间不一定充足
VideoWriter vw = new VideoWriter(videoFile, FourCC.H264, fps, BackgroundFrame.Size());
Stopwatch stopwatch = new Stopwatch();//计时器
Debug.Print("开始写入视频线程");
while (!end || mats.Count > 0)
{
if (mats.Count > 0)
{
if (mats.TryDequeue(out var frame))
{
stopwatch.Restart();
vw.Write(frame);
frame.Dispose();
stopwatch.Stop();
Debug.Print($"video frame write {stopwatch.ElapsedMilliseconds}");
//var sleep = delay - (int)stopwatch.ElapsedMilliseconds;//每帧时间减去每帧处理时间为sleep时间
//if (sleep < 0)//如果处理时间超过了每帧时间,记录下来
//{
// delayEqualize += sleep;
//}
//if (delayEqualize < 0 && sleep > 0)
//{
// delayEqualize += sleep;
// if (delayEqualize > 0)
// {
// delayEqualize = 0;
// }
//}
//var lastsleep = sleep + delayEqualize;//理论休眠时间再去掉补偿时间
//if (lastsleep > 0)
//{
// Thread.Sleep(lastsleep);
//}
}
else
{
Thread.Sleep(delay);
}
}
else
{
Thread.Sleep(delay);
}
}
vw.Dispose();
Debug.Print("结束写入视频线程");
}, TaskCreationOptions.LongRunning);
}
/// <summary>

View File

@ -52,14 +52,14 @@ namespace JianGongYun.TRTC.Windows
Rad2.Foreground = color;
Rad3.Foreground = color;
Rad4.Foreground = color;
//LiveClassroom.PauseAllView(false);//切前台启动实时预览渲染
LiveClassroom.PauseAllView(false);//切前台启动实时预览渲染
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
//LiveClassroom.PauseAllView(true);//切后台停止实时预览渲染
LiveClassroom.PauseAllView(true);//切后台停止实时预览渲染
}
private Window settingWindow;

View File

@ -103,6 +103,7 @@
<DockPanel Style="{StaticResource SettingItem}">
<TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 录制倒计时</TextBlock>
<Metro:AduComboBox Margin="0 0 10 0" SelectedValuePath="Tag" SelectedValue="{Binding ScreenRecordingCountdown}" ComBoxItemPanelBackground="{StaticResource ComboBoxBrush}">
<ComboBoxItem Content="1s" Tag="1" />
<ComboBoxItem Content="3s" Tag="3" />
<ComboBoxItem Content="5s" Tag="5"/>
<ComboBoxItem Content="7s" Tag="7"/>
@ -136,8 +137,8 @@
<TextBlock Style="{StaticResource SettingItemText}" DockPanel.Dock="Left"> 帧率(fps)</TextBlock>
<Metro:AduComboBox Margin="0 0 10 0" SelectedValuePath="Content" SelectedValue="{Binding LiveFps}" ComBoxItemPanelBackground="{StaticResource ComboBoxBrush}">
<ComboBoxItem Content="10" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="15" />
<!--<ComboBoxItem Content="12" />
<ComboBoxItem Content="15" />-->
<ComboBoxItem Content="20" />
<!--<ComboBoxItem Content="24" />-->
</Metro:AduComboBox>

Binary file not shown.

Binary file not shown.