Created
June 2, 2023 06:52
-
-
Save jinleileiking/66d1712e369bc6a5636e7d03879dc7e3 to your computer and use it in GitHub Desktop.
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
func pub(addr string, f string, name string) error { | |
tlA, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus}, "tid-audio-"+name, sidA) | |
if err != nil { | |
panic(err) | |
} | |
if _, err := pc.AddTrack(tlA); err != nil { | |
panic(err) | |
} | |
tlVL, err := NewLocalSimulcastSampleTrack(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video-"+name, "rid-l-"+name) | |
if err != nil { | |
panic(err) | |
} | |
var sender *webrtc.RTPSender | |
if sender, err = pc.AddTrack(tlVL); err != nil { | |
panic(err) | |
} | |
tlVM, err := NewLocalSimulcastSampleTrack(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video-"+name, "rid-m-"+name) | |
if err != nil { | |
panic(err) | |
} | |
if err := sender.AddEncoding(tlVM); err != nil { | |
panic(err) | |
} | |
tlVH, err := NewLocalSimulcastSampleTrack(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video-"+name, "rid-h-"+name) | |
if err != nil { | |
panic(err) | |
} | |
if err := sender.AddEncoding(tlVH); err != nil { | |
panic(err) | |
} | |
// tlVL, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "tid-video-"+name, sidV, webrtc.WithRTPStreamID("rid-l-"+name)) | |
// if err != nil { | |
// panic(err) | |
// } | |
// var sender *webrtc.RTPSender | |
// | |
// if sender, err = pc.AddTrack(tlVL); err != nil { | |
// panic(err) | |
// } | |
// | |
// tlVM, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "tid-video-"+name, sidV, webrtc.WithRTPStreamID("rid-m-"+name)) | |
// if err != nil { | |
// panic(err) | |
// } | |
// | |
// if err := sender.AddEncoding(tlVM); err != nil { | |
// panic(err) | |
// } | |
// | |
// tlVH, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "tid-video-"+name, sidV, webrtc.WithRTPStreamID("rid-h-"+name)) | |
// if err != nil { | |
// panic(err) | |
// } | |
// | |
// if err := sender.AddEncoding(tlVH); err != nil { | |
// panic(err) | |
// } | |
// log.Printf("publishing") | |
g := new(errgroup.Group) | |
g.Go(func() error { return writeIvf(f+"240", tlVL) }) | |
g.Go(func() error { return writeIvf(f+"360", tlVM) }) | |
g.Go(func() error { return writeIvf(f+"480", tlVH) }) | |
g.Go(func() error { return writeOgg(f, tlA) }) | |
if err := g.Wait(); err == nil { | |
return fmt.Errorf("g.Wait failed: %w", err) | |
} | |
return nil | |
} | |
func writeIvf(f string, tl *LocalSampleSimulcastTrack) error { | |
file, err := os.Open(f + ".ivf") | |
if err != nil { | |
return fmt.Errorf("os.Open failed: %w", err) | |
} | |
ivf, header, err := ivfreader.NewWith(file) | |
if err != nil { | |
return fmt.Errorf("ivfreader.NewWith failed: %w", err) | |
} | |
ticker := time.NewTicker(time.Millisecond * time.Duration((float32(header.TimebaseNumerator)/float32(header.TimebaseDenominator))*1000)) | |
log.GetLogger().InfoFields(context.Background(), nil, "start send video sample") | |
logged := false | |
for range ticker.C { | |
frame, _, err := ivf.ParseNextFrame() | |
if errors.Is(err, io.EOF) { | |
return nil | |
} | |
if err != nil { | |
return fmt.Errorf("ivf.ParseNextFrame failed: %w", err) | |
} | |
if !logged { | |
log.GetLogger().InfoFields(context.Background(), map[string]interface{}{constant.Module: constant.ModuleTrack}, "sending video....") | |
logged = true | |
} | |
if err = tl.WriteSample(media.Sample{Data: frame, Duration: time.Second}, nil); err != nil { | |
// if err = tlV.WriteSample(media.Sample{Data: frame, Duration: delta}); err != nil { | |
return fmt.Errorf("tl.WriteSample failed: %w", err) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment