Avoid defaultAPI

Relates to #434
This commit is contained in:
backkem
2019-02-21 20:58:41 +01:00
committed by Michiel De Backker
parent 4fe2728721
commit 36cf0df239
9 changed files with 33 additions and 57 deletions

27
api.go
View File

@@ -43,30 +43,3 @@ func WithSettingEngine(s SettingEngine) func(a *API) {
a.settingEngine = &s a.settingEngine = &s
} }
} }
// defaultAPI is used to support the legacy global API.
// This global API should not be extended and may be phased out
// in the future.
var defaultAPI = NewAPI()
// Media Engine API
// RegisterCodec on the default API.
// See MediaEngine for details.
func RegisterCodec(codec *RTPCodec) {
defaultAPI.mediaEngine.RegisterCodec(codec)
}
// RegisterDefaultCodecs on the default API.
// See MediaEngine for details.
func RegisterDefaultCodecs() {
defaultAPI.mediaEngine.RegisterDefaultCodecs()
}
// PeerConnection API
// NewPeerConnection using the default API.
// See API.NewRTCPeerConnection for details.
func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
return defaultAPI.NewPeerConnection(configuration)
}

View File

@@ -17,10 +17,6 @@ import (
func gstreamerReceiveMain() { func gstreamerReceiveMain() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Setup the codecs you want to use.
// We'll use the default ones but you can also define your own
webrtc.RegisterDefaultCodecs()
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{

View File

@@ -12,10 +12,6 @@ import (
func main() { func main() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Setup the codecs you want to use.
// We'll use the default ones but you can also define your own
webrtc.RegisterDefaultCodecs()
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{

View File

@@ -17,10 +17,6 @@ func main() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Setup the codecs you want to use.
// We'll use the default ones but you can also define your own
webrtc.RegisterDefaultCodecs()
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{

View File

@@ -33,10 +33,6 @@ func watchHandle(handle *janus.Handle) {
func main() { func main() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Setup the codecs you want to use.
// We'll use the default ones but you can also define your own
webrtc.RegisterDefaultCodecs()
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{

View File

@@ -34,10 +34,6 @@ func watchHandle(handle *janus.Handle) {
func main() { func main() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️. // Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Setup the codecs you want to use.
// We'll use the default ones but you can also define your own
webrtc.RegisterDefaultCodecs()
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{ ICEServers: []webrtc.ICEServer{

View File

@@ -12,12 +12,19 @@ import (
) )
func main() { func main() {
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Create a MediaEngine object to configure the supported codec
m := webrtc.MediaEngine{}
// Setup the codecs you want to use. // Setup the codecs you want to use.
// We'll use a VP8 codec but you can also define your own // We'll use a VP8 codec but you can also define your own
webrtc.RegisterCodec(webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000, 2)) m.RegisterCodec(webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000, 2))
webrtc.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000)) m.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000))
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
// Everything below is the pion-WebRTC API! Thanks for using it ❤️.
// Prepare the configuration // Prepare the configuration
config := webrtc.Configuration{ config := webrtc.Configuration{
@@ -29,7 +36,7 @@ func main() {
} }
// Create a new RTCPeerConnection // Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config) peerConnection, err := api.NewPeerConnection(config)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -45,6 +45,16 @@ const (
) )
func main() { func main() {
// Create a MediaEngine object to configure the supported codec
m := webrtc.MediaEngine{}
// Setup the codecs you want to use.
// Only support VP8, this makes our proxying code simpler
m.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000))
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
port := flag.Int("port", 8080, "http server port") port := flag.Int("port", 8080, "http server port")
flag.Parse() flag.Parse()
@@ -66,13 +76,10 @@ func main() {
signal.Decode(mustReadHTTP(sdp), &offer) signal.Decode(mustReadHTTP(sdp), &offer)
fmt.Println("") fmt.Println("")
/* Everything below is the pion-WebRTC API, thanks for using it! */ // Everything below is the pion-WebRTC API, thanks for using it ❤️.
// Only support VP8, this makes our proxying code simpler
webrtc.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000))
// Create a new RTCPeerConnection // Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig) peerConnection, err := api.NewPeerConnection(peerConnectionConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -146,7 +153,7 @@ func main() {
signal.Decode(mustReadHTTP(sdp), &recvOnlyOffer) signal.Decode(mustReadHTTP(sdp), &recvOnlyOffer)
// Create a new PeerConnection // Create a new PeerConnection
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfig) peerConnection, err := api.NewPeerConnection(peerConnectionConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -115,6 +115,15 @@ type PeerConnection struct {
api *API api *API
} }
// NewPeerConnection creates a peerconnection with the default
// codecs. See API.NewRTCPeerConnection for details.
func NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
m := MediaEngine{}
m.RegisterDefaultCodecs()
api := NewAPI(WithMediaEngine(m))
return api.NewPeerConnection(configuration)
}
// NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object // NewPeerConnection creates a new PeerConnection with the provided configuration against the received API object
func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) { func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, error) {
// https://w3c.github.io/webrtc-pc/#constructor (Step #2) // https://w3c.github.io/webrtc-pc/#constructor (Step #2)