66
77 "github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4"
88 "github.com/bluenviron/mediacommon/v2/pkg/formats/fmp4/seekablebuffer"
9+ "github.com/bluenviron/mediamtx/internal/recordstore"
910)
1011
1112const (
@@ -73,7 +74,7 @@ func (w *muxerFMP4) writeSample(
7374 if w .curTrack .firstDTS < 0 {
7475 w .curTrack .firstDTS = dts
7576
76- // if frame is a IDR, remove previous GOP
77+ // if sample is a IDR, remove previous GOP
7778 if ! isNonSyncSample {
7879 w .curTrack .samples = nil
7980 }
@@ -101,14 +102,14 @@ func (w *muxerFMP4) writeSample(
101102 }
102103 }
103104 } else {
104- // store GOP of the first frame, and set PTSOffset = 0 and Duration = 0 in each sample
105- if ! isNonSyncSample { // if frame is a IDR, reset GOP
105+ if ! isNonSyncSample { // sample is IDR
106+ // reset GOP
106107 w .curTrack .samples = []* fmp4.PartSample {{
107108 IsNonSyncSample : isNonSyncSample ,
108109 Payload : pl ,
109110 }}
110- } else {
111- // append frame to current GOP
111+ } else { // sample is not IDR
112+ // append sample to current GOP, with PTSOffset = 0 and Duration = 0
112113 w .curTrack .samples = append (w .curTrack .samples , & fmp4.PartSample {
113114 IsNonSyncSample : isNonSyncSample ,
114115 Payload : pl ,
@@ -156,26 +157,20 @@ func (w *muxerFMP4) innerFlush(final bool) error {
156157 }
157158 }
158159
159- if part .Tracks != nil {
160- part .SequenceNumber = w .nextSequenceNumber
161- w .nextSequenceNumber ++
162-
160+ // no samples to write
161+ if part .Tracks == nil {
162+ // if no samples has been written before, return an error
163163 if w .init != nil {
164- err := w .init .Marshal (& w .outBuf )
165- if err != nil {
166- return err
167- }
168-
169- _ , err = w .w .Write (w .outBuf .Bytes ())
170- if err != nil {
171- return err
172- }
173-
174- w .init = nil
175- w .outBuf .Reset ()
164+ return recordstore .ErrNoSegmentsFound
176165 }
166+ return nil
167+ }
177168
178- err := part .Marshal (& w .outBuf )
169+ part .SequenceNumber = w .nextSequenceNumber
170+ w .nextSequenceNumber ++
171+
172+ if w .init != nil {
173+ err := w .init .Marshal (& w .outBuf )
179174 if err != nil {
180175 return err
181176 }
@@ -185,9 +180,22 @@ func (w *muxerFMP4) innerFlush(final bool) error {
185180 return err
186181 }
187182
183+ w .init = nil
188184 w .outBuf .Reset ()
189185 }
190186
187+ err := part .Marshal (& w .outBuf )
188+ if err != nil {
189+ return err
190+ }
191+
192+ _ , err = w .w .Write (w .outBuf .Bytes ())
193+ if err != nil {
194+ return err
195+ }
196+
197+ w .outBuf .Reset ()
198+
191199 return nil
192200}
193201
0 commit comments