|
| 1 | +package goav |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/thesyncim/goav/av" |
| 8 | + "github.com/thesyncim/goav/errcode" |
| 9 | + "github.com/thesyncim/goav/pipeline" |
| 10 | +) |
| 11 | + |
| 12 | +// This file holds what the two timed-gate families (sync, playout) share: |
| 13 | +// stage naming, the refusal for media without a usable PTS timebase, and the |
| 14 | +// pre-mutation validation that a stream carries timebase facts. The families |
| 15 | +// keep their own literal fix lists so refusals stay exact. |
| 16 | + |
| 17 | +// gateMessageTime extracts the stream and PTS a timed gate schedules on; ok |
| 18 | +// is false for media without a valid PTS timebase (and for events). |
| 19 | +func gateMessageTime(msg *pipeline.Message) (av.StreamID, time.Duration, bool) { |
| 20 | + switch msg.Kind { |
| 21 | + case pipeline.MessagePacket: |
| 22 | + if msg.Packet == nil || !msg.Packet.PTS.Base.Valid() { |
| 23 | + return "", 0, false |
| 24 | + } |
| 25 | + pts, ok := msg.Packet.PTS.ToDuration() |
| 26 | + return msg.Packet.StreamID, pts, ok |
| 27 | + case pipeline.MessageFrame: |
| 28 | + if msg.Frame == nil || !msg.Frame.PTS.Base.Valid() { |
| 29 | + return "", 0, false |
| 30 | + } |
| 31 | + pts, ok := msg.Frame.PTS.ToDuration() |
| 32 | + return msg.Frame.StreamID, pts, ok |
| 33 | + default: |
| 34 | + return "", 0, false |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// gateStageName builds the diagnostic node name for a timed gate: |
| 39 | +// prefix-<sanitized policy name>, with the prefix doubling as the default name. |
| 40 | +func gateStageName(prefix string, name string) string { |
| 41 | + name = strings.TrimSpace(name) |
| 42 | + if name == "" { |
| 43 | + name = prefix |
| 44 | + } |
| 45 | + replacer := strings.NewReplacer(" ", "-", "/", "-", "\\", "-", "\t", "-", "\n", "-") |
| 46 | + return prefix + "-" + replacer.Replace(name) |
| 47 | +} |
| 48 | + |
| 49 | +// gateTimebaseError refuses one media message that reached a timed gate |
| 50 | +// without a valid PTS timebase; the caller supplies its gate-specific fixes. |
| 51 | +func gateTimebaseError(operation string, node string, msg *pipeline.Message, fixes []string) error { |
| 52 | + kind := "" |
| 53 | + switch { |
| 54 | + case msg == nil: |
| 55 | + case msg.Packet != nil: |
| 56 | + kind = "packet" |
| 57 | + case msg.Frame != nil: |
| 58 | + kind = "frame" |
| 59 | + } |
| 60 | + return &BuildError{ |
| 61 | + Phase: phaseBuild, |
| 62 | + Family: errcode.FamilyForCode(runtimeBranchInvalidCode), |
| 63 | + Code: runtimeBranchInvalidCode, |
| 64 | + Operation: operation, |
| 65 | + Node: node, |
| 66 | + Reason: "media message has no valid PTS timebase", |
| 67 | + fields: errDetails(errDetail("message", firstNonEmpty(kind, "unknown"))), |
| 68 | + fixes: buildErrorFixes(fixes), |
| 69 | + cause: errUnsupportedBuild, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// validateGatePolicyForBranch refuses, before graph mutation, a branch whose |
| 74 | +// stream declares no timebase facts a timed gate could schedule on; a valid |
| 75 | +// TimeBase or a Codec.ClockRate (RTP-style) satisfies it. |
| 76 | +func validateGatePolicyForBranch(operation string, branchName string, stream av.Stream, reason string, fixes []string) error { |
| 77 | + if stream.TimeBase.Valid() { |
| 78 | + return nil |
| 79 | + } |
| 80 | + if stream.Codec.ClockRate != 0 { |
| 81 | + return nil |
| 82 | + } |
| 83 | + return &BuildError{ |
| 84 | + Phase: phaseBuild, |
| 85 | + Family: errcode.FamilyForCode(runtimeBranchInvalidCode), |
| 86 | + Code: runtimeBranchInvalidCode, |
| 87 | + Operation: operation, |
| 88 | + Node: firstNonEmpty(branchName, string(stream.ID), "branch"), |
| 89 | + Reason: reason, |
| 90 | + fixes: buildErrorFixes(fixes), |
| 91 | + cause: errUnsupportedBuild, |
| 92 | + } |
| 93 | +} |
0 commit comments