From 31d99b8b1f6d8633b8abba2f9c6db852ed5b0dbf Mon Sep 17 00:00:00 2001 From: John Varghese <126874846+John-Varghese-EH@users.noreply.github.com> Date: Tue, 3 Jun 2025 18:47:22 +0530 Subject: [PATCH] onvif_server.cpp --- ESP32CAM-ONVIF/onvif_server.cpp | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 ESP32CAM-ONVIF/onvif_server.cpp diff --git a/ESP32CAM-ONVIF/onvif_server.cpp b/ESP32CAM-ONVIF/onvif_server.cpp new file mode 100644 index 0000000..7f4e7cd --- /dev/null +++ b/ESP32CAM-ONVIF/onvif_server.cpp @@ -0,0 +1,102 @@ +#include "onvif_server.h" +#include "rtsp_server.h" +#include +#include +#include "utils.h" + +WebServer onvifServer(8000); +WiFiUDP onvifUDP; + +String getCapabilitiesResponse() { + String ip = WiFi.localIP().toString(); + return + "" + "" + "" + "" + "" + "" + "http://" + ip + ":8000/onvif/device_service" + "" + "" + "" + "" + ""; +} + +String getDeviceInfoResponse() { + return + "" + "" + "" + "" + "ESP32-CAM-J0X" + "ONVIF-ESPCAM" + "1.0" + "J0X-00001" + "ESP32CAM-J0X" + "" + "" + ""; +} + +void handle_onvif_soap() { + String req = onvifServer.arg(0); + if (req.indexOf("GetCapabilities") > 0) { + onvifServer.send(200, "application/soap+xml", getCapabilitiesResponse()); + } else if (req.indexOf("GetStreamUri") > 0) { + onvifServer.send(200, "application/soap+xml", getStreamUriResponse()); + } else if (req.indexOf("GetDeviceInformation") > 0) { + onvifServer.send(200, "application/soap+xml", getDeviceInfoResponse()); + } else { + onvifServer.send(200, "application/soap+xml", ""); + } +} + +void handle_onvif_discovery() { + int packetSize = onvifUDP.parsePacket(); + if (packetSize) { + char packet[1024]; + int len = onvifUDP.read(packet, 1024); + packet[len] = 0; + String pktStr = String(packet); + if (pktStr.indexOf("Probe") > 0) { + String ip = WiFi.localIP().toString(); + String resp = + "" + "" + "" + "" + "" + "
urn:uuid:esp32-cam-onvif
" + "dn:NetworkVideoTransmitter" + "http://" + ip + ":8000/onvif/device_service" + "onvif://www.onvif.org/Profile/Streaming" + "1" + "
" + "
" + "
" + "
"; + onvifUDP.beginPacket(onvifUDP.remoteIP(), onvifUDP.remotePort()); + onvifUDP.write((const uint8_t*)resp.c_str(), resp.length()); + onvifUDP.endPacket(); + } + } +} + +void onvif_server_start() { + onvifServer.on("/onvif/device_service", HTTP_POST, handle_onvif_soap); + onvifServer.begin(); + onvifUDP.beginMulticast(WiFi.localIP(), IPAddress(239,255,255,250), 3702); + Serial.println("[INFO] ONVIF server started."); +} + +void onvif_server_loop() { + onvifServer.handleClient(); + handle_onvif_discovery(); +}