mirror of
				https://github.com/owncast/owncast.git
				synced 2025-11-04 13:27:21 +08:00 
			
		
		
		
	Simplify appending the offline segment to playlist
This commit is contained in:
		@ -1,7 +1,6 @@
 | 
				
			|||||||
package core
 | 
					package core
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bufio"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
@ -14,23 +13,12 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func appendOfflineToVariantPlaylist(index int, playlistFilePath string) {
 | 
					func appendOfflineToVariantPlaylist(index int, playlistFilePath string) {
 | 
				
			||||||
	f, err := os.OpenFile(playlistFilePath, os.O_CREATE|os.O_RDWR, os.ModePerm) //nolint
 | 
						existingPlaylistContents, err := os.ReadFile(playlistFilePath) // nolint: gosec
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalln(err)
 | 
							log.Debugln("unable to read existing playlist file", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	playlist, _, err := m3u8.DecodeFrom(bufio.NewReader(f), true)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Fatalln(err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err := f.Close(); err != nil {
 | 
					 | 
				
			||||||
		log.Errorln("error closing playlist file", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	variantPlaylist := playlist.(*m3u8.MediaPlaylist)
 | 
					 | 
				
			||||||
	variantPlaylist.MediaType = m3u8.EVENT
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	tmpFileName := fmt.Sprintf("tmp-stream-%d.m3u8", index)
 | 
						tmpFileName := fmt.Sprintf("tmp-stream-%d.m3u8", index)
 | 
				
			||||||
	atomicWriteTmpPlaylistFile, err := os.CreateTemp(os.TempDir(), tmpFileName)
 | 
						atomicWriteTmpPlaylistFile, err := os.CreateTemp(os.TempDir(), tmpFileName)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@ -38,11 +26,13 @@ func appendOfflineToVariantPlaylist(index int, playlistFilePath string) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if _, err := atomicWriteTmpPlaylistFile.Write(variantPlaylist.Encode().Bytes()); err != nil {
 | 
						// Write the existing playlist contents
 | 
				
			||||||
		log.Errorln(err)
 | 
						if _, err := atomicWriteTmpPlaylistFile.Write(existingPlaylistContents); err != nil {
 | 
				
			||||||
 | 
							log.Debugln("error writing existing playlist contents to tmp playlist file", err)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Manually add the offline clip to the end of the media playlist.
 | 
						// Manually append the offline clip to the end of the media playlist.
 | 
				
			||||||
	_, _ = atomicWriteTmpPlaylistFile.WriteString("#EXT-X-DISCONTINUITY\n")
 | 
						_, _ = atomicWriteTmpPlaylistFile.WriteString("#EXT-X-DISCONTINUITY\n")
 | 
				
			||||||
	// If "offline" content gets changed then change the duration below
 | 
						// If "offline" content gets changed then change the duration below
 | 
				
			||||||
	_, _ = atomicWriteTmpPlaylistFile.WriteString("#EXTINF:8.000000,\n")
 | 
						_, _ = atomicWriteTmpPlaylistFile.WriteString("#EXTINF:8.000000,\n")
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user