Move MP4 mutex from HTTP to Muxer
This commit is contained in:
+1
-7
@@ -4,7 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/cmd/api"
|
"github.com/AlexxIT/go2rtc/cmd/api"
|
||||||
@@ -110,14 +109,9 @@ func handlerMP4(w http.ResponseWriter, r *http.Request) {
|
|||||||
Medias: core.ParseQuery(r.URL.Query()),
|
Medias: core.ParseQuery(r.URL.Query()),
|
||||||
}
|
}
|
||||||
|
|
||||||
var mu sync.Mutex
|
|
||||||
cons.Listen(func(msg any) {
|
cons.Listen(func(msg any) {
|
||||||
if data, ok := msg.([]byte); ok {
|
if data, ok := msg.([]byte); ok {
|
||||||
mu.Lock()
|
if _, err := w.Write(data); err != nil && exit != nil {
|
||||||
_, err := w.Write(data)
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
if err != nil && exit != nil {
|
|
||||||
select {
|
select {
|
||||||
case exit <- err:
|
case exit <- err:
|
||||||
default:
|
default:
|
||||||
|
|||||||
+11
-4
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/AlexxIT/go2rtc/pkg/h264"
|
"github.com/AlexxIT/go2rtc/pkg/h264"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/h265"
|
"github.com/AlexxIT/go2rtc/pkg/h265"
|
||||||
"github.com/pion/rtp"
|
"github.com/pion/rtp"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Consumer struct {
|
type Consumer struct {
|
||||||
@@ -19,6 +20,7 @@ type Consumer struct {
|
|||||||
senders []*core.Sender
|
senders []*core.Sender
|
||||||
|
|
||||||
muxer *Muxer
|
muxer *Muxer
|
||||||
|
mu sync.Mutex
|
||||||
wait byte
|
wait byte
|
||||||
|
|
||||||
send int
|
send int
|
||||||
@@ -70,10 +72,12 @@ func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiv
|
|||||||
c.wait = waitNone
|
c.wait = waitNone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// important to use Mutex because right fragment order
|
||||||
|
c.mu.Lock()
|
||||||
buf := c.muxer.Marshal(trackID, packet)
|
buf := c.muxer.Marshal(trackID, packet)
|
||||||
c.Fire(buf)
|
c.Fire(buf)
|
||||||
|
|
||||||
c.send += len(buf)
|
c.send += len(buf)
|
||||||
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if track.Codec.IsRTP() {
|
if track.Codec.IsRTP() {
|
||||||
@@ -97,10 +101,11 @@ func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiv
|
|||||||
c.wait = waitNone
|
c.wait = waitNone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
buf := c.muxer.Marshal(trackID, packet)
|
buf := c.muxer.Marshal(trackID, packet)
|
||||||
c.Fire(buf)
|
c.Fire(buf)
|
||||||
|
|
||||||
c.send += len(buf)
|
c.send += len(buf)
|
||||||
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if track.Codec.IsRTP() {
|
if track.Codec.IsRTP() {
|
||||||
@@ -113,10 +118,11 @@ func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiv
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
buf := c.muxer.Marshal(trackID, packet)
|
buf := c.muxer.Marshal(trackID, packet)
|
||||||
c.Fire(buf)
|
c.Fire(buf)
|
||||||
|
|
||||||
c.send += len(buf)
|
c.send += len(buf)
|
||||||
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if track.Codec.IsRTP() {
|
if track.Codec.IsRTP() {
|
||||||
@@ -129,10 +135,11 @@ func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiv
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.mu.Lock()
|
||||||
buf := c.muxer.Marshal(trackID, packet)
|
buf := c.muxer.Marshal(trackID, packet)
|
||||||
c.Fire(buf)
|
c.Fire(buf)
|
||||||
|
|
||||||
c.send += len(buf)
|
c.send += len(buf)
|
||||||
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user