private async Task getElementLibraryThumbnailAsync(CitpPeer peer, GetElementLibraryThumbnailMessagePacket requestPacket) { var msexIds = requestPacket.Version == MsexVersion.Version1_0 ? requestPacket.LibraryNumbers.Select(n => new MsexId(n)).ToList() : requestPacket.LibraryIds.Select(i => new MsexId(i)).ToList(); var imageRequest = new CitpImageRequest(requestPacket.ThumbnailWidth, requestPacket.ThumbnailHeight, requestPacket.ThumbnailFormat, requestPacket.ThumbnailFlags.HasFlag(MsexThumbnailFlags.PreserveAspectRatio), requestPacket.ThumbnailFormat == MsexImageFormat.Rgb8 && requestPacket.Version == MsexVersion.Version1_0); var thumbs = _device.GetElementLibraryThumbnails(imageRequest, requestPacket.LibraryType, msexIds); var packets = thumbs.Select(t => new ElementLibraryThumbnailMessagePacket { LibraryType = requestPacket.LibraryType, LibraryNumber = t.Item1.LibraryNumber.GetValueOrDefault(), LibraryId = t.Item1.LibraryId.GetValueOrDefault(), ThumbnailFormat = requestPacket.ThumbnailFormat, ThumbnailWidth = (ushort)t.Item2.ActualWidth, ThumbnailHeight = (ushort)t.Item2.ActualHeight, ThumbnailBuffer = t.Item2.Data, }); foreach (var packet in packets) { await _networkService.SendPacketAsync(packet, peer, requestPacket).ConfigureAwait(false); } }
public CitpImage(CitpImageRequest request, [NotNull] byte[] data, int actualWidth, int actualHeight) { if (data == null) { throw new ArgumentNullException(nameof(data)); } Request = request; Data = data; ActualWidth = actualWidth; ActualHeight = actualHeight; }
private async Task getElementThumbnailAsync(CitpPeer peer, GetElementThumbnailMessagePacket requestPacket) { MsexId msexId; if (requestPacket.Version == MsexVersion.Version1_0) { msexId = new MsexId(requestPacket.LibraryNumber); } else { Debug.Assert(requestPacket.LibraryId.HasValue, "LibraryId must have value for MSEX 1.1 or 1.2"); msexId = new MsexId(requestPacket.LibraryId.Value); } var imageRequest = new CitpImageRequest(requestPacket.ThumbnailWidth, requestPacket.ThumbnailHeight, requestPacket.ThumbnailFormat, requestPacket.ThumbnailFlags.HasFlag(MsexThumbnailFlags.PreserveAspectRatio), requestPacket.ThumbnailFormat == MsexImageFormat.Rgb8 && requestPacket.Version == MsexVersion.Version1_0); var thumbs = _device.GetElementThumbnails(imageRequest, requestPacket.LibraryType, msexId, requestPacket.ElementNumbers); var packets = thumbs.Select(t => new ElementThumbnailMessagePacket { LibraryType = requestPacket.LibraryType, LibraryNumber = requestPacket.LibraryNumber, LibraryId = requestPacket.LibraryId, ElementNumber = t.Item1, ThumbnailFormat = requestPacket.ThumbnailFormat, ThumbnailWidth = (ushort)t.Item2.ActualWidth, ThumbnailHeight = (ushort)t.Item2.ActualHeight, ThumbnailBuffer = t.Item2.Data }); foreach (var packet in packets) { await _networkService.SendPacketAsync(packet, peer, requestPacket).ConfigureAwait(false); } }
public async Task ProcessStreamRequestsAsync() { foreach (var request in _streamRequests.Values.ToList()) { foreach (var formatRequest in request.Formats) { if (Math.Abs(request.Fps) < float.Epsilon || DateTime.Now < formatRequest.LastOutput + TimeSpan.FromSeconds(1.0f / request.Fps)) { break; } var imageRequest = new CitpImageRequest(request.FrameWidth, request.FrameHeight, formatRequest.FrameFormat, true, formatRequest.FrameFormat == MsexImageFormat.Rgb8 && formatRequest.Version == MsexVersion.Version1_0); var frame = _streamProvider.GetVideoSourceFrame(request.SourceIdentifier, imageRequest); if (frame == null) { break; } var packet = new StreamFrameMessagePacket { Version = formatRequest.Version, MediaServerUuid = _streamProvider.Uuid, SourceIdentifier = Convert.ToUInt16(request.SourceIdentifier), FrameFormat = formatRequest.FrameFormat, FrameWidth = Convert.ToUInt16(frame.ActualWidth), FrameHeight = Convert.ToUInt16(frame.ActualHeight) }; if (formatRequest.FrameFormat == MsexImageFormat.FragmentedJpeg || formatRequest.FrameFormat == MsexImageFormat.FragmentedPng) { var fragments = frame.Data.Split(MaximumImageBufferSize); packet.FragmentInfo = new StreamFrameMessagePacket.FragmentPreamble() { FrameIndex = request.FrameCounter, FragmentCount = (ushort)fragments.Length }; if (fragments.Length > ushort.MaxValue) { _log.LogWarning("Cannot send streaming frame, too many image fragments"); return; } for (uint i = 0; i < fragments.Length; ++i) { packet.FragmentInfo.FragmentIndex = (ushort)i; packet.FragmentInfo.FragmentByteOffset = MaximumImageBufferSize * i; packet.FrameBuffer = fragments[i]; await _networkService.SendMulticastPacketAsync(packet).ConfigureAwait(false); } } else { if (frame.Data.Length > MaximumImageBufferSize) { _log.LogWarning($"Cannot send streaming frame request '{imageRequest}', image buffer too large"); return; } packet.FrameBuffer = frame.Data; await _networkService.SendMulticastPacketAsync(packet).ConfigureAwait(false); } formatRequest.LastOutput = DateTime.Now; } request.RemoveTimedOutRequests(); if (request.Formats.Count == 0) { _streamRequests.Remove(request.SourceIdentifier); } ++request.FrameCounter; } }