forked from Monibuca/plugin-record
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
52 lines (48 loc) · 1.1 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
package record
import (
"errors"
"io"
"os"
"path/filepath"
"time"
. "github.com/Monibuca/engine/v2"
"github.com/Monibuca/engine/v2/avformat"
)
type FlvFile struct {
Publisher
}
func PublishFlvFile(streamPath string) error {
if file, err := os.Open(filepath.Join(config.Path, streamPath+".flv")); err == nil {
stream := FlvFile{}
if stream.Publish(streamPath) {
stream.Type = "FlvFile"
defer stream.Close()
stream.UseTimestamp = true
file.Seek(int64(len(avformat.FLVHeader)), io.SeekStart)
var lastTime uint32
for {
if t, timestamp, payload, err := avformat.ReadFLVTag(file); err == nil {
switch t {
case avformat.FLV_TAG_TYPE_AUDIO:
stream.PushAudio(timestamp, payload)
case avformat.FLV_TAG_TYPE_VIDEO:
if timestamp != 0 {
if lastTime == 0 {
lastTime = timestamp
}
}
stream.PushVideo(timestamp, payload)
time.Sleep(time.Duration(timestamp-lastTime) * time.Millisecond)
lastTime = timestamp
}
} else {
return err
}
}
} else {
return errors.New("Bad Name")
}
} else {
return err
}
}