|
| 1 | +// Copyright 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package media |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/go-gst/go-gst/gst" |
| 23 | + "github.com/livekit/ingress/pkg/lksdk_output" |
| 24 | + "github.com/livekit/ingress/pkg/utils" |
| 25 | + "github.com/livekit/protocol/livekit" |
| 26 | + "github.com/livekit/protocol/logger" |
| 27 | +) |
| 28 | + |
| 29 | +type Splice struct { |
| 30 | + Immediate bool |
| 31 | + OutOfNetworkIndicator bool |
| 32 | + EventId uint32 |
| 33 | + EventCancelIndicator bool |
| 34 | + RunningTime time.Duration |
| 35 | +} |
| 36 | + |
| 37 | +type SpliceProcessor struct { |
| 38 | + outputSync *utils.OutputSynchronizer |
| 39 | + sdkOut *lksdk_output.LKSDKOutput |
| 40 | + |
| 41 | + ctx context.Context |
| 42 | + cancel context.CancelFunc |
| 43 | +} |
| 44 | + |
| 45 | +func NewSpliceProcessor(sdkOut *lksdk_output.LKSDKOutput, outputSync *utils.OutputSynchronizer) *SpliceProcessor { |
| 46 | + su := &SpliceProcessor{ |
| 47 | + sdkOut: sdkOut, |
| 48 | + outputSync: outputSync, |
| 49 | + } |
| 50 | + |
| 51 | + su.ctx, su.cancel = context.WithCancel(context.Background()) |
| 52 | + |
| 53 | + return su |
| 54 | +} |
| 55 | + |
| 56 | +func (su *SpliceProcessor) Close() { |
| 57 | + su.cancel() |
| 58 | +} |
| 59 | + |
| 60 | +func (su *SpliceProcessor) ProcessSpliceEvent(ev *gst.Event) error { |
| 61 | + ts := ev.ParseMpegtsSection() |
| 62 | + if ts == nil { |
| 63 | + return nil |
| 64 | + } |
| 65 | + if ts.SectionType() != gst.MpegtsSectionSCTESIT { |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + scteSit := ts.GetSCTESIT() |
| 70 | + if scteSit == nil { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + if scteSit.SpliceCommandType() != gst.MpegtsSCTESpliceCommandInsert { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + str, _ := ev.GetStructure().GetValue("running-time-map") |
| 79 | + rMap, _ := str.(*gst.Structure) |
| 80 | + |
| 81 | + splices := scteSit.Splices() |
| 82 | + for _, sp := range splices { |
| 83 | + var rTime time.Duration |
| 84 | + |
| 85 | + if !sp.SpliceImmediateFlag() { |
| 86 | + if rMap != nil { |
| 87 | + val, _ := rMap.GetValue(fmt.Sprintf("event-%d-splice-time", sp.SpliceEventId())) |
| 88 | + if val != nil { |
| 89 | + rTime = time.Duration(val.(uint64)) |
| 90 | + } |
| 91 | + } |
| 92 | + if rTime == 0 && scteSit.SpliceTimeSpecified() { |
| 93 | + val, _ := rMap.GetValue("splice-time") |
| 94 | + if val != nil { |
| 95 | + rTime = time.Duration(val.(uint64)) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + sp := &Splice{ |
| 101 | + Immediate: sp.SpliceImmediateFlag(), |
| 102 | + OutOfNetworkIndicator: sp.OutOfNetworkIndicator(), |
| 103 | + EventId: sp.SpliceEventId(), |
| 104 | + EventCancelIndicator: sp.SpliceEventCancelIndicator(), |
| 105 | + RunningTime: time.Duration(rTime), |
| 106 | + } |
| 107 | + |
| 108 | + err := su.processSplice(sp) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func (su *SpliceProcessor) processSplice(sp *Splice) error { |
| 118 | + var attr string |
| 119 | + |
| 120 | + if sp.OutOfNetworkIndicator { |
| 121 | + attr = fmt.Sprintf("%d", sp.EventId) |
| 122 | + } |
| 123 | + |
| 124 | + logger.Debugw("spice event", "eventID", sp.EventId, "outOfNetworkIndicator", sp.OutOfNetworkIndicator, "immediate", sp.Immediate, "runningTime", sp.RunningTime) |
| 125 | + |
| 126 | + event := func() { |
| 127 | + su.sdkOut.UpdateLocalParticipantAttributes(map[string]string{livekit.AttrIngressOutOfNetworkEventID: attr}) |
| 128 | + } |
| 129 | + |
| 130 | + if sp.Immediate { |
| 131 | + event() |
| 132 | + } else { |
| 133 | + err := su.outputSync.ScheduleEvent(su.ctx, sp.RunningTime, event) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments