Files
Ayay/SHH.Contracts/VideoPayload.cs
2025-12-31 20:43:54 +08:00

86 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace SHH.Contracts
{
/// <summary>
/// 视频数据传输契约(增强版)
/// </summary>
public class VideoPayload
{
public List<string> SubscriberIds { get; } = new List<string>(16);
// ==========================================
// 1. 基础元数据 (将被序列化到 JSON)
// ==========================================
public string CameraId { get; set; } // 摄像头唯一标记
// 时间信息 (建议使用 DateTime调试看日志更直观)
public DateTime CaptureTime { get; set; } // 采集时间 (SDK产生图的时间)
public DateTime DispatchTime { get; set; } // 分发时间 (Server发出图的时间)
// ==========================================
// 2. 图像规格信息
// ==========================================
public int OriginalWidth { get; set; } // 原始宽度
public int OriginalHeight { get; set; } // 原始高度
public int TargetWidth { get; set; } // 目标/处理后宽度
public int TargetHeight { get; set; } // 目标/处理后高度
// ==========================================
// 3. 核心二进制数据 (严禁序列化到 JSON)
// ==========================================
/// <summary>
/// 原始图像数据 (例如海康SDK出来的原始 JPG)
/// JsonIgnore 防止误操作导致序列化性能崩塌
/// </summary>
[JsonIgnore]
public byte[] OriginalImageBytes { get; set; }
/// <summary>
/// 处理后的目标图像 (例如 Yolo 画框后的图,或者缩放后的图)
/// 可为空
/// </summary>
[JsonIgnore]
public byte[] TargetImageBytes { get; set; }
// ==========================================
// 4. 辅助方法
// ==========================================
/// <summary>
/// 仅获取元数据的 JSON 字符串
/// </summary>
public string GetMetadataJson()
{
// 创建一个纯净的匿名对象用于序列化
var meta = new
{
CameraId,
CaptureTime,
DispatchTime,
OriginalWidth,
OriginalHeight,
TargetWidth,
TargetHeight,
SubscriberIds,
// 标记一下是否有目标图方便接收端判断要不要读第3帧
HasTargetImage = (TargetImageBytes != null && TargetImageBytes.Length > 0)
};
return JsonConvert.SerializeObject(meta);
}
/// <summary>
/// 从 JSON 还原元数据 (还原出来的对象 ImageBytes 默认为空,需后续填充)
/// </summary>
public static VideoPayload FromMetadataJson(string json)
{
return JsonConvert.DeserializeObject<VideoPayload>(json);
}
}
}