Skip to content

Commit

Permalink
Support video-only streams if there is no audio in the inbound stream. (
Browse files Browse the repository at this point in the history
  • Loading branch information
gabek authored Dec 2, 2020
1 parent cdbfd89 commit 0b66f3b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
22 changes: 19 additions & 3 deletions core/ffmpeg/transcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Transcoder struct {
ffmpegPath string
segmentIdentifier string
internalListenerPort int
videoOnly bool // If true ignore any audio, if any
TranscoderCompleted func(error)
}

Expand Down Expand Up @@ -80,6 +81,9 @@ func (t *Transcoder) Start() {
command := t.getString()

log.Tracef("Video transcoder started with %d stream variants.", len(t.variants))
if t.videoOnly {
log.Tracef("Transcoder requested to operate on video only, ignoring audio.")
}

if config.Config.EnableDebugFeatures {
log.Println(command)
Expand Down Expand Up @@ -252,7 +256,14 @@ func (t *Transcoder) getVariantsString() string {

for _, variant := range t.variants {
variantsCommandFlags = variantsCommandFlags + " " + variant.getVariantString(t)
variantsStreamMaps = variantsStreamMaps + fmt.Sprintf("v:%d,a:%d ", variant.index, variant.index)
singleVariantMap := ""
if t.videoOnly {
singleVariantMap = fmt.Sprintf("v:%d ", variant.index)
} else {
singleVariantMap = fmt.Sprintf("v:%d,a:%d ", variant.index, variant.index)
}

variantsStreamMaps = variantsStreamMaps + singleVariantMap
}
variantsCommandFlags = variantsCommandFlags + " " + variantsStreamMaps + "\""

Expand Down Expand Up @@ -339,12 +350,12 @@ func (v *HLSVariant) SetAudioBitrate(bitrate string) {

func (v *HLSVariant) getAudioQualityString() string {
if v.isAudioPassthrough {
return fmt.Sprintf("-map a:0 -c:a:%d copy", v.index)
return fmt.Sprintf("-map a:0? -c:a:%d copy", v.index)
}

// libfdk_aac is not a part of every ffmpeg install, so use "aac" instead
encoderCodec := "aac"
return fmt.Sprintf("-map a:0 -c:a:%d %s -b:a:%d %s", v.index, encoderCodec, v.index, v.audioBitrate)
return fmt.Sprintf("-map a:0? -c:a:%d %s -b:a:%d %s", v.index, encoderCodec, v.index, v.audioBitrate)
}

// AddVariant adds a new HLS variant to include in the output.
Expand Down Expand Up @@ -386,3 +397,8 @@ func (t *Transcoder) SetIdentifier(output string) {
func (t *Transcoder) SetInternalHTTPPort(port int) {
t.internalListenerPort = port
}

// SetVideoOnly will ignore any audio streams, if any.
func (t *Transcoder) SetVideoOnly(videoOnly bool) {
t.videoOnly = videoOnly
}
2 changes: 1 addition & 1 deletion core/ffmpeg/transcoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestFFmpegCommand(t *testing.T) {

cmd := transcoder.getString()

expected := `/fake/path/ffmpeg -hide_banner -loglevel warning -i fakecontent.flv -map v:0 -c:v:0 libx264 -b:v:0 1200k -maxrate:v:0 1272k -bufsize:v:0 1440k -g:v:0 119 -profile:v:0 high -r:v:0 30 -x264-params:v:0 "scenecut=0:open_gop=0:min-keyint=119:keyint=119" -map a:0 -c:a:0 copy -preset veryfast -map v:0 -c:v:1 libx264 -b:v:1 3500k -maxrate:v:1 3710k -bufsize:v:1 4200k -g:v:1 95 -profile:v:1 high -r:v:1 24 -x264-params:v:1 "scenecut=0:open_gop=0:min-keyint=95:keyint=95" -map a:0 -c:a:1 copy -preset faster -map v:0 -c:v:2 copy -map a:0 -c:a:2 copy -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 " -f hls -hls_time 4 -hls_list_size 10 -hls_delete_threshold 10 -tune zerolatency -pix_fmt yuv420p -sc_threshold 0 -master_pl_name stream.m3u8 -strftime 1 -hls_segment_filename http://127.0.0.1:8123/%v/stream-jdofFGg%s.ts -max_muxing_queue_size 400 -method PUT -http_persistent 0 -fflags +genpts http://127.0.0.1:8123/%v/stream.m3u8 2> transcoder.log`
expected := `/fake/path/ffmpeg -hide_banner -loglevel warning -i fakecontent.flv -map v:0 -c:v:0 libx264 -b:v:0 1200k -maxrate:v:0 1272k -bufsize:v:0 1440k -g:v:0 119 -profile:v:0 high -r:v:0 30 -x264-params:v:0 "scenecut=0:open_gop=0:min-keyint=119:keyint=119" -map a:0? -c:a:0 copy -preset veryfast -map v:0 -c:v:1 libx264 -b:v:1 3500k -maxrate:v:1 3710k -bufsize:v:1 4200k -g:v:1 95 -profile:v:1 high -r:v:1 24 -x264-params:v:1 "scenecut=0:open_gop=0:min-keyint=95:keyint=95" -map a:0? -c:a:1 copy -preset faster -map v:0 -c:v:2 copy -map a:0? -c:a:2 copy -var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2 " -f hls -hls_time 4 -hls_list_size 10 -hls_delete_threshold 10 -tune zerolatency -pix_fmt yuv420p -sc_threshold 0 -master_pl_name stream.m3u8 -strftime 1 -hls_segment_filename http://127.0.0.1:8123/%v/stream-jdofFGg%s.ts -max_muxing_queue_size 400 -method PUT -http_persistent 0 -fflags +genpts http://127.0.0.1:8123/%v/stream.m3u8 2> transcoder.log`

if cmd != expected {
t.Errorf("ffmpeg command does not match expected.\nGot %s\n, want: %s", cmd, expected)
Expand Down
1 change: 1 addition & 0 deletions core/rtmp/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func setCurrentBroadcasterInfo(t flvio.Tag, remoteAddr string) {
AudioBitrate: int(data.AudioBitrate),
AudioCodec: getAudioCodec(data.AudioCodec),
Encoder: data.Encoder,
VideoOnly: data.AudioCodec == nil,
},
}

Expand Down
2 changes: 1 addition & 1 deletion core/rtmp/rtmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
if t.Type == flvio.TAG_AMF0 {
log.Tracef("%+v\n", t.DebugFields())
setCurrentBroadcasterInfo(t, nc.RemoteAddr().String())
_setStreamAsConnected()
}
}

Expand Down Expand Up @@ -98,7 +99,6 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
}

_hasInboundRTMPConnection = true
_setStreamAsConnected()
_rtmpConnection = nc

f, err := os.OpenFile(pipePath, os.O_RDWR, os.ModeNamedPipe)
Expand Down
4 changes: 4 additions & 0 deletions core/rtmp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func getInboundDetailsFromMetadata(metadata []interface{}) (models.RTMPStreamMet
}

func getAudioCodec(codec interface{}) string {
if codec == nil {
return "No audio"
}

var codecID float64
if assertedCodecID, ok := codec.(float64); ok {
codecID = assertedCodecID
Expand Down
1 change: 1 addition & 0 deletions core/streamState.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func setStreamAsConnected() {

go func() {
_transcoder = ffmpeg.NewTranscoder()
_transcoder.SetVideoOnly(_broadcaster.StreamDetails.VideoOnly)
_transcoder.TranscoderCompleted = func(error) {
SetStreamAsDisconnected()
}
Expand Down
1 change: 1 addition & 0 deletions models/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type InboundStreamDetails struct {
AudioBitrate int `json:"audioBitrate"`
AudioCodec string `json:"audioCodec"`
Encoder string `json:"encoder"`
VideoOnly bool `json:"-"`
}

// RTMPStreamMetadata is the raw metadata that comes in with a RTMP connection.
Expand Down

0 comments on commit 0b66f3b

Please sign in to comment.