sdp交换存在问题

This commit is contained in:
TonyChyi 2022-09-30 12:24:32 +08:00
parent 5c93e698b6
commit acf74153ef
4 changed files with 69 additions and 49 deletions

View File

@ -71,7 +71,6 @@ func (w *PCMStreamDriver) AudioRecord(p prop.Media) (audio.Reader, error) {
case <-w.closed: case <-w.closed:
return nil, func() {}, io.EOF return nil, func() {}, io.EOF
case pcmData, ok := <-w.PCM: case pcmData, ok := <-w.PCM:
logrus.Debug("got %d bytes pcm data", len(pcmData))
if !ok { if !ok {
return nil, func() {}, io.ErrClosedPipe return nil, func() {}, io.ErrClosedPipe
} }
@ -85,7 +84,7 @@ func (w *PCMStreamDriver) AudioRecord(p prop.Media) (audio.Reader, error) {
func bytesTo16BitSamples(b []byte) []int16 { func bytesTo16BitSamples(b []byte) []int16 {
samples := make([]int16, 0) samples := make([]int16, 0)
for i := 0; i < len(b); i += 2 { for i := 0; i < len(b); i += 2 {
sample := binary.LittleEndian.Uint16(b[i : i+1]) sample := binary.LittleEndian.Uint16(b[i : i+2])
samples = append(samples, int16(sample)) samples = append(samples, int16(sample))
} }
return samples return samples

View File

@ -52,6 +52,7 @@ func (v *PPMStreamDriver) Properties() []prop.Media {
} }
func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) { func (v *PPMStreamDriver) VideoRecord(p prop.Media) (video.Reader, error) {
logrus.Debug(p)
r := video.ReaderFunc(func() (img image.Image, release func(), err error) { r := video.ReaderFunc(func() (img image.Image, release func(), err error) {
canvas := image.NewYCbCr( canvas := image.NewYCbCr(
image.Rect(0, 0, p.Width, p.Height), image.Rect(0, 0, p.Width, p.Height),

View File

@ -77,13 +77,6 @@ func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDe
rtc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) { rtc.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
logrus.Debug("connection state has changed: ", connectionState.String()) logrus.Debug("connection state has changed: ", connectionState.String())
switch connectionState {
case webrtc.ICEConnectionStateFailed:
fallthrough
case webrtc.ICEConnectionStateClosed:
rtc.Close()
}
}) })
for _, track := range c.stream.GetTracks() { for _, track := range c.stream.GetTracks() {
@ -110,14 +103,12 @@ func (c *Connection) Regist(offer *webrtc.SessionDescription) (*webrtc.SessionDe
return nil, err return nil, err
} }
gatherComplete := webrtc.GatheringCompletePromise(rtc)
if err := rtc.SetLocalDescription(answer); err != nil { if err := rtc.SetLocalDescription(answer); err != nil {
return nil, err return nil, err
} }
logrus.Debug("answer set") logrus.Debug("answer set")
<-gatherComplete <-webrtc.GatheringCompletePromise(rtc)
defer logrus.Debug("regist complete") defer logrus.Debug("regist complete")
return rtc.LocalDescription(), nil return rtc.LocalDescription(), nil

View File

@ -2,32 +2,35 @@ package qemuserver
import ( import (
"fmt" "fmt"
"io"
"net/url" "net/url"
"os" "os"
"time" "time"
"git.sense-t.eu.org/ACE/ace/drivers/audio"
"git.sense-t.eu.org/ACE/ace/drivers/video"
"git.sense-t.eu.org/ACE/ace/lib/audiodriver" "git.sense-t.eu.org/ACE/ace/lib/audiodriver"
"git.sense-t.eu.org/ACE/ace/lib/qemuconnection" "git.sense-t.eu.org/ACE/ace/lib/qemuconnection"
"github.com/digitalocean/go-qemu/qemu" "github.com/digitalocean/go-qemu/qemu"
"github.com/digitalocean/go-qemu/qmp" "github.com/digitalocean/go-qemu/qmp"
"github.com/pion/mediadevices/pkg/driver" "github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/driver/vncdriver"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
const waveHeaderSize = audiodriver.FmtHeaderSizeDefault const waveHeaderSize = audio.FmtHeaderSizeDefault
var waveHeader *audiodriver.WavHeader var waveHeader *audio.WavHeader
func init() { func init() {
waveHeader = audiodriver.DefaultHeader() waveHeader = audio.DefaultHeader()
} }
type Server struct { type Server struct {
options *Options options *Options
qemu *qemu.Domain qemu *qemu.Domain
audioHeader chan *audiodriver.WavHeader audioHeader chan *audio.WavHeader
pcm chan [audiodriver.BufferSize]byte pcm chan []byte
ppm chan io.ReadCloser
} }
var DefaultServer *Server var DefaultServer *Server
@ -39,8 +42,9 @@ func NewServer(o *Options) (*Server, error) {
server := &Server{ server := &Server{
options: o, options: o,
audioHeader: make(chan *audiodriver.WavHeader, 1), audioHeader: make(chan *audio.WavHeader, 1),
pcm: make(chan [audiodriver.BufferSize]byte), pcm: make(chan []byte),
ppm: make(chan io.ReadCloser),
} }
u, err := url.Parse(o.QmpAddress) u, err := url.Parse(o.QmpAddress)
@ -70,7 +74,11 @@ func NewServer(o *Options) (*Server, error) {
} }
server.qemu = qemu server.qemu = qemu
audio := audiodriver.New() audio := &audio.PCMStreamDriver{
PCM: server.pcm,
WaveHeader: waveHeader,
BufferSizeByBytes: 2048, // to be configured
}
if err := driver.GetManager().Register( if err := driver.GetManager().Register(
audio, audio,
driver.Info{ driver.Info{
@ -81,11 +89,15 @@ func NewServer(o *Options) (*Server, error) {
); err != nil { ); err != nil {
return nil, err return nil, err
} }
audio.PCM = server.pcm
audio.WaveHeader = waveHeader
video := &video.PPMStreamDriver{
Height: 768,
Width: 1024,
FPS: 60,
PPMImage: server.ppm,
}
if err := driver.GetManager().Register( if err := driver.GetManager().Register(
vncdriver.NewVnc(o.VNCAddress), video,
driver.Info{ driver.Info{
Label: "vnc", Label: "vnc",
DeviceType: driver.Camera, DeviceType: driver.Camera,
@ -101,6 +113,7 @@ func NewServer(o *Options) (*Server, error) {
func (s *Server) Run() error { func (s *Server) Run() error {
logrus.Debug("qemu server running") logrus.Debug("qemu server running")
go func() {
f, err := os.Open(s.options.AudioPipe) f, err := os.Open(s.options.AudioPipe)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
@ -108,6 +121,28 @@ func (s *Server) Run() error {
defer f.Close() defer f.Close()
logrus.Debug("start reading fifo") logrus.Debug("start reading fifo")
logrus.Debug("skip wave headers, to the PCM!")
// skip to pcm data, for 44 bytes.
var _dataChunkHeader [audiodriver.FmtHeaderOffset +
audiodriver.FmtHeaderIDSize + audiodriver.FmtHeaderChunkSizeSize + waveHeaderSize +
audiodriver.DataChunkIDSize + audiodriver.DataChunkSizeSize]byte
if _, err := f.Read(_dataChunkHeader[:]); err != nil {
logrus.Fatal(err)
}
defer close(s.pcm)
for {
b := make([]byte, 2048) // to be configured
if _, err := f.Read(b[:]); err != nil {
logrus.Error(err)
}
select {
case s.pcm <- b:
case <-time.After(waveHeader.GetLatnecy(2048)):
}
}
}()
go func() { go func() {
logrus.Debug("setting audio capture") logrus.Debug("setting audio capture")
if _, err := s.qemu.Run(qmp.Command{ if _, err := s.qemu.Run(qmp.Command{
@ -128,26 +163,20 @@ func (s *Server) Run() error {
logrus.Debug("audio capture set") logrus.Debug("audio capture set")
}() }()
logrus.Debug("skip wave headers, to the PCM!") go func() {
// skip to pcm data, for 44 bytes. logrus.Debug("screen capture start")
var _dataChunkHeader [audiodriver.FmtHeaderOffset + defer close(s.ppm)
audiodriver.FmtHeaderIDSize + audiodriver.FmtHeaderChunkSizeSize + waveHeaderSize + for range time.Tick(time.Second / 60) { // to be configured
audiodriver.DataChunkIDSize + audiodriver.DataChunkSizeSize]byte ppm, err := s.qemu.ScreenDump()
if _, err := f.Read(_dataChunkHeader[:]); err != nil { if err != nil {
logrus.Fatal(err)
}
defer close(s.pcm)
for {
var b [audiodriver.BufferSize]byte
if _, err := f.Read(b[:]); err != nil {
logrus.Error(err) logrus.Error(err)
continue
} }
select { s.ppm <- ppm
case s.pcm <- b:
case <-time.After(waveHeader.GetLatnecy()):
}
} }
}()
select {}
} }
func (s *Server) SendEvent(b []byte) error { func (s *Server) SendEvent(b []byte) error {