public void Open(CameraVideoSettings cameraSettings) { if (IsActive) { return; // or raise exception ? } //if (_stream != null) //{ // _stream.Close(); //} #if DEBUG Pi.Camera.OpenVideoStream(cameraSettings, onDataCallback: (byte[] data) => { if (IsActive && _stream.CanWrite) { _stream.Write(data); } }, onExitCallback: () => { IsActive = false; }); #endif IsActive = true; }
private static async Task RunAsync(string host, int port, CancellationToken cancellationToken) { var endpoint = new IPEndPoint(IPAddress.Parse(host), port); Console.WriteLine($"Broadcasting to {endpoint}"); using (var udpClient = new UdpClient()) { udpClient.EnableBroadcast = true; using (var controller = CameraController.Instance) { Console.WriteLine("Opening video stream"); var settings = new CameraVideoSettings { CaptureFramerate = 30, }; controller.OpenVideoStream(settings, bytes => { udpClient.Send(bytes, bytes.Length, endpoint); }); while (!cancellationToken.IsCancellationRequested) { await Task.Delay(1000, cancellationToken); } Console.WriteLine("Closing video stream"); controller.CloseVideoStream(); } } }
private static void CaptureVideo() { Console.Clear(); Console.WriteLine("Set the video width:"); var videoWidth = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat); Console.WriteLine("Set the video height:"); var videoHeight = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat); Console.WriteLine("Set the file name:"); var videoPath = Console.ReadLine(); Console.Clear(); var videoByteCount = 0; var videoEventCount = 0; var videoSettings = new CameraVideoSettings { CaptureDisplayPreview = false, CaptureExposure = CameraExposureMode.Night, CaptureHeight = videoHeight, CaptureTimeoutMilliseconds = 0, CaptureWidth = videoWidth, ImageFlipVertically = true, VideoFileName = $"{DefaultPicturePath}/{videoPath}.h264", }; "Press any key to START recording . . .".Info(); Console.ReadLine(); Console.Clear(); var startTime = DateTime.UtcNow; Pi.Camera.OpenVideoStream( videoSettings, onDataCallback: data => { videoByteCount += data.Length; videoEventCount++; }, onExitCallback: null); Console.ForegroundColor = ConsoleColor.Red; Console.Write($" {(char)0x25CF} "); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("Recording\n"); Console.WriteLine("Press any key to STOP recording. . ."); Console.ReadLine(); Console.Clear(); Pi.Camera.CloseVideoStream(); var megaBytesReceived = (videoByteCount / (1024f * 1024f)).ToString("0.000", CultureInfo.InvariantCulture.NumberFormat); var recordedSeconds = DateTime.UtcNow.Subtract(startTime).TotalSeconds.ToString("0.000", CultureInfo.InvariantCulture.NumberFormat); "Recording stopped. . .\n\n".Info(); $"Recorded {megaBytesReceived}MB\n{videoEventCount} callbacks\nRecorded {recordedSeconds} seconds\nCreated {DateTime.Now}\nAt {videoSettings.VideoFileName}\n\n".Info(); }
private static void TestCaptureVideo() { // Setup our working variables var videoByteCount = 0; var videoEventCount = 0; var startTime = DateTime.UtcNow; // Configure video settings var videoSettings = new CameraVideoSettings() { CaptureTimeoutMilliseconds = 0, CaptureDisplayPreview = false, ImageFlipVertically = true, CaptureExposure = CameraExposureMode.Night, CaptureWidth = 1920, CaptureHeight = 1080 }; try { "Press any key to START reading the video stream . . .".Info(); Console.ReadLine(); // Start the video recording Pi.Camera.OpenVideoStream(videoSettings, onDataCallback: (data) => { videoByteCount += data.Length; videoEventCount++; }, onExitCallback: null); // Wait for user interaction startTime = DateTime.UtcNow; "Press any key to STOP reading the video stream . . .".Info(); Console.ReadLine(); } catch (Exception ex) { $"{ex.GetType()}: {ex.Message}".Error(); } finally { // Always close the video stream to ensure raspivid quits Pi.Camera.CloseVideoStream(); // Output the stats var megaBytesReceived = (videoByteCount / (1024f * 1024f)).ToString("0.000"); var recordedSeconds = DateTime.UtcNow.Subtract(startTime).TotalSeconds.ToString("0.000"); $"Capture Stopped. Received {megaBytesReceived} Mbytes in {videoEventCount} callbacks in {recordedSeconds} seconds" .Info(); } }
public PiCamera(CameraVideoSettings cameraSettings, int chunkDuration) { CameraSettings = cameraSettings; ChunkDuration = chunkDuration; }
public static void StartSocketAndSendVideo() { var videoSettings = new CameraVideoSettings { CaptureTimeoutMilliseconds = 0, CaptureDisplayPreview = false, ImageFlipVertically = true, CaptureExposure = CameraExposureMode.Night, CaptureWidth = 1280, CaptureHeight = 720 }; try { if (Pi.Camera.IsBusy) { Pi.Camera.CloseVideoStream(); } bool isExited = false; byte[] bytes = null; Pi.Camera.OpenVideoStream( videoSettings, data => { bytes = data; }, () => { isExited = true; }); //var serverSocket = // new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080)); //serverSocket.Listen(4); //while (true) //{ // var handler = serverSocket.Accept(); // Console.WriteLine("Received connection"); // var receivedBytes = new byte[1024]; // handler.Receive(receivedBytes); // if (bytes != null) // { // handler.Send(bytes); // } //} var listener = new TcpListener(IPAddress.Any, 8080); listener.Start(); Console.WriteLine("Server started..."); ////Console.ReadLine(); TcpClient client; while (true) { client = listener.AcceptTcpClient(); Console.WriteLine("Client Connected..."); var stream = client.GetStream(); while (true) { if (client.Connected) { Console.WriteLine("Writing..."); stream.WriteAsync(bytes); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); } Console.WriteLine(ex.StackTrace); Pi.Camera.CloseVideoStream(); } }
static void Main(string[] args) { try { // Subscribe to the daemon service cancel/end events AssemblyLoadContext.Default.Unloading += SigTermEventHandler; Console.CancelKeyPress += CancelHandler; SetEnvVars(); // Configure video settings var cameraSettings = new CameraVideoSettings() { CaptureTimeoutMilliseconds = 0, //CaptureQuantisation = 10, CaptureDisplayPreview = false, ImageFlipVertically = false, //CaptureFramerate = 25, //CaptureKeyframeRate = 1, CaptureExposure = CameraExposureMode.Night, CaptureWidth = 1280, CaptureHeight = 720, //CaptureProfile = CameraH264Profile.High, CaptureDisplayPreviewEncoded = false, ImageAnnotationsText = "Time %X", ImageAnnotations = CameraAnnotation.Time | CameraAnnotation.FrameNumber }; // Configure trap settings RaspberryPiCameraTrapSettings trapSettings = new RaspberryPiCameraTrapSettings() { ComputerVisionApiKey = Environment.GetEnvironmentVariable("computerVisionApiKey"), HueBridgeIp = Environment.GetEnvironmentVariable("hueBridgeIp"), HueKey = Environment.GetEnvironmentVariable("hueKey"), IotHubDeviceId = Environment.GetEnvironmentVariable("iotHubDeviceId"), IotHubUri = Environment.GetEnvironmentVariable("iotHubUri"), IotHubDeviceKey = Environment.GetEnvironmentVariable("iotHubDeviceKey"), SensorPin = Pi.Gpio.Pin07, SpotlightPin = Pi.Gpio.Pin00, TempProcessingBaseDirectory = "/home/pi/images", TempProcessingImageFile = "frame", TempProcessingImageFileExt = "jpg", TempProcessingVideoFile = "input", TempProcessingVideoFileExt = "h264", VideoChunkDuration = -8, CameraSettings = cameraSettings }; // Do the needful cameraTrap = new RaspberryPiCameraTrap("frontdoor", trapSettings); while (true) { Thread.Sleep(3000); } } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}: {ex.InnerException?.Message}"); Console.WriteLine(ex.StackTrace); } finally { Console.WriteLine("Finally block"); cameraTrap?.Dispose(); } }