mirror of
https://github.com/pion/webrtc.git
synced 2025-09-27 11:32:19 +08:00
Add vnet to SettingEngine
Also add simple example to show usage Resolves #778
This commit is contained in:
15
examples/vnet/README.md
Normal file
15
examples/vnet/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# vnet
|
||||||
|
vnet is the virtual network layer for Pion. This allows developers to simulate issues that cause issues
|
||||||
|
with production WebRTC deployments.
|
||||||
|
|
||||||
|
See the full documentation for vnet [here](https://github.com/pion/transport/tree/master/vnet#vnet)
|
||||||
|
|
||||||
|
## What can vnet do
|
||||||
|
* Simulate different network topologies. Assert when a STUN/TURN server is actually needed.
|
||||||
|
* Simulate packet loss, jitter, re-ordering. See how your application performs under adverse conditions.
|
||||||
|
* Measure the total bandwidth used. Determine the total cost of running your application.
|
||||||
|
* More! We would love to continue extending this to support everyones needs.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
Each directory contains a single `main.go` that aims to demonstrate a single feature of vnet.
|
||||||
|
They can all be run directly, and require no additional setup.
|
78
examples/vnet/show-network-usage/main.go
Normal file
78
examples/vnet/show-network-usage/main.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pion/logging"
|
||||||
|
"github.com/pion/transport/vnet"
|
||||||
|
"github.com/pion/webrtc/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
wan, err := vnet.NewRouter(&vnet.RouterConfig{
|
||||||
|
CIDR: "1.2.3.0/24",
|
||||||
|
LoggerFactory: logging.NewDefaultLoggerFactory(),
|
||||||
|
})
|
||||||
|
panicIfError(err)
|
||||||
|
|
||||||
|
offerVNet := vnet.NewNet(&vnet.NetConfig{})
|
||||||
|
panicIfError(wan.AddNet(offerVNet))
|
||||||
|
|
||||||
|
offerSettingEngine := webrtc.SettingEngine{}
|
||||||
|
offerSettingEngine.SetVNet(offerVNet)
|
||||||
|
offerAPI := webrtc.NewAPI(webrtc.WithSettingEngine(offerSettingEngine))
|
||||||
|
|
||||||
|
answerVNet := vnet.NewNet(&vnet.NetConfig{})
|
||||||
|
panicIfError(wan.AddNet(answerVNet))
|
||||||
|
|
||||||
|
answerSettingEngine := webrtc.SettingEngine{}
|
||||||
|
answerSettingEngine.SetVNet(answerVNet)
|
||||||
|
answerAPI := webrtc.NewAPI(webrtc.WithSettingEngine(answerSettingEngine))
|
||||||
|
|
||||||
|
panicIfError(wan.Start())
|
||||||
|
|
||||||
|
offerPeerConnection, err := offerAPI.NewPeerConnection(webrtc.Configuration{})
|
||||||
|
panicIfError(err)
|
||||||
|
|
||||||
|
answerPeerConnection, err := answerAPI.NewPeerConnection(webrtc.Configuration{})
|
||||||
|
panicIfError(err)
|
||||||
|
|
||||||
|
offerDataChannel, err := offerPeerConnection.CreateDataChannel("label", nil)
|
||||||
|
panicIfError(err)
|
||||||
|
|
||||||
|
msgSendLoop := func(dc *webrtc.DataChannel) {
|
||||||
|
for {
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
panicIfError(dc.SendText("My DataChannel Message"))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
offerDataChannel.OnOpen(func() {
|
||||||
|
msgSendLoop(offerDataChannel)
|
||||||
|
})
|
||||||
|
|
||||||
|
answerPeerConnection.OnDataChannel(func(answerDataChannel *webrtc.DataChannel) {
|
||||||
|
answerDataChannel.OnOpen(func() {
|
||||||
|
msgSendLoop(answerDataChannel)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
offer, err := offerPeerConnection.CreateOffer(nil)
|
||||||
|
panicIfError(err)
|
||||||
|
panicIfError(offerPeerConnection.SetLocalDescription(offer))
|
||||||
|
panicIfError(answerPeerConnection.SetRemoteDescription(offer))
|
||||||
|
|
||||||
|
answer, err := answerPeerConnection.CreateAnswer(nil)
|
||||||
|
panicIfError(err)
|
||||||
|
panicIfError(answerPeerConnection.SetLocalDescription(answer))
|
||||||
|
panicIfError(offerPeerConnection.SetRemoteDescription(answer))
|
||||||
|
|
||||||
|
select {}
|
||||||
|
}
|
||||||
|
|
||||||
|
func panicIfError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
@@ -96,6 +96,7 @@ func (g *ICEGatherer) createAgent() error {
|
|||||||
InterfaceFilter: g.api.settingEngine.candidates.InterfaceFilter,
|
InterfaceFilter: g.api.settingEngine.candidates.InterfaceFilter,
|
||||||
NAT1To1IPs: g.api.settingEngine.candidates.NAT1To1IPs,
|
NAT1To1IPs: g.api.settingEngine.candidates.NAT1To1IPs,
|
||||||
NAT1To1IPCandidateType: nat1To1CandiTyp,
|
NAT1To1IPCandidateType: nat1To1CandiTyp,
|
||||||
|
Net: g.api.settingEngine.vnet,
|
||||||
}
|
}
|
||||||
|
|
||||||
requestedNetworkTypes := g.api.settingEngine.candidates.ICENetworkTypes
|
requestedNetworkTypes := g.api.settingEngine.candidates.ICENetworkTypes
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/pion/ice"
|
"github.com/pion/ice"
|
||||||
"github.com/pion/logging"
|
"github.com/pion/logging"
|
||||||
|
"github.com/pion/transport/vnet"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SettingEngine allows influencing behavior in ways that are not
|
// SettingEngine allows influencing behavior in ways that are not
|
||||||
@@ -39,6 +40,7 @@ type SettingEngine struct {
|
|||||||
NAT1To1IPCandidateType ICECandidateType
|
NAT1To1IPCandidateType ICECandidateType
|
||||||
}
|
}
|
||||||
answeringDTLSRole DTLSRole
|
answeringDTLSRole DTLSRole
|
||||||
|
vnet *vnet.Net
|
||||||
LoggerFactory logging.LoggerFactory
|
LoggerFactory logging.LoggerFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,3 +165,12 @@ func (e *SettingEngine) SetAnsweringDTLSRole(role DTLSRole) error {
|
|||||||
e.answeringDTLSRole = role
|
e.answeringDTLSRole = role
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetVNet sets the VNet instance that is passed to pion/ice
|
||||||
|
//
|
||||||
|
// VNet is a virtual network layer for Pion, allowing users to simulate
|
||||||
|
// different topologies, latency, loss and jitter. This can be useful for
|
||||||
|
// learning WebRTC concepts or testing your application in a lab environment
|
||||||
|
func (e *SettingEngine) SetVNet(vnet *vnet.Net) {
|
||||||
|
e.vnet = vnet
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user