mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-28 12:36:30 +00:00
Use default WebRTC codecs
This commit is contained in:
parent
891e397104
commit
be52ee18c5
3 changed files with 11 additions and 93 deletions
|
|
@ -6,13 +6,9 @@ import (
|
|||
pion "github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
func NewInterceptedPeerConnection(conf conf.Webrtc, ics []interceptor.Interceptor, vCodec string) (*pion.PeerConnection, error) {
|
||||
func NewInterceptedPeerConnection(conf conf.Webrtc, ics []interceptor.Interceptor) (*pion.PeerConnection, error) {
|
||||
m := &pion.MediaEngine{}
|
||||
//if err := m.RegisterDefaultCodecs(); err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
|
||||
if err := RegisterCodecs(m, vCodec); err != nil {
|
||||
if err := m.RegisterDefaultCodecs(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -52,60 +48,3 @@ func NewInterceptedPeerConnection(conf conf.Webrtc, ics []interceptor.Intercepto
|
|||
)
|
||||
return api.NewPeerConnection(peerConf)
|
||||
}
|
||||
|
||||
// RegisterCodecs registers the default codecs supported by WebRTC.
|
||||
func RegisterCodecs(m *pion.MediaEngine, vCodec string) error {
|
||||
audioRTPCodecParameters := []pion.RTPCodecParameters{
|
||||
{
|
||||
RTPCodecCapability: pion.RTPCodecCapability{MimeType: pion.MimeTypeOpus, ClockRate: 48000, Channels: 2},
|
||||
PayloadType: 111,
|
||||
},
|
||||
}
|
||||
for _, codec := range audioRTPCodecParameters {
|
||||
if err := m.RegisterCodec(codec, pion.RTPCodecTypeAudio); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
videoRTCPFeedback := []pion.RTCPFeedback{
|
||||
{"goog-remb", ""},
|
||||
{"ccm", "fir"},
|
||||
{"nack", ""},
|
||||
{"nack", "pli"},
|
||||
}
|
||||
video := pion.RTPCodecCapability{MimeType: vCodec, ClockRate: 90000, RTCPFeedback: videoRTCPFeedback}
|
||||
var videoRTPCodecParameters []pion.RTPCodecParameters
|
||||
if vCodec == pion.MimeTypeH264 {
|
||||
videoRTPCodecParameters = []pion.RTPCodecParameters{
|
||||
{RTPCodecCapability: pion.RTPCodecCapability{
|
||||
MimeType: video.MimeType, ClockRate: video.ClockRate, RTCPFeedback: video.RTCPFeedback,
|
||||
//SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f",
|
||||
}, PayloadType: 102},
|
||||
{RTPCodecCapability: pion.RTPCodecCapability{
|
||||
MimeType: video.MimeType, ClockRate: video.ClockRate, RTCPFeedback: video.RTCPFeedback,
|
||||
SDPFmtpLine: "level-asymmetry-allowed=1;profile-level-id=42e01f",
|
||||
}, PayloadType: 108},
|
||||
{RTPCodecCapability: video, PayloadType: 123},
|
||||
{RTPCodecCapability: pion.RTPCodecCapability{
|
||||
MimeType: video.MimeType, ClockRate: video.ClockRate, RTCPFeedback: video.RTCPFeedback,
|
||||
SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f",
|
||||
}, PayloadType: 125},
|
||||
{RTPCodecCapability: pion.RTPCodecCapability{
|
||||
MimeType: video.MimeType, ClockRate: video.ClockRate, RTCPFeedback: video.RTCPFeedback,
|
||||
SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=0;profile-level-id=42001f",
|
||||
}, PayloadType: 127},
|
||||
}
|
||||
} else {
|
||||
videoRTPCodecParameters = []pion.RTPCodecParameters{
|
||||
{RTPCodecCapability: video, PayloadType: 96},
|
||||
}
|
||||
}
|
||||
|
||||
for _, codec := range videoRTPCodecParameters {
|
||||
if err := m.RegisterCodec(codec, pion.RTPCodecTypeVideo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
// credit to https://github.com/poi5305/go-yuv2webRTC/blob/master/webrtc/webrtc.go
|
||||
package webrtc
|
||||
|
||||
import (
|
||||
|
|
@ -39,17 +38,8 @@ type WebRTC struct {
|
|||
InputChannel chan []byte
|
||||
|
||||
Done bool
|
||||
//lastTime time.Time
|
||||
//curFPS int
|
||||
|
||||
RoomID string
|
||||
|
||||
// store thing related to game
|
||||
GameMeta GameMeta
|
||||
}
|
||||
|
||||
// Game Meta
|
||||
type GameMeta struct {
|
||||
RoomID string
|
||||
PlayerIndex int
|
||||
}
|
||||
|
||||
|
|
@ -118,15 +108,14 @@ func (w *WebRTC) StartClient(iceCB OnIceCallback) (string, error) {
|
|||
|
||||
log.Println("=== StartClient ===")
|
||||
|
||||
videoCodec := w.getVideoCodec()
|
||||
w.tsInterceptor = itc.ReTime{}
|
||||
w.connection, err = NewInterceptedPeerConnection(w.cfg.Webrtc, []interceptor.Interceptor{&w.tsInterceptor}, videoCodec)
|
||||
w.connection, err = NewInterceptedPeerConnection(w.cfg.Webrtc, []interceptor.Interceptor{&w.tsInterceptor})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// add video track
|
||||
rtpCodec := webrtc.RTPCodecCapability{MimeType: videoCodec}
|
||||
rtpCodec := webrtc.RTPCodecCapability{MimeType: w.getVideoCodec()}
|
||||
if videoTrack, err = webrtc.NewTrackLocalStaticSample(rtpCodec, "video", "game-video"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -301,10 +290,10 @@ func (w *WebRTC) StopClient() {
|
|||
return
|
||||
}
|
||||
|
||||
log.Println("===StopClient===")
|
||||
w.isConnected = false
|
||||
if w.connection != nil {
|
||||
w.connection.Close()
|
||||
err := w.connection.Close()
|
||||
log.Printf("error: couldn't close WebRTC connection, %v", err)
|
||||
}
|
||||
w.connection = nil
|
||||
//close(w.InputChannel)
|
||||
|
|
@ -314,12 +303,10 @@ func (w *WebRTC) StopClient() {
|
|||
close(w.AudioChannel)
|
||||
//close(w.VoiceInChannel)
|
||||
//close(w.VoiceOutChannel)
|
||||
log.Println("===StopClient===")
|
||||
}
|
||||
|
||||
// IsConnected comment
|
||||
func (w *WebRTC) IsConnected() bool {
|
||||
return w.isConnected
|
||||
}
|
||||
func (w *WebRTC) IsConnected() bool { return w.isConnected }
|
||||
|
||||
func (w *WebRTC) startStreaming(vp8Track *webrtc.TrackLocalStaticSample, opusTrack *webrtc.TrackLocalStaticSample) {
|
||||
log.Println("Start streaming")
|
||||
|
|
@ -383,11 +370,3 @@ func (w *WebRTC) startStreaming(vp8Track *webrtc.TrackLocalStaticSample, opusTra
|
|||
// }
|
||||
//}()
|
||||
}
|
||||
|
||||
//func (w *WebRTC) calculateFPS() int {
|
||||
// elapsedTime := time.Now().Sub(w.lastTime)
|
||||
// w.lastTime = time.Now()
|
||||
// curFPS := time.Second / elapsedTime
|
||||
// w.curFPS = int(float32(w.curFPS)*0.9 + float32(curFPS)*0.1)
|
||||
// return w.curFPS
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ func (r *Room) AddConnectionToRoom(peerconnection *webrtc.WebRTC) {
|
|||
|
||||
func (r *Room) UpdatePlayerIndex(peerconnection *webrtc.WebRTC, playerIndex int) {
|
||||
log.Println("Updated player Index to: ", playerIndex)
|
||||
peerconnection.GameMeta.PlayerIndex = playerIndex
|
||||
peerconnection.PlayerIndex = playerIndex
|
||||
}
|
||||
|
||||
func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC) {
|
||||
|
|
@ -282,7 +282,7 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC) {
|
|||
|
||||
if peerconnection.IsConnected() {
|
||||
select {
|
||||
case r.inputChannel <- nanoarch.InputEvent{RawState: input, PlayerIdx: peerconnection.GameMeta.PlayerIndex, ConnID: peerconnection.ID}:
|
||||
case r.inputChannel <- nanoarch.InputEvent{RawState: input, PlayerIdx: peerconnection.PlayerIndex, ConnID: peerconnection.ID}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue