Skip to content

Commit

Permalink
Allow rtp/rtsp urls
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelGoerentz committed Nov 26, 2024
1 parent e1b06f1 commit 2ca6c61
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 107 deletions.
4 changes: 3 additions & 1 deletion src/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func GetBufferConfig() (bufferType, path, options string) {
switch bufferType {
case "FFMPEG":
return bufferType, Settings.FFmpegPath, Settings.FFmpegOptions
case "VLC":
return bufferType, Settings.VLCPath, Settings.VLCOptions
case "THREADFIN":
return bufferType, "", ""
default:
Expand Down Expand Up @@ -60,7 +62,7 @@ func StartBuffer(stream *Stream, useBackup bool, backupNumber int, errorChan cha
showInfo("Streaming URL:" + stream.URL)

switch Settings.Buffer {
case "ffmpeg":
case "ffmpeg", "vlc":
if buffer, err := RunBufferCommand(bufferType, path, options, stream, errorChan); err != nil {
return HandleBufferError(err, backupNumber, useBackup, stream, errorChan)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func Init() (err error) {
System.Compatibility = "0.1.0"

// FFmpeg Default Einstellungen
System.FFmpeg.DefaultOptions = "-hide_banner -loglevel error -i [URL] -c copy -f mpegts pipe:1"
System.VLC.DefaultOptions = "-I dummy [URL] --sout #std{mux=ts,access=file,dst=-}"
System.FFmpeg.DefaultOptions = "-hide_banner -loglevel error -i [URL] -hls_time 6 -hls_list_size 3 -c copy -f hls pipe:1"
System.VLC.DefaultOptions = "-I dummy [URL] --sout \"#std{mux=ts,access=file,dst=-}\" --no-sout-all"

// Default Logeinträge, wird später von denen aus der settings.json überschrieben. Muss gemacht werden, damit die ersten Einträge auch im Log (webUI aangezeigt werden)
Settings.LogEntriesRAM = 500
Expand Down
4 changes: 1 addition & 3 deletions src/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func updateServerSettings(request RequestStruct) (settings SettingsStruct, err e
var serverProtocolChanged = false
var debug string

// -vvv [URL] --sout '#transcode{vcodec=mp4v, acodec=mpga} :standard{access=http, mux=ogg}'

for key, value := range newSettings {

if _, ok := oldSettings[key]; ok {
Expand Down Expand Up @@ -103,7 +101,7 @@ func updateServerSettings(request RequestStruct) (settings SettingsStruct, err e
return
}

case "ffmpeg.path", "vlc.path":
case "ffmpeg.path":
var path = value.(string)
if len(path) > 0 {

Expand Down
4 changes: 1 addition & 3 deletions src/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func getErrMsg(errCode int) (errMsg string) {
case 1203:
errMsg = "Steaming URL could not be found in any playlist"
case 1204:
errMsg = "Streaming was stopped by third party transcoder (FFmpeg / VLC)"
errMsg = "Streaming was stopped by third party transcoder (FFmpeg)"

// Warnings
case 2000:
Expand All @@ -318,8 +318,6 @@ func getErrMsg(errCode int) (errMsg string) {
errMsg = "No valid streaming URL"
case 2020:
errMsg = "FFmpeg binary was not found. Check the FFmpeg binary path in the Threadfin settings."
case 2021:
errMsg = "VLC binary was not found. Check the VLC path binary in the Threadfin settings."

case 2098:
errMsg = "Updates are disabled in the settings"
Expand Down
57 changes: 32 additions & 25 deletions src/serveStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (sm *StreamManager) StartStream(streamInfo StreamInfo, w http.ResponseWrite
if IsNewStreamPossible(sm, streamInfo, w) {
// create a new buffer and add the stream to the map within the new playlist
sm.playlists[playlistID].streams[streamID] = CreateStream(streamInfo, sm.errorChan)
if sm.playlists[playlistID].streams[streamID] == nil {
return "", ""
}
showInfo(fmt.Sprintf("Streaming:Started streaming for %s", streamID))
} else {
return "", ""
Expand Down Expand Up @@ -135,6 +138,9 @@ func CreateStream(streamInfo StreamInfo, errorChan chan ErrorInfo) *Stream {
clients: make(map[string]Client),
}
buffer := StartBuffer(stream, false, 0, errorChan)
if buffer == nil {
return nil
}
stream.Buffer = buffer
return stream
}
Expand Down Expand Up @@ -235,24 +241,19 @@ GetTuner returns the maximum number of connections for a playlist.
It will check if the buffer type is matching the third party buffers
*/
func GetTuner(id, playlistType string) (tuner int) {

switch Settings.Buffer {

case "-":
tuner = Settings.Tuner

case "threadfin", "ffmpeg", "vlc":

case "ffmpeg", "vlc":
i, err := strconv.Atoi(getProviderParameter(id, playlistType, "tuner"))
if err == nil {
tuner = i
} else {
ShowError(err, 0)
tuner = 1
}

}

return
}

Expand All @@ -276,36 +277,39 @@ It will use the third party tool defined in the settings and starts a process fo
*/
func CreateAlternativNoMoreStreamsVideo(pathToFile string) error {
cmd := new(exec.Cmd)
vlcArguments := []string{"--no-audio", "--loop", "--sout", fmt.Sprintf("'#transcode{vcodec=h264,vb=1024,scale=1,width=1920,height=1080,acodec=none,venc=x264{preset=ultrafast}}:standard{access=file,mux=ts,dst=%sstream-limit.ts}'", System.Folder.Video), System.Folder.Video, pathToFile}
ffmpegArguments := []string{"-loop", "1", "-i", pathToFile, "-c:v", "libx264", "-t", "1", "-pix_fmt", "yuv420p", "-vf", "scale=1920:1080", fmt.Sprintf("%sstream-limit.ts", System.Folder.Video)}
switch Settings.Buffer {
case "ffmpeg":
cmd = exec.Command(Settings.FFmpegPath, ffmpegArguments...)
case "vlc":
cmd = exec.Command(Settings.VLCPath, vlcArguments...)
default:
if Settings.FFmpegPath != "" {
if _, err := os.Stat(Settings.FFmpegPath); err != nil {
return fmt.Errorf("ffmpeg path is not valid. Can not convert custom image to video")
} else {
cmd = exec.Command(Settings.FFmpegPath, ffmpegArguments...)
}
} else {
return fmt.Errorf("no ffmpeg path given")
path, arguments := prepareArguments(pathToFile)
if len(arguments) == 0 {
if _, err := os.Stat(Settings.FFmpegPath); err != nil {
return fmt.Errorf("ffmpeg path is not valid. Can not convert custom image to video")
}
}
if len(cmd.Args) > 0 {

cmd = exec.Command(path, arguments...)

if len(cmd.Args) > 0 && path != "" {
showInfo("Streaming Status:Creating video from uploaded image for a customized no more stream video")
err := cmd.Run()
if err != nil {
return err
}
showInfo("Streaming Status:Successfully created video from custom image")
return nil
} else {
return fmt.Errorf("path for third party tool ")
}
return nil
}

// TODO: Add Function to get third party tool path and arguments
// TODO: Add description
func prepareArguments(pathToFile string) (string, []string){
switch Settings.Buffer {
case "ffmpeg", "threadfin", "-":
return Settings.FFmpegPath, []string{"--no-audio", "--loop", "--sout", fmt.Sprintf("'#transcode{vcodec=h264,vb=1024,scale=1,width=1920,height=1080,acodec=none,venc=x264{preset=ultrafast}}:standard{access=file,mux=ts,dst=%sstream-limit.ts}'", System.Folder.Video), System.Folder.Video, pathToFile}
case "vlc":
return Settings.VLCPath, []string{"-loop", "1", "-i", pathToFile, "-c:v", "libx264", "-t", "1", "-pix_fmt", "yuv420p", "-vf", "scale=1920:1080", fmt.Sprintf("%sstream-limit.ts", System.Folder.Video)}
default:
return "", []string{}
}
}

/*
StopStream stops the third party tool process when there are no more clients receiving the stream
Expand Down Expand Up @@ -401,6 +405,9 @@ ServeStream will ensure that the clients is getting the stream requested
func (sm *StreamManager) ServeStream(streamInfo StreamInfo, w http.ResponseWriter, r *http.Request) {
clientID, playlistID := sm.StartStream(streamInfo, w)
if clientID == "" || playlistID == "" {
if sm.playlists[streamInfo.PlaylistID].streams[streamInfo.URLid] == nil {
delete(sm.playlists, streamInfo.PlaylistID)
}
return
}
defer sm.StopStream(playlistID, streamInfo.URLid, clientID)
Expand Down
23 changes: 8 additions & 15 deletions src/thirdPartyBuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ import (
/*
StartThirdPartyBuffer starts the third party tool and capture its output
*/
func StartThirdPartyBuffer(stream *Stream, useBackup bool, backupNumber int, errorChan chan ErrorInfo) *Buffer {
func StartThirdPartyBuffer(stream *Stream, useBackup bool, backupNumber int, errorChan chan ErrorInfo) (*Buffer, error) {
if useBackup {
UpdateStreamURLForBackup(stream, backupNumber)
}

bufferType, path, options := GetBufferConfig()

bufferType, path, options := Settings.Buffer, Settings.FFmpegPath, Settings.FFmpegOptions
if bufferType == "" {
return nil
return nil, nil
}

if err := PrepareBufferFolder(stream.Folder); err != nil {
ShowError(err, 4008)
HandleBufferError(err, backupNumber, useBackup, stream, errorChan)
return nil
return nil, nil
}

showInfo(fmt.Sprintf("%s path:%s", bufferType, path))
showInfo("Streaming URL:" + stream.URL)

if buffer, err := RunBufferCommand(bufferType, path, options, stream, errorChan); err != nil {
return HandleBufferError(err, backupNumber, useBackup, stream, errorChan)
return HandleBufferError(err, backupNumber, useBackup, stream, errorChan), err
} else {
return buffer
return buffer, nil
}
}

Expand Down Expand Up @@ -77,14 +77,7 @@ PrepareBufferArguments
func PrepareBufferArguments(options, url string) []string {
args := []string{}
for i, a := range strings.Split(options, " ") {
if strings.Contains(a, "[URL]") {
a = strings.Replace(a, "[URL]", url, 1)
args = append(args, a)
if Settings.Buffer == "vlc" {
args = append(args, fmt.Sprintf("--http-user-agent=\"%s\"", Settings.UserAgent))
}
continue
}
a = strings.Replace(a, "[URL]", url, 1)
if i == 0 && len(Settings.UserAgent) != 0 && Settings.Buffer == "ffmpeg" {
args = append(args, "-user_agent", Settings.UserAgent)
}
Expand Down
2 changes: 2 additions & 0 deletions src/threadfinBuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func StartThreadfinBuffer(stream *Stream, useBackup bool, backupNumber int, erro
resp.Body.Close()
time.Sleep(200 * time.Millisecond) // Let the buffer stop before going on
return
default:
continue
}
}
}()
Expand Down
Loading

0 comments on commit 2ca6c61

Please sign in to comment.