|
| 1 | +package rtsp |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/url" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/bluenviron/gortsplib/v5/pkg/base" |
| 10 | + "github.com/bluenviron/gortsplib/v5/pkg/liberrors" |
| 11 | +) |
| 12 | + |
| 13 | +// SourceHealth is a point-in-time view of a Source's connection attempts. |
| 14 | +// |
| 15 | +// Frame arrival alone cannot tell an on-demand camera resting between events |
| 16 | +// from one that is awake and rejecting us: both deliver nothing. The difference |
| 17 | +// is in how the connection attempt fails, which only the Source sees, so it has |
| 18 | +// to be recorded here and carried out to the API and the logs. |
| 19 | +type SourceHealth struct { |
| 20 | + Connected bool |
| 21 | + // LastConnected is when the source last reached PLAY. Zero means it has not |
| 22 | + // connected since this Source was created. A camera whose stream is simply |
| 23 | + // never published (unplugged, removed from its bridge, wrong path) looks |
| 24 | + // exactly like a sleeping one on every other signal; the age of this |
| 25 | + // timestamp is what separates them. |
| 26 | + LastConnected time.Time |
| 27 | + LastAttempt time.Time |
| 28 | + // LastError is the failure from the most recent attempt that did not reach |
| 29 | + // PLAY, with any credentials from the URL redacted. Empty after a success. |
| 30 | + LastError string |
| 31 | + // Unpublished records that LastError was the server answering "no stream at |
| 32 | + // this path" rather than refusing, rejecting, or ignoring us. |
| 33 | + Unpublished bool |
| 34 | + // ConsecutiveFailures counts attempts since the last successful connection. |
| 35 | + // An attempt that reached PLAY and then dropped resets it: that is a |
| 36 | + // disconnection, not a failure to reach the camera. |
| 37 | + ConsecutiveFailures int64 |
| 38 | +} |
| 39 | + |
| 40 | +// onDemandFaultThreshold is how many consecutive non-routine failures a source |
| 41 | +// must accumulate before Faulted reports it as broken. At onDemandRetry that is |
| 42 | +// roughly ten seconds: long enough to ride out a bridge reboot without flapping |
| 43 | +// a camera between SLEEPING and OFFLINE, short enough that a wrong password |
| 44 | +// surfaces while the operator is still looking at the screen. |
| 45 | +const onDemandFaultThreshold = 3 |
| 46 | + |
| 47 | +// Faulted reports whether the source is failing for a reason other than its |
| 48 | +// stream not being published. |
| 49 | +// |
| 50 | +// This is the distinction the rest of the system needs. A battery camera behind |
| 51 | +// a bridge answers 404 between events and that is its resting state; the same |
| 52 | +// camera answering 401, refusing the connection, or timing out is broken. Both |
| 53 | +// deliver zero frames, so without this every misconfiguration on an on-demand |
| 54 | +// camera reads as a healthy nap and nothing ever reports it. |
| 55 | +func (h SourceHealth) Faulted() bool { |
| 56 | + return !h.Connected && |
| 57 | + !h.Unpublished && |
| 58 | + h.LastError != "" && |
| 59 | + h.ConsecutiveFailures >= onDemandFaultThreshold |
| 60 | +} |
| 61 | + |
| 62 | +// streamUnpublished reports whether err is the server saying it has no stream |
| 63 | +// at this path. Anything else (a refused connection, rejected credentials, a |
| 64 | +// timeout) means the far end is reachable-but-wrong or not reachable at all, |
| 65 | +// which is a fault however the camera is powered. |
| 66 | +func streamUnpublished(err error) bool { |
| 67 | + var bad liberrors.ErrClientBadStatusCode |
| 68 | + if !errors.As(err, &bad) { |
| 69 | + return false |
| 70 | + } |
| 71 | + return bad.Code == base.StatusNotFound |
| 72 | +} |
| 73 | + |
| 74 | +// redactCredentials removes RTSP userinfo from a message destined for a log |
| 75 | +// line or the HTTP API. Error strings from the client library quote the URL |
| 76 | +// they were given, which carries the camera password. |
| 77 | +// |
| 78 | +// Only whole credential-bearing substrings are replaced. Substituting the |
| 79 | +// username or password wherever they appear would mangle unrelated text when |
| 80 | +// either is a short or common word. |
| 81 | +func redactCredentials(msg, rawURL string) string { |
| 82 | + if msg == "" || rawURL == "" { |
| 83 | + return msg |
| 84 | + } |
| 85 | + msg = strings.ReplaceAll(msg, rawURL, SanitizeURL(rawURL)) |
| 86 | + u, err := url.Parse(rawURL) |
| 87 | + if err != nil || u.User == nil { |
| 88 | + return msg |
| 89 | + } |
| 90 | + msg = strings.ReplaceAll(msg, u.User.String()+"@", "***:***@") |
| 91 | + // url.UserPassword percent-encodes on String(); the URL may have been |
| 92 | + // configured with the literal form, which is what the library echoes back. |
| 93 | + if pw, ok := u.User.Password(); ok { |
| 94 | + msg = strings.ReplaceAll(msg, u.User.Username()+":"+pw+"@", "***:***@") |
| 95 | + } |
| 96 | + return msg |
| 97 | +} |
0 commit comments