Пример #1
0
        public void StartPush(CommandDto commandDto)
        {
            //压入推送对象进入推送列表
            ChannelPusher channelPusher = ChannelPusherService.PushChannelIntoWorkingList(commandDto);

            if (channelPusher != null)
            {
                Task.Factory.StartNew((objStat) =>
                {
                    try
                    {
                        PushAMsgIn(channelPusher.CommandDto.ChanneId, channelPusher.CommandDto.Id, $"推流日志: 开始推送成功.", LiveStateType.Started);
                        //执行推送
                        using (var ffPublisher = new FFmpegPublisher(channelPusher.CommandDto))
                        {
                            int countTimes = 0;
                            ffPublisher.OnFinishedWritePktEvent += (retId, command) =>
                            {
                                AppendLog(retId.ToString());
                                ////推流速度太快, 采取30条提取一条.
                                //++countTimes;
                                //if (countTimes >= 30)
                                //{
                                //    if (retId == 0)
                                //    {
                                //        PushAMsgIn(command.ChanneId, command.Id, $"推流日志: 推送Pack成功.", LiveStateType.Pushing);
                                //    }
                                //    else
                                //    {
                                //        PushAMsgIn(command.ChanneId, command.Id, $"推流日志: 推送Pack失败, 返回值:{FFmpegHelper.GetStrError(retId)}.", LiveStateType.Error);
                                //    }
                                //    countTimes = 0;
                                //}
                            };
                            ffPublisher.OnErrorEvent += (errorId, command, errorMsg) =>
                            {
                                PushAMsgIn(command.ChanneId, command.Id, $"推流日志:推流异常, 异常ID:{errorId} 异常信息: {errorMsg }", LiveStateType.Error);
                            };
                            ffPublisher.OnPushStopedEvent += command =>
                            {
                                PushAMsgIn(command.ChanneId, command.Id, $"推流日志:停止推流.", LiveStateType.Stoped);
                            };
                            ffPublisher.OnStartCompletedEvent += command =>
                            {
                                PushAMsgIn(command.ChanneId, command.Id, $"推流日志:拉流/推流初始化成功,即将开始发送数据包.", LiveStateType.Started);
                            };
                            //启动推送
                            ffPublisher.Start(channelPusher.CTS.Token);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.SetLogError($"推流日志:{ex.ToString()}");
                    }
                }, channelPusher.CTS.Token, TaskCreationOptions.LongRunning);

                if (channelPusher.CommandDto.CommandType == CommandTypeEnum.Start)
                {
                    //Write commands to local file.
                    SerializationCommangs();
                }
            }
            else
            {
                PushAMsgIn(commandDto.ChanneId, commandDto.Id, $"推流日志: 频道正在推送中,未执行此命令.", LiveStateType.Error);
            }
        }