diff --git a/BililiveRecorder.Core/Events.cs b/BililiveRecorder.Core/Events.cs index a445935..c0fd1a7 100644 --- a/BililiveRecorder.Core/Events.cs +++ b/BililiveRecorder.Core/Events.cs @@ -7,6 +7,6 @@ namespace BililiveRecorder.Core public delegate void StreamStatusChangedEvent(object sender, StreamStatusChangedArgs e); public class StreamStatusChangedArgs { - public StreamStatus status; + public TriggerType status; } } diff --git a/BililiveRecorder.Core/StreamMonitor.cs b/BililiveRecorder.Core/StreamMonitor.cs index 8457556..999466a 100644 --- a/BililiveRecorder.Core/StreamMonitor.cs +++ b/BililiveRecorder.Core/StreamMonitor.cs @@ -1,22 +1,112 @@ using System; using System.Collections.Generic; using System.Text; +using System.Threading.Tasks; namespace BililiveRecorder.Core { public class StreamMonitor { - + public int Roomid { get; private set; } = 0; public event StreamStatusChangedEvent StreamStatusChanged; + public readonly DanmakuReceiver receiver = new DanmakuReceiver(); + + public StreamMonitor(int roomid) + { + Roomid = roomid; + + receiver.Connected += Receiver_Connected; + receiver.Disconnected += Receiver_Disconnected; + receiver.LogMessage += Receiver_LogMessage; + receiver.ReceivedDanmaku += Receiver_ReceivedDanmaku; + receiver.ReceivedRoomCount += Receiver_ReceivedRoomCount; + + } + + private void Receiver_ReceivedRoomCount(object sender, ReceivedRoomCountArgs e) + { + throw new NotImplementedException(); + } + + private void Receiver_ReceivedDanmaku(object sender, ReceivedDanmakuArgs e) + { + switch (e.Danmaku.MsgType) + { + case MsgTypeEnum.LiveStart: + _StartRecord(TriggerType.Danmaku); + break; + case MsgTypeEnum.LiveEnd: + break; + default: + break; + } + } + + private void Receiver_LogMessage(object sender, LogMessageArgs e) + { + // TODO: Log + } + + private void Receiver_Disconnected(object sender, DisconnectEvtArgs e) + { + if (e.Error != null) + { + + } + } + + private void Receiver_Connected(object sender, ConnectedEvtArgs e) + { } + + private void _StartRecord(TriggerType status) + { + Task.Run(() => StreamStatusChanged?.Invoke(this, new StreamStatusChangedArgs() { status = status })); + } + + public void Start() + { + if (receiver.Connect(Roomid)) + { + var info = BililiveAPI.GetRoomInfo(Roomid); + if (info.isStreaming) + { + _StartRecord(TriggerType.HttpApi); + } + } + else + { + throw receiver.Error; + } + + } + + public void Stop() + { + if (receiver.isConnected) + { + receiver.Disconnect(); + } + } public void Check() { - throw new NotImplementedException(); + var info = BililiveAPI.GetRoomInfo(Roomid); + if (info.isStreaming) + { + _StartRecord(TriggerType.HttpApiRecheck); + } } - public void CheckAfterSeconeds(uint seconds) + public void CheckAfterSeconeds(int seconds) { - throw new NotImplementedException(); + if (seconds < 0) + throw new ArgumentOutOfRangeException(nameof(seconds), "不能小于0"); + + Task.Run(() => + { + Task.Delay(seconds * 1000).Wait(); + Check(); + }); } } } diff --git a/BililiveRecorder.Core/StreamStatus.cs b/BililiveRecorder.Core/StreamStatus.cs index 290a0c9..7d33a50 100644 --- a/BililiveRecorder.Core/StreamStatus.cs +++ b/BililiveRecorder.Core/StreamStatus.cs @@ -4,40 +4,11 @@ using System.Text; namespace BililiveRecorder.Core { - public class StreamStatus + public enum TriggerType { - // TODO: - public bool isStreaming - { - get - { - return false; - } - } - - public bool HttpAPIStreaming - { - get - { - return false; - } - set - { - - } - } - - public bool DanmakuStreaming - { - get - { - return false; - } - set - { - - } - } - + Danmaku, + HttpApi, + HttpApiRecheck, + Manual, } } diff --git a/BililiveRecorder.FlvProcessor/FlvClipProcessor.cs b/BililiveRecorder.FlvProcessor/FlvClipProcessor.cs index 99b2e5e..adea0dc 100644 --- a/BililiveRecorder.FlvProcessor/FlvClipProcessor.cs +++ b/BililiveRecorder.FlvProcessor/FlvClipProcessor.cs @@ -8,7 +8,7 @@ namespace BililiveRecorder.FlvProcessor public class FlvClipProcessor : IDisposable { public readonly FlvMetadata Header; - public List Tags; + public readonly List Tags; private int target = -1; public Func GetFileName; @@ -59,6 +59,7 @@ namespace BililiveRecorder.FlvProcessor fs.Close(); } + Tags.Clear(); ClipFinalized?.Invoke(this, new ClipFinalizedArgs() { ClipProcessor = this }); }