2020-10-04 19:01:06 +02:00
|
|
|
package diskwriter
|
2020-05-23 01:48:36 +02:00
|
|
|
|
|
|
|
import (
|
2020-09-18 10:23:53 +02:00
|
|
|
crand "crypto/rand"
|
2020-10-11 22:40:00 +02:00
|
|
|
"encoding/hex"
|
2020-05-23 01:48:36 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-23 21:51:25 +01:00
|
|
|
"log"
|
2024-04-30 18:17:00 +02:00
|
|
|
"net"
|
2020-05-23 01:48:36 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-12-23 23:13:22 +01:00
|
|
|
"runtime"
|
2020-12-04 01:15:52 +01:00
|
|
|
"strings"
|
2020-05-30 00:23:54 +02:00
|
|
|
"sync"
|
2020-05-23 01:48:36 +02:00
|
|
|
"time"
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
"github.com/at-wat/ebml-go/mkvcore"
|
2020-05-23 01:48:36 +02:00
|
|
|
"github.com/at-wat/ebml-go/webm"
|
|
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/pion/rtp/codecs"
|
2021-07-11 23:17:44 +02:00
|
|
|
"github.com/pion/webrtc/v3/pkg/media"
|
2021-07-11 20:18:44 +02:00
|
|
|
|
|
|
|
"github.com/jech/samplebuilder"
|
2020-09-13 11:04:16 +02:00
|
|
|
|
2021-07-29 23:01:22 +02:00
|
|
|
gcodecs "github.com/jech/galene/codecs"
|
2020-12-19 17:37:48 +01:00
|
|
|
"github.com/jech/galene/conn"
|
|
|
|
"github.com/jech/galene/group"
|
2022-04-06 21:42:24 +02:00
|
|
|
"github.com/jech/galene/rtptime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
audioMaxLate = 32
|
|
|
|
videoMaxLate = 256
|
2020-05-23 01:48:36 +02:00
|
|
|
)
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
var Directory string
|
|
|
|
|
|
|
|
type Client struct {
|
2020-09-13 11:56:35 +02:00
|
|
|
group *group.Group
|
2020-05-30 00:23:54 +02:00
|
|
|
id string
|
|
|
|
|
|
|
|
mu sync.Mutex
|
2020-06-10 19:43:08 +02:00
|
|
|
down map[string]*diskConn
|
2020-05-23 01:48:36 +02:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:14:13 +02:00
|
|
|
func newId() string {
|
2020-09-18 10:23:53 +02:00
|
|
|
b := make([]byte, 16)
|
|
|
|
crand.Read(b)
|
|
|
|
return hex.EncodeToString(b)
|
2020-09-13 14:14:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func New(g *group.Group) *Client {
|
|
|
|
return &Client{group: g, id: newId()}
|
2020-09-13 14:14:13 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func (client *Client) Group() *group.Group {
|
2020-05-23 01:48:36 +02:00
|
|
|
return client.group
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func (client *Client) Id() string {
|
2020-05-23 01:48:36 +02:00
|
|
|
return client.id
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:26:42 +01:00
|
|
|
func (client *Client) Username() string {
|
|
|
|
return "RECORDING"
|
|
|
|
}
|
|
|
|
|
2022-02-20 15:32:18 +01:00
|
|
|
func (client *Client) SetUsername(string) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func (client *Client) SetPermissions(perms []string) {
|
2020-09-13 10:16:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func (client *Client) Permissions() []string {
|
|
|
|
return []string{"system"}
|
2021-01-14 03:56:37 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:54:44 +01:00
|
|
|
func (client *Client) Data() map[string]interface{} {
|
2021-04-28 14:45:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:43:44 +01:00
|
|
|
func (client *Client) PushClient(group, kind, id, username string, perms []string, data map[string]interface{}) error {
|
2020-05-31 20:41:17 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 19:20:56 +02:00
|
|
|
func (client *Client) RequestConns(target group.Client, g *group.Group, id string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func (client *Client) Close() error {
|
2020-05-30 00:23:54 +02:00
|
|
|
client.mu.Lock()
|
|
|
|
defer client.mu.Unlock()
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
for _, down := range client.down {
|
|
|
|
down.Close()
|
|
|
|
}
|
|
|
|
client.down = nil
|
|
|
|
client.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
func (client *Client) Kick(id string, user *string, message string) error {
|
2020-09-12 14:00:14 +02:00
|
|
|
err := client.Close()
|
2020-09-13 11:56:35 +02:00
|
|
|
group.DelClient(client)
|
2020-09-12 14:00:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:17:00 +02:00
|
|
|
func (client *Client) Addr() net.Addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:41:00 +02:00
|
|
|
func (client *Client) Joined(group, kind string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:00:09 +01:00
|
|
|
func (client *Client) PushConn(g *group.Group, id string, up conn.Up, tracks []conn.UpTrack, replace string) error {
|
2020-12-05 00:07:34 +01:00
|
|
|
if client.group != g {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
client.mu.Lock()
|
|
|
|
defer client.mu.Unlock()
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
if client.closed {
|
|
|
|
return errors.New("disk client is closed")
|
|
|
|
}
|
|
|
|
|
2021-02-04 22:01:41 +01:00
|
|
|
if replace != "" {
|
|
|
|
rp := client.down[replace]
|
|
|
|
if rp != nil {
|
|
|
|
rp.Close()
|
|
|
|
delete(client.down, replace)
|
|
|
|
} else {
|
|
|
|
log.Printf("Disk writer: replacing unknown connection")
|
|
|
|
}
|
2021-01-31 19:00:09 +01:00
|
|
|
}
|
|
|
|
|
2020-06-10 19:43:08 +02:00
|
|
|
old := client.down[id]
|
|
|
|
if old != nil {
|
|
|
|
old.Close()
|
|
|
|
delete(client.down, id)
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
if up == nil {
|
2020-06-10 19:43:08 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
directory := filepath.Join(Directory, client.group.Name())
|
2020-05-23 01:48:36 +02:00
|
|
|
err := os.MkdirAll(directory, 0700)
|
|
|
|
if err != nil {
|
2020-12-23 21:51:25 +01:00
|
|
|
g.WallOps("Write to disk: " + err.Error())
|
2020-05-23 01:48:36 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:43:08 +02:00
|
|
|
if client.down == nil {
|
|
|
|
client.down = make(map[string]*diskConn)
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
down, err := newDiskConn(client, directory, up, tracks)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
2020-12-23 21:51:25 +01:00
|
|
|
g.WallOps("Write to disk: " + err.Error())
|
2020-05-23 01:48:36 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
client.down[up.Id()] = down
|
2020-05-23 01:48:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type diskConn struct {
|
2020-12-23 21:48:02 +01:00
|
|
|
client *Client
|
2020-05-30 00:23:54 +02:00
|
|
|
directory string
|
2021-01-03 12:04:39 +01:00
|
|
|
username string
|
2020-06-08 23:09:23 +02:00
|
|
|
hasVideo bool
|
2020-05-30 00:23:54 +02:00
|
|
|
|
|
|
|
mu sync.Mutex
|
2020-05-23 01:48:36 +02:00
|
|
|
file *os.File
|
2020-09-13 11:04:16 +02:00
|
|
|
remote conn.Up
|
2020-05-23 01:48:36 +02:00
|
|
|
tracks []*diskTrack
|
|
|
|
width, height uint32
|
2020-12-23 21:51:25 +01:00
|
|
|
lastWarning time.Time
|
2022-04-06 21:42:24 +02:00
|
|
|
originLocal time.Time
|
|
|
|
originRemote uint64
|
2020-12-23 21:51:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// called locked
|
|
|
|
func (conn *diskConn) warn(message string) {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(conn.lastWarning) < 10*time.Second {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println(message)
|
|
|
|
conn.client.group.WallOps(message)
|
|
|
|
conn.lastWarning = now
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
// called locked
|
2022-04-17 18:24:08 +02:00
|
|
|
func (conn *diskConn) open(extension string) error {
|
|
|
|
if conn.file != nil {
|
|
|
|
return errors.New("already open")
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
file, err := openDiskFile(conn.directory, conn.username, extension)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.file = file
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:24:08 +02:00
|
|
|
// called locked
|
|
|
|
func (conn *diskConn) close() []*diskTrack {
|
2022-04-06 21:42:24 +02:00
|
|
|
conn.originLocal = time.Time{}
|
|
|
|
conn.originRemote = 0
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
tracks := make([]*diskTrack, 0, len(conn.tracks))
|
2020-05-23 01:48:36 +02:00
|
|
|
for _, t := range conn.tracks {
|
2022-01-25 22:04:57 +01:00
|
|
|
t.writeBuffered(true)
|
2020-05-23 01:48:36 +02:00
|
|
|
if t.writer != nil {
|
|
|
|
t.writer.Close()
|
|
|
|
t.writer = nil
|
|
|
|
}
|
2022-04-06 21:42:24 +02:00
|
|
|
t.origin = none
|
2020-05-30 00:23:54 +02:00
|
|
|
tracks = append(tracks, t)
|
|
|
|
}
|
2022-04-17 18:24:08 +02:00
|
|
|
conn.file = nil
|
|
|
|
return tracks
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *diskConn) Close() error {
|
|
|
|
conn.remote.DelLocal(conn)
|
|
|
|
|
|
|
|
conn.mu.Lock()
|
|
|
|
tracks := conn.close()
|
2020-05-30 00:23:54 +02:00
|
|
|
conn.mu.Unlock()
|
|
|
|
|
|
|
|
for _, t := range tracks {
|
2020-09-13 11:04:16 +02:00
|
|
|
t.remote.DelLocal(t)
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
func openDiskFile(directory, username, extension string) (*os.File, error) {
|
2020-12-23 23:13:22 +01:00
|
|
|
filenameFormat := "2006-01-02T15:04:05.000"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
filenameFormat = "2006-01-02T15-04-05-000"
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := time.Now().Format(filenameFormat)
|
2021-01-03 12:04:39 +01:00
|
|
|
if username != "" {
|
|
|
|
filename = filename + "-" + username
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
for counter := 0; counter < 100; counter++ {
|
|
|
|
var fn string
|
|
|
|
if counter == 0 {
|
2021-07-29 23:44:24 +02:00
|
|
|
fn = fmt.Sprintf("%v.%v", filename, extension)
|
2020-05-23 01:48:36 +02:00
|
|
|
} else {
|
2021-07-29 23:44:24 +02:00
|
|
|
fn = fmt.Sprintf("%v-%02d.%v",
|
|
|
|
filename, counter, extension,
|
|
|
|
)
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn = filepath.Join(directory, fn)
|
|
|
|
f, err := os.OpenFile(
|
|
|
|
fn, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600,
|
|
|
|
)
|
|
|
|
if err == nil {
|
|
|
|
return f, nil
|
|
|
|
} else if !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("couldn't create file")
|
|
|
|
}
|
|
|
|
|
2021-07-11 22:44:50 +02:00
|
|
|
type maybeUint32 uint64
|
|
|
|
|
|
|
|
const none maybeUint32 = 0
|
|
|
|
|
|
|
|
func some(value uint32) maybeUint32 {
|
2021-07-11 23:09:19 +02:00
|
|
|
return maybeUint32(uint64(1<<32) | uint64(value))
|
2021-07-11 22:44:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func valid(m maybeUint32) bool {
|
|
|
|
return (m & (1 << 32)) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func value(m maybeUint32) uint32 {
|
|
|
|
return uint32(m)
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
type diskTrack struct {
|
2020-09-13 11:04:16 +02:00
|
|
|
remote conn.UpTrack
|
2020-05-30 00:23:54 +02:00
|
|
|
conn *diskConn
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
writer mkvcore.BlockWriteCloser
|
2021-07-11 23:09:19 +02:00
|
|
|
builder *samplebuilder.SampleBuilder
|
|
|
|
lastSeqno maybeUint32
|
2022-04-06 21:42:24 +02:00
|
|
|
|
|
|
|
origin maybeUint32
|
|
|
|
|
|
|
|
remoteNTP uint64
|
|
|
|
remoteRTP uint32
|
2020-10-11 22:40:00 +02:00
|
|
|
|
2021-05-17 03:10:00 +02:00
|
|
|
kfRequested time.Time
|
|
|
|
lastKf time.Time
|
|
|
|
savedKf *rtp.Packet
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
func newDiskConn(client *Client, directory string, up conn.Up, remoteTracks []conn.UpTrack) (*diskConn, error) {
|
2021-05-12 16:39:46 +02:00
|
|
|
var audio, video conn.UpTrack
|
|
|
|
|
|
|
|
for _, remote := range remoteTracks {
|
|
|
|
codec := remote.Codec().MimeType
|
|
|
|
if strings.EqualFold(codec, "audio/opus") {
|
|
|
|
if audio == nil {
|
|
|
|
audio = remote
|
|
|
|
} else {
|
|
|
|
client.group.WallOps("Multiple audio tracks, recording just one")
|
|
|
|
}
|
|
|
|
} else if strings.EqualFold(codec, "video/vp8") ||
|
2021-07-29 23:44:24 +02:00
|
|
|
strings.EqualFold(codec, "video/vp9") ||
|
|
|
|
strings.EqualFold(codec, "video/h264") {
|
2021-05-12 16:39:46 +02:00
|
|
|
if video == nil || video.Label() == "l" {
|
|
|
|
video = remote
|
|
|
|
} else if remote.Label() != "l" {
|
|
|
|
client.group.WallOps("Multiple video tracks, recording just one")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
client.group.WallOps("Unknown codec, " + codec + ", not recording")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if video == nil && audio == nil {
|
|
|
|
return nil, errors.New("no usable tracks found")
|
|
|
|
}
|
|
|
|
|
|
|
|
tracks := make([]conn.UpTrack, 0, 2)
|
|
|
|
if audio != nil {
|
|
|
|
tracks = append(tracks, audio)
|
|
|
|
}
|
|
|
|
if video != nil {
|
|
|
|
tracks = append(tracks, video)
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:04:39 +01:00
|
|
|
_, username := up.User()
|
2020-05-23 01:48:36 +02:00
|
|
|
conn := diskConn{
|
2020-12-23 21:48:02 +01:00
|
|
|
client: client,
|
2020-05-23 01:48:36 +02:00
|
|
|
directory: directory,
|
2021-01-03 12:04:39 +01:00
|
|
|
username: username,
|
2021-05-12 16:39:46 +02:00
|
|
|
tracks: make([]*diskTrack, 0, len(tracks)),
|
2020-05-23 01:48:36 +02:00
|
|
|
remote: up,
|
|
|
|
}
|
2021-07-11 21:01:10 +02:00
|
|
|
|
2021-05-12 16:39:46 +02:00
|
|
|
for _, remote := range tracks {
|
2020-05-23 01:48:36 +02:00
|
|
|
var builder *samplebuilder.SampleBuilder
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := remote.Codec()
|
2021-05-12 16:18:23 +02:00
|
|
|
if strings.EqualFold(codec.MimeType, "audio/opus") {
|
2020-10-12 15:20:34 +02:00
|
|
|
builder = samplebuilder.New(
|
2022-04-06 21:42:24 +02:00
|
|
|
audioMaxLate,
|
|
|
|
&codecs.OpusPacket{}, codec.ClockRate,
|
2020-10-12 15:20:34 +02:00
|
|
|
)
|
2021-05-12 16:18:23 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/vp8") {
|
2020-10-12 15:20:34 +02:00
|
|
|
builder = samplebuilder.New(
|
2022-04-06 21:42:24 +02:00
|
|
|
videoMaxLate,
|
|
|
|
&codecs.VP8Packet{}, codec.ClockRate,
|
2020-10-12 15:20:34 +02:00
|
|
|
)
|
2020-06-08 23:09:23 +02:00
|
|
|
conn.hasVideo = true
|
2021-05-12 16:18:23 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/vp9") {
|
2021-01-03 21:02:53 +01:00
|
|
|
builder = samplebuilder.New(
|
2022-04-06 21:42:24 +02:00
|
|
|
videoMaxLate, &codecs.VP9Packet{},
|
|
|
|
codec.ClockRate,
|
2021-01-03 21:02:53 +01:00
|
|
|
)
|
|
|
|
conn.hasVideo = true
|
2021-07-29 23:44:24 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/h264") {
|
|
|
|
builder = samplebuilder.New(
|
2022-04-06 21:42:24 +02:00
|
|
|
videoMaxLate, &codecs.H264Packet{},
|
|
|
|
codec.ClockRate,
|
2021-07-29 23:44:24 +02:00
|
|
|
)
|
|
|
|
conn.hasVideo = true
|
2021-05-12 16:18:23 +02:00
|
|
|
} else {
|
2021-05-12 16:39:46 +02:00
|
|
|
// this shouldn't happen
|
|
|
|
return nil, errors.New(
|
|
|
|
"cannot record codec " + codec.MimeType,
|
2020-12-25 21:04:17 +01:00
|
|
|
)
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
track := &diskTrack{
|
|
|
|
remote: remote,
|
|
|
|
builder: builder,
|
|
|
|
conn: &conn,
|
|
|
|
}
|
|
|
|
conn.tracks = append(conn.tracks, track)
|
|
|
|
}
|
|
|
|
|
2021-02-28 16:26:56 +01:00
|
|
|
// Only do this after all tracks have been added to conn, to avoid
|
|
|
|
// racing on hasVideo.
|
|
|
|
for _, t := range conn.tracks {
|
|
|
|
err := t.remote.AddLocal(t)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Couldn't add disk track: %v", err)
|
|
|
|
conn.warn("Couldn't add disk track: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 11:04:16 +02:00
|
|
|
err := up.AddLocal(&conn)
|
2020-05-30 03:36:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
|
|
|
|
return &conn, nil
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
func (t *diskTrack) SetCname(string) {
|
2020-09-03 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 23:48:17 +02:00
|
|
|
func (t *diskTrack) Write(buf []byte) (int, error) {
|
2020-05-30 00:23:54 +02:00
|
|
|
t.conn.mu.Lock()
|
|
|
|
defer t.conn.mu.Unlock()
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
if t.builder == nil {
|
2021-05-11 23:48:17 +02:00
|
|
|
return 0, nil
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 23:09:19 +02:00
|
|
|
// samplebuilder retains packets
|
2021-05-11 23:48:17 +02:00
|
|
|
data := make([]byte, len(buf))
|
|
|
|
copy(data, buf)
|
|
|
|
p := new(rtp.Packet)
|
|
|
|
err := p.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Diskwriter: %v", err)
|
|
|
|
return 0, nil
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2021-07-11 23:09:19 +02:00
|
|
|
if valid(t.lastSeqno) {
|
|
|
|
lastSeqno := uint16(value(t.lastSeqno))
|
|
|
|
if ((p.SequenceNumber - lastSeqno) & 0x8000) == 0 {
|
2021-11-28 13:58:07 +01:00
|
|
|
// jump forward
|
2021-07-11 23:09:19 +02:00
|
|
|
count := p.SequenceNumber - lastSeqno
|
2021-11-28 13:58:07 +01:00
|
|
|
if count < 256 {
|
|
|
|
for i := uint16(1); i < count; i++ {
|
2022-01-26 02:31:34 +01:00
|
|
|
fetch(t, lastSeqno+i)
|
2021-07-11 23:09:19 +02:00
|
|
|
}
|
2021-11-28 13:58:07 +01:00
|
|
|
} else {
|
|
|
|
requestKeyframe(t)
|
|
|
|
}
|
|
|
|
t.lastSeqno = some(uint32(p.SequenceNumber))
|
|
|
|
} else {
|
|
|
|
// jump backward
|
|
|
|
count := lastSeqno - p.SequenceNumber
|
|
|
|
if count >= 512 {
|
|
|
|
t.lastSeqno = none
|
|
|
|
requestKeyframe(t)
|
2021-07-11 23:09:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-28 13:58:07 +01:00
|
|
|
} else {
|
|
|
|
t.lastSeqno = some(uint32(p.SequenceNumber))
|
2021-07-11 23:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = t.writeRTP(p)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:31:34 +01:00
|
|
|
func fetch(t *diskTrack, seqno uint16) {
|
2021-11-28 13:58:07 +01:00
|
|
|
// since the samplebuilder retains packets, use a fresh buffer
|
|
|
|
buf := make([]byte, 1504)
|
2022-01-26 02:31:34 +01:00
|
|
|
n := t.remote.GetPacket(seqno, buf, false)
|
2021-11-28 13:58:07 +01:00
|
|
|
if n == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p := new(rtp.Packet)
|
|
|
|
err := p.Unmarshal(buf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.writeRTP(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func requestKeyframe(t *diskTrack) {
|
|
|
|
now := time.Now()
|
|
|
|
if now.Sub(t.kfRequested) > 500*time.Millisecond {
|
|
|
|
t.remote.RequestKeyframe()
|
|
|
|
t.kfRequested = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 02:31:34 +01:00
|
|
|
// writeRTP writes the packet without fetching lost packets
|
2021-07-11 23:09:19 +02:00
|
|
|
// Called locked.
|
|
|
|
func (t *diskTrack) writeRTP(p *rtp.Packet) error {
|
2021-07-29 23:01:22 +02:00
|
|
|
codec := t.remote.Codec().MimeType
|
|
|
|
if len(codec) > 6 && strings.EqualFold(codec[:6], "video/") {
|
|
|
|
kf, _ := gcodecs.Keyframe(codec, p)
|
|
|
|
if kf {
|
|
|
|
t.savedKf = p
|
2022-01-25 23:11:03 +01:00
|
|
|
t.lastKf = time.Now()
|
2022-04-06 21:42:24 +02:00
|
|
|
if !valid(t.origin) {
|
|
|
|
t.setOrigin(
|
|
|
|
p.Timestamp, time.Now(),
|
|
|
|
t.remote.Codec().ClockRate,
|
|
|
|
)
|
|
|
|
}
|
2022-01-25 23:11:03 +01:00
|
|
|
} else if time.Since(t.lastKf) > 4*time.Second {
|
|
|
|
requestKeyframe(t)
|
2021-01-03 21:02:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
if !valid(t.origin) {
|
|
|
|
if !t.conn.hasVideo || !t.conn.originLocal.Equal(time.Time{}) {
|
|
|
|
t.setOrigin(
|
|
|
|
p.Timestamp, time.Now(),
|
|
|
|
t.remote.Codec().ClockRate,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
t.builder.Push(p)
|
|
|
|
|
2021-07-11 23:17:44 +02:00
|
|
|
return t.writeBuffered(false)
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
// writeBuffered writes buffered samples to disk. If force is true, then
|
|
|
|
// samples will be flushed even if they are preceded by incomplete
|
2021-07-11 23:17:44 +02:00
|
|
|
// samples.
|
|
|
|
func (t *diskTrack) writeBuffered(force bool) error {
|
2021-07-29 23:01:22 +02:00
|
|
|
codec := t.remote.Codec().MimeType
|
2021-07-11 23:17:44 +02:00
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
for {
|
2021-07-11 23:17:44 +02:00
|
|
|
var sample *media.Sample
|
|
|
|
var ts uint32
|
|
|
|
if !force {
|
|
|
|
sample, ts = t.builder.PopWithTimestamp()
|
|
|
|
} else {
|
|
|
|
sample, ts = t.builder.ForcePopWithTimestamp()
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
if sample == nil {
|
2021-07-11 23:09:19 +02:00
|
|
|
return nil
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
if valid(t.origin) && int32(ts-value(t.origin)) < 0 {
|
|
|
|
if value(t.origin)-ts < 0x10000 {
|
|
|
|
// late packet before origin, drop
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// we've gone around 2^31 timestamps, force
|
|
|
|
// creating a new file to avoid wraparound
|
|
|
|
t.conn.close()
|
|
|
|
}
|
|
|
|
|
2021-07-29 23:01:22 +02:00
|
|
|
var keyframe bool
|
|
|
|
if len(codec) > 6 && strings.EqualFold(codec[:6], "video/") {
|
|
|
|
if t.savedKf == nil {
|
|
|
|
keyframe = false
|
|
|
|
} else {
|
|
|
|
keyframe = (ts == t.savedKf.Timestamp)
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
|
|
|
|
if keyframe {
|
2022-04-06 21:42:24 +02:00
|
|
|
w, h := gcodecs.KeyframeDimensions(
|
|
|
|
codec, t.savedKf,
|
2021-01-03 21:02:53 +01:00
|
|
|
)
|
2022-04-06 21:42:24 +02:00
|
|
|
err := t.conn.initWriter(w, h, t, ts)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
2020-12-23 21:51:25 +01:00
|
|
|
t.conn.warn(
|
|
|
|
"Write to disk " + err.Error(),
|
|
|
|
)
|
2021-07-11 23:09:19 +02:00
|
|
|
return err
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:18:23 +02:00
|
|
|
} else {
|
2022-04-16 01:18:34 +02:00
|
|
|
keyframe = true
|
2020-06-08 23:09:23 +02:00
|
|
|
if t.writer == nil {
|
|
|
|
if !t.conn.hasVideo {
|
2022-04-06 21:42:24 +02:00
|
|
|
err := t.conn.initWriter(0, 0, t, ts)
|
2020-06-08 23:09:23 +02:00
|
|
|
if err != nil {
|
2020-12-23 21:51:25 +01:00
|
|
|
t.conn.warn(
|
|
|
|
"Write to disk " +
|
|
|
|
err.Error(),
|
|
|
|
)
|
2021-07-11 23:09:19 +02:00
|
|
|
return err
|
2020-06-08 23:09:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
2020-06-08 23:09:23 +02:00
|
|
|
|
2021-05-17 03:10:00 +02:00
|
|
|
if t.writer == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-11 22:44:50 +02:00
|
|
|
if !valid(t.origin) {
|
2022-04-06 21:42:24 +02:00
|
|
|
log.Println("Invalid origin")
|
|
|
|
return nil
|
2020-06-08 23:09:23 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
tm := (ts - value(t.origin)) /
|
|
|
|
(t.remote.Codec().ClockRate / 1000)
|
2020-05-23 01:48:36 +02:00
|
|
|
_, err := t.writer.Write(keyframe, int64(tm), sample.Data)
|
|
|
|
if err != nil {
|
2021-07-11 23:09:19 +02:00
|
|
|
return err
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
// setOrigin sets the origin of track t after receiving a packet with
|
|
|
|
// timestamp ts at local time now.
|
|
|
|
// called locked
|
|
|
|
func (t *diskTrack) setOrigin(ts uint32, now time.Time, clockrate uint32) {
|
|
|
|
sub := func(a, b uint32, hz uint32) time.Duration {
|
|
|
|
return rtptime.ToDuration(int64(int32(a-b)), hz)
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.conn.originLocal.Equal(time.Time{}) {
|
|
|
|
t.origin = some(ts)
|
|
|
|
t.conn.originLocal = now
|
|
|
|
if t.remoteNTP != 0 {
|
|
|
|
remote := rtptime.NTPToTime(t.remoteNTP).Add(
|
|
|
|
sub(ts, t.remoteRTP, clockrate),
|
|
|
|
)
|
|
|
|
t.conn.originRemote = rtptime.TimeToNTP(remote)
|
|
|
|
} else {
|
|
|
|
t.conn.originRemote = 0
|
|
|
|
}
|
|
|
|
} else if t.conn.originRemote != 0 && t.remoteNTP != 0 {
|
|
|
|
remote := rtptime.NTPToTime(t.remoteNTP).Add(
|
|
|
|
sub(ts, t.remoteRTP, clockrate),
|
|
|
|
)
|
|
|
|
origin := rtptime.NTPToTime(t.conn.originRemote)
|
|
|
|
delta := rtptime.FromDuration(remote.Sub(origin), clockrate)
|
|
|
|
t.origin = some(ts - uint32(delta))
|
|
|
|
} else {
|
|
|
|
d := now.Sub(t.conn.originLocal)
|
|
|
|
delta := rtptime.FromDuration(d, clockrate)
|
|
|
|
t.origin = some(ts - uint32(delta))
|
|
|
|
if t.remoteNTP != 0 {
|
|
|
|
remote := rtptime.NTPToTime(t.remoteNTP).Add(
|
|
|
|
sub(ts, t.remoteRTP, clockrate),
|
|
|
|
)
|
|
|
|
t.conn.originRemote = rtptime.TimeToNTP(
|
|
|
|
remote.Add(-d),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTimeOffset adjusts the origin of track t given remote sync information.
|
|
|
|
func (t *diskTrack) SetTimeOffset(ntp uint64, rtp uint32) {
|
|
|
|
t.conn.mu.Lock()
|
|
|
|
defer t.conn.mu.Unlock()
|
|
|
|
t.setTimeOffset(ntp, rtp, t.remote.Codec().ClockRate)
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
// called locked
|
2022-04-06 21:42:24 +02:00
|
|
|
func (t *diskTrack) setTimeOffset(ntp uint64, rtp uint32, clockrate uint32) {
|
|
|
|
if valid(t.origin) {
|
|
|
|
local := rtptime.ToDuration(
|
|
|
|
int64(int32(rtp-value(t.origin))), clockrate,
|
|
|
|
)
|
|
|
|
if t.conn.originRemote == 0 {
|
|
|
|
t.conn.originRemote =
|
|
|
|
rtptime.TimeToNTP(
|
|
|
|
rtptime.NTPToTime(ntp).Add(-local))
|
|
|
|
} else {
|
|
|
|
remote := rtptime.NTPToTime(ntp).Sub(
|
|
|
|
rtptime.NTPToTime(t.conn.originRemote))
|
|
|
|
delta := rtptime.FromDuration(remote-local, clockrate)
|
|
|
|
t.origin = some(value(t.origin) - uint32(delta))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.remoteNTP = ntp
|
|
|
|
t.remoteRTP = rtp
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjustOrigin adjusts all origin-related fields of all tracks so that
|
|
|
|
// the origin of track t is equal to ts.
|
|
|
|
// Called locked.
|
|
|
|
func (t *diskTrack) adjustOrigin(ts uint32) {
|
|
|
|
if !valid(t.origin) || value(t.origin) == ts {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := rtptime.ToDuration(
|
|
|
|
int64(int32(ts-value(t.origin))), t.remote.Codec().ClockRate,
|
|
|
|
)
|
|
|
|
|
|
|
|
if !t.conn.originLocal.Equal(time.Time{}) {
|
|
|
|
t.conn.originLocal = t.conn.originLocal.Add(offset)
|
|
|
|
}
|
|
|
|
if t.conn.originRemote != 0 {
|
|
|
|
t.conn.originRemote =
|
|
|
|
rtptime.TimeToNTP(
|
|
|
|
rtptime.NTPToTime(
|
|
|
|
t.conn.originRemote,
|
|
|
|
).Add(offset),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range t.conn.tracks {
|
|
|
|
if valid(tt.origin) {
|
|
|
|
tt.origin = some(value(tt.origin) +
|
|
|
|
uint32(rtptime.FromDuration(
|
|
|
|
offset,
|
|
|
|
tt.remote.Codec().ClockRate,
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// called locked
|
|
|
|
func (conn *diskConn) initWriter(width, height uint32, track *diskTrack, ts uint32) error {
|
2022-04-17 18:24:08 +02:00
|
|
|
if conn.file != nil {
|
|
|
|
if width == conn.width && height == conn.height {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
conn.close()
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
2022-04-17 18:24:08 +02:00
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
isWebm := true
|
|
|
|
var desc []mkvcore.TrackDescription
|
2020-05-23 01:48:36 +02:00
|
|
|
for i, t := range conn.tracks {
|
|
|
|
var entry webm.TrackEntry
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := t.remote.Codec()
|
2021-05-12 16:18:23 +02:00
|
|
|
if strings.EqualFold(codec.MimeType, "audio/opus") {
|
2020-05-23 01:48:36 +02:00
|
|
|
entry = webm.TrackEntry{
|
|
|
|
Name: "Audio",
|
|
|
|
TrackNumber: uint64(i + 1),
|
|
|
|
CodecID: "A_OPUS",
|
|
|
|
TrackType: 2,
|
|
|
|
Audio: &webm.Audio{
|
|
|
|
SamplingFrequency: float64(codec.ClockRate),
|
|
|
|
Channels: uint64(codec.Channels),
|
|
|
|
},
|
|
|
|
}
|
2021-05-12 16:18:23 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/vp8") {
|
2020-05-23 01:48:36 +02:00
|
|
|
entry = webm.TrackEntry{
|
|
|
|
Name: "Video",
|
|
|
|
TrackNumber: uint64(i + 1),
|
|
|
|
CodecID: "V_VP8",
|
|
|
|
TrackType: 1,
|
|
|
|
Video: &webm.Video{
|
|
|
|
PixelWidth: uint64(width),
|
|
|
|
PixelHeight: uint64(height),
|
|
|
|
},
|
|
|
|
}
|
2021-05-12 16:18:23 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/vp9") {
|
2021-01-03 21:02:53 +01:00
|
|
|
entry = webm.TrackEntry{
|
|
|
|
Name: "Video",
|
|
|
|
TrackNumber: uint64(i + 1),
|
|
|
|
CodecID: "V_VP9",
|
|
|
|
TrackType: 1,
|
|
|
|
Video: &webm.Video{
|
|
|
|
PixelWidth: uint64(width),
|
|
|
|
PixelHeight: uint64(height),
|
|
|
|
},
|
|
|
|
}
|
2021-07-29 23:44:24 +02:00
|
|
|
} else if strings.EqualFold(codec.MimeType, "video/h264") {
|
|
|
|
entry = webm.TrackEntry{
|
|
|
|
Name: "Video",
|
|
|
|
TrackNumber: uint64(i + 1),
|
|
|
|
CodecID: "V_MPEG4/ISO/AVC",
|
|
|
|
TrackType: 1,
|
|
|
|
Video: &webm.Video{
|
|
|
|
PixelWidth: uint64(width),
|
|
|
|
PixelHeight: uint64(height),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
isWebm = false
|
2021-05-12 16:18:23 +02:00
|
|
|
} else {
|
2020-05-23 01:48:36 +02:00
|
|
|
return errors.New("unknown track type")
|
|
|
|
}
|
2021-07-29 23:44:24 +02:00
|
|
|
desc = append(desc,
|
|
|
|
mkvcore.TrackDescription{
|
|
|
|
TrackNumber: uint64(i + 1),
|
|
|
|
TrackEntry: entry,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension := "webm"
|
|
|
|
header := webm.DefaultEBMLHeader
|
|
|
|
if !isWebm {
|
|
|
|
extension = "mkv"
|
|
|
|
h := *header
|
|
|
|
h.DocType = "matroska"
|
|
|
|
header = &h
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-06 21:42:24 +02:00
|
|
|
if track != nil {
|
|
|
|
track.adjustOrigin(ts)
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:24:08 +02:00
|
|
|
err := conn.open(extension)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:38:09 +01:00
|
|
|
interceptor, err := mkvcore.NewMultiTrackBlockSorter(
|
|
|
|
// must be larger than the samplebuilder's MaxLate.
|
2022-04-06 21:42:24 +02:00
|
|
|
mkvcore.WithMaxDelayedPackets(videoMaxLate+16),
|
|
|
|
mkvcore.WithSortRule(mkvcore.BlockSorterWriteOutdated),
|
2022-03-21 23:38:09 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
conn.file.Close()
|
|
|
|
conn.file = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
ws, err := mkvcore.NewSimpleBlockWriter(
|
|
|
|
conn.file, desc,
|
|
|
|
mkvcore.WithEBMLHeader(header),
|
|
|
|
mkvcore.WithSegmentInfo(webm.DefaultSegmentInfo),
|
2022-03-21 23:38:09 +01:00
|
|
|
mkvcore.WithBlockInterceptor(interceptor),
|
2021-07-29 23:44:24 +02:00
|
|
|
)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
|
|
|
conn.file.Close()
|
|
|
|
conn.file = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-29 23:44:24 +02:00
|
|
|
if len(ws) != len(conn.tracks) {
|
2020-05-23 01:48:36 +02:00
|
|
|
conn.file.Close()
|
|
|
|
conn.file = nil
|
|
|
|
return errors.New("unexpected number of writers")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.width = width
|
|
|
|
conn.height = height
|
|
|
|
|
|
|
|
for i, t := range conn.tracks {
|
2021-07-29 23:44:24 +02:00
|
|
|
t.writer = ws[i]
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:23:07 +02:00
|
|
|
func (t *diskTrack) GetMaxBitrate() (uint64, int, int) {
|
|
|
|
return ^uint64(0), -1, -1
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|