예제 #1
0
        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}"
                });
            }
        }
예제 #2
0
        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);
        }
예제 #3
0
        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
                        });
                    }
                }
            });
        }
예제 #4
0
        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);
            };
        }
예제 #5
0
        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;
            }
        }
예제 #6
0
        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
                    });
                }
            });
        }