diff --git a/server/src/Controllers/VideoStreamController.cs b/server/src/Controllers/VideoStreamController.cs index 30e1c4c..879d5b7 100644 --- a/server/src/Controllers/VideoStreamController.cs +++ b/server/src/Controllers/VideoStreamController.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations; +using System.Threading.Tasks; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; @@ -185,15 +186,11 @@ public class VideoStreamController : ControllerBase [EnableCors("Users")] [ProducesResponseType(typeof(object), StatusCodes.Status200OK)] [ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)] - public IResult SetEnabled([FromQuery] bool enabled) + public async Task SetEnabled([FromQuery] bool enabled) { logger.Info("设置视频流服务开关: {Enabled}", enabled); - _videoStreamService.Enabled = enabled; - return TypedResults.Ok(new - { - success = true, - enabled = _videoStreamService.Enabled - }); + await _videoStreamService.SetEnable(enabled); + return TypedResults.Ok(); } /// diff --git a/server/src/Peripherals/CameraClient.cs b/server/src/Peripherals/CameraClient.cs index 78651ab..b06f903 100644 --- a/server/src/Peripherals/CameraClient.cs +++ b/server/src/Peripherals/CameraClient.cs @@ -25,291 +25,206 @@ class Camera const uint CAM_I2C_ADDR = 0x3C; const Peripherals.I2cClient.I2cProtocol CAM_PROTO = Peripherals.I2cClient.I2cProtocol.SCCB; - const UInt16 V_CMOS_DISP = 480; - const UInt16 H_CMOS_DISP = 640; - const UInt16 TOTAL_H_PIXEL = 2570; - const UInt16 TOTAL_V_PIXEL = 980; + + const UInt16 ISP_INPUT_SIZE_X = 2624; + const UInt16 ISP_INPUT_SIZE_Y = 1952; + const UInt16 X_OFFSET = 16; + const UInt16 Y_OFFSET = 4; + const UInt16 X_OUTPUT_SIZE = 640; + const UInt16 Y_OUTPUT_SIZE = 480; + const UInt16 X_TOTAL_OUTPUT_SIZE = 2844; + const UInt16 Y_TOTAL_OUTPUT_SIZE = 1968; + const UInt16 PISYCAL_PIXEL_START_X = 336; + const UInt16 PISYCAL_PIXEL_START_Y = 434; + const UInt16 PISYCAL_PIXEL_END_X = PISYCAL_PIXEL_START_X + ISP_INPUT_SIZE_X - 1; + const UInt16 PISYCAL_PIXEL_END_Y = PISYCAL_PIXEL_START_Y + ISP_INPUT_SIZE_Y - 1; + const byte PLL_MUX = 105; const UInt32 FrameAddr = 0x00; - const UInt32 FrameLength = V_CMOS_DISP * H_CMOS_DISP * 16 / 32; + const UInt32 FrameLength = X_OUTPUT_SIZE * Y_OUTPUT_SIZE * 16 / 32; - static byte[][] data = new byte[][] { - // 软件复位寄存器,恢复初始值 - new byte[] {0x30, 0x0a, 0x00}, // 0x300a = 0x00 - new byte[] {0x30, 0x0b, 0x00}, // 0x300b = 0x00 - new byte[] { 0x30, 0x08, 0x82 }, // 0x3008 = 0x82, Bit[7]:复位 Bit[6]:电源休眠 - new byte[] { 0x30, 0x08, 0x02 }, // 0x3008 = 0x02, 正常工作模式 - new byte[] { 0x31, 0x03, 0x02 }, // 0x3103 = 0x02, Bit[1]:1 PLL Clock - - // 引脚输入/输出控制 - new byte[] { 0x30, 0x17, 0xff }, // 0x3017 = 0xff, FREX/VSYNC/HREF/PCLK/D[9:6] - new byte[] { 0x30, 0x18, 0xff }, // 0x3018 = 0xff, D[5:0]/GPIO1/GPIO0 - new byte[] { 0x30, 0x37, 0x13 }, // 0x3037 = 0x13, PLL分频控制 - new byte[] { 0x31, 0x08, 0x01 }, // 0x3108 = 0x01, 系统根分频器 - new byte[] { 0x36, 0x30, 0x36 }, // 0x3630 = 0x36 - new byte[] { 0x36, 0x31, 0x0e }, // 0x3631 = 0x0e - new byte[] { 0x36, 0x32, 0xe2 }, // 0x3632 = 0xe2 - new byte[] { 0x36, 0x33, 0x12 }, // 0x3633 = 0x12 - new byte[] { 0x36, 0x21, 0xe0 }, // 0x3621 = 0xe0 - new byte[] { 0x37, 0x04, 0xa0 }, // 0x3704 = 0xa0 - new byte[] { 0x37, 0x03, 0x5a }, // 0x3703 = 0x5a - new byte[] { 0x37, 0x15, 0x78 }, // 0x3715 = 0x78 - new byte[] { 0x37, 0x17, 0x01 }, // 0x3717 = 0x01 - new byte[] { 0x37, 0x0b, 0x60 }, // 0x370b = 0x60 - new byte[] { 0x37, 0x05, 0x1a }, // 0x3705 = 0x1a - new byte[] { 0x39, 0x05, 0x02 }, // 0x3905 = 0x02 - new byte[] { 0x39, 0x06, 0x10 }, // 0x3906 = 0x10 - new byte[] { 0x39, 0x01, 0x0a }, // 0x3901 = 0x0a - new byte[] { 0x37, 0x31, 0x12 }, // 0x3731 = 0x12 - new byte[] { 0x36, 0x00, 0x08 }, // 0x3600 = 0x08, VCM控制,用于自动聚焦 - new byte[] { 0x36, 0x01, 0x33 }, // 0x3601 = 0x33, VCM控制,用于自动聚焦 - new byte[] { 0x30, 0x2d, 0x60 }, // 0x302d = 0x60, 系统控制 - new byte[] { 0x36, 0x20, 0x52 }, // 0x3620 = 0x52 - new byte[] { 0x37, 0x1b, 0x20 }, // 0x371b = 0x20 - new byte[] { 0x47, 0x1c, 0x50 }, // 0x471c = 0x50 - new byte[] { 0x3a, 0x13, 0x43 }, // 0x3a13 = 0x43, AEC(自动曝光控制) - new byte[] { 0x3a, 0x18, 0x00 }, // 0x3a18 = 0x00, AEC增益上限 - new byte[] { 0x3a, 0x19, 0xf8 }, // 0x3a19 = 0xf8, AEC增益上限 - new byte[] { 0x36, 0x35, 0x13 }, // 0x3635 = 0x13 - new byte[] { 0x36, 0x36, 0x03 }, // 0x3636 = 0x03 - new byte[] { 0x36, 0x34, 0x40 }, // 0x3634 = 0x40 - new byte[] { 0x36, 0x22, 0x01 }, // 0x3622 = 0x01 - new byte[] { 0x3c, 0x01, 0x34 }, // 0x3c01 = 0x34 - new byte[] { 0x3c, 0x04, 0x28 }, // 0x3c04 = 0x28 - new byte[] { 0x3c, 0x05, 0x98 }, // 0x3c05 = 0x98 - new byte[] { 0x3c, 0x06, 0x00 }, // 0x3c06 = 0x00, light meter 1 阈值[15:8] - new byte[] { 0x3c, 0x07, 0x08 }, // 0x3c07 = 0x08, light meter 1 阈值[7:0] - new byte[] { 0x3c, 0x08, 0x00 }, // 0x3c08 = 0x00, light meter 2 阈值[15:8] - new byte[] { 0x3c, 0x09, 0x1c }, // 0x3c09 = 0x1c, light meter 2 阈值[7:0] - new byte[] { 0x3c, 0x0a, 0x9c }, // 0x3c0a = 0x9c, sample number[15:8] - new byte[] { 0x3c, 0x0b, 0x40 }, // 0x3c0b = 0x40, sample number[7:0] - new byte[] { 0x38, 0x10, 0x00 }, // 0x3810 = 0x00, Timing Hoffset[11:8] - new byte[] { 0x38, 0x11, 0x10 }, // 0x3811 = 0x10, Timing Hoffset[7:0] - new byte[] { 0x38, 0x12, 0x00 }, // 0x3812 = 0x00, Timing Voffset[10:8] - new byte[] { 0x37, 0x08, 0x64 }, // 0x3708 = 0x64 - new byte[] { 0x40, 0x01, 0x02 }, // 0x4001 = 0x02, BLC(黑电平校准)补偿起始行号 - new byte[] { 0x40, 0x05, 0x1a }, // 0x4005 = 0x1a, BLC(黑电平校准)补偿始终更新 - new byte[] { 0x30, 0x00, 0x00 }, // 0x3000 = 0x00, 系统块复位控制 - new byte[] { 0x30, 0x04, 0xff }, // 0x3004 = 0xff, 时钟使能控制 - new byte[] { 0x43, 0x00, 0x61 }, // 0x4300 = 0x61, 格式控制 RGB565 - new byte[] { 0x50, 0x1f, 0x01 }, // 0x501f = 0x01, ISP RGB - new byte[] { 0x44, 0x0e, 0x00 }, // 0x440e = 0x00 - new byte[] { 0x50, 0x00, 0xa7 }, // 0x5000 = 0xa7, ISP控制 - new byte[] { 0x3a, 0x0f, 0x30 }, // 0x3a0f = 0x30, AEC控制;stable range in high - new byte[] { 0x3a, 0x10, 0x28 }, // 0x3a10 = 0x28, AEC控制;stable range in low - new byte[] { 0x3a, 0x1b, 0x30 }, // 0x3a1b = 0x30, AEC控制;stable range out high - new byte[] { 0x3a, 0x1e, 0x26 }, // 0x3a1e = 0x26, AEC控制;stable range out low - new byte[] { 0x3a, 0x11, 0x60 }, // 0x3a11 = 0x60, AEC控制; fast zone high - new byte[] { 0x3a, 0x1f, 0x14 }, // 0x3a1f = 0x14, AEC控制; fast zone low - - // LENC(镜头校正)控制 0x5800~0x583d - new byte[] { 0x58, 0x00, 0x23 }, - new byte[] { 0x58, 0x01, 0x14 }, - new byte[] { 0x58, 0x02, 0x0f }, - new byte[] { 0x58, 0x03, 0x0f }, - new byte[] { 0x58, 0x04, 0x12 }, - new byte[] { 0x58, 0x05, 0x26 }, - new byte[] { 0x58, 0x06, 0x0c }, - new byte[] { 0x58, 0x07, 0x08 }, - new byte[] { 0x58, 0x08, 0x05 }, - new byte[] { 0x58, 0x09, 0x05 }, - new byte[] { 0x58, 0x0a, 0x08 }, - new byte[] { 0x58, 0x0b, 0x0d }, - new byte[] { 0x58, 0x0c, 0x08 }, - new byte[] { 0x58, 0x0d, 0x03 }, - new byte[] { 0x58, 0x0e, 0x00 }, - new byte[] { 0x58, 0x0f, 0x00 }, - new byte[] { 0x58, 0x10, 0x03 }, - new byte[] { 0x58, 0x11, 0x09 }, - new byte[] { 0x58, 0x12, 0x07 }, - new byte[] { 0x58, 0x13, 0x03 }, - new byte[] { 0x58, 0x14, 0x00 }, - new byte[] { 0x58, 0x15, 0x01 }, - new byte[] { 0x58, 0x16, 0x03 }, - new byte[] { 0x58, 0x17, 0x08 }, - new byte[] { 0x58, 0x18, 0x0d }, - new byte[] { 0x58, 0x19, 0x08 }, - new byte[] { 0x58, 0x1a, 0x05 }, - new byte[] { 0x58, 0x1b, 0x06 }, - new byte[] { 0x58, 0x1c, 0x08 }, - new byte[] { 0x58, 0x1d, 0x0e }, - new byte[] { 0x58, 0x1e, 0x29 }, - new byte[] { 0x58, 0x1f, 0x17 }, - new byte[] { 0x58, 0x20, 0x11 }, - new byte[] { 0x58, 0x21, 0x11 }, - new byte[] { 0x58, 0x22, 0x15 }, - new byte[] { 0x58, 0x23, 0x28 }, - new byte[] { 0x58, 0x24, 0x46 }, - new byte[] { 0x58, 0x25, 0x26 }, - new byte[] { 0x58, 0x26, 0x08 }, - new byte[] { 0x58, 0x27, 0x26 }, - new byte[] { 0x58, 0x28, 0x64 }, - new byte[] { 0x58, 0x29, 0x26 }, - new byte[] { 0x58, 0x2a, 0x24 }, - new byte[] { 0x58, 0x2b, 0x22 }, - new byte[] { 0x58, 0x2c, 0x24 }, - new byte[] { 0x58, 0x2d, 0x24 }, - new byte[] { 0x58, 0x2e, 0x06 }, - new byte[] { 0x58, 0x2f, 0x22 }, - new byte[] { 0x58, 0x30, 0x40 }, - new byte[] { 0x58, 0x31, 0x42 }, - new byte[] { 0x58, 0x32, 0x24 }, - new byte[] { 0x58, 0x33, 0x26 }, - new byte[] { 0x58, 0x34, 0x24 }, - new byte[] { 0x58, 0x35, 0x22 }, - new byte[] { 0x58, 0x36, 0x22 }, - new byte[] { 0x58, 0x37, 0x26 }, - new byte[] { 0x58, 0x38, 0x44 }, - new byte[] { 0x58, 0x39, 0x24 }, - new byte[] { 0x58, 0x3a, 0x26 }, - new byte[] { 0x58, 0x3b, 0x28 }, - new byte[] { 0x58, 0x3c, 0x42 }, - new byte[] { 0x58, 0x3d, 0xce }, - - // AWB(自动白平衡控制) 0x5180~0x519e - new byte[] { 0x51, 0x80, 0xff }, - new byte[] { 0x51, 0x81, 0xf2 }, - new byte[] { 0x51, 0x82, 0x00 }, - new byte[] { 0x51, 0x83, 0x14 }, - new byte[] { 0x51, 0x84, 0x25 }, - new byte[] { 0x51, 0x85, 0x24 }, - new byte[] { 0x51, 0x86, 0x09 }, - new byte[] { 0x51, 0x87, 0x09 }, - new byte[] { 0x51, 0x88, 0x09 }, - new byte[] { 0x51, 0x89, 0x75 }, - new byte[] { 0x51, 0x8a, 0x54 }, - new byte[] { 0x51, 0x8b, 0xe0 }, - new byte[] { 0x51, 0x8c, 0xb2 }, - new byte[] { 0x51, 0x8d, 0x42 }, - new byte[] { 0x51, 0x8e, 0x3d }, - new byte[] { 0x51, 0x8f, 0x56 }, - new byte[] { 0x51, 0x90, 0x46 }, - new byte[] { 0x51, 0x91, 0xf8 }, - new byte[] { 0x51, 0x92, 0x04 }, - new byte[] { 0x51, 0x93, 0x70 }, - new byte[] { 0x51, 0x94, 0xf0 }, - new byte[] { 0x51, 0x95, 0xf0 }, - new byte[] { 0x51, 0x96, 0x03 }, - new byte[] { 0x51, 0x97, 0x01 }, - new byte[] { 0x51, 0x98, 0x04 }, - new byte[] { 0x51, 0x99, 0x12 }, - new byte[] { 0x51, 0x9a, 0x04 }, - new byte[] { 0x51, 0x9b, 0x00 }, - new byte[] { 0x51, 0x9c, 0x06 }, - new byte[] { 0x51, 0x9d, 0x82 }, + static byte[][] InitCmdData = new byte[][] { + // Stop streaming + new byte[] { 0x30, 0x08, 0x42 }, + new byte[] { 0x31, 0x03, 0x02 }, + new byte[] { 0x30, 0x17, 0xff }, + new byte[] { 0x30, 0x18, 0xff }, + new byte[] { 0x30, 0x37, 0x13 }, + new byte[] { 0x31, 0x08, 0x01 }, + new byte[] { 0x36, 0x30, 0x36 }, + new byte[] { 0x36, 0x31, 0x0e }, + new byte[] { 0x36, 0x32, 0xe2 }, + new byte[] { 0x36, 0x33, 0x12 }, + new byte[] { 0x36, 0x21, 0xe0 }, + new byte[] { 0x37, 0x04, 0xa0 }, + new byte[] { 0x37, 0x03, 0x5a }, + new byte[] { 0x37, 0x15, 0x78 }, + new byte[] { 0x37, 0x17, 0x01 }, + new byte[] { 0x37, 0x0b, 0x60 }, + new byte[] { 0x37, 0x05, 0x1a }, + new byte[] { 0x39, 0x05, 0x02 }, + new byte[] { 0x39, 0x06, 0x10 }, + new byte[] { 0x39, 0x01, 0x0a }, + new byte[] { 0x37, 0x31, 0x12 }, + new byte[] { 0x36, 0x00, 0x08 }, + new byte[] { 0x36, 0x01, 0x33 }, + new byte[] { 0x30, 0x2d, 0x60 }, + new byte[] { 0x36, 0x20, 0x52 }, + new byte[] { 0x37, 0x1b, 0x20 }, + new byte[] { 0x47, 0x1c, 0x50 }, + new byte[] { 0x3a, 0x13, 0x43 }, + new byte[] { 0x3a, 0x18, 0x00 }, + new byte[] { 0x3a, 0x19, 0xf8 }, + new byte[] { 0x36, 0x35, 0x13 }, + new byte[] { 0x36, 0x36, 0x03 }, + new byte[] { 0x36, 0x34, 0x40 }, + new byte[] { 0x36, 0x22, 0x01 }, + new byte[] { 0x3c, 0x01, 0x34 }, + new byte[] { 0x3c, 0x04, 0x28 }, + new byte[] { 0x3c, 0x05, 0x98 }, + new byte[] { 0x3c, 0x06, 0x00 }, + new byte[] { 0x3c, 0x07, 0x07 }, + new byte[] { 0x3c, 0x08, 0x00 }, + new byte[] { 0x3c, 0x09, 0x1c }, + new byte[] { 0x3c, 0x0a, 0x9c }, + new byte[] { 0x3c, 0x0b, 0x40 }, + // X_OFFSET/Y_OFFSET + new byte[] { 0x38, 0x10, unchecked((byte)((X_OFFSET >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x11, unchecked((byte)(X_OFFSET & 0xFF)) }, + new byte[] { 0x38, 0x12, unchecked((byte)((Y_OFFSET >> 8) & 0xFF)) }, + new byte[] { 0x37, 0x08, 0x64 }, + new byte[] { 0x40, 0x01, 0x02 }, + new byte[] { 0x40, 0x05, 0x1a }, + new byte[] { 0x30, 0x00, 0x00 }, + new byte[] { 0x30, 0x04, 0xff }, + new byte[] { 0x43, 0x00, 0x00 }, // RAW: BGGR + new byte[] { 0x50, 0x1f, 0x03 }, // ISP RAW DPC + new byte[] { 0x44, 0x0e, 0x00 }, + new byte[] { 0x50, 0x00, 0x06 }, + new byte[] { 0x3a, 0x0f, 0x78 }, + new byte[] { 0x3a, 0x10, 0x68 }, + new byte[] { 0x3a, 0x1b, 0x78 }, + new byte[] { 0x3a, 0x1e, 0x68 }, + new byte[] { 0x3a, 0x11, 0xD0 }, + new byte[] { 0x3a, 0x1f, 0x40 }, + // LENC + new byte[] { 0x58, 0x00, 0x23 }, new byte[] { 0x58, 0x01, 0x14 }, new byte[] { 0x58, 0x02, 0x0f }, + new byte[] { 0x58, 0x03, 0x0f }, new byte[] { 0x58, 0x04, 0x12 }, new byte[] { 0x58, 0x05, 0x26 }, + new byte[] { 0x58, 0x06, 0x0c }, new byte[] { 0x58, 0x07, 0x08 }, new byte[] { 0x58, 0x08, 0x05 }, + new byte[] { 0x58, 0x09, 0x05 }, new byte[] { 0x58, 0x0a, 0x08 }, new byte[] { 0x58, 0x0b, 0x0d }, + new byte[] { 0x58, 0x0c, 0x08 }, new byte[] { 0x58, 0x0d, 0x03 }, new byte[] { 0x58, 0x0e, 0x00 }, + new byte[] { 0x58, 0x0f, 0x00 }, new byte[] { 0x58, 0x10, 0x03 }, new byte[] { 0x58, 0x11, 0x09 }, + new byte[] { 0x58, 0x12, 0x07 }, new byte[] { 0x58, 0x13, 0x03 }, new byte[] { 0x58, 0x14, 0x00 }, + new byte[] { 0x58, 0x15, 0x01 }, new byte[] { 0x58, 0x16, 0x03 }, new byte[] { 0x58, 0x17, 0x08 }, + new byte[] { 0x58, 0x18, 0x0d }, new byte[] { 0x58, 0x19, 0x08 }, new byte[] { 0x58, 0x1a, 0x05 }, + new byte[] { 0x58, 0x1b, 0x06 }, new byte[] { 0x58, 0x1c, 0x08 }, new byte[] { 0x58, 0x1d, 0x0e }, + new byte[] { 0x58, 0x1e, 0x29 }, new byte[] { 0x58, 0x1f, 0x17 }, new byte[] { 0x58, 0x20, 0x11 }, + new byte[] { 0x58, 0x21, 0x11 }, new byte[] { 0x58, 0x22, 0x15 }, new byte[] { 0x58, 0x23, 0x28 }, + new byte[] { 0x58, 0x24, 0x46 }, new byte[] { 0x58, 0x25, 0x26 }, new byte[] { 0x58, 0x26, 0x08 }, + new byte[] { 0x58, 0x27, 0x26 }, new byte[] { 0x58, 0x28, 0x64 }, new byte[] { 0x58, 0x29, 0x26 }, + new byte[] { 0x58, 0x2a, 0x24 }, new byte[] { 0x58, 0x2b, 0x22 }, new byte[] { 0x58, 0x2c, 0x24 }, + new byte[] { 0x58, 0x2d, 0x24 }, new byte[] { 0x58, 0x2e, 0x06 }, new byte[] { 0x58, 0x2f, 0x22 }, + new byte[] { 0x58, 0x30, 0x40 }, new byte[] { 0x58, 0x31, 0x42 }, new byte[] { 0x58, 0x32, 0x24 }, + new byte[] { 0x58, 0x33, 0x26 }, new byte[] { 0x58, 0x34, 0x24 }, new byte[] { 0x58, 0x35, 0x22 }, + new byte[] { 0x58, 0x36, 0x22 }, new byte[] { 0x58, 0x37, 0x26 }, new byte[] { 0x58, 0x38, 0x44 }, + new byte[] { 0x58, 0x39, 0x24 }, new byte[] { 0x58, 0x3a, 0x26 }, new byte[] { 0x58, 0x3b, 0x28 }, + new byte[] { 0x58, 0x3c, 0x42 }, new byte[] { 0x58, 0x3d, 0xce }, + // AWB + new byte[] { 0x51, 0x80, 0xff }, new byte[] { 0x51, 0x81, 0xf2 }, new byte[] { 0x51, 0x82, 0x00 }, + new byte[] { 0x51, 0x83, 0x14 }, new byte[] { 0x51, 0x84, 0x25 }, new byte[] { 0x51, 0x85, 0x24 }, + new byte[] { 0x51, 0x86, 0x09 }, new byte[] { 0x51, 0x87, 0x09 }, new byte[] { 0x51, 0x88, 0x09 }, + new byte[] { 0x51, 0x89, 0x75 }, new byte[] { 0x51, 0x8a, 0x54 }, new byte[] { 0x51, 0x8b, 0xe0 }, + new byte[] { 0x51, 0x8c, 0xb2 }, new byte[] { 0x51, 0x8d, 0x42 }, new byte[] { 0x51, 0x8e, 0x3d }, + new byte[] { 0x51, 0x8f, 0x56 }, new byte[] { 0x51, 0x90, 0x46 }, new byte[] { 0x51, 0x91, 0xf8 }, + new byte[] { 0x51, 0x92, 0x04 }, new byte[] { 0x51, 0x93, 0x70 }, new byte[] { 0x51, 0x94, 0xf0 }, + new byte[] { 0x51, 0x95, 0xf0 }, new byte[] { 0x51, 0x96, 0x03 }, new byte[] { 0x51, 0x97, 0x01 }, + new byte[] { 0x51, 0x98, 0x04 }, new byte[] { 0x51, 0x99, 0x12 }, new byte[] { 0x51, 0x9a, 0x04 }, + new byte[] { 0x51, 0x9b, 0x00 }, new byte[] { 0x51, 0x9c, 0x06 }, new byte[] { 0x51, 0x9d, 0x82 }, new byte[] { 0x51, 0x9e, 0x38 }, - - // Gamma(伽马)控制 0x5480~0x5490 - new byte[] { 0x54, 0x80, 0x01 }, - new byte[] { 0x54, 0x81, 0x08 }, - new byte[] { 0x54, 0x82, 0x14 }, - new byte[] { 0x54, 0x83, 0x28 }, - new byte[] { 0x54, 0x84, 0x51 }, - new byte[] { 0x54, 0x85, 0x65 }, - new byte[] { 0x54, 0x86, 0x71 }, - new byte[] { 0x54, 0x87, 0x7d }, - new byte[] { 0x54, 0x88, 0x87 }, - new byte[] { 0x54, 0x89, 0x91 }, - new byte[] { 0x54, 0x8a, 0x9a }, - new byte[] { 0x54, 0x8b, 0xaa }, - new byte[] { 0x54, 0x8c, 0xb8 }, - new byte[] { 0x54, 0x8d, 0xcd }, - new byte[] { 0x54, 0x8e, 0xdd }, - new byte[] { 0x54, 0x8f, 0xea }, - new byte[] { 0x54, 0x90, 0x1d }, - - // CMX(彩色矩阵控制) 0x5381~0x538b - new byte[] { 0x53, 0x81, 0x1e }, - new byte[] { 0x53, 0x82, 0x5b }, - new byte[] { 0x53, 0x83, 0x08 }, - new byte[] { 0x53, 0x84, 0x0a }, - new byte[] { 0x53, 0x85, 0x7e }, - new byte[] { 0x53, 0x86, 0x88 }, - new byte[] { 0x53, 0x87, 0x7c }, - new byte[] { 0x53, 0x88, 0x6c }, - new byte[] { 0x53, 0x89, 0x10 }, - new byte[] { 0x53, 0x8a, 0x01 }, - new byte[] { 0x53, 0x8b, 0x98 }, - - // SDE(特殊数码效果)控制 0x5580~0x558b - new byte[] { 0x55, 0x80, 0x06 }, - new byte[] { 0x55, 0x83, 0x40 }, - new byte[] { 0x55, 0x84, 0x10 }, - new byte[] { 0x55, 0x89, 0x10 }, - new byte[] { 0x55, 0x8a, 0x00 }, - new byte[] { 0x55, 0x8b, 0xf8 }, - - new byte[] { 0x50, 0x1d, 0x40 }, //ISP MISC - - // CIP(颜色插值)控制 0x5300~0x530c - new byte[] { 0x53, 0x00, 0x08 }, - new byte[] { 0x53, 0x01, 0x30 }, - new byte[] { 0x53, 0x02, 0x10 }, - new byte[] { 0x53, 0x03, 0x00 }, - new byte[] { 0x53, 0x04, 0x08 }, - new byte[] { 0x53, 0x05, 0x30 }, - new byte[] { 0x53, 0x06, 0x08 }, - new byte[] { 0x53, 0x07, 0x16 }, - new byte[] { 0x53, 0x09, 0x08 }, - new byte[] { 0x53, 0x0a, 0x30 }, - new byte[] { 0x53, 0x0b, 0x04 }, - new byte[] { 0x53, 0x0c, 0x06 }, + // Gamma + new byte[] { 0x54, 0x80, 0x01 }, new byte[] { 0x54, 0x81, 0x08 }, new byte[] { 0x54, 0x82, 0x14 }, + new byte[] { 0x54, 0x83, 0x28 }, new byte[] { 0x54, 0x84, 0x51 }, new byte[] { 0x54, 0x85, 0x65 }, + new byte[] { 0x54, 0x86, 0x71 }, new byte[] { 0x54, 0x87, 0x7d }, new byte[] { 0x54, 0x88, 0x87 }, + new byte[] { 0x54, 0x89, 0x91 }, new byte[] { 0x54, 0x8a, 0x9a }, new byte[] { 0x54, 0x8b, 0xaa }, + new byte[] { 0x54, 0x8c, 0xb8 }, new byte[] { 0x54, 0x8d, 0xcd }, new byte[] { 0x54, 0x8e, 0xdd }, + new byte[] { 0x54, 0x8f, 0xea }, new byte[] { 0x54, 0x90, 0x1d }, + // CMX + new byte[] { 0x53, 0x81, 0x1e }, new byte[] { 0x53, 0x82, 0x5b }, new byte[] { 0x53, 0x83, 0x08 }, + new byte[] { 0x53, 0x84, 0x0a }, new byte[] { 0x53, 0x85, 0x7e }, new byte[] { 0x53, 0x86, 0x88 }, + new byte[] { 0x53, 0x87, 0x7c }, new byte[] { 0x53, 0x88, 0x6c }, new byte[] { 0x53, 0x89, 0x10 }, + new byte[] { 0x53, 0x8a, 0x01 }, new byte[] { 0x53, 0x8b, 0x98 }, + // SDE + new byte[] { 0x55, 0x80, 0x06 }, new byte[] { 0x55, 0x83, 0x40 }, new byte[] { 0x55, 0x84, 0x10 }, + new byte[] { 0x55, 0x89, 0x10 }, new byte[] { 0x55, 0x8a, 0x00 }, new byte[] { 0x55, 0x8b, 0xf8 }, + new byte[] { 0x50, 0x1d, 0x40 }, + // CIP + new byte[] { 0x53, 0x00, 0x08 }, new byte[] { 0x53, 0x01, 0x30 }, new byte[] { 0x53, 0x02, 0x10 }, + new byte[] { 0x53, 0x03, 0x00 }, new byte[] { 0x53, 0x04, 0x08 }, new byte[] { 0x53, 0x05, 0x30 }, + new byte[] { 0x53, 0x06, 0x08 }, new byte[] { 0x53, 0x07, 0x16 }, new byte[] { 0x53, 0x09, 0x08 }, + new byte[] { 0x53, 0x0a, 0x30 }, new byte[] { 0x53, 0x0b, 0x04 }, new byte[] { 0x53, 0x0c, 0x06 }, new byte[] { 0x50, 0x25, 0x00 }, - - new byte[] { 0x30, 0x35, 0x11 }, // 0x3035 = 0x11, 系统时钟分频 input clock =24Mhz, PCLK = 48Mhz - new byte[] { 0x30, 0x36, 0x3c }, // 0x3036 = 0x3c, PLL倍频 - new byte[] { 0x3c, 0x07, 0x08 }, // 0x3c07 = 0x08 - - // 时序控制 0x3800~0x3821 - new byte[] { 0x38, 0x20, 0x46 }, // 0x3820 = 0x46 - new byte[] { 0x38, 0x21, 0x01 }, // 0x3821 = 0x01 - new byte[] { 0x38, 0x14, 0x31 }, // 0x3814 = 0x31 - new byte[] { 0x38, 0x15, 0x31 }, // 0x3815 = 0x31 - new byte[] { 0x38, 0x00, 0x00 }, // 0x3800 = 0x00 - new byte[] { 0x38, 0x01, 0x00 }, // 0x3801 = 0x00 - new byte[] { 0x38, 0x02, 0x00 }, // 0x3802 = 0x00 - new byte[] { 0x38, 0x03, 0x04 }, // 0x3803 = 0x04 - new byte[] { 0x38, 0x04, 0x0a }, // 0x3804 = 0x0a - new byte[] { 0x38, 0x05, 0x3f }, // 0x3805 = 0x3f - new byte[] { 0x38, 0x06, 0x07 }, // 0x3806 = 0x07 - new byte[] { 0x38, 0x07, 0x9b }, // 0x3807 = 0x9b - - // 设置输出像素个数 - new byte[] { 0x38, 0x08, unchecked((byte)((H_CMOS_DISP >> 8) & 0x0F)) }, // 0x3808, DVP输出水平像素点数高4位 - new byte[] { 0x38, 0x09, unchecked((byte)(H_CMOS_DISP & 0xFF)) }, // 0x3809, DVP输出水平像素点数低8位 - new byte[] { 0x38, 0x0A, unchecked((byte)((V_CMOS_DISP >> 8) & 0x07)) }, // 0x380a, DVP输出垂直像素点数高3位 - new byte[] { 0x38, 0x0B, unchecked((byte)(V_CMOS_DISP & 0xFF)) }, // 0x380b, DVP输出垂直像素点数低8位 - new byte[] { 0x38, 0x0C, unchecked((byte)((TOTAL_H_PIXEL >> 8) & 0x1F)) }, // 0x380c, 水平总像素大小高5位 - new byte[] { 0x38, 0x0D, unchecked((byte)(TOTAL_H_PIXEL & 0xFF)) }, // 0x380d, 水平总像素大小低8位 - new byte[] { 0x38, 0x0E, unchecked((byte)((TOTAL_V_PIXEL >> 8) & 0x1F)) }, // 0x380e, 垂直总像素大小高5位 - new byte[] { 0x38, 0x0F, unchecked((byte)(TOTAL_V_PIXEL & 0xFF)) }, // 0x380f, 垂直总像素大小低8位 - - new byte[] { 0x38, 0x13, 0x06 }, // 0x3813 = 0x06 - new byte[] { 0x36, 0x18, 0x00 }, // 0x3618 = 0x00 - new byte[] { 0x36, 0x12, 0x29 }, // 0x3612 = 0x29 - new byte[] { 0x37, 0x09, 0x52 }, // 0x3709 = 0x52 - new byte[] { 0x37, 0x0c, 0x03 }, // 0x370c = 0x03 - new byte[] { 0x3a, 0x02, 0x17 }, // 0x3a02 = 0x17, 60Hz max exposure - new byte[] { 0x3a, 0x03, 0x10 }, // 0x3a03 = 0x10, 60Hz max exposure - new byte[] { 0x3a, 0x14, 0x17 }, // 0x3a14 = 0x17, 50Hz max exposure - new byte[] { 0x3a, 0x15, 0x10 }, // 0x3a15 = 0x10, 50Hz max exposure - new byte[] { 0x40, 0x04, 0x02 }, // 0x4004 = 0x02, BLC(背光) 2 lines - new byte[] { 0x47, 0x13, 0x03 }, // 0x4713 = 0x03, JPEG mode 3 - new byte[] { 0x44, 0x07, 0x04 }, // 0x4407 = 0x04, 量化标度 - new byte[] { 0x46, 0x0c, 0x22 }, // 0x460c = 0x22 - new byte[] { 0x48, 0x37, 0x22 }, // 0x4837 = 0x22, DVP CLK divider - new byte[] { 0x38, 0x24, 0x02 }, // 0x3824 = 0x02, DVP CLK divider - new byte[] { 0x50, 0x01, 0xa3 }, // 0x5001 = 0xa3, ISP控制 - new byte[] { 0x3b, 0x07, 0x0a }, // 0x3b07 = 0x0a, 帧曝光模式 - + // 系统时钟分频 + new byte[] { 0x30, 0x35, 0x21 }, // 30fps + new byte[] { 0x30, 0x36, PLL_MUX }, // PLL倍频 + new byte[] { 0x3c, 0x07, 0x08 }, + // 时序控制 + new byte[] { 0x38, 0x20, 0x41 }, // vflip + new byte[] { 0x38, 0x21, 0x00 }, // mirror + new byte[] { 0x38, 0x14, 0x11 }, // timing X inc + new byte[] { 0x38, 0x15, 0x11 }, // timing Y inc + // PISYCAL_PIXEL_START_X/Y + new byte[] { 0x38, 0x00, unchecked((byte)((PISYCAL_PIXEL_START_X >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x01, unchecked((byte)(PISYCAL_PIXEL_START_X & 0xFF)) }, + new byte[] { 0x38, 0x02, unchecked((byte)((PISYCAL_PIXEL_START_Y >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x03, unchecked((byte)(PISYCAL_PIXEL_START_Y & 0xFF)) }, + // PISYCAL_PIXEL_END_X/Y + new byte[] { 0x38, 0x04, unchecked((byte)((PISYCAL_PIXEL_END_X >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x05, unchecked((byte)(PISYCAL_PIXEL_END_X & 0xFF)) }, + new byte[] { 0x38, 0x06, unchecked((byte)((PISYCAL_PIXEL_END_Y >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x07, unchecked((byte)(PISYCAL_PIXEL_END_Y & 0xFF)) }, + // 输出像素个数 + new byte[] { 0x38, 0x08, unchecked((byte)((X_OUTPUT_SIZE >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x09, unchecked((byte)(X_OUTPUT_SIZE & 0xFF)) }, + new byte[] { 0x38, 0x0A, unchecked((byte)((Y_OUTPUT_SIZE >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x0B, unchecked((byte)(Y_OUTPUT_SIZE & 0xFF)) }, + // 总像素 + new byte[] { 0x38, 0x0C, unchecked((byte)((X_TOTAL_OUTPUT_SIZE >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x0D, unchecked((byte)(X_TOTAL_OUTPUT_SIZE & 0xFF)) }, + new byte[] { 0x38, 0x0E, unchecked((byte)((Y_TOTAL_OUTPUT_SIZE >> 8) & 0xFF)) }, + new byte[] { 0x38, 0x0F, unchecked((byte)(Y_TOTAL_OUTPUT_SIZE & 0xFF)) }, + new byte[] { 0x38, 0x13, unchecked((byte)(Y_OFFSET & 0xFF)) }, // Timing Voffset + new byte[] { 0x36, 0x18, 0x00 }, + new byte[] { 0x36, 0x12, 0x29 }, + new byte[] { 0x37, 0x09, 0x52 }, + new byte[] { 0x37, 0x0c, 0x03 }, + new byte[] { 0x3a, 0x02, 0x17 }, + new byte[] { 0x3a, 0x03, 0x10 }, + new byte[] { 0x3a, 0x14, 0x17 }, + new byte[] { 0x3a, 0x15, 0x10 }, + new byte[] { 0x40, 0x04, 0x02 }, + new byte[] { 0x47, 0x13, 0x00 }, + new byte[] { 0x44, 0x07, 0x04 }, + new byte[] { 0x46, 0x0c, 0x20 }, + new byte[] { 0x48, 0x37, 0x22 }, + new byte[] { 0x38, 0x24, 0x02 }, + new byte[] { 0x50, 0x01, 0x80 }, // 关闭缩放 + new byte[] { 0x3b, 0x07, 0x0a }, // 彩条测试使能 - new byte[] { 0x50, 0x3d, 0x00 }, // 0x503d = 0x00, 0x00:正常模式 0x80:彩条显示 - - // 测试闪光灯功能 - new byte[] { 0x30, 0x16, 0x02 }, // 0x3016 = 0x02 - new byte[] { 0x30, 0x1c, 0x02 }, // 0x301c = 0x02 - new byte[] { 0x30, 0x19, 0x02 }, // 0x3019 = 0x02, 打开闪光灯 - new byte[] { 0x30, 0x19, 0x00 }, // 0x3019 = 0x00, 关闭闪光灯 + new byte[] { 0x50, 0x3d, 0x00 }, + // 闪光灯禁用 + new byte[] { 0x30, 0x16, 0x00 }, + new byte[] { 0x30, 0x1c, 0x00 }, + new byte[] { 0x30, 0x19, 0x00 }, + new byte[] { 0x30, 0x31, 0x08 }, + new byte[] { 0x30, 0x2c, 0xC2 }, + new byte[] { 0x46, 0x00, 0xA0 }, + // for subsample=OFF + new byte[] { 0x36, 0x18, 0x04 }, + new byte[] { 0x36, 0x12, 0x2b }, + new byte[] { 0x37, 0x09, 0x12 }, + new byte[] { 0x37, 0x0c, 0x00 }, + // Start streaming + new byte[] { 0x30, 0x08, 0x02 } }; @@ -359,23 +274,9 @@ class Camera } } - { - var ret = await UDPClientPool.WriteAddr(this.ep, 2, CameraAddr.CAPTURE_ON, 0x01); - if (!ret.IsSuccessful) - { - logger.Error($"Failed to write CAPTURE_ON to camera at {this.address}:{this.port}, error: {ret.Error}"); - return new(ret.Error); - } - if (!ret.Value) - { - logger.Error($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}"); - return new(new Exception($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}")); - } - } - var i2c = new Peripherals.I2cClient.I2c(this.address, this.port, this.timeout); - foreach (var cmd in data) + foreach (var cmd in InitCmdData) { var ret = await i2c.WriteData(CAM_I2C_ADDR, cmd, CAM_PROTO); @@ -395,6 +296,22 @@ class Camera return true; } + public async ValueTask> EnableCamera(bool isEnable) + { + var ret = await UDPClientPool.WriteAddr(this.ep, 2, CameraAddr.CAPTURE_ON, Convert.ToUInt32(isEnable)); + if (!ret.IsSuccessful) + { + logger.Error($"Failed to write CAPTURE_ON to camera at {this.address}:{this.port}, error: {ret.Error}"); + return new(ret.Error); + } + if (!ret.Value) + { + logger.Error($"CAPTURE_ON write returned false for camera at {this.address}:{this.port}"); + return false; + } + return true; + } + /// /// 读取一帧图像数据 /// diff --git a/server/src/Services/HttpVideoStreamService.cs b/server/src/Services/HttpVideoStreamService.cs index 73e2819..fa4e83a 100644 --- a/server/src/Services/HttpVideoStreamService.cs +++ b/server/src/Services/HttpVideoStreamService.cs @@ -1,5 +1,6 @@ using System.Net; using System.Text; +using System.Threading.Tasks; using Peripherals.CameraClient; // 添加摄像头客户端引用 namespace server.Services; @@ -86,6 +87,7 @@ public class HttpVideoStreamService : BackgroundService // 摄像头客户端 private Camera? _camera; + private bool _cameraEnable = false; private string _cameraAddress = "192.168.1.100"; // 默认FPGA地址 private int _cameraPort = 8888; // 默认端口 private readonly object _cameraLock = new object(); @@ -95,11 +97,6 @@ public class HttpVideoStreamService : BackgroundService private readonly List _activeClients = new List(); private readonly object _clientsLock = new object(); - /// - /// 获取 / 设置视频流服务是否启用 - /// - public bool Enabled { get; set; } = false; - /// /// 获取当前连接的客户端数量 /// @@ -144,6 +141,21 @@ public class HttpVideoStreamService : BackgroundService logger.Info("HttpVideoStreamService 初始化完成,默认摄像头地址: {Address}:{Port}", _cameraAddress, _cameraPort); } + /// + /// [TODO:description] + /// + /// [TODO:parameter] + /// [TODO:return] + public async Task SetEnable(bool isEnabled) + { + if (_camera == null) + { + throw new Exception("Please config camera first"); + } + _cameraEnable = isEnabled; + await _camera.EnableCamera(_cameraEnable); + } + /// /// 配置摄像头连接参数 /// @@ -292,7 +304,7 @@ public class HttpVideoStreamService : BackgroundService // 开始生成视频帧 while (!stoppingToken.IsCancellationRequested) { - if (Enabled) + if (_cameraEnable) { await GenerateVideoFrames(stoppingToken); } @@ -548,7 +560,7 @@ public class HttpVideoStreamService : BackgroundService { var frameInterval = TimeSpan.FromMilliseconds(1000.0 / _frameRate); - while (!cancellationToken.IsCancellationRequested && Enabled) + while (!cancellationToken.IsCancellationRequested && _cameraEnable) { try { @@ -751,7 +763,7 @@ public class HttpVideoStreamService : BackgroundService return new ServiceStatus { - IsRunning = (_httpListener?.IsListening ?? false) && Enabled, + IsRunning = (_httpListener?.IsListening ?? false) && _cameraEnable, ServerPort = _serverPort, FrameRate = _frameRate, Resolution = $"{_frameWidth}x{_frameHeight}", @@ -761,6 +773,7 @@ public class HttpVideoStreamService : BackgroundService }; } + /// /// 停止 HTTP 视频流服务 /// @@ -768,7 +781,7 @@ public class HttpVideoStreamService : BackgroundService { logger.Info("正在停止 HTTP 视频流服务..."); - Enabled = false; + _cameraEnable = false; if (_httpListener != null && _httpListener.IsListening) {