mirror of
https://github.com/pion/webrtc.git
synced 2025-10-18 21:15:20 +08:00
Upgrade golangci-lint, more linters
Introduces new linters, upgrade golangci-lint to version (v1.63.4)
This commit is contained in:
@@ -30,22 +30,25 @@ type udpConn struct {
|
||||
payloadType uint8
|
||||
}
|
||||
|
||||
// nolint:gocognit
|
||||
func main() {
|
||||
func main() { //nolint:gocognit,cyclop,maintidx
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Create a MediaEngine object to configure the supported codec
|
||||
m := &webrtc.MediaEngine{}
|
||||
mediaEngine := &webrtc.MediaEngine{}
|
||||
|
||||
// Setup the codecs you want to use.
|
||||
// We'll use a VP8 and Opus but you can also define your own
|
||||
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
|
||||
if err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil,
|
||||
},
|
||||
}, webrtc.RTPCodecTypeVideo); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
|
||||
if err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{
|
||||
MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil,
|
||||
},
|
||||
}, webrtc.RTPCodecTypeAudio); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -54,7 +57,7 @@ func main() {
|
||||
// This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
|
||||
// this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
|
||||
// for each PeerConnection.
|
||||
i := &interceptor.Registry{}
|
||||
interceptorRegistry := &interceptor.Registry{}
|
||||
|
||||
// Register a intervalpli factory
|
||||
// This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
|
||||
@@ -64,15 +67,15 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
i.Add(intervalPliFactory)
|
||||
interceptorRegistry.Add(intervalPliFactory)
|
||||
|
||||
// Use the default set of Interceptors
|
||||
if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
|
||||
if err = webrtc.RegisterDefaultInterceptors(mediaEngine, interceptorRegistry); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create the API object with the MediaEngine
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(interceptorRegistry))
|
||||
|
||||
// Prepare the configuration
|
||||
config := webrtc.Configuration{
|
||||
@@ -114,22 +117,22 @@ func main() {
|
||||
"audio": {port: 4000, payloadType: 111},
|
||||
"video": {port: 4002, payloadType: 96},
|
||||
}
|
||||
for _, c := range udpConns {
|
||||
for _, conn := range udpConns {
|
||||
// Create remote addr
|
||||
var raddr *net.UDPAddr
|
||||
if raddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", c.port)); err != nil {
|
||||
if raddr, err = net.ResolveUDPAddr("udp", fmt.Sprintf("127.0.0.1:%d", conn.port)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Dial udp
|
||||
if c.conn, err = net.DialUDP("udp", laddr, raddr); err != nil {
|
||||
if conn.conn, err = net.DialUDP("udp", laddr, raddr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func(conn net.PacketConn) {
|
||||
if closeErr := conn.Close(); closeErr != nil {
|
||||
panic(closeErr)
|
||||
}
|
||||
}(c.conn)
|
||||
}(conn.conn)
|
||||
}
|
||||
|
||||
// Set a handler for when a new remote track starts, this handler will forward data to
|
||||
@@ -137,33 +140,33 @@ func main() {
|
||||
// In your application this is where you would handle/process audio/video
|
||||
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { //nolint: revive
|
||||
// Retrieve udp connection
|
||||
c, ok := udpConns[track.Kind().String()]
|
||||
conn, ok := udpConns[track.Kind().String()]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
b := make([]byte, 1500)
|
||||
buf := make([]byte, 1500)
|
||||
rtpPacket := &rtp.Packet{}
|
||||
for {
|
||||
// Read
|
||||
n, _, readErr := track.Read(b)
|
||||
n, _, readErr := track.Read(buf)
|
||||
if readErr != nil {
|
||||
panic(readErr)
|
||||
}
|
||||
|
||||
// Unmarshal the packet and update the PayloadType
|
||||
if err = rtpPacket.Unmarshal(b[:n]); err != nil {
|
||||
if err = rtpPacket.Unmarshal(buf[:n]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
rtpPacket.PayloadType = c.payloadType
|
||||
rtpPacket.PayloadType = conn.payloadType
|
||||
|
||||
// Marshal into original buffer with updated PayloadType
|
||||
if n, err = rtpPacket.MarshalTo(b); err != nil {
|
||||
if n, err = rtpPacket.MarshalTo(buf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Write
|
||||
if _, writeErr := c.conn.Write(b[:n]); writeErr != nil {
|
||||
if _, writeErr := conn.conn.Write(buf[:n]); writeErr != nil {
|
||||
// For this particular example, third party applications usually timeout after a short
|
||||
// amount of time during which the user doesn't have enough time to provide the answer
|
||||
// to the browser.
|
||||
@@ -191,18 +194,19 @@ func main() {
|
||||
|
||||
// Set the handler for Peer connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
|
||||
peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
fmt.Printf("Peer Connection State has changed: %s\n", state.String())
|
||||
|
||||
if s == webrtc.PeerConnectionStateFailed {
|
||||
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
|
||||
if state == webrtc.PeerConnectionStateFailed {
|
||||
// Wait until PeerConnection has had no network activity for 30 seconds or another failure.
|
||||
// It may be reconnected using an ICE Restart.
|
||||
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
|
||||
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
|
||||
fmt.Println("Done forwarding")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if s == webrtc.PeerConnectionStateClosed {
|
||||
if state == webrtc.PeerConnectionStateClosed {
|
||||
// PeerConnection was explicitly closed. This usually happens from a DTLS CloseNotify
|
||||
fmt.Println("Done forwarding")
|
||||
os.Exit(0)
|
||||
@@ -244,7 +248,7 @@ func main() {
|
||||
select {}
|
||||
}
|
||||
|
||||
// Read from stdin until we get a newline
|
||||
// Read from stdin until we get a newline.
|
||||
func readUntilNewline() (in string) {
|
||||
var err error
|
||||
|
||||
@@ -261,10 +265,11 @@ func readUntilNewline() (in string) {
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// JSON encode + base64 a SessionDescription
|
||||
// JSON encode + base64 a SessionDescription.
|
||||
func encode(obj *webrtc.SessionDescription) string {
|
||||
b, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
@@ -274,7 +279,7 @@ func encode(obj *webrtc.SessionDescription) string {
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Decode a base64 and unmarshal JSON into a SessionDescription
|
||||
// Decode a base64 and unmarshal JSON into a SessionDescription.
|
||||
func decode(in string, obj *webrtc.SessionDescription) {
|
||||
b, err := base64.StdEncoding.DecodeString(in)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user