Add more webhook types

Added StreamStarted and StreamEnded
Close #269
This commit is contained in:
genteure 2022-05-17 18:08:53 +08:00
parent a993fca683
commit 34ba4ded08
15 changed files with 140 additions and 53 deletions

View File

@ -30,6 +30,8 @@ namespace BililiveRecorder.Cli
{
private static int Main(string[] args)
{
ServicePointManager.Expect100Continue = false;
var cmd_run = new Command("run", "Run BililiveRecorder in standard mode")
{
new Option<string?>(new []{ "--web-bind", "--bind", "-b" }, () => null, "Bind address for web api"),

View File

@ -0,0 +1,9 @@
using System;
namespace BililiveRecorder.Core.Event
{
public interface IRecordSessionEventArgs
{
Guid SessionId { get; }
}
}

View File

@ -4,9 +4,7 @@ namespace BililiveRecorder.Core.Event
{
public abstract class RecordEventArgsBase : EventArgs
{
public RecordEventArgsBase() { }
public RecordEventArgsBase(IRoom room)
protected RecordEventArgsBase(IRoom room)
{
this.RoomId = room.RoomConfig.RoomId;
this.ShortId = room.ShortId;
@ -14,20 +12,20 @@ namespace BililiveRecorder.Core.Event
this.Title = room.Title;
this.AreaNameParent = room.AreaNameParent;
this.AreaNameChild = room.AreaNameChild;
this.Recording = room.Recording;
this.Streaming = room.Streaming;
this.DanmakuConnected = room.DanmakuConnected;
}
public Guid SessionId { get; set; }
public int RoomId { get; protected set; }
public int ShortId { get; protected set; }
public string Name { get; protected set; } = string.Empty;
public string Title { get; protected set; } = string.Empty;
public string AreaNameParent { get; protected set; } = string.Empty;
public string AreaNameChild { get; protected set; } = string.Empty;
public int RoomId { get; set; }
public int ShortId { get; set; }
public string Name { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string AreaNameParent { get; set; } = string.Empty;
public string AreaNameChild { get; set; } = string.Empty;
public bool Recording { get; protected set; }
public bool Streaming { get; protected set; }
public bool DanmakuConnected { get; protected set; }
}
}

View File

@ -1,13 +1,17 @@
using System;
using BililiveRecorder.Core.SimpleWebhook;
using Newtonsoft.Json;
namespace BililiveRecorder.Core.Event
{
public sealed class RecordFileClosedEventArgs : RecordEventArgsBase
/// <summary>
/// <see cref="EventType.FileClosed"/>
/// </summary>
public sealed class RecordFileClosedEventArgs : RecordEventArgsBase, IRecordSessionEventArgs
{
public RecordFileClosedEventArgs() { }
internal RecordFileClosedEventArgs(IRoom room) : base(room) { }
public RecordFileClosedEventArgs(IRoom room) : base(room) { }
public Guid SessionId { get; set; }
[JsonIgnore]
public string FullPath { get; set; } = string.Empty;

View File

@ -1,13 +1,17 @@
using System;
using BililiveRecorder.Core.SimpleWebhook;
using Newtonsoft.Json;
namespace BililiveRecorder.Core.Event
{
public sealed class RecordFileOpeningEventArgs : RecordEventArgsBase
/// <summary>
/// <see cref="EventType.FileOpening"/>
/// </summary>
public sealed class RecordFileOpeningEventArgs : RecordEventArgsBase, IRecordSessionEventArgs
{
public RecordFileOpeningEventArgs() { }
internal RecordFileOpeningEventArgs(IRoom room) : base(room) { }
public RecordFileOpeningEventArgs(IRoom room) : base(room) { }
public Guid SessionId { get; set; }
[JsonIgnore]
public string FullPath { get; set; } = string.Empty;

View File

@ -1,9 +1,15 @@
using System;
using BililiveRecorder.Core.SimpleWebhook;
namespace BililiveRecorder.Core.Event
{
public sealed class RecordSessionEndedEventArgs : RecordEventArgsBase
/// <summary>
/// <see cref="EventType.SessionEnded"/>
/// </summary>
public sealed class RecordSessionEndedEventArgs : RecordEventArgsBase, IRecordSessionEventArgs
{
public RecordSessionEndedEventArgs() { }
internal RecordSessionEndedEventArgs(IRoom room) : base(room) { }
public RecordSessionEndedEventArgs(IRoom room) : base(room) { }
public Guid SessionId { get; set; }
}
}

View File

@ -1,9 +1,15 @@
using System;
using BililiveRecorder.Core.SimpleWebhook;
namespace BililiveRecorder.Core.Event
{
public sealed class RecordSessionStartedEventArgs : RecordEventArgsBase
/// <summary>
/// <see cref="EventType.SessionStarted"/>
/// </summary>
public sealed class RecordSessionStartedEventArgs : RecordEventArgsBase, IRecordSessionEventArgs
{
public RecordSessionStartedEventArgs() { }
internal RecordSessionStartedEventArgs(IRoom room) : base(room) { }
public RecordSessionStartedEventArgs(IRoom room) : base(room) { }
public Guid SessionId { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using BililiveRecorder.Core.SimpleWebhook;
namespace BililiveRecorder.Core.Event
{
/// <summary>
/// <see cref="EventType.StreamEnded"/>
/// </summary>
public sealed class StreamEndedEventArgs : RecordEventArgsBase
{
internal StreamEndedEventArgs(IRoom room) : base(room)
{
}
}
}

View File

@ -0,0 +1,14 @@
using BililiveRecorder.Core.SimpleWebhook;
namespace BililiveRecorder.Core.Event
{
/// <summary>
/// <see cref="EventType.StreamStarted"/>
/// </summary>
public sealed class StreamStartedEventArgs : RecordEventArgsBase
{
internal StreamStartedEventArgs(IRoom room) : base(room)
{
}
}
}

View File

@ -167,6 +167,20 @@ namespace BililiveRecorder.Core
private void Room_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (sender is not IRoom room)
return;
if (e.PropertyName == nameof(IRoom.Streaming))
{
if (room.Streaming)
{
_ = Task.Run(async () => await this.basicWebhookV2.SendStreamStartedAsync(new StreamStartedEventArgs(room)).ConfigureAwait(false));
}
else
{
_ = Task.Run(async () => await this.basicWebhookV2.SendStreamEndedAsync(new StreamEndedEventArgs(room)).ConfigureAwait(false));
}
}
// TODO
// throw new NotImplementedException();
}

View File

@ -37,6 +37,12 @@ namespace BililiveRecorder.Core.SimpleWebhook
public Task SendFileClosedAsync(RecordFileClosedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordFileClosedEventArgs>(args) { EventType = EventType.FileClosed });
public Task SendStreamStartedAsync(StreamStartedEventArgs args) =>
this.SendAsync(new EventWrapper<StreamStartedEventArgs>(args) { EventType = EventType.StreamStarted });
public Task SendStreamEndedAsync(StreamEndedEventArgs args) =>
this.SendAsync(new EventWrapper<StreamEndedEventArgs>(args) { EventType = EventType.StreamEnded });
private async Task SendAsync(object data)
{
var urls = this.config.WebHookUrlsV2;
@ -46,7 +52,7 @@ namespace BililiveRecorder.Core.SimpleWebhook
var dataStr = JsonConvert.SerializeObject(data, Formatting.None);
logger.Debug("尝试发送 WebhookV2 数据 {WebhookData}, 地址 {Urls}", dataStr, urls);
logger.Debug("尝试发送 WebhookV2 到 {Urls}, 数据 {WebhookData}", urls, dataStr);
var bytes = Encoding.UTF8.GetBytes(dataStr);

View File

@ -1,3 +1,4 @@
using BililiveRecorder.Core.Event;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
@ -7,28 +8,34 @@ namespace BililiveRecorder.Core.SimpleWebhook
internal enum EventType
{
Unknown,
/// <summary>
/// 录制开始
/// 录制开始 <see cref="RecordSessionStartedEventArgs"/>
/// </summary>
SessionStarted,
/// <summary>
/// 录制结束
/// 录制结束 <see cref="RecordSessionEndedEventArgs"/>
/// </summary>
SessionEnded,
/// <summary>
/// 新建了文件
/// 新建了文件 <see cref="RecordFileOpeningEventArgs"/>
/// </summary>
FileOpening,
/// <summary>
/// 文件写入结束
/// 文件写入结束 <see cref="RecordFileClosedEventArgs"/>
/// </summary>
FileClosed,
/// <summary>
/// 直播开始
/// 直播开始 <see cref="StreamStartedEventArgs"/>
/// </summary>
StreamStarted,
/// <summary>
/// 直播结束
/// 直播结束 <see cref="StreamEndedEventArgs"/>
/// </summary>
StreamEnded,
}

View File

@ -1,12 +1,11 @@
using System;
using BililiveRecorder.Core.Event;
namespace BililiveRecorder.Core.SimpleWebhook
{
internal class EventWrapper<T> where T : class
internal class EventWrapper<T> where T : RecordEventArgsBase
{
public EventWrapper()
{
}
public EventWrapper() { }
public EventWrapper(T data)
{

View File

@ -78,6 +78,7 @@ namespace BililiveRecorder.WPF
{ }
}
});
ServicePointManager.Expect100Continue = false;
update = new Update(logger);
}

View File

@ -283,22 +283,25 @@ namespace BililiveRecorder.Core.Event
public double NetworkMbps { get; set; }
public System.DateTimeOffset StartTime { get; set; }
}
public interface IRecordSessionEventArgs
{
System.Guid SessionId { get; }
}
public abstract class RecordEventArgsBase : System.EventArgs
{
public RecordEventArgsBase() { }
public RecordEventArgsBase(BililiveRecorder.Core.IRoom room) { }
protected RecordEventArgsBase(BililiveRecorder.Core.IRoom room) { }
public string AreaNameChild { get; set; }
public string AreaNameParent { get; set; }
public bool DanmakuConnected { get; set; }
public string Name { get; set; }
public bool Recording { get; set; }
public int RoomId { get; set; }
public System.Guid SessionId { get; set; }
public int ShortId { get; set; }
public bool Streaming { get; set; }
public string Title { get; set; }
}
public sealed class RecordFileClosedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase
public sealed class RecordFileClosedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase, BililiveRecorder.Core.Event.IRecordSessionEventArgs
{
public RecordFileClosedEventArgs() { }
public RecordFileClosedEventArgs(BililiveRecorder.Core.IRoom room) { }
public double Duration { get; set; }
public System.DateTimeOffset FileCloseTime { get; set; }
public System.DateTimeOffset FileOpenTime { get; set; }
@ -306,25 +309,23 @@ namespace BililiveRecorder.Core.Event
[Newtonsoft.Json.JsonIgnore]
public string FullPath { get; set; }
public string RelativePath { get; set; }
public System.Guid SessionId { get; set; }
}
public sealed class RecordFileOpeningEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase
public sealed class RecordFileOpeningEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase, BililiveRecorder.Core.Event.IRecordSessionEventArgs
{
public RecordFileOpeningEventArgs() { }
public RecordFileOpeningEventArgs(BililiveRecorder.Core.IRoom room) { }
public System.DateTimeOffset FileOpenTime { get; set; }
[Newtonsoft.Json.JsonIgnore]
public string FullPath { get; set; }
public string RelativePath { get; set; }
public System.Guid SessionId { get; set; }
}
public sealed class RecordSessionEndedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase
public sealed class RecordSessionEndedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase, BililiveRecorder.Core.Event.IRecordSessionEventArgs
{
public RecordSessionEndedEventArgs() { }
public RecordSessionEndedEventArgs(BililiveRecorder.Core.IRoom room) { }
public System.Guid SessionId { get; set; }
}
public sealed class RecordSessionStartedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase
public sealed class RecordSessionStartedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase, BililiveRecorder.Core.Event.IRecordSessionEventArgs
{
public RecordSessionStartedEventArgs() { }
public RecordSessionStartedEventArgs(BililiveRecorder.Core.IRoom room) { }
public System.Guid SessionId { get; set; }
}
public sealed class RecordingStatsEventArgs : System.EventArgs
{
@ -351,6 +352,8 @@ namespace BililiveRecorder.Core.Event
public long TotalOutputVideoBytes { get; set; }
public int TotalOutputVideoFrames { get; set; }
}
public sealed class StreamEndedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase { }
public sealed class StreamStartedEventArgs : BililiveRecorder.Core.Event.RecordEventArgsBase { }
}
namespace BililiveRecorder.Core
{