-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ffmpeg auto codec selection logic
- Loading branch information
Showing
2 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package ffmpeg | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/AlexxIT/go2rtc/internal/streams" | ||
"github.com/AlexxIT/go2rtc/pkg/aac" | ||
"github.com/AlexxIT/go2rtc/pkg/core" | ||
) | ||
|
||
type Producer struct { | ||
core.SuperProducer | ||
url string | ||
query url.Values | ||
ffmpeg core.Producer | ||
} | ||
|
||
// NewProducer - FFmpeg producer with auto selection video/audio codec based on client capabilities | ||
func NewProducer(url string) (core.Producer, error) { | ||
p := &Producer{} | ||
|
||
i := strings.IndexByte(url, '#') | ||
p.url, p.query = url[:i], streams.ParseQuery(url[i+1:]) | ||
|
||
// ffmpeg.NewProducer support only one audio | ||
if len(p.query["video"]) != 0 || len(p.query["audio"]) != 1 { | ||
return nil, errors.New("ffmpeg: unsupported params: " + url[i:]) | ||
} | ||
|
||
p.Type = "FFmpeg producer" | ||
p.Medias = []*core.Media{ | ||
{ | ||
Kind: core.KindAudio, | ||
Direction: core.DirectionRecvonly, | ||
Codecs: []*core.Codec{ | ||
{Name: core.CodecOpus, ClockRate: 48000, Channels: 2}, | ||
{Name: core.CodecAAC, ClockRate: 16000, FmtpLine: aac.FMTP + "1408"}, | ||
{Name: core.CodecPCM, ClockRate: 16000}, | ||
{Name: core.CodecPCM, ClockRate: 8000}, | ||
{Name: core.CodecPCMA, ClockRate: 16000}, | ||
{Name: core.CodecPCMA, ClockRate: 8000}, | ||
{Name: core.CodecPCMU, ClockRate: 16000}, | ||
{Name: core.CodecPCMU, ClockRate: 8000}, | ||
}, | ||
}, | ||
} | ||
return p, nil | ||
} | ||
|
||
func (p *Producer) Start() error { | ||
var err error | ||
if p.ffmpeg, err = streams.GetProducer(p.newURL()); err != nil { | ||
return err | ||
} | ||
|
||
for i, media := range p.ffmpeg.GetMedias() { | ||
track, err := p.ffmpeg.GetTrack(media, media.Codecs[0]) | ||
if err != nil { | ||
return err | ||
} | ||
p.Receivers[i].Replace(track) | ||
} | ||
|
||
return p.ffmpeg.Start() | ||
} | ||
|
||
func (p *Producer) Stop() error { | ||
if p.ffmpeg == nil { | ||
return nil | ||
} | ||
return p.ffmpeg.Stop() | ||
} | ||
|
||
func (p *Producer) MarshalJSON() ([]byte, error) { | ||
if p.ffmpeg == nil { | ||
return json.Marshal(p.SuperProducer) | ||
} | ||
return json.Marshal(p.ffmpeg) | ||
} | ||
|
||
func (p *Producer) newURL() string { | ||
s := p.url | ||
// rewrite codecs in url from auto to known presets from defaults | ||
for _, receiver := range p.Receivers { | ||
codec := receiver.Codec | ||
switch codec.Name { | ||
case core.CodecPCMU, core.CodecPCMA: | ||
s += "#audio=" + strings.ToLower(codec.Name) | ||
if codec.ClockRate != 0 { | ||
s += "/" + strconv.Itoa(int(codec.ClockRate)) | ||
} | ||
case core.CodecAAC: | ||
s += "#audio=aac/16000" | ||
case core.CodecOpus: | ||
s += "#audio=opus/48000/2" | ||
} | ||
} | ||
// add other params | ||
for key, values := range p.query { | ||
if key != "audio" { | ||
for _, value := range values { | ||
s += "#" + key + "=" + value | ||
} | ||
} | ||
} | ||
|
||
return s | ||
} |