123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using WingInterfaceLibrary.DB.Request;
- using WingInterfaceLibrary.DTO.Device;
- using WingInterfaceLibrary.DTO.LiveRoom;
- using WingInterfaceLibrary.DTO.Share;
- using WingInterfaceLibrary.Enum;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Internal.Request;
- using WingInterfaceLibrary.Notifications;
- using WingInterfaceLibrary.Notifications.Live;
- using WingInterfaceLibrary.Notifications.Remote;
- using WingInterfaceLibrary.Request;
- using WingInterfaceLibrary.Request.Authentication;
- using WingInterfaceLibrary.Request.Device;
- using WingInterfaceLibrary.Request.Remote;
- using WingServerCommon.Config;
- using WingServerCommon.Interfaces.Cache;
- using WingServerCommon.Service;
- namespace WingDeviceService.Service
- {
- /// <summary>
- /// 设备直播
- /// </summary>
- public partial class DeviceService : JsonRpcService, IDeviceService
- {
- /// <summary>
- /// 用户进入设备直播房间
- /// </summary>
- /// <param name="request">用户进入设备直播房间请求实体</param>
- /// <returns></returns>
- public async Task<JoinDeviceLiveRoomResult> JoinDeviceLiveRoomAsync(JoinDeviceLiveRoomRequest request)
- {
- var userInfo = await _authenticationService.GetTokenAsync(request);
- var deviceCode = request.DeviceCode;
- var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == deviceCode).FirstOrDefault();
- if (deviceTokenInfo == null || !deviceTokenInfo.IsOnline)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotOnline, "DeviceNotOnline");
- }
- if (deviceTokenInfo.IsOldPlatform)
- {
- await _deviceForwardService.GetDeviceVideoInfosAsync(new GetDeviceVideoInfosRequest { DeviceCode = deviceCode });
- }
- var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
- if (deviceInfo == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- if (!deviceInfo.LiveOpened)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceLiveClosed, "DeviceLiveClosed");
- }
- return await JoinDeviceLiveRoomAsync(deviceCode, userInfo);
- }
- /// <summary>
- /// 用户离开设备直播房间
- /// </summary>
- /// <param name="request">用户离开设备直播房间请求实体</param>
- /// <returns></returns>
- public async Task<bool> LeaveDeviceLiveRoomAsync(LeaveDeviceLiveRoomRequest request)
- {
- var userInfo = await _authenticationService.GetTokenAsync(request);
- var userCode = userInfo.ClientId;
- var deviceCode = request.DeviceCode;
- var liveRoom = await _rtcService.GetInitiatingRoomByDeviceCodeAsync(new GetInitiatingRoomByDeviceCodeRequest
- {
- DeviceCode = deviceCode
- });
- if (liveRoom != null)
- {
- //移除观众
- var removeViewerResult = await _rtcService.RemoveViewerAsync(new RemoveViewerRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode,
- UserCode = userInfo.ClientId,
- });
- var timeoutTime = DateTime.UtcNow.AddSeconds(-1 * _reportStateTimeout);
- if (liveRoom.BusinessModule == BusinessModuleEnum.DeviceLiving && !liveRoom.ViewerInfos.Any(x => x.UserCode != userCode && x.LastReportTime >= timeoutTime))
- {
- await _rtcService.CloseAsync(new CloseRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode
- });
- }
- }
- return true;
- }
- /// <summary>
- /// 上报观看直播状态
- /// </summary>
- /// <param name="request">上报观看直播状态请求实体</param>
- /// <returns></returns>
- public async Task<bool> ReportLiveViewStateAsync(ReportLiveViewStateRequest request)
- {
- var userInfo = await _authenticationService.GetTokenAsync(request);
- var deviceCode = request.DeviceCode;
- var liveRoom = await _rtcService.GetInitiatingRoomByDeviceCodeAsync(new GetInitiatingRoomByDeviceCodeRequest
- {
- DeviceCode = deviceCode
- });
- if (liveRoom != null)
- {
- //添加观众
- var addOrUpdateViewerResult = await _rtcService.SaveViewerAsync(new SaveViewerRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode,
- UserCode = userInfo.ClientId,
- UserName = userInfo.AccountName,
- });
- }
- return true;
- }
- /// <summary>
- /// 生成设备直播分享地址
- /// </summary>
- /// <param name="request">生成设备直播分享地址请求实体</param>
- /// <errorCodes></errorCodes>
- /// <returns></returns>
- public async Task<CreateLiveShareInfoResult> CreateLiveShareInfoAsync(CreateLiveShareInfoRequest request)
- {
- var deviceCode = request.DeviceCode;
- var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
- if (deviceInfo == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- if (!deviceInfo.LiveOpened)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceLiveClosed, "DeviceLiveClosed");
- }
- var userInfo = await _authenticationService.GetTokenAsync(request);
- var shareInfo = new DeviceLiveShareInfoDTO
- {
- DeviceCode = request.DeviceCode,
- ShareUserCode = userInfo.Code,
- ShareUserName = userInfo.AccountName,
- ShareTime = DateTime.UtcNow,
- };
- var shareContent = Newtonsoft.Json.JsonConvert.SerializeObject(shareInfo);
- var shortCode = await _shareDBService.AddShareInfoAsync(new AddShareInfoDBRequest { Data = new ShareDTO { ShareContent = shareContent } });
- return new CreateLiveShareInfoResult
- {
- ShareUrl = string.Format(_deviceShareUrl, shortCode),
- };
- }
- /// <summary>
- /// 用户进入设备直播房间(无token)
- /// </summary>
- /// <param name="request">用户进入设备直播房间请求实体</param>
- /// <errorCodes></errorCodes>
- /// <returns></returns>
- public async Task<JoinDeviceLiveRoomByShareResult> JoinDeviceLiveRoomByShareAsync(JoinDeviceLiveRoomByShareRequest request)
- {
- if (string.IsNullOrWhiteSpace(request.ShareCode))
- {
- ThrowCustomerException(CustomerRpcCode.ShareCodeIsEmpty, "ShareCodeIsEmpty");
- }
- var shareResult = await _shareDBService.GetShareInfoAsync(new GetShareInfoDBRequest
- {
- ShortCode = request.ShareCode,
- });
- if (string.IsNullOrWhiteSpace(shareResult.ShareContent))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceShareDataError, "DeviceShareDataError");
- }
- DeviceLiveShareInfoDTO shareInfo = null;
- try
- {
- shareInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceLiveShareInfoDTO>(shareResult.ShareContent);
- }
- catch (Exception ex)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceShareDataError, "DeviceShareDataError");
- }
- if (shareInfo == null || shareInfo.ShareTime.AddSeconds(_shareEffectiveSeconds) < DateTime.UtcNow)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceShareExpired, "DeviceShareExpired");
- }
- var deviceCode = shareInfo.DeviceCode;
- var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
- if (deviceInfo == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- if (!deviceInfo.LiveOpened)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceLiveClosed, "DeviceLiveClosed");
- }
- var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == deviceCode).FirstOrDefault();
- if (deviceTokenInfo == null || !deviceTokenInfo.IsOnline)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotOnline, "DeviceNotOnline");
- }
- if (deviceTokenInfo.IsOldPlatform)
- {
- await _deviceForwardService.GetDeviceVideoInfosAsync(new GetDeviceVideoInfosRequest { DeviceCode = deviceCode });
- }
- var joinResult = await JoinDeviceLiveRoomAsync(deviceCode);
- return new JoinDeviceLiveRoomByShareResult
- {
- RoomNo = joinResult.RoomNo,
- LiveProtocol = EnvironmentConfigs.General.LiveProtocolDetail<TransactionStatusEnum>(),
- ReportStateIntervalSeconds = joinResult.ReportStateIntervalSeconds,
- DeviceCode = joinResult.DeviceCode,
- MergedChannel = joinResult.MergedChannel,
- MergedVideoOutputWidth = joinResult.MergedVideoOutputWidth,
- MergedVideoOutputHeight = joinResult.MergedVideoOutputHeight,
- VideoDeviceInfos = joinResult.VideoDeviceInfos,
- IsOldPlatform = joinResult.IsOldPlatform,
- SupportRtc = joinResult.SupportRtc,
- };
- }
- /// <summary>
- /// 用户离开设备直播房间(无token)
- /// </summary>
- /// <param name="request">用户离开设备直播房间请求实体</param>
- /// <errorCodes></errorCodes>
- /// <returns></returns>
- public async Task<bool> LeaveDeviceLiveRoomByShareAsync(LeaveDeviceLiveRoomByShareRequest request)
- {
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "DeviceCodeIsEmpty");
- }
- if (string.IsNullOrWhiteSpace(request.ViewerUniqueId))
- {
- ThrowCustomerException(CustomerRpcCode.UniqueIdIsEmpty, "UniqueIdIsEmpty");
- }
- var userCode = request.ViewerUniqueId;
- var deviceCode = request.DeviceCode;
- var liveRoom = await _rtcService.GetInitiatingRoomByDeviceCodeAsync(new GetInitiatingRoomByDeviceCodeRequest
- {
- DeviceCode = deviceCode
- });
- if (liveRoom != null)
- {
- //移除观众
- var removeViewerResult = await _rtcService.RemoveViewerAsync(new RemoveViewerRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode,
- UserCode = userCode,
- });
- var timeoutTime = DateTime.UtcNow.AddSeconds(-1 * _reportStateTimeout);
- if (liveRoom.BusinessModule == BusinessModuleEnum.DeviceLiving && !liveRoom.ViewerInfos.Any(x => x.UserCode != userCode && x.LastReportTime >= timeoutTime))
- {
- await _rtcService.CloseAsync(new CloseRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode
- });
- }
- }
- return true;
- }
- /// <summary>
- /// 上报观看直播状态(无token)
- /// </summary>
- /// <param name="request">上报观看直播状态请求实体</param>
- /// <errorCodes></errorCodes>
- /// <returns></returns>
- public async Task<bool> ReportLiveViewStateByShareAsync(ReportLiveViewStateByShareRequest request)
- {
- if (string.IsNullOrWhiteSpace(request.DeviceCode))
- {
- ThrowCustomerException(CustomerRpcCode.DeviceCodeIsEmpty, "DeviceCodeIsEmpty");
- }
- if (string.IsNullOrWhiteSpace(request.ViewerUniqueId))
- {
- ThrowCustomerException(CustomerRpcCode.UniqueIdIsEmpty, "UniqueIdIsEmpty");
- }
- var deviceCode = request.DeviceCode;
- var liveRoom = await _rtcService.GetInitiatingRoomByDeviceCodeAsync(new GetInitiatingRoomByDeviceCodeRequest
- {
- DeviceCode = deviceCode
- });
- if (liveRoom != null)
- {
- //添加观众
- var uniqueId = request.ViewerUniqueId;
- var addOrUpdateViewerResult = await _rtcService.SaveViewerAsync(new SaveViewerRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode,
- UserCode = uniqueId,
- UserName = uniqueId,
- });
- }
- return true;
- }
- /// <summary>
- /// 开始直播
- /// </summary>
- /// <param name="deviceCode"></param>
- /// <param name="userInfo"></param>
- /// <returns></returns>
- private async Task<JoinDeviceLiveRoomResult> JoinDeviceLiveRoomAsync(string deviceCode, TokenDTO userInfo = null)
- {
- var deviceInfo = CacheMaintenance.Instance.Get<IDeviceInfosManager>().Get(deviceCode);
- if (deviceInfo == null)
- {
- ThrowCustomerException(CustomerRpcCode.DeviceNotExist, "Not find device");
- }
- var liveRoom = await _rtcService.GetInitiatingRoomByDeviceCodeAsync(new GetInitiatingRoomByDeviceCodeRequest
- {
- DeviceCode = deviceCode
- });
- var deviceTokenInfo = CacheMaintenance.Instance.Get<ITokensManager>().Where(x => x.ClientId == deviceCode)?.FirstOrDefault();
- LiveMemberDTO deviceMemberInfo;
- if (liveRoom != null)
- {
- if (liveRoom.LiveRoomCode == deviceCode)
- {
- //设备正在推流
- deviceMemberInfo = liveRoom.DeviceInfos.FirstOrDefault(x => x.Code == deviceCode);
- if (deviceMemberInfo?.Status == LiveMemberStatus.Joined)
- {
- if (userInfo != null)
- {
- //添加观众
- await _rtcService.SaveViewerAsync(new SaveViewerRequest
- {
- LiveRoomCode = liveRoom.LiveRoomCode,
- UserCode = userInfo.ClientId,
- UserName = userInfo.AccountName,
- });
- }
- return new JoinDeviceLiveRoomResult
- {
- RoomNo = liveRoom.RtcRoomId,
- LiveProtocol = EnvironmentConfigs.General.LiveProtocolDetail<TransactionStatusEnum>(),
- ReportStateIntervalSeconds = _reportStateIntervalSeconds,
- DeviceCode = deviceCode,
- MergedChannel = deviceInfo.MergedChannel,
- MergedVideoOutputWidth = deviceInfo.MergedVideoOutputWidth,
- MergedVideoOutputHeight = deviceInfo.MergedVideoOutputHeight,
- VideoDeviceInfos = deviceMemberInfo.VideoDeviceInfos?.Select(x => new VideoDeviceInfoDTO
- {
- VideoDeviceId = x.VideoDeviceId,
- VideoDeviceSourceType = x.VideoDeviceSourceType,
- LiveData = x.LiveData
- })?.ToList() ?? new List<VideoDeviceInfoDTO>(),
- IsOldPlatform = deviceTokenInfo != null && deviceTokenInfo.IsOnline && deviceTokenInfo.IsOldPlatform,
- SupportRtc = deviceInfo.SupportRtc,
- };
- }
- }
- }
- //新增或更新设备直播间
- var deviceName = deviceInfo.DisplayName;
- var deviceLiveInfo = new LiveMemberDTO
- {
- Code = deviceCode,
- Name = deviceName,
- MemberType = LiveMemberEnum.Device,
- HeadImageToken = deviceInfo.HeadPicUrl,
- Status = LiveMemberStatus.Accepted,
- };
- var room = new LiveRoomDTO
- {
- LiveRoomCode = deviceCode,
- Name = deviceName,
- RelatedCode = deviceCode,
- BusinessModule = BusinessModuleEnum.DeviceLiving,
- Status = LiveRoomStatus.Default,
- UserInfos = new List<LiveMemberDTO>(),
- DeviceInfos = new List<LiveMemberDTO> { deviceLiveInfo }
- };
- await _rtcService.AddOrUpdateLiveRoomAsync(new AddOrUpdateLiveRoomRequest
- {
- LiveRoom = room,
- });
- var initiateResult = await _rtcService.InitiateAsync(new InitiateRequest
- {
- LiveRoomCode = deviceCode,
- InitiatorCode = deviceCode,
- });
- deviceMemberInfo = initiateResult.DeviceInfos.FirstOrDefault(x => x.Code == deviceCode);
- if (userInfo != null)
- {
- //添加观众
- await _rtcService.SaveViewerAsync(new SaveViewerRequest
- {
- LiveRoomCode = initiateResult.LiveRoomCode,
- UserCode = userInfo.ClientId,
- UserName = userInfo.AccountName,
- });
- }
- return new JoinDeviceLiveRoomResult
- {
- RoomNo = initiateResult.RtcRoomId,
- LiveProtocol = EnvironmentConfigs.General.LiveProtocolDetail<TransactionStatusEnum>(),
- ReportStateIntervalSeconds = _reportStateIntervalSeconds,
- DeviceCode = deviceCode,
- MergedChannel = deviceInfo.MergedChannel,
- MergedVideoOutputWidth = deviceInfo.MergedVideoOutputWidth,
- MergedVideoOutputHeight = deviceInfo.MergedVideoOutputHeight,
- VideoDeviceInfos = deviceMemberInfo.VideoDeviceInfos?.Select(x => new VideoDeviceInfoDTO
- {
- VideoDeviceId = x.VideoDeviceId,
- VideoDeviceSourceType = x.VideoDeviceSourceType,
- LiveData = x.LiveData
- })?.ToList() ?? new List<VideoDeviceInfoDTO>(),
- IsOldPlatform = deviceTokenInfo != null && deviceTokenInfo.IsOnline && deviceTokenInfo.IsOldPlatform,
- SupportRtc = deviceInfo.SupportRtc,
- };
- }
- }
- }
|