修改 Recorder 添加接口

This commit is contained in:
Genteure 2018-11-28 22:29:35 +08:00
parent a8a3f71340
commit a354b89cbf
5 changed files with 106 additions and 26 deletions

View File

@ -17,7 +17,7 @@ namespace BililiveRecorder.Core
builder.RegisterType<TcpClient>().AsSelf().ExternallyOwned();
builder.RegisterType<StreamMonitor>().As<IStreamMonitor>().ExternallyOwned();
builder.RegisterType<RecordedRoom>().As<IRecordedRoom>().ExternallyOwned();
builder.RegisterType<Recorder>().AsSelf().InstancePerMatchingLifetimeScope("recorder_root");
builder.RegisterType<Recorder>().As<IRecorder>().InstancePerMatchingLifetimeScope("recorder_root");
}
}
}

View File

@ -0,0 +1,22 @@
using BililiveRecorder.Core.Config;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
namespace BililiveRecorder.Core
{
public interface IRecorder : INotifyPropertyChanged, INotifyCollectionChanged, IEnumerable<IRecordedRoom>, ICollection<IRecordedRoom>
{
ConfigV1 Config { get; }
bool Initialize(string workdir);
void AddRoom(int roomid);
void AddRoom(int roomid, bool enabled);
void RemoveRoom(IRecordedRoom rr);
void Shutdown();
}
}

View File

@ -1,26 +1,41 @@
using BililiveRecorder.Core.Config;
using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace BililiveRecorder.Core
{
public class Recorder
public class Recorder : IRecorder
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public ObservableCollection<IRecordedRoom> Rooms { get; } = new ObservableCollection<IRecordedRoom>();
private ObservableCollection<IRecordedRoom> Rooms { get; } = new ObservableCollection<IRecordedRoom>();
public ConfigV1 Config { get; }
ConfigV1 IRecorder.Config => Config;
public int Count => Rooms.Count;
public bool IsReadOnly => true;
int ICollection<IRecordedRoom>.Count => Rooms.Count;
bool ICollection<IRecordedRoom>.IsReadOnly => true;
private readonly Func<int, IRecordedRoom> newIRecordedRoom;
private CancellationTokenSource tokenSource;
private bool _valid = false;
public IRecordedRoom this[int index] => Rooms[index];
public Recorder(ConfigV1 config, Func<int, IRecordedRoom> iRecordedRoom)
{
Config = config;
@ -37,6 +52,32 @@ namespace BililiveRecorder.Core
};
}
public event PropertyChangedEventHandler PropertyChanged
{
add => (Rooms as INotifyPropertyChanged).PropertyChanged += value;
remove => (Rooms as INotifyPropertyChanged).PropertyChanged -= value;
}
public event NotifyCollectionChangedEventHandler CollectionChanged
{
add => (Rooms as INotifyCollectionChanged).CollectionChanged += value;
remove => (Rooms as INotifyCollectionChanged).CollectionChanged -= value;
}
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add => (Rooms as INotifyPropertyChanged).PropertyChanged += value;
remove => (Rooms as INotifyPropertyChanged).PropertyChanged -= value;
}
event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add => (Rooms as INotifyCollectionChanged).CollectionChanged += value;
remove => (Rooms as INotifyCollectionChanged).CollectionChanged -= value;
}
bool IRecorder.Initialize(string workdir) => Initialize(workdir);
public bool Initialize(string workdir)
{
logger.Debug("Initialize: " + workdir);
@ -158,5 +199,20 @@ namespace BililiveRecorder.Core
}
}
void ICollection<IRecordedRoom>.Add(IRecordedRoom item) => throw new NotSupportedException("Collection is readonly");
void ICollection<IRecordedRoom>.Clear() => throw new NotSupportedException("Collection is readonly");
bool ICollection<IRecordedRoom>.Remove(IRecordedRoom item) => throw new NotSupportedException("Collection is readonly");
bool ICollection<IRecordedRoom>.Contains(IRecordedRoom item) => Rooms.Contains(item);
void ICollection<IRecordedRoom>.CopyTo(IRecordedRoom[] array, int arrayIndex) => Rooms.CopyTo(array, arrayIndex);
public IEnumerator<IRecordedRoom> GetEnumerator() => Rooms.GetEnumerator();
IEnumerator<IRecordedRoom> IEnumerable<IRecordedRoom>.GetEnumerator() => Rooms.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Rooms.GetEnumerator();
}
}

View File

@ -32,7 +32,7 @@
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="RoomList" ItemsSource="{Binding Recorder.Rooms}"
<DataGrid x:Name="RoomList" ItemsSource="{Binding Recorder}"
SelectionMode="Extended" SelectionUnit="FullRow"
CanUserAddRows="False" CanUserDeleteRows="False"
AutoGenerateColumns="False" IsReadOnly="True">
@ -44,20 +44,21 @@
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Roomid}" Header="房间号"/>
<DataGridTextColumn Binding="{Binding RealRoomid}" Header="原房间号"/>
<DataGridTextColumn Binding="{Binding StreamerName}" Header="主播名字"/>
<DataGridTextColumn Binding="{Binding Roomid,Mode=OneWay}" Header="房间号"/>
<DataGridTextColumn Binding="{Binding RealRoomid,Mode=OneWay}" Header="原房间号"/>
<DataGridTextColumn Binding="{Binding StreamerName,Mode=OneWay}" Header="主播名字"/>
<DataGridTextColumn Header="当前状态">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource RSC}">
<Binding Path="IsMonitoring"/>
<Binding Path="IsRecording"/>
<MultiBinding Converter="{StaticResource RSC}" Mode="OneWay">
<Binding Path="IsMonitoring" Mode="OneWay"/>
<Binding Path="IsRecording" Mode="OneWay"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding DownloadSpeedKiBps,StringFormat=0.## KiB/s}" Header="下载速度"/>
<DataGridTextColumn Binding="{Binding DownloadSpeedPersentage,StringFormat=0.## %}" Header="速度百分比"/>
<DataGridTextColumn Binding="{Binding Processor.Clips.Count}" Header="剪辑数量"/>
<DataGridTextColumn Binding="{Binding DownloadSpeedKiBps,StringFormat=0.## KiB/s,Mode=OneWay}" Header="下载速度"/>
<DataGridTextColumn Binding="{Binding DownloadSpeedPersentage,StringFormat=0.## %,Mode=OneWay}" Header="速度百分比"/>
<DataGridTextColumn Binding="{Binding Processor.Clips.Count,Mode=OneWay}" Header="剪辑数量"/>
<!-- TODO: Fix Binding Memory Leak -->
</DataGrid.Columns>
</DataGrid>
<ItemsControl Grid.Row="1" Grid.RowSpan="2" x:Name="Log" ItemsSource="{Binding Logs}" ItemTemplate="{StaticResource LogTemplate}" ToolTip="右键点击可以复制单行日志">
@ -95,32 +96,33 @@
<TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">房间号:</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Roomid}"/>
<TextBlock Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Roomid,Mode=OneWay}"/>
<TextBlock Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">原房间号:</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding RealRoomid}"/>
<TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding RealRoomid,Mode=OneWay}"/>
<TextBlock Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">主播名字:</TextBlock>
<TextBlock Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding StreamerName}"/>
<TextBlock Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding StreamerName,Mode=OneWay}"/>
<TextBlock Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">录制状态:</TextBlock>
<TextBlock Grid.Row="4" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource RSC}">
<Binding Path="IsMonitoring"/>
<Binding Path="IsRecording"/>
<MultiBinding Converter="{StaticResource RSC}" Mode="OneWay">
<Binding Path="IsMonitoring" Mode="OneWay"/>
<Binding Path="IsRecording" Mode="OneWay"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<TextBlock Grid.Row="5" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">剪辑数量:</TextBlock>
<TextBlock Grid.Row="5" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Processor.Clips.Count}"/>
<TextBlock Grid.Row="5" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding Processor.Clips.Count,Mode=OneWay}"/>
<!-- TODO: Fix Binding Memory Leak -->
<TextBlock Grid.Row="6" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">下载速度:</TextBlock>
<TextBlock Grid.Row="6" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding DownloadSpeedKiBps,StringFormat=0.## KiB/s}"/>
<TextBlock Grid.Row="6" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding DownloadSpeedKiBps,StringFormat=0.## KiB/s,Mode=OneWay}"/>
<TextBlock Grid.Row="7" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right">速度百分比:</TextBlock>
<TextBlock Grid.Row="7" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding DownloadSpeedPersentage,StringFormat=0.## %}"/>
<TextBlock Grid.Row="7" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding DownloadSpeedPersentage,StringFormat=0.## %,Mode=OneWay}"/>
<Grid Grid.Row="9" Grid.ColumnSpan="2">

View File

@ -27,7 +27,7 @@ namespace BililiveRecorder.WPF
private IContainer Container { get; set; }
private ILifetimeScope RootScope { get; set; }
public Recorder Recorder { get; set; }
public IRecorder Recorder { get; set; }
public ObservableCollection<string> Logs { get; set; } =
new ObservableCollection<string>()
{
@ -54,7 +54,7 @@ namespace BililiveRecorder.WPF
Container = builder.Build();
RootScope = Container.BeginLifetimeScope("recorder_root");
Recorder = RootScope.Resolve<Recorder>();
Recorder = RootScope.Resolve<IRecorder>();
InitializeComponent();
@ -216,7 +216,7 @@ namespace BililiveRecorder.WPF
/// <param name="e"></param>
private void EnableAllAutoRec(object sender, RoutedEventArgs e)
{
Recorder.Rooms.ToList().ForEach(rr => Task.Run(() => rr.Start()));
Recorder.ToList().ForEach(rr => Task.Run(() => rr.Start()));
}
/// <summary>
@ -226,7 +226,7 @@ namespace BililiveRecorder.WPF
/// <param name="e"></param>
private void DisableAllAutoRec(object sender, RoutedEventArgs e)
{
Recorder.Rooms.ToList().ForEach(rr => Task.Run(() => rr.Stop()));
Recorder.ToList().ForEach(rr => Task.Run(() => rr.Stop()));
}
/// <summary>