Skip to content

Commit

Permalink
优化快照生成等待时间
Browse files Browse the repository at this point in the history
  • Loading branch information
CHKZL committed Sep 9, 2024
1 parent 2a75205 commit 8b3ebb8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
39 changes: 25 additions & 14 deletions Core/RuntimeObject/Download/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,39 @@ public class Snapshot
/// </summary>
/// <param name="Uid"></param>
/// <returns></returns>
public static (bool state,string message) CreateRecordingSnapshot(long Uid)
public static (bool state, string message) CreateRecordingSnapshot(long Uid)
{
if (Uid == 0) return (false,"UID不能为空");
if (Uid == 0) return (false, "UID不能为空");

RoomCardClass Card = new();
if (!_Room.GetCardForUID(Uid, ref Card) || Card == null || !Card.DownInfo.IsDownload) return (false,"当前房间未开播或未录制");
if (!_Room.GetCardForUID(Uid, ref Card) || Card == null || !Card.DownInfo.IsDownload) return (false, "当前房间未开播或未录制");

string videoFile = Card.DownInfo.DownloadFileList.CurrentOperationVideoFile;
if (!File.Exists(videoFile)) return (false,"当前直播间还未开始录制流");
try
{
if (Card.DownInfo.DownloadFileList.SnapshotGenerationInProgress) Card.DownInfo.DownloadFileList.SnapshotGenerationInProgress = true;
else return (false, "当前房间已有快照正在生成中");

string videoFile = Card.DownInfo.DownloadFileList.CurrentOperationVideoFile;
if (!File.Exists(videoFile)) return (false, "当前直播间还未开始录制流");

string tempFile = $"{Core.Config.Core_RunConfig._TemporaryFileDirectory}{Path.GetFileName(videoFile)}";
File.Copy(videoFile, tempFile, true);
string tempFile = $"{Core.Config.Core_RunConfig._TemporaryFileDirectory}{Path.GetFileName(videoFile)}";
File.Copy(videoFile, tempFile, true);

var listener = Card.DownInfo.LiveChatListener;
if (listener != null)
var listener = Card.DownInfo.LiveChatListener;
if (listener != null)
{
Danmu.SevaDanmu(listener.DanmuMessage.Danmu, tempFile, Card.Name, Card.RoomId);
Danmu.SevaGift(listener.DanmuMessage.Gift, tempFile);
Danmu.SevaGuardBuy(listener.DanmuMessage.GuardBuy, tempFile);
Danmu.SevaSuperChat(listener.DanmuMessage.SuperChat, tempFile);
}
return (true, tempFile);
}
finally
{
Danmu.SevaDanmu(listener.DanmuMessage.Danmu, tempFile, Card.Name, Card.RoomId);
Danmu.SevaGift(listener.DanmuMessage.Gift, tempFile);
Danmu.SevaGuardBuy(listener.DanmuMessage.GuardBuy, tempFile);
Danmu.SevaSuperChat(listener.DanmuMessage.SuperChat, tempFile);
Card.DownInfo.DownloadFileList.SnapshotGenerationInProgress = false;
}
return (true,tempFile);

}
}
}
4 changes: 4 additions & 0 deletions Core/RuntimeObject/RoomInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,10 @@ public class DownloadFile
/// 当前正在进行文件写入的视频文件
/// </summary>
public string CurrentOperationVideoFile { get; set; } = string.Empty;
/// <summary>
/// 是否正在生成快照
/// </summary>
public bool SnapshotGenerationInProgress { get; set; } = false;
}

public DownloadInfo Clone()
Expand Down
27 changes: 18 additions & 9 deletions Desktop/NetWork/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class Post
/// </summary>
/// <param name="url">URL</param>
/// <param name="_dic">POST要发送的键值对</param>
/// <param name="ExpandList">需要额外携带的LongList</param>
/// <param name="TimeoutPeriod">超时时间</param>
/// <returns>请求返回体</returns>
public static async Task<T> PostBody<T>(string url, Dictionary<string, string> _dic = null)
public static async Task<T> PostBody<T>(string url, Dictionary<string, string> _dic = null, TimeSpan TimeoutPeriod = default(TimeSpan))
{

if (!string.IsNullOrEmpty(url) && url.Length > 5 && url.Substring(0, 4) != "http")
Expand All @@ -37,11 +37,11 @@ public static async Task<T> PostBody<T>(string url, Dictionary<string, string> _
try
{
Dictionary<string, string> dic = new Dictionary<string, string>
{
{ "access_key_id", Core.Config.Core_RunConfig._DesktopAccessKeyId },
{ "access_key_secret", Core.Config.Core_RunConfig._DesktopAccessKeySecret },
{ "time", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()}
};
{
{ "access_key_id", Core.Config.Core_RunConfig._DesktopAccessKeyId },
{ "access_key_secret", Core.Config.Core_RunConfig._DesktopAccessKeySecret },
{ "time", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()}
};
if (_dic != null)
{
foreach (var item in _dic)
Expand All @@ -55,12 +55,15 @@ public static async Task<T> PostBody<T>(string url, Dictionary<string, string> _
dic.Remove("access_key_secret");
using (HttpClient client = new HttpClient())
{
client.Timeout = new TimeSpan(0, 0, 8);
if (TimeoutPeriod == default(TimeSpan))
client.Timeout = new TimeSpan(0, 0, 8); // 设置默认值为8秒
else
client.Timeout = TimeoutPeriod;
var content = new FormUrlEncodedContent(dic);
var response = await client.PostAsync(url, content);
var responseString = response.Content.ReadAsStringAsync().Result;
OperationQueue.pack<T> A = JsonConvert.DeserializeObject<OperationQueue.pack<T>>(responseString);

return A.data;
}
}
Expand All @@ -81,6 +84,12 @@ public static async Task<T> PostBody<T>(string url, Dictionary<string, string> _
{
Log.Warn(nameof(PostBody), $"发起Post请求出错,URL:[{url}],错误堆栈:\r\n{ex.ToString()}", ex, false);
}

if (ex is TaskCanceledException)
{
Log.Warn(nameof(PostBody), $"发起Post请求超时,URL:[{url}]", ex);
}

return default;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Desktop/Views/Control/CardControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private void Snapshot_Task_Click(object sender, RoutedEventArgs e)
};
Task.Run(() =>
{
var message = NetWork.Post.PostBody<(bool state, string message)>($"{Config.Core_RunConfig._DesktopIP}:{Config.Core_RunConfig._DesktopPort}/api/rec_task/generate_snapshot", dic).Result;
var message = NetWork.Post.PostBody<(bool state, string message)>($"{Config.Core_RunConfig._DesktopIP}:{Config.Core_RunConfig._DesktopPort}/api/rec_task/generate_snapshot", dic,new TimeSpan(0,1,0)).Result;
if (!message.state)
{
Log.Info(nameof(Snapshot_Task_Click), $"生成直播间录制快照失败,原因:{message.message}");
Expand Down

0 comments on commit 8b3ebb8

Please sign in to comment.