public async void Check(TriggerType type, int millisecondsDelay = 0) { if (disposedValue) { throw new ObjectDisposedException(nameof(StreamMonitor)); } if (millisecondsDelay < 0) { throw new ArgumentOutOfRangeException(nameof(millisecondsDelay), @"不能小于0"); } try { await Task.Delay(millisecondsDelay); StreamStarted?.Invoke(this, (await FetchRoomInfoAsync()).IsStreaming ? new StreamStartedArgs { Type = type, IsLive = true } : new StreamStartedArgs { Type = type, IsLive = false }); } catch (Exception ex) { LogEvent?.Invoke(this, new LogEventArgs { Log = $@"[{RoomId}] 获取直播间开播状态出错:{ex.Message}" }); } }
public ClientStream AddClient(ClientConnection client) { ClientStream stream = new ClientStream(client, Encoder); Task.Run(() => { stream.Run().Wait(); stream.Stop(); CurrentStreams.Remove(stream); StreamStopped?.Invoke(this, new StreamStoppedEventArgs(stream)); if (!CurrentStreams.Any()) { Source.StopCapture(); } }); if (!CurrentStreams.Any()) { Source.StartCapture(); } CurrentStreams.Add(stream); StreamStarted?.Invoke(this, new StreamStartedEventArgs(stream)); return(stream); }
public void Check(TriggerType type, int millisecondsDelay = 0) { if (this.disposedValue) { throw new ObjectDisposedException(nameof(StreamMonitor)); } if (millisecondsDelay < 0) { throw new ArgumentOutOfRangeException(nameof(millisecondsDelay), "不能小于0"); } Task.Run(async() => { await Task.Delay(millisecondsDelay).ConfigureAwait(false); if ((await this.FetchRoomInfoAsync().ConfigureAwait(false))?.IsStreaming ?? false) { if (!isLiveRunning) { isLiveRunning = true;//http来的只触发一次 StreamStarted?.Invoke(this, new StreamStartedArgs() { type = type }); } } }); }
public StreamMonitor(RoomSetting setting) { RoomId = setting.RoomId; #if DEBUG StreamStarted += (o, args) => { LogEvent?.Invoke(this, args.IsLive ? new LogEventArgs { Log = $@"[{RoomId}] [{args.Type}] [{setting.UserName}] 开播:{setting.Title}" } : new LogEventArgs { Log = $@"[{RoomId}] [{args.Type}] [{setting.UserName}] 下播/未开播" }); }; #endif _danMuClient = new DanMuClient(RoomId, TimeSpan.FromMilliseconds(setting.TimingDanmakuRetry)); _danMuClient.LogEvent += (o, args) => LogEvent?.Invoke(o, args); _danMuClient.ReceivedDanmaku += (o, args) => { switch (args.Danmaku.MsgType) { case MsgType.LiveStart: StreamStarted?.Invoke(this, new StreamStartedArgs { Type = TriggerType.弹幕, IsLive = true }); break; case MsgType.LiveEnd: StreamStarted?.Invoke(this, new StreamStartedArgs { Type = TriggerType.弹幕, IsLive = false }); break; } }; _httpTimer = new Timer(TimeSpan.FromSeconds(setting.TimingCheckInterval).TotalMilliseconds) { Enabled = false, AutoReset = true, SynchronizingObject = null, Site = null }; _httpTimer.Elapsed += (sender, e) => { Check(TriggerType.HttpApi); }; }
private void Receiver_ReceivedDanmaku(object sender, ReceivedDanmakuArgs e) { switch (e.Danmaku.MsgType) { case MsgTypeEnum.LiveStart: if (IsMonitoring) { Task.Run(() => StreamStarted?.Invoke(this, new StreamStartedArgs() { type = TriggerType.Danmaku })); } break; case MsgTypeEnum.LiveEnd: break; default: break; } }
public void Check(TriggerType type, int millisecondsDelay = 0) { if (disposedValue) { throw new ObjectDisposedException(nameof(StreamMonitor)); } if (millisecondsDelay < 0) { throw new ArgumentOutOfRangeException(nameof(millisecondsDelay), "不能小于0"); } Task.Run(async() => { Task.Delay(millisecondsDelay).Wait(); if ((await FetchRoomInfoAsync()).IsStreaming) { StreamStarted?.Invoke(this, new StreamStartedArgs() { type = type }); } }); }