voiceactivedetector

package
v0.0.0-...-0cdedd5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: MIT Imports: 8 Imported by: 0

README

Voice Active Detector Interceptor

Voice Active Detector is a Pion Interceptor will allow you to detect any voice activity on the audio track that published to the client. It will provide you with the voice activity status and the audio level of the track.

How to use

  1. Import the package

    import "github.com/inlivedev/sfu/pkg/interceptors/voiceactivedetector"
    
  2. Register the interceptor extension in the media engine when creating a PeerConnection

    m := &webrtc.MediaEngine{}
    voiceactivedetector.RegisterAudioLevelHeaderExtension(m)
    
  3. Create a new VoiceActiveDetectorInterceptor

    var vad *voiceactivedetector.Interceptor
    
    i := &interceptor.Registry{}
    
    vadInterceptorFactory := voiceactivedetector.NewInterceptor(ctx)
    
    // enable voice detector
    vadInterceptorFactory.OnNew(func(i *voiceactivedetector.Interceptor) {
    	vad = i
    })
    
    i.Add(vadInterceptorFactory)
    
  4. Use the interceptor registry to create PeerConnection

    // Create a new RTCPeerConnection
    peerConnection, err := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithSettingEngine(settingEngine), webrtc.WithInterceptorRegistry(i)).NewPeerConnection(peerConnectionConfig)
    
  5. Use the voice active detector to detect voice activity on LocalStaticTrack

    // Create a new LocalStaticTrack
    localStaticTrack, _ := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "audio/opus"}, "audio", "pion")
    detector := vad.AddAudioTrack(localStaticTrack)
    detector.OnVoiceDetected(func(activity voiceactivedetector.VoiceActivity) {
    	// do something like sending it to client over datachanel or websocket
    
    })
    

Documentation

Index

Constants

View Source
const ATTRIBUTE_KEY = "audioLevel"

Variables

This section is empty.

Functions

func RegisterAudioLevelHeaderExtension

func RegisterAudioLevelHeaderExtension(m *webrtc.MediaEngine)

Types

type Config

type Config struct {
	HeadMargin time.Duration
	TailMargin time.Duration
	Threshold  uint8
}

func DefaultConfig

func DefaultConfig() Config

type Interceptor

type Interceptor struct {
	// contains filtered or unexported fields
}

func (*Interceptor) BindLocalStream

func (v *Interceptor) BindLocalStream(info *interceptor.StreamInfo, writer interceptor.RTPWriter) interceptor.RTPWriter

BindLocalStream lets you modify any outgoing RTP packets. It is called once for per LocalStream. The returned method will be called once per rtp packet.

func (*Interceptor) BindRTCPReader

func (v *Interceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.RTCPReader

BindRTCPReader lets you modify any incoming RTCP packets. It is called once per sender/receiver, however this might change in the future. The returned method will be called once per packet batch.

func (*Interceptor) BindRTCPWriter

func (v *Interceptor) BindRTCPWriter(writer interceptor.RTCPWriter) interceptor.RTCPWriter

BindRTCPWriter lets you modify any outgoing RTCP packets. It is called once per PeerConnection. The returned method will be called once per packet batch.

func (*Interceptor) BindRemoteStream

func (v *Interceptor) BindRemoteStream(info *interceptor.StreamInfo, reader interceptor.RTPReader) interceptor.RTPReader

BindRemoteStream lets you modify any incoming RTP packets. It is called once for per RemoteStream. The returned method will be called once per rtp packet.

func (*Interceptor) Close

func (v *Interceptor) Close() error

func (*Interceptor) MapAudioTrack

func (v *Interceptor) MapAudioTrack(ssrc uint32, t webrtc.TrackLocal) *VoiceDetector

AddAudioTrack adds audio track to interceptor

func (*Interceptor) OnNewVAD

func (v *Interceptor) OnNewVAD(callback func(vad *VoiceDetector))

func (*Interceptor) SetConfig

func (v *Interceptor) SetConfig(config Config)

func (*Interceptor) UnbindLocalStream

func (v *Interceptor) UnbindLocalStream(info *interceptor.StreamInfo)

UnbindLocalStream is called when the Stream is removed. It can be used to clean up any data related to that track.

func (*Interceptor) UnbindRemoteStream

func (v *Interceptor) UnbindRemoteStream(info *interceptor.StreamInfo)

type InterceptorFactory

type InterceptorFactory struct {
	// contains filtered or unexported fields
}

func NewInterceptor

func NewInterceptor(ctx context.Context) *InterceptorFactory

func (*InterceptorFactory) NewInterceptor

func (g *InterceptorFactory) NewInterceptor(_ string) (interceptor.Interceptor, error)

NewInterceptor constructs a new ReceiverInterceptor

func (*InterceptorFactory) OnNew

func (g *InterceptorFactory) OnNew(callback func(i *Interceptor))

type VoiceActivity

type VoiceActivity struct {
	TrackID     string            `json:"trackID"`
	StreamID    string            `json:"streamID"`
	SSRC        uint32            `json:"ssrc"`
	ClockRate   uint32            `json:"clockRate"`
	AudioLevels []VoicePacketData `json:"audioLevels"`
}

type VoiceDetector

type VoiceDetector struct {
	VoicePackets []VoicePacketData
	// contains filtered or unexported fields
}

func (*VoiceDetector) OnVoiceDetected

func (v *VoiceDetector) OnVoiceDetected(callback func(VoiceActivity))

func (*VoiceDetector) Stop

func (v *VoiceDetector) Stop()

func (*VoiceDetector) UpdateTrack

func (v *VoiceDetector) UpdateTrack(trackID, streamID string)

type VoicePacketData

type VoicePacketData struct {
	SequenceNo uint16 `json:"sequenceNo"`
	Timestamp  uint32 `json:"timestamp"`
	AudioLevel uint8  `json:"audioLevel"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL