MCPcopy
hub / github.com/pion/webrtc / CreateDataChannel

Method CreateDataChannel

peerconnection.go:2380–2460  ·  view source on GitHub ↗

CreateDataChannel creates a new DataChannel object with the given label and optional DataChannelInit used to configure properties of the underlying channel such as data reliability. nolint:cyclop

(label string, options *DataChannelInit)

Source from the content-addressed store, hash-verified

2378//
2379//nolint:cyclop
2380func (pc *PeerConnection) CreateDataChannel(label string, options *DataChannelInit) (*DataChannel, error) {
2381 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #2)
2382 if pc.isClosed.Load() {
2383 return nil, &rtcerr.InvalidStateError{Err: ErrConnectionClosed}
2384 }
2385
2386 params := &DataChannelParameters{
2387 Label: label,
2388 Ordered: true,
2389 }
2390
2391 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #19)
2392 if options != nil {
2393 params.ID = options.ID
2394 }
2395
2396 if options != nil { //nolint:nestif
2397 // Ordered indicates if data is allowed to be delivered out of order. The
2398 // default value of true, guarantees that data will be delivered in order.
2399 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #9)
2400 if options.Ordered != nil {
2401 params.Ordered = *options.Ordered
2402 }
2403
2404 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #7)
2405 if options.MaxPacketLifeTime != nil {
2406 params.MaxPacketLifeTime = options.MaxPacketLifeTime
2407 }
2408
2409 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #8)
2410 if options.MaxRetransmits != nil {
2411 params.MaxRetransmits = options.MaxRetransmits
2412 }
2413
2414 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #10)
2415 if options.Protocol != nil {
2416 params.Protocol = *options.Protocol
2417 }
2418
2419 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #11)
2420 if len(params.Protocol) > 65535 {
2421 return nil, &rtcerr.TypeError{Err: ErrProtocolTooLarge}
2422 }
2423
2424 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #12)
2425 if options.Negotiated != nil {
2426 params.Negotiated = *options.Negotiated
2427 }
2428 }
2429
2430 dataChannel, err := pc.api.newDataChannel(params, nil, pc.log)
2431 if err != nil {
2432 return nil, err
2433 }
2434
2435 // https://w3c.github.io/webrtc-pc/#peer-to-peer-data-api (Step #16)
2436 if dataChannel.maxPacketLifeTime != nil && dataChannel.maxRetransmits != nil {
2437 return nil, &rtcerr.TypeError{Err: ErrRetransmitsOrPacketLifeTime}

Calls 5

onNegotiationNeededMethod · 0.95
newDataChannelMethod · 0.80
openMethod · 0.80
IDMethod · 0.65
StateMethod · 0.45