Browse Source

直播,调整

Jeremy 2 years ago
parent
commit
2afe8ccba7
1 changed files with 141 additions and 20 deletions
  1. 141 20
      Service/DeviceService.cs

+ 141 - 20
Service/DeviceService.cs

@@ -29,6 +29,10 @@ using WingInterfaceLibrary.Internal.Interface;
 using WingInterfaceLibrary.Notifications;
 using WingInterfaceLibrary.Internal.Request;
 using WingInterfaceLibrary.Rtc;
+using WingInterfaceLibrary.Enum.CommentEnum;
+using WingInterfaceLibrary.DTO.RTC;
+using Newtonsoft.Json;
+using WingInterfaceLibrary.Notifications.Live;
 
 namespace WingDeviceService.Service
 {
@@ -59,6 +63,8 @@ namespace WingDeviceService.Service
         private int _liveConsultationRateSeconds = 0;
         private bool _isRTCSelf = false;
         private int _sdkAppId = 0;
+        private int _reportStateIntervalSeconds = 0;
+        private int _reportStateTimeout = 0;
 
         /// <summary>
         /// Init service
@@ -86,6 +92,8 @@ namespace WingDeviceService.Service
             {
                 _sdkAppId = sdkAppId;
             }
+            var _reportStateIntervalSeconds = ConfigurationManager.GetParammeter<IntParameter>("Live", "ReportStateIntervalSeconds").Value;
+            var _reportStateTimeout = ConfigurationManager.GetParammeter<IntParameter>("Live", "ReportStateTimeout").Value;
         }
 
         /// <summary>
@@ -1008,7 +1016,7 @@ namespace WingDeviceService.Service
                     Width = video.Width,
                     Height = video.Height,
                     OutputWidth = video.Width,
-                    OutputHeight = video.Height              
+                    OutputHeight = video.Height
                 };
                 if (deviceDTO.VideoDeviceInfos != null && deviceDTO.VideoDeviceInfos.Any(x => x.VideoDeviceId == video.VideoDeviceId))
                 {
@@ -1090,15 +1098,64 @@ namespace WingDeviceService.Service
         /// <returns></returns>
         public async Task<JoinDeviceLiveRoomResult> JoinDeviceLiveRoomAsync(JoinDeviceLiveRoomRequest request)
         {
-            // var r = await _rtcService.GetOrAddLiveRoomAsync(new FindLiveRoomRequest
-            // {
-
-            // });
-            // var i = await _rtcService.InitiateLiveAsync(new InitiateLiveRequest
-            // {
-
-            // });
-            throw new NotImplementedException();
+            var userInfo = await _authenticationService.GetTokenAsync(request);
+            var deviceCode = request.DeviceCode;
+            var liveRooms = await _rtcService.GetLiveRoomsByDeviceCodeAsync(new GetLiveRoomsByDeviceCodeRequest
+            {
+                DeviceCode = deviceCode
+            });
+            var liveRoom = liveRooms?.FirstOrDefault(x => x.Status == LiveRoomStatus.Initiating);
+            LiveMemberDTO deviceMemberInfo;
+            if (liveRoom != null)
+            {
+                //设备正在推流
+                deviceMemberInfo = liveRoom.Members.FirstOrDefault(x => x.Id == deviceCode);
+                if (deviceMemberInfo?.Status == LiveMemberStatusEnum.Joined)
+                {
+                    return new JoinDeviceLiveRoomResult
+                    {
+                        RoomNo = liveRoom.RoomNo,
+                        RtmpUrl = deviceMemberInfo.LiveData.RtmpPullUrl,
+                    };
+                }
+            }
+            //新增或更新设备直播间
+            var rtcRoomResult = await _rtcService.GetRoomIdAsync(new GetRoomIdRequest
+            {
+                UniqueId = deviceCode,
+            });
+            var rtcRoomId = (int)rtcRoomResult.RoomId;
+            var initiateResult = await _rtcService.InitiateLiveAsync(new InitiateLiveRequest
+            {
+                RoomId = deviceCode,
+                InitiatorCode = deviceCode,
+                IntegerRoomId = rtcRoomId
+            });
+            //添加观众
+            var addOrUpdateViewerResult = await _rtcService.SaveViewerAsync(new SaveViewerRequest
+            {
+                LiveRoomCode = initiateResult.RoomId,
+                UserCode = userInfo.ClientId,
+                UserName = userInfo.AccountName,
+            });
+            //通知设备推流
+            deviceMemberInfo = initiateResult.Members.FirstOrDefault(x => x.Id == deviceCode);
+            var message = new StartLiveToDeviceNotification
+            {
+                LiveRoomCode = initiateResult.RoomId,
+                RoomNo = initiateResult.RoomNo,
+                AppId = _sdkAppId,
+                MergedChannel = deviceMemberInfo.MergedChannel,
+                MergedVideoOutputWidth = deviceMemberInfo.MergedVideoOutputWidth,
+                MergedVideoOutputHeight = deviceMemberInfo.MergedVideoOutputHeight,
+                VideoDeviceOutputList = deviceMemberInfo.VideoDeviceInfos.ToList(),
+            };
+            return new JoinDeviceLiveRoomResult
+            {
+                RoomNo = initiateResult.RoomNo,
+                RtmpUrl = deviceMemberInfo.LiveData.RtmpPullUrl,
+                ReportStateIntervalSeconds = _reportStateIntervalSeconds,
+            };
         }
 
         /// <summary>
@@ -1108,7 +1165,33 @@ namespace WingDeviceService.Service
         /// <returns></returns>
         public async Task<bool> LeaveDeviceLiveRoomAsync(LeaveDeviceLiveRoomRequest request)
         {
-            throw new NotImplementedException();
+            var userInfo = await _authenticationService.GetTokenAsync(request);
+            var userCode = userInfo.ClientId;
+            var rtcRoomId = request.RoomNo;
+            var liveRooms = await _rtcService.GetLiveRoomsByRtcRoomIdAsync(new GetLiveRoomsByRtcRoomIdRequest
+            {
+                RtcRoomId = rtcRoomId
+            });
+            var liveRoom = liveRooms?.FirstOrDefault(x => x.Status == LiveRoomStatus.Initiating);
+            if (liveRoom != null)
+            {
+                //移除观众
+                var removeViewerResult = await _rtcService.RemoveViewerAsync(new RemoveViewerRequest
+                {
+                    LiveRoomCode = liveRoom.RoomId,
+                    UserCode = userInfo.ClientId,
+                });
+                var expireTime = DateTime.UtcNow.AddSeconds(_reportStateTimeout * -1);
+                if (!liveRoom.ViewerInfos.Any(x => x.UserCode != userCode && x.LastReportTime >= expireTime))
+                {
+                    //关闭直播 to device
+                    var message = new CloseLiveToDeviceNotification
+                    {
+                        LiveRoomCode = liveRoom.RoomId,
+                    };
+                }
+            }
+            return true;
         }
 
         /// <summary>
@@ -1118,7 +1201,24 @@ namespace WingDeviceService.Service
         /// <returns></returns>
         public async Task<bool> ReportLiveViewStateAsync(ReportLiveViewStateRequest request)
         {
-            throw new NotImplementedException();
+            var userInfo = await _authenticationService.GetTokenAsync(request);
+            var rtcRoomId = request.RoomNo;
+            var liveRooms = await _rtcService.GetLiveRoomsByRtcRoomIdAsync(new GetLiveRoomsByRtcRoomIdRequest
+            {
+                RtcRoomId = rtcRoomId
+            });
+            var liveRoom = liveRooms?.FirstOrDefault(x => x.Status == LiveRoomStatus.Initiating);
+            if (liveRoom != null)
+            {
+                //添加观众
+                var addOrUpdateViewerResult = await _rtcService.SaveViewerAsync(new SaveViewerRequest
+                {
+                    LiveRoomCode = liveRoom.RoomId,
+                    UserCode = userInfo.ClientId,
+                    UserName = userInfo.AccountName,
+                });
+            }
+            return true;
         }
 
         /// <summary>
@@ -1129,6 +1229,7 @@ namespace WingDeviceService.Service
         public async Task<bool> ReportLiveStateAsync(ReportLiveStateRequest request)
         {
             var tokenInfo = await _authenticationService.GetTokenAsync(request);
+            var deviceCode = tokenInfo.ClientId;
             switch (request.LiveState)
             {
                 case DeviceLiveStateEnum.Error:
@@ -1139,18 +1240,38 @@ namespace WingDeviceService.Service
                     Logger.WriteLineInfo($"DeviceService ReportLiveStateAsync, [{tokenInfo.AccountType.ToString()}]{tokenInfo.AccountName}, state:{request.LiveState.ToString()}, message:{request.Message}");
                     break;
             }
-            // var cc = await _rtcService.GetLiveRoomByCodesAsync(new GetLiveRoomByCodesRequest
-            // {
-            //     RoomCodes = new List<string> { tokenInfo.ClientId }
-            // });
-            if (request.LiveState == DeviceLiveStateEnum.Pushing)
+            if (request.LiveState == DeviceLiveStateEnum.Pushing || request.LiveState == DeviceLiveStateEnum.Closed)
             {
+                var rtcRoomId = request.RoomNo;
+                var liveRooms = await _rtcService.GetLiveRoomsByRtcRoomIdAsync(new GetLiveRoomsByRtcRoomIdRequest
+                {
+                    RtcRoomId = rtcRoomId
+                });
+                var liveRoom = liveRooms?.FirstOrDefault(x => x.Status == LiveRoomStatus.Initiating);
+                if (liveRoom != null)
+                {
+                    var expireTime = DateTime.UtcNow.AddSeconds(_reportStateTimeout * -1);
+                    var validViewers = liveRoom.ViewerInfos.Where(x => x.LastReportTime >= expireTime)?.ToList() ?? new List<LiveViewerDTO>();
 
+                    if (request.LiveState == DeviceLiveStateEnum.Closed && validViewers.Any())
+                    {
+                        //关闭直播 to users
+                        var message = new DeviceLiveFinishedNotification
+                        {
+                            LiveRoomCode = liveRoom.RoomId,
+                        };
+                    }
+                    if (request.LiveState == DeviceLiveStateEnum.Pushing && !validViewers.Any())
+                    {
+                        //关闭直播 to device
+                        var message = new CloseLiveToDeviceNotification
+                        {
+                            LiveRoomCode = liveRoom.RoomId,
+                        };
+                    }
+                }
             }
-            else if (request.LiveState == DeviceLiveStateEnum.Closed)
-            {
 
-            }
             return await Task.FromResult(true);
         }