Core: Implement watchdog, use api host in config

This commit is contained in:
Genteure 2021-04-15 19:13:19 +08:00
parent c6eae11f95
commit ec58a3c5b9
2 changed files with 20 additions and 9 deletions

View File

@ -95,7 +95,7 @@ namespace BililiveRecorder.Core.Api.Http
if (this.disposedValue) if (this.disposedValue)
throw new ObjectDisposedException(nameof(HttpApiClient)); throw new ObjectDisposedException(nameof(HttpApiClient));
var url = $@"https://api.live.bilibili.com/room/v1/Room/get_info?id={roomid}"; var url = $@"{this.config.LiveApiHost}/room/v1/Room/get_info?id={roomid}";
return FetchAsync<RoomInfo>(this.mainClient, url); return FetchAsync<RoomInfo>(this.mainClient, url);
} }
@ -104,7 +104,7 @@ namespace BililiveRecorder.Core.Api.Http
if (this.disposedValue) if (this.disposedValue)
throw new ObjectDisposedException(nameof(HttpApiClient)); throw new ObjectDisposedException(nameof(HttpApiClient));
var url = $@"https://api.live.bilibili.com/live_user/v1/UserInfo/get_anchor_in_room?roomid={roomid}"; var url = $@"{this.config.LiveApiHost}/live_user/v1/UserInfo/get_anchor_in_room?roomid={roomid}";
return FetchAsync<UserInfo>(this.mainClient, url); return FetchAsync<UserInfo>(this.mainClient, url);
} }
@ -113,7 +113,7 @@ namespace BililiveRecorder.Core.Api.Http
if (this.disposedValue) if (this.disposedValue)
throw new ObjectDisposedException(nameof(HttpApiClient)); throw new ObjectDisposedException(nameof(HttpApiClient));
var url = $@"https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id={roomid}&protocol=0%2C1&format=0%2C2&codec=0%2C1&qn=10000&platform=web&ptype=16"; var url = $@"{this.config.LiveApiHost}/xlive/web-room/v2/index/getRoomPlayInfo?room_id={roomid}&protocol=0%2C1&format=0%2C2&codec=0%2C1&qn=10000&platform=web&ptype=16";
return FetchAsync<RoomPlayInfo>(this.mainClient, url); return FetchAsync<RoomPlayInfo>(this.mainClient, url);
} }
@ -122,7 +122,7 @@ namespace BililiveRecorder.Core.Api.Http
if (this.disposedValue) if (this.disposedValue)
throw new ObjectDisposedException(nameof(HttpApiClient)); throw new ObjectDisposedException(nameof(HttpApiClient));
var url = $@"https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id={roomid}&type=0"; var url = $@"{this.config.LiveApiHost}/xlive/web-room/v1/index/getDanmuInfo?id={roomid}&type=0";
return FetchAsync<DanmuInfo>(this.anonClient, url); return FetchAsync<DanmuInfo>(this.anonClient, url);
} }

View File

@ -25,8 +25,9 @@ namespace BililiveRecorder.Core.Recording
private const string HttpHeaderReferer = "https://live.bilibili.com/"; private const string HttpHeaderReferer = "https://live.bilibili.com/";
private const string HttpHeaderUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"; private const string HttpHeaderUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36";
private const int timer_inverval = 2;
private readonly Timer timer = new Timer(1000 * timer_inverval);
private readonly Random random = new Random(); private readonly Random random = new Random();
private readonly Timer timer = new Timer(1000 * 2);
private readonly CancellationTokenSource cts = new CancellationTokenSource(); private readonly CancellationTokenSource cts = new CancellationTokenSource();
private readonly CancellationToken ct; private readonly CancellationToken ct;
@ -52,7 +53,8 @@ namespace BililiveRecorder.Core.Recording
private readonly object fillerStatsLock = new object(); private readonly object fillerStatsLock = new object();
private int fillerDownloadedBytes; private int fillerDownloadedBytes;
private DateTimeOffset fillerLastStatsTrigger; private DateTimeOffset fillerStatsLastTrigger;
private TimeSpan durationSinceNoDataReceived;
public RecordTask(IRoom room, public RecordTask(IRoom room,
ILogger logger, ILogger logger,
@ -149,7 +151,8 @@ namespace BililiveRecorder.Core.Recording
}); });
}; };
this.fillerLastStatsTrigger = DateTimeOffset.UtcNow; this.fillerStatsLastTrigger = DateTimeOffset.UtcNow;
this.durationSinceNoDataReceived = TimeSpan.Zero;
this.filler = Task.Run(async () => await this.FillPipeAsync(stream, pipe.Writer).ConfigureAwait(false)); this.filler = Task.Run(async () => await this.FillPipeAsync(stream, pipe.Writer).ConfigureAwait(false));
_ = Task.Run(this.RecordingLoopAsync); _ = Task.Run(this.RecordingLoopAsync);
@ -202,9 +205,11 @@ namespace BililiveRecorder.Core.Recording
{ {
bytes = Interlocked.Exchange(ref this.fillerDownloadedBytes, 0); bytes = Interlocked.Exchange(ref this.fillerDownloadedBytes, 0);
end = DateTimeOffset.UtcNow; end = DateTimeOffset.UtcNow;
start = this.fillerLastStatsTrigger; start = this.fillerStatsLastTrigger;
this.fillerLastStatsTrigger = end; this.fillerStatsLastTrigger = end;
diff = end - start; diff = end - start;
this.durationSinceNoDataReceived = bytes > 0 ? TimeSpan.Zero : this.durationSinceNoDataReceived + diff;
} }
var mbps = bytes * 8d / 1024d / 1024d / diff.TotalSeconds; var mbps = bytes * 8d / 1024d / 1024d / diff.TotalSeconds;
@ -217,6 +222,12 @@ namespace BililiveRecorder.Core.Recording
EndTime = end, EndTime = end,
Mbps = mbps Mbps = mbps
}); });
if (this.durationSinceNoDataReceived.TotalMilliseconds > this.room.RoomConfig.TimingWatchdogTimeout)
{
this.logger.Warning("直播服务器未断开连接但停止发送直播数据,将会主动断开连接");
this.RequestStop();
}
} }
private void Writer_BeforeScriptTagWrite(ScriptTagBody scriptTagBody) private void Writer_BeforeScriptTagWrite(ScriptTagBody scriptTagBody)