Repository navigation
feat(onvif): add Events service with PullPoint motion events - #2722
machadolucas wants to merge 2 commits into
Conversation
|
conflict with #2130 |
|
What I don't like:
I haven't compared this implementation to #2130; there might be some good ideas there. |
Protocol-neutral motion state per stream with change handlers, so any module can report motion to its clients. The state is set from any external detector with POST/DELETE /api/motion?src=<stream>, and GET /api/motion returns the states of all streams with a known state.
ONVIF server now has an Events service (PullPoint subscriptions) with the motion topics tns1:RuleEngine/CellMotionDetector/Motion and tns1:VideoSource/MotionAlarm, so NVRs like UniFi Protect can receive motion from go2rtc cameras. Events come from the motion state of streams. The service is always advertised in GetCapabilities and GetServices.
d6b6545 to
f514fac
Compare
|
Reworked and rebased on master:
#2130: the server code is in Caveat: an NVR will now assume every go2rtc camera can report motion. Tested: unit tests with |
|
The approval of this PR will be delayed until the event architecture for the entire project has been reviewed. |
|
I took a quick look at the implementation. It looks pretty clean. How do you see this API being used in real life? What software will manage this flag, and how? |
|
In my setup nothing external manages it. The camera is HomeKit-only (Eve Outdoor Cam). go2rtc keeps a HAP session to it, subscribes to its MotionDetected characteristic and calls So the main users are inside go2rtc: sources (a HomeKit client, or the ONVIF events client and the detector from #2130) set the state, and servers (ONVIF Events, a HomeKit motion sensor) read it. The HTTP endpoint is for cameras with no such source: Home Assistant or a script calling it from a PIR sensor, Frigate, or a camera's own HTTP alarm push. I haven't used it that way myself. |
The ONVIF server has no Events service, so NVRs that read motion over ONVIF get nothing from a go2rtc camera. This adds a protocol-neutral motion state for streams, settable from any external detector (go2rtc doesn't detect motion itself), and a PullPoint Events service in the ONVIF server that reports it.
UniFi Protect reads ONVIF motion from third-party cameras since 7.1. According to the docs and tests of danielwoz/ubiquiti-protect-onvif-event-listener (I haven't seen Protect's code), it calls
GetEventProperties(needs PullPoint support and a motion topic in the TopicSet), thenCreatePullPointSubscription,PullMessagesin a loop (TimeoutPT2S,MessageLimit10),Renewabout every 60 s andUnsubscribeon stop. It sends no SOAPAction and no WS-Addressing on Pull/Renew/Unsubscribe, reads both motion topics below, and needs thefalsemessage to end an event.Related: #1520 (Protect support for the ONVIF server), #2639 (missing
get_event_properties/ motion topic), #2130 (HomeKit Secure Video, see below).1. Motion state of streams (
internal/streams/motion.go)streams.SetMotion(name, motion),streams.GetMotion(name) (motion, ok bool),streams.GetMotions()andstreams.HandleMotionFunc(handler)for change handlers (named likeHandleFunc/HandleConsumerFunc). Handlers are called on the first state of a stream and on every change, under the motion lock, so their order is the order of the changes.api/streamsandapi/preload:Unknown stream: 404, other methods: 405. A stream has no state until the first call. States are kept in memory only. Added to
website/api/openapi.yamland the streams README.2. ONVIF Events service (
pkg/onvif/server_events.go,internal/onvif)Eventsstore (subscriptions with their own message queue, long-pollPull,Subscribe/Renew/Unsubscribe/Sync) and the SOAP responses forGetEventProperties,CreatePullPointSubscription,PullMessages,Renew,UnsubscribeandSetSynchronizationPoint.internal/onvifregistersevents.SetMotionwithstreams.HandleMotionFunc; it has no motion API of its own.tns1:RuleEngine/CellMotionDetector/Motion(IsMotion) andtns1:VideoSource/MotionAlarm(State). The source token is the stream name, XML escaped.Initializedmessage per stream with a known motion state; streams never reported send nothing until their first change, which comes asChanged.SetSynchronizationPointqueues the current states again.GetServiceCapabilitiesis aStaticResponseentry under its own keyEventsGetServiceCapabilities, chosen when the request path is/onvif/events_service(the operation name is the same as in the Media service). The media answer is unchanged.GetCapabilitiesandGetServicesalways advertise the Events service.GetServiceCapabilitiesstays open like the media one.PullMessageskeeps its subscription alive, returns early when the subscription is removed, and ends with the request context when the client disconnects.xs:duration(PT60S,PT1M,P1D) and absolutexs:dateTimeare accepted forInitialTerminationTime,TerminationTimeandTimeout./onvif/subscription/<id>. Unknown or expired subscription: SOAPResourceUnknownFaultwith HTTP 400.Trade-off: because Events is always advertised, an NVR will assume every go2rtc camera can report motion, even when nothing ever sets its state.
Relation to #2130
#2130 adds an ONVIF events client in
pkg/onvif/events.go; the server side here is inserver_events.go(next toclient.go/server.go) and no exported identifiers clash. The neutral motion state could serve #2130 too:api/homekit/motioncould become astreams.HandleMotionFunchandler that setsMotionDetectedon the HomeKit server of the stream, and its ONVIF watcher could callstreams.SetMotion.Testing
go test -race ./pkg/onvif/ ./internal/streams/: well-formed XML of all responses (also with"<&>in the stream name and host), events always advertised inGetCapabilities/GetServices, static events capabilities next to the unchanged media one,Initializedonly for streams with a known state, long poll woken by a change, timeout, sync point, renew, unsubscribe, expiry, subscription limit, unsubscribe and client disconnect during a pending pull, concurrentSetMotion/Pull/Sync/Renew/Unsubscribe; motion state changes and handlers, concurrent use, and theapi/motionhandler.Changedon the first state), 401 for an unauthenticated pull, subscription withInitialized, idle/motion on/motion off pulls via/api/motion, renew, sync point, unsubscribe, fault after unsubscribe.Not tested: Home Assistant ONVIF integration, Scrypted, Frigate, ONVIF Device Manager, or other NVRs. WS-BaseNotification (push
Subscribe) and pausable subscriptions are not implemented and are reported as unsupported in the capabilities.