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"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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"
|
|
|
|
|
|
|
|
"github.com/at-wat/ebml-go/webm"
|
|
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/pion/rtp/codecs"
|
2020-07-16 20:17:32 +02:00
|
|
|
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
|
2020-09-13 11:04:16 +02:00
|
|
|
|
2020-12-06 19:43:17 +01:00
|
|
|
"galene/conn"
|
|
|
|
"galene/group"
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Challenge(group string, cred group.ClientCredentials) bool {
|
|
|
|
return true
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-01 13:38:09 +02:00
|
|
|
func (client *Client) OverridePermissions(g *group.Group) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func (client *Client) SetPermissions(perms group.ClientPermissions) {
|
2020-09-13 10:16:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-13 14:12:00 +02:00
|
|
|
func (client *Client) PushClient(id, username string, add bool) error {
|
2020-05-31 20:41:17 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-30 16:26:11 +01:00
|
|
|
func (client *Client) Kick(id, user, 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
|
|
|
|
}
|
|
|
|
|
2020-12-05 00:07:34 +01:00
|
|
|
func (client *Client) PushConn(g *group.Group, id string, up conn.Up, tracks []conn.UpTrack, label string) error {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:43:08 +02:00
|
|
|
if client.down == nil {
|
|
|
|
client.down = make(map[string]*diskConn)
|
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
down, err := newDiskConn(directory, label, up, tracks)
|
2020-05-23 01:48:36 +02:00
|
|
|
if err != nil {
|
|
|
|
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-05-30 00:23:54 +02:00
|
|
|
directory string
|
|
|
|
label 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-05-30 00:23:54 +02:00
|
|
|
// called locked
|
2020-05-23 01:48:36 +02:00
|
|
|
func (conn *diskConn) reopen() error {
|
|
|
|
for _, t := range conn.tracks {
|
|
|
|
if t.writer != nil {
|
|
|
|
t.writer.Close()
|
|
|
|
t.writer = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.file = nil
|
|
|
|
|
|
|
|
file, err := openDiskFile(conn.directory, conn.label)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.file = file
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *diskConn) Close() error {
|
2020-09-13 11:04:16 +02:00
|
|
|
conn.remote.DelLocal(conn)
|
2020-05-23 01:48:36 +02:00
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
conn.mu.Lock()
|
|
|
|
tracks := make([]*diskTrack, 0, len(conn.tracks))
|
2020-05-23 01:48:36 +02:00
|
|
|
for _, t := range conn.tracks {
|
|
|
|
if t.writer != nil {
|
|
|
|
t.writer.Close()
|
|
|
|
t.writer = nil
|
|
|
|
}
|
2020-05-30 00:23:54 +02:00
|
|
|
tracks = append(tracks, t)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDiskFile(directory, label string) (*os.File, error) {
|
2020-06-08 23:09:23 +02:00
|
|
|
filename := time.Now().Format("2006-01-02T15:04:05.000")
|
2020-05-23 01:48:36 +02:00
|
|
|
if label != "" {
|
|
|
|
filename = filename + "-" + label
|
|
|
|
}
|
|
|
|
for counter := 0; counter < 100; counter++ {
|
|
|
|
var fn string
|
|
|
|
if counter == 0 {
|
|
|
|
fn = fmt.Sprintf("%v.webm", filename)
|
|
|
|
} else {
|
|
|
|
fn = fmt.Sprintf("%v-%02d.webm", filename, counter)
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
type diskTrack struct {
|
2020-09-13 11:04:16 +02:00
|
|
|
remote conn.UpTrack
|
2020-05-30 00:23:54 +02:00
|
|
|
conn *diskConn
|
|
|
|
|
2020-06-08 23:09:23 +02:00
|
|
|
writer webm.BlockWriteCloser
|
|
|
|
builder *samplebuilder.SampleBuilder
|
|
|
|
|
|
|
|
// bit 32 is a boolean indicating that the origin is valid
|
|
|
|
origin uint64
|
2020-10-11 22:40:00 +02:00
|
|
|
|
|
|
|
lastKf uint32
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
func newDiskConn(directory, label string, up conn.Up, remoteTracks []conn.UpTrack) (*diskConn, error) {
|
2020-05-23 01:48:36 +02:00
|
|
|
conn := diskConn{
|
|
|
|
directory: directory,
|
|
|
|
label: label,
|
|
|
|
tracks: make([]*diskTrack, 0, len(remoteTracks)),
|
|
|
|
remote: up,
|
|
|
|
}
|
|
|
|
for _, remote := range remoteTracks {
|
|
|
|
var builder *samplebuilder.SampleBuilder
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := remote.Codec()
|
|
|
|
switch strings.ToLower(codec.MimeType) {
|
|
|
|
case "audio/opus":
|
2020-10-12 15:20:34 +02:00
|
|
|
builder = samplebuilder.New(
|
2020-12-04 01:15:52 +01:00
|
|
|
16, &codecs.OpusPacket{}, codec.ClockRate,
|
2020-10-12 15:20:34 +02:00
|
|
|
samplebuilder.WithPartitionHeadChecker(
|
|
|
|
&codecs.OpusPartitionHeadChecker{},
|
|
|
|
),
|
|
|
|
)
|
2020-12-04 01:15:52 +01:00
|
|
|
case "video/vp8":
|
2020-06-08 23:09:23 +02:00
|
|
|
if conn.hasVideo {
|
2020-05-23 01:48:36 +02:00
|
|
|
return nil, errors.New("multiple video tracks not supported")
|
|
|
|
}
|
2020-10-12 15:20:34 +02:00
|
|
|
builder = samplebuilder.New(
|
2020-12-04 01:15:52 +01:00
|
|
|
128, &codecs.VP8Packet{}, codec.ClockRate,
|
2020-10-12 15:20:34 +02:00
|
|
|
samplebuilder.WithPartitionHeadChecker(
|
|
|
|
&codecs.VP8PartitionHeadChecker{},
|
|
|
|
),
|
|
|
|
)
|
2020-06-08 23:09:23 +02:00
|
|
|
conn.hasVideo = true
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
track := &diskTrack{
|
|
|
|
remote: remote,
|
|
|
|
builder: builder,
|
|
|
|
conn: &conn,
|
|
|
|
}
|
|
|
|
conn.tracks = append(conn.tracks, track)
|
2020-09-13 11:04:16 +02:00
|
|
|
remote.AddLocal(track)
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
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) SetTimeOffset(ntp uint64, rtp uint32) {
|
2020-06-08 23:54:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 11:04:16 +02:00
|
|
|
func (t *diskTrack) SetCname(string) {
|
2020-09-03 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
func clonePacket(packet *rtp.Packet) *rtp.Packet {
|
|
|
|
buf, err := packet.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var p rtp.Packet
|
|
|
|
err = p.Unmarshal(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *diskTrack) WriteRTP(packet *rtp.Packet) error {
|
2020-05-30 00:23:54 +02:00
|
|
|
// since we call initWriter, we take the connection lock for simplicity.
|
|
|
|
t.conn.mu.Lock()
|
|
|
|
defer t.conn.mu.Unlock()
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
if t.builder == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p := clonePacket(packet)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 22:40:00 +02:00
|
|
|
kfNeeded := false
|
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
t.builder.Push(p)
|
|
|
|
|
|
|
|
for {
|
2020-06-08 23:09:23 +02:00
|
|
|
sample, ts := t.builder.PopWithTimestamp()
|
2020-05-23 01:48:36 +02:00
|
|
|
if sample == nil {
|
2020-10-11 22:40:00 +02:00
|
|
|
if kfNeeded {
|
|
|
|
return conn.ErrKeyframeNeeded
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
keyframe := true
|
|
|
|
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := t.remote.Codec()
|
|
|
|
switch strings.ToLower(codec.MimeType) {
|
|
|
|
case "video/vp8":
|
2020-05-23 01:48:36 +02:00
|
|
|
if len(sample.Data) < 1 {
|
2020-10-11 22:40:00 +02:00
|
|
|
continue
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
keyframe = (sample.Data[0]&0x1 == 0)
|
|
|
|
if keyframe {
|
|
|
|
err := t.initWriter(sample.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-11 22:40:00 +02:00
|
|
|
t.lastKf = ts
|
|
|
|
} else if t.writer != nil {
|
|
|
|
// Request a keyframe every 10s
|
|
|
|
delta := ts - t.lastKf
|
|
|
|
if (delta&0x80000000) == 0 &&
|
|
|
|
delta > 10*90000 {
|
|
|
|
kfNeeded = true
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
2020-06-08 23:09:23 +02:00
|
|
|
default:
|
|
|
|
if t.writer == nil {
|
|
|
|
if !t.conn.hasVideo {
|
|
|
|
err := t.conn.initWriter(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
2020-06-08 23:09:23 +02:00
|
|
|
|
2020-05-23 01:48:36 +02:00
|
|
|
if t.writer == nil {
|
|
|
|
if !keyframe {
|
2020-09-13 11:04:16 +02:00
|
|
|
return conn.ErrKeyframeNeeded
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:09:23 +02:00
|
|
|
if t.origin == 0 {
|
|
|
|
t.origin = uint64(ts) | (1 << 32)
|
|
|
|
}
|
|
|
|
ts -= uint32(t.origin)
|
|
|
|
|
|
|
|
tm := ts / (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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
// called locked
|
2020-05-23 01:48:36 +02:00
|
|
|
func (t *diskTrack) initWriter(data []byte) error {
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := t.remote.Codec()
|
|
|
|
switch strings.ToLower(codec.MimeType) {
|
|
|
|
case "video/vp8":
|
2020-05-23 01:48:36 +02:00
|
|
|
if len(data) < 10 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
keyframe := (data[0]&0x1 == 0)
|
|
|
|
if !keyframe {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
raw := uint32(data[6]) | uint32(data[7])<<8 |
|
|
|
|
uint32(data[8])<<16 | uint32(data[9])<<24
|
|
|
|
width := raw & 0x3FFF
|
|
|
|
height := (raw >> 16) & 0x3FFF
|
|
|
|
return t.conn.initWriter(width, height)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:23:54 +02:00
|
|
|
// called locked
|
2020-05-23 01:48:36 +02:00
|
|
|
func (conn *diskConn) initWriter(width, height uint32) error {
|
|
|
|
if conn.file != nil && width == conn.width && height == conn.height {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var entries []webm.TrackEntry
|
|
|
|
for i, t := range conn.tracks {
|
|
|
|
var entry webm.TrackEntry
|
2020-12-04 01:15:52 +01:00
|
|
|
codec := t.remote.Codec()
|
|
|
|
switch strings.ToLower(codec.MimeType) {
|
|
|
|
case "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),
|
|
|
|
},
|
|
|
|
}
|
2020-12-04 01:15:52 +01:00
|
|
|
case "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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("unknown track type")
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.reopen()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writers, err := webm.NewSimpleBlockWriter(conn.file, entries)
|
|
|
|
if err != nil {
|
|
|
|
conn.file.Close()
|
|
|
|
conn.file = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(writers) != len(conn.tracks) {
|
|
|
|
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 {
|
|
|
|
t.writer = writers[i]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:39:16 +02:00
|
|
|
func (down *diskConn) GetMaxBitrate(now uint64) uint64 {
|
|
|
|
return ^uint64(0)
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 17:39:16 +02:00
|
|
|
func (t *diskTrack) Accumulate(bytes uint32) {
|
|
|
|
return
|
2020-05-23 01:48:36 +02:00
|
|
|
}
|