forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
57 lines (53 loc) · 1.29 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package record
import (
"errors"
"io"
"os"
"path/filepath"
"time"
. "github.com/Monibuca/engine/v3"
. "github.com/Monibuca/utils/v3"
"github.com/Monibuca/utils/v3/codec"
)
type FlvFile struct {
Publisher
}
func PublishFlvFile(streamPath string) error {
flvPath := filepath.Join(config.Path, streamPath+".flv")
os.MkdirAll(filepath.Dir(flvPath), 0755)
if file, err := os.Open(flvPath); err == nil {
var stream FlvFile
if stream.Publish(streamPath) {
stream.Type = "FlvFile"
defer stream.Close()
file.Seek(int64(len(codec.FLVHeader)), io.SeekStart)
var lastTime uint32
at := NewAudioTrack()
vt := NewVideoTrack()
stream.SetOriginAT(at)
for {
if t, timestamp, payload, err := codec.ReadFLVTag(file); err == nil {
switch t {
case codec.FLV_TAG_TYPE_AUDIO:
at.Push(timestamp, payload)
case codec.FLV_TAG_TYPE_VIDEO:
if timestamp != 0 {
if lastTime == 0 {
lastTime = timestamp
}
}
vt.Push(VideoPack{Timestamp: timestamp, CompositionTime: BigEndian.Uint24(payload[2:5]), Payload: payload[5:]})
time.Sleep(time.Duration(timestamp-lastTime) * time.Millisecond)
lastTime = timestamp
}
} else {
return err
}
}
} else {
return errors.New("Bad Name")
}
} else {
return err
}
}