Add support mic mode for wyoming module

This commit is contained in:
Alex X
2025-04-22 11:49:08 +03:00
parent 6df1e68a5f
commit 3b7309d9f7
3 changed files with 57 additions and 8 deletions
+35
View File
@@ -0,0 +1,35 @@
package wyoming
import (
"fmt"
"net"
"github.com/AlexxIT/go2rtc/pkg/core"
)
func (s *Server) HandleMic(conn net.Conn) error {
defer conn.Close()
var closed core.Waiter
var timestamp int
api := NewAPI(conn)
mic := newMicConsumer(func(chunk []byte) {
data := fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, timestamp)
evt := &Event{Type: "audio-chunk", Data: []byte(data), Payload: chunk}
if err := api.WriteEvent(evt); err != nil {
closed.Done(nil)
}
timestamp += len(chunk) / 2
})
mic.RemoteAddr = api.conn.RemoteAddr().String()
if err := s.MicHandler(mic); err != nil {
return err
}
defer mic.Stop()
return closed.Wait()
}
+7 -3
View File
@@ -142,9 +142,6 @@ func (s *satellite) pause() {
s.state = stateUnavailable
if s.mic != nil {
if s.mic.onClose != nil {
s.mic.onClose()
}
_ = s.mic.Stop()
s.mic = nil
}
@@ -296,6 +293,13 @@ func (c *micConsumer) AddTrack(media *core.Media, codec *core.Codec, track *core
return nil
}
func (c *micConsumer) Stop() error {
if c.onClose != nil {
c.onClose()
}
return c.Connection.Stop()
}
type sndProducer struct {
core.Connection
data []byte