全部使用 interface (Core)

This commit is contained in:
Genteure 2018-10-24 14:33:05 +08:00
parent 4e0791c35a
commit b2cc98e14f
12 changed files with 131 additions and 24 deletions

View File

@ -0,0 +1,12 @@
namespace BililiveRecorder.Core
{
public interface IRecordInfo
{
string SavePath { get; set; }
string StreamFilePrefix { get; set; }
string ClipFilePrefix { get; set; }
string StreamName { get; set; }
string GetStreamFilePath();
string GetClipFilePath();
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.ComponentModel;
namespace BililiveRecorder.Core
{
public interface IRecordedRoom : INotifyPropertyChanged
{
int Roomid { get; }
int RealRoomid { get; }
string StreamerName { get; }
RoomInfo RoomInfo { get; }
IRecordInfo RecordInfo { get; }
IStreamMonitor StreamMonitor { get; }
bool IsMonitoring { get; }
bool IsRecording { get; }
double DownloadSpeedKiBps { get; }
DateTime LastUpdateDateTime { get; }
bool Start();
void Stop();
void StartRecord();
void StopRecord();
bool UpdateRoomInfo();
void Clip();
}
}

View File

@ -0,0 +1,12 @@
using System.ComponentModel;
namespace BililiveRecorder.Core
{
public interface ISettings : INotifyPropertyChanged
{
uint Clip_Past { get; set; }
uint Clip_Future { get; set; }
string SavePath { get; set; }
EnabledFeature Feature { get; set; }
}
}

View File

@ -0,0 +1,13 @@
namespace BililiveRecorder.Core
{
public interface IStreamMonitor
{
int Roomid { get; }
event StreamStatusChangedEvent StreamStatusChanged;
DanmakuReceiver receiver { get; } // TODO: 改掉这个写法
bool Start();
void Stop();
void Check(TriggerType type = TriggerType.HttpApiRecheck);
void CheckAfterSeconeds(int seconds, TriggerType type = TriggerType.HttpApiRecheck);
}
}

View File

@ -3,16 +3,16 @@ using System.IO;
namespace BililiveRecorder.Core
{
public class RecordInfo
public class RecordInfo : IRecordInfo
{
private static readonly Random random = new Random();
public string SavePath;
public string SavePath { get; set; }
public string StreamFilePrefix = "录制";
public string ClipFilePrefix = "剪辑";
public string StreamFilePrefix { get; set; } = "录制";
public string ClipFilePrefix { get; set; } = "剪辑";
public string StreamName = "某直播间";
public string StreamName { get; set; } = "某直播间";
public string GetStreamFilePath()
=> Path.Combine(SavePath, RemoveInvalidFileName($@"{StreamFilePrefix}-{StreamName}-{DateTime.Now.ToString("yyyyMMddHHmmss")}-{random.Next(100, 999)}.flv"));
@ -23,7 +23,10 @@ namespace BililiveRecorder.Core
private static string RemoveInvalidFileName(string name)
{
foreach (char c in Path.GetInvalidFileNameChars())
{
name = name.Replace(c, '_');
}
return name;
}

View File

@ -1,7 +1,6 @@
using BililiveRecorder.FlvProcessor;
using NLog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
@ -11,7 +10,7 @@ using System.Net;
namespace BililiveRecorder.Core
{
public class RecordedRoom : INotifyPropertyChanged
public class RecordedRoom : IRecordedRoom
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
@ -19,16 +18,16 @@ namespace BililiveRecorder.Core
public int RealRoomid { get => RoomInfo?.RealRoomid ?? Roomid; }
public string StreamerName { get => RoomInfo?.Username ?? string.Empty; }
public RoomInfo RoomInfo { get; private set; }
public RecordInfo RecordInfo { get; private set; }
public IRecordInfo RecordInfo { get; private set; }
public bool IsMonitoring => StreamMonitor.receiver.IsConnected;
public bool IsRecording => flvStream != null;
public IFlvStreamProcessor Processor; // FlvProcessor
public IFlvStreamProcessor Processor { get; set; } // FlvProcessor
public ObservableCollection<IFlvClipProcessor> Clips { get; private set; } = new ObservableCollection<IFlvClipProcessor>();
internal StreamMonitor StreamMonitor { get; }
private Settings _settings { get; }
public IStreamMonitor StreamMonitor { get; }
private ISettings _settings { get; }
private HttpWebRequest webRequest;
private Stream flvStream;
private bool flv_shutdown = false;
@ -42,7 +41,7 @@ namespace BililiveRecorder.Core
public DateTime LastUpdateDateTime { get; private set; } = DateTime.Now;
public long LastUpdateSize { get; private set; } = 0;
public RecordedRoom(Settings settings, int roomid)
public RecordedRoom(ISettings settings, int roomid)
{
_settings = settings;
_settings.PropertyChanged += _settings_PropertyChanged;
@ -78,17 +77,23 @@ namespace BililiveRecorder.Core
if (e.PropertyName == nameof(_settings.Clip_Past))
{
if (Processor != null)
{
Processor.Clip_Past = _settings.Clip_Past;
}
}
else if (e.PropertyName == nameof(_settings.Clip_Future))
{
if (Processor != null)
{
Processor.Clip_Future = _settings.Clip_Future;
}
}
else if (e.PropertyName == nameof(_settings.SavePath))
{
if (RecordInfo != null)
{
RecordInfo.SavePath = _settings.SavePath;
}
}
}
@ -183,14 +188,20 @@ namespace BililiveRecorder.Core
_CleanupFlvRequest();
logger.Log(RealRoomid, LogLevel.Info, "直播已结束,停止录制。" + (triggerType != TriggerType.HttpApiRecheck ? "将在30秒后重试启动。" : ""));
if (triggerType != TriggerType.HttpApiRecheck)
{
StreamMonitor.CheckAfterSeconeds(30);
}
}
else
{
if (bytesRead != buffer.Length)
{
Processor.AddBytes(buffer.Take(bytesRead).ToArray());
}
else
{
Processor.AddBytes(buffer);
}
flvStream.BeginRead(buffer, 0, BUF_SIZE, callback, null);
}
@ -202,7 +213,9 @@ namespace BililiveRecorder.Core
{
logger.Log(RealRoomid, LogLevel.Info, "直播流下载连接出错。" + (triggerType != TriggerType.HttpApiRecheck ? "将在30秒后重试启动。" : ""), ex);
if (triggerType != TriggerType.HttpApiRecheck)
{
StreamMonitor.CheckAfterSeconeds(30);
}
}
else
{
@ -220,7 +233,9 @@ namespace BililiveRecorder.Core
_CleanupFlvRequest();
logger.Log(RealRoomid, LogLevel.Warn, "启动直播流下载出错。" + (triggerType != TriggerType.HttpApiRecheck ? "将在30秒后重试启动。" : ""), ex);
if (triggerType != TriggerType.HttpApiRecheck)
{
StreamMonitor.CheckAfterSeconeds(30);
}
}
}
@ -286,7 +301,11 @@ namespace BililiveRecorder.Core
// Called by API or GUI
public void Clip()
{
if (Processor == null) return;
if (Processor == null)
{
return;
}
var clip = Processor.Clip();
clip.ClipFinalized += CallBack_ClipFinalized;
clip.GetFileName = RecordInfo.GetClipFilePath;

View File

@ -13,8 +13,8 @@ namespace BililiveRecorder.Core
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public ObservableCollection<RecordedRoom> Rooms { get; } = new ObservableCollection<RecordedRoom>();
public Settings Settings { get; } = new Settings();
public ObservableCollection<IRecordedRoom> Rooms { get; } = new ObservableCollection<IRecordedRoom>();
public ISettings Settings { get; } = new Settings();
private CancellationTokenSource tokenSource;
@ -66,7 +66,7 @@ namespace BililiveRecorder.Core
/// 从录播姬移除直播间
/// </summary>
/// <param name="rr">直播间</param>
public void RemoveRoom(RecordedRoom rr)
public void RemoveRoom(IRecordedRoom rr)
{
rr.Stop();
rr.StopRecord();

View File

@ -4,7 +4,7 @@ using System.ComponentModel;
namespace BililiveRecorder.Core
{
public class Settings : INotifyPropertyChanged
public class Settings : ISettings
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
@ -45,7 +45,11 @@ namespace BililiveRecorder.Core
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
if (EqualityComparer<T>.Default.Equals(field, value))
{
return false;
}
logger.Debug("设置 [{0}] 的值已从 [{1}] 修改到 [{2}]", propertyName, field, value);
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

View File

@ -5,13 +5,13 @@ using System.Threading.Tasks;
namespace BililiveRecorder.Core
{
public class StreamMonitor
public class StreamMonitor : IStreamMonitor
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public int Roomid { get; private set; } = 0;
public event StreamStatusChangedEvent StreamStatusChanged;
public readonly DanmakuReceiver receiver = new DanmakuReceiver();
public DanmakuReceiver receiver { get; } = new DanmakuReceiver();
private CancellationTokenSource TokenSource = null;
public StreamMonitor(int roomid)
@ -69,7 +69,9 @@ namespace BililiveRecorder.Core
try
{
if (BililiveAPI.GetRoomInfo(Roomid).isStreaming)
{
_StartRecord(TriggerType.HttpApi);
}
}
catch (Exception ex)
{
@ -80,8 +82,13 @@ namespace BililiveRecorder.Core
public bool Start()
{
if (!receiver.IsConnected)
{
if (!receiver.Connect(Roomid))
{
return false;
}
}
logger.Log(Roomid, LogLevel.Info, "弹幕服务器连接成功");
// Run 96 times a day.
@ -96,7 +103,10 @@ namespace BililiveRecorder.Core
public void Stop()
{
if (receiver.IsConnected)
{
receiver.Disconnect();
}
TokenSource?.Cancel();
TokenSource = null;
}
@ -113,7 +123,9 @@ namespace BililiveRecorder.Core
public void CheckAfterSeconeds(int seconds, TriggerType type = TriggerType.HttpApiRecheck)
{
if (seconds < 0)
{
throw new ArgumentOutOfRangeException(nameof(seconds), "不能小于0");
}
Task.Run(() =>
{

View File

@ -29,7 +29,7 @@ namespace BililiveRecorder.Core
}
}
public static void ApplyTo(this Settings val1, Settings val2)
public static void ApplyTo(this ISettings val1, ISettings val2)
{
foreach (var p in val1.GetType().GetProperties())
{

View File

@ -373,7 +373,7 @@ namespace BililiveRecorder.WPF
}
}
private RecordedRoom _GetSenderAsRecordedRoom(object sender) => (sender as Button)?.DataContext as RecordedRoom;
private IRecordedRoom _GetSenderAsRecordedRoom(object sender) => (sender as Button)?.DataContext as IRecordedRoom;
}

View File

@ -21,9 +21,9 @@ namespace BililiveRecorder.WPF
/// </summary>
public partial class SettingsWindow : Window
{
public Settings Settings { get; set; } = new Settings();
public ISettings Settings { get; set; } = new Settings();
public SettingsWindow(MainWindow mainWindow, Settings settings)
public SettingsWindow(MainWindow mainWindow, ISettings settings)
{
Owner = mainWindow;
settings.ApplyTo(Settings);