-
Notifications
You must be signed in to change notification settings - Fork 1k
WebCodecs Support #2160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seydx
wants to merge
6
commits into
AlexxIT:master
Choose a base branch
from
seydx:webcodecs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WebCodecs Support #2160
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a72b48f
add webcodecs support
seydx 3fbfc3f
Merge branch 'AlexxIT:master' into webcodecs
seydx 44d29db
Refactor timestamp handling in buildFrame and AddTrack functions to u…
seydx 5b70d2f
Add VideoRenderer class for cascading video rendering with WebGPU, We…
seydx 0c66b28
Update timestamp handling
seydx 2027345
Implement WebCodecsPlayer for video/audio decoding and rendering
seydx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,45 @@ | ||
| package webcodecs | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/AlexxIT/go2rtc/internal/api" | ||
| "github.com/AlexxIT/go2rtc/internal/api/ws" | ||
| "github.com/AlexxIT/go2rtc/internal/app" | ||
| "github.com/AlexxIT/go2rtc/internal/streams" | ||
| "github.com/AlexxIT/go2rtc/pkg/webcodecs" | ||
| "github.com/rs/zerolog" | ||
| ) | ||
|
|
||
| func Init() { | ||
| log = app.GetLogger("webcodecs") | ||
|
|
||
| ws.HandleFunc("webcodecs", handlerWS) | ||
| } | ||
|
|
||
| var log zerolog.Logger | ||
|
|
||
| func handlerWS(tr *ws.Transport, msg *ws.Message) error { | ||
| stream, _ := streams.GetOrPatch(tr.Request.URL.Query()) | ||
| if stream == nil { | ||
| return errors.New(api.StreamNotFound) | ||
| } | ||
|
|
||
| cons := webcodecs.NewConsumer(nil) | ||
| cons.WithRequest(tr.Request) | ||
|
|
||
| if err := stream.AddConsumer(cons); err != nil { | ||
| log.Debug().Err(err).Msg("[webcodecs] add consumer") | ||
| return err | ||
| } | ||
|
|
||
| tr.Write(&ws.Message{Type: "webcodecs", Value: cons.GetInitInfo()}) | ||
|
|
||
| go cons.WriteTo(tr.Writer()) | ||
|
|
||
| tr.OnClose(func() { | ||
| stream.RemoveConsumer(cons) | ||
| }) | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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 hidden or 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,267 @@ | ||
| package webcodecs | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "errors" | ||
| "io" | ||
| "sync" | ||
|
|
||
| "github.com/AlexxIT/go2rtc/pkg/aac" | ||
| "github.com/AlexxIT/go2rtc/pkg/core" | ||
| "github.com/AlexxIT/go2rtc/pkg/h264" | ||
| "github.com/AlexxIT/go2rtc/pkg/h264/annexb" | ||
| "github.com/AlexxIT/go2rtc/pkg/h265" | ||
| "github.com/pion/rtp" | ||
| ) | ||
|
|
||
| // Binary frame header (9 bytes): | ||
| // Byte 0: flags (bit7=video, bit6=keyframe, bits0-5=trackID) | ||
| // Byte 1-8: timestamp in microseconds (uint64 BE) | ||
| // Byte 9+: payload | ||
|
|
||
| const headerSize = 9 | ||
|
|
||
| type Consumer struct { | ||
| core.Connection | ||
| wr *core.WriteBuffer | ||
| mu sync.Mutex | ||
| start bool | ||
|
|
||
| UseGOP bool | ||
| } | ||
|
|
||
| type InitInfo struct { | ||
| Video *VideoInfo `json:"video,omitempty"` | ||
| Audio *AudioInfo `json:"audio,omitempty"` | ||
| } | ||
|
|
||
| type VideoInfo struct { | ||
| Codec string `json:"codec"` | ||
| } | ||
|
|
||
| type AudioInfo struct { | ||
| Codec string `json:"codec"` | ||
| SampleRate int `json:"sampleRate"` | ||
| Channels int `json:"channels"` | ||
| } | ||
|
|
||
| func NewConsumer(medias []*core.Media) *Consumer { | ||
| if medias == nil { | ||
| medias = []*core.Media{ | ||
| { | ||
| Kind: core.KindVideo, | ||
| Direction: core.DirectionSendonly, | ||
| Codecs: []*core.Codec{ | ||
| {Name: core.CodecH264}, | ||
| {Name: core.CodecH265}, | ||
| }, | ||
| }, | ||
| { | ||
| Kind: core.KindAudio, | ||
| Direction: core.DirectionSendonly, | ||
| Codecs: []*core.Codec{ | ||
| {Name: core.CodecAAC}, | ||
| {Name: core.CodecOpus}, | ||
| {Name: core.CodecPCMA}, | ||
| {Name: core.CodecPCMU}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| wr := core.NewWriteBuffer(nil) | ||
| return &Consumer{ | ||
| Connection: core.Connection{ | ||
| ID: core.NewID(), | ||
| FormatName: "webcodecs", | ||
| Medias: medias, | ||
| Transport: wr, | ||
| }, | ||
| wr: wr, | ||
| } | ||
| } | ||
|
|
||
| func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { | ||
| trackID := byte(len(c.Senders)) | ||
|
|
||
| codec := track.Codec.Clone() | ||
| handler := core.NewSender(media, codec) | ||
|
|
||
| switch track.Codec.Name { | ||
| case core.CodecH264: | ||
| clockRate := codec.ClockRate | ||
| handler.Handler = func(packet *rtp.Packet) { | ||
| keyframe := h264.IsKeyframe(packet.Payload) | ||
| if !c.start { | ||
| if !keyframe { | ||
| return | ||
| } | ||
| c.start = true | ||
| } | ||
|
|
||
| payload := annexb.DecodeAVCC(packet.Payload, true) | ||
| flags := byte(0x80) | trackID // video flag | ||
| if keyframe { | ||
| flags |= 0x40 // keyframe flag | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| msg := buildFrame(flags, rtpToMicroseconds(packet.Timestamp, clockRate), payload) | ||
| if n, err := c.wr.Write(msg); err == nil { | ||
| c.Send += n | ||
| } | ||
| c.mu.Unlock() | ||
| } | ||
|
|
||
| if track.Codec.IsRTP() { | ||
| handler.Handler = h264.RTPDepay(track.Codec, handler.Handler) | ||
| } else { | ||
| handler.Handler = h264.RepairAVCC(track.Codec, handler.Handler) | ||
| } | ||
|
|
||
| case core.CodecH265: | ||
| clockRate := codec.ClockRate | ||
| handler.Handler = func(packet *rtp.Packet) { | ||
| keyframe := h265.IsKeyframe(packet.Payload) | ||
| if !c.start { | ||
| if !keyframe { | ||
| return | ||
| } | ||
| c.start = true | ||
| } | ||
|
|
||
| payload := annexb.DecodeAVCC(packet.Payload, true) | ||
| flags := byte(0x80) | trackID // video flag | ||
| if keyframe { | ||
| flags |= 0x40 // keyframe flag | ||
| } | ||
|
|
||
| c.mu.Lock() | ||
| msg := buildFrame(flags, rtpToMicroseconds(packet.Timestamp, clockRate), payload) | ||
| if n, err := c.wr.Write(msg); err == nil { | ||
| c.Send += n | ||
| } | ||
| c.mu.Unlock() | ||
| } | ||
|
|
||
| if track.Codec.IsRTP() { | ||
| handler.Handler = h265.RTPDepay(track.Codec, handler.Handler) | ||
| } else { | ||
| handler.Handler = h265.RepairAVCC(track.Codec, handler.Handler) | ||
| } | ||
|
|
||
| default: | ||
| clockRate := codec.ClockRate | ||
| handler.Handler = func(packet *rtp.Packet) { | ||
| if !c.start { | ||
| return | ||
| } | ||
|
|
||
| flags := trackID // audio flag (bit7=0) | ||
|
|
||
| c.mu.Lock() | ||
| msg := buildFrame(flags, rtpToMicroseconds(packet.Timestamp, clockRate), packet.Payload) | ||
| if n, err := c.wr.Write(msg); err == nil { | ||
| c.Send += n | ||
| } | ||
| c.mu.Unlock() | ||
| } | ||
|
|
||
| switch track.Codec.Name { | ||
| case core.CodecAAC: | ||
| if track.Codec.IsRTP() { | ||
| handler.Handler = aac.RTPDepay(handler.Handler) | ||
| } | ||
| case core.CodecOpus, core.CodecPCMA, core.CodecPCMU: | ||
| // pass through directly — WebCodecs decodes these natively | ||
| default: | ||
| handler.Handler = nil | ||
| } | ||
| } | ||
|
|
||
| if handler.Handler == nil { | ||
| s := "webcodecs: unsupported codec: " + track.Codec.String() | ||
| println(s) | ||
| return errors.New(s) | ||
| } | ||
|
|
||
| handler.HandleRTP(track) | ||
| c.Senders = append(c.Senders, handler) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (c *Consumer) GetInitInfo() *InitInfo { | ||
| info := &InitInfo{} | ||
|
|
||
| for _, sender := range c.Senders { | ||
| codec := sender.Codec | ||
| switch codec.Name { | ||
| case core.CodecH264: | ||
| info.Video = &VideoInfo{ | ||
| Codec: "avc1." + h264.GetProfileLevelID(codec.FmtpLine), | ||
| } | ||
| case core.CodecH265: | ||
| info.Video = &VideoInfo{ | ||
| Codec: "hvc1.1.6.L153.B0", | ||
| } | ||
| case core.CodecAAC: | ||
| channels := int(codec.Channels) | ||
| if channels == 0 { | ||
| channels = 1 | ||
| } | ||
| info.Audio = &AudioInfo{ | ||
| Codec: "mp4a.40.2", | ||
| SampleRate: int(codec.ClockRate), | ||
| Channels: channels, | ||
| } | ||
| case core.CodecOpus: | ||
| channels := int(codec.Channels) | ||
| if channels == 0 { | ||
| channels = 2 | ||
| } | ||
| info.Audio = &AudioInfo{ | ||
| Codec: "opus", | ||
| SampleRate: int(codec.ClockRate), | ||
| Channels: channels, | ||
| } | ||
| case core.CodecPCMA: | ||
| info.Audio = &AudioInfo{ | ||
| Codec: "alaw", | ||
| SampleRate: int(codec.ClockRate), | ||
| Channels: 1, | ||
| } | ||
| case core.CodecPCMU: | ||
| info.Audio = &AudioInfo{ | ||
| Codec: "ulaw", | ||
| SampleRate: int(codec.ClockRate), | ||
| Channels: 1, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return info | ||
| } | ||
|
|
||
| func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { | ||
| if len(c.Senders) == 1 && c.Senders[0].Codec.IsAudio() { | ||
| c.start = true | ||
| } | ||
|
|
||
| return c.wr.WriteTo(wr) | ||
| } | ||
|
|
||
| func buildFrame(flags byte, timestamp uint64, payload []byte) []byte { | ||
| msg := make([]byte, headerSize+len(payload)) | ||
| msg[0] = flags | ||
| binary.BigEndian.PutUint64(msg[1:9], timestamp) | ||
| copy(msg[headerSize:], payload) | ||
| return msg | ||
| } | ||
|
|
||
| func rtpToMicroseconds(timestamp uint32, clockRate uint32) uint64 { | ||
| if clockRate == 0 { | ||
| return uint64(timestamp) | ||
| } | ||
| return uint64(timestamp) * 1_000_000 / uint64(clockRate) | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://caniuse.com/?search=VideoDecoder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebCodecs is supported in all major browser, but hevc not:
Microsoft Edge on Windows
By default, Chromium browsers uses FFMpeg internally for WebCodecs API. However, Microsoft Edge, when running on Windows, uses Media Foundation decoders instead.
Decoding H.265 requires the HEVC Video Extensions ($0.99) or HEVC Video Extensions from Device Manufacturer (free but not available anymore) app from Microsoft Store.
Firefox
Firefox 133 supports playing H.265 videos, but does not support decoding H.265 streams using WebCodecs API yet.