forked from jamessanford/rtmp-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalize.go
More file actions
98 lines (84 loc) · 2.12 KB
/
finalize.go
File metadata and controls
98 lines (84 loc) · 2.12 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/golang/glog"
)
// Collate interesting commands from RTMP messages within a single TCP stream.
// MessageFinalizer receives all decoded data for all messages within a single TCP stream.
type MessageFinalizer struct {
sync.RWMutex
r chan string
complete bool
connect map[string]interface{} // options to "connect"
play string // URL given to play command
// TODO: retain and display optional args
}
// input: 'some/long/path?optional&args'
// output: 'path'
func (c *MessageFinalizer) makeFilename() string {
f := filepath.Base(filepath.Clean(c.play))
return strings.SplitN(f, "?", 2)[0]
}
func addFlag(s string, v string) string {
return fmt.Sprintf(" -%v '%v'", s, v)
}
// RTMPDumpCommand combines parsed message data into an "rtmpdump" command.
func (c *MessageFinalizer) RTMPDumpCommand() {
if !c.complete {
return
}
cmd := "rtmpdump"
if tc := c.connect["tcUrl"]; tc != nil {
cmd += fmt.Sprintf(" -r '%v'", tc)
}
cmd += addFlag("y", c.play)
var keys []string
for k := range c.connect {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
s := fmt.Sprintf("%v", c.connect[k])
if k == "app" {
cmd += addFlag("a", s)
} else if k == "tcUrl" {
cmd += addFlag("t", s)
} else if k == "pageUrl" {
cmd += addFlag("p", s)
} else if k == "flashVer" {
cmd += addFlag("f", s)
}
}
cmd += fmt.Sprintf(" -R -o '%s'", c.makeFilename())
c.r <- cmd
}
func (c *MessageFinalizer) add(d []interface{}) {
if len(d) == 0 {
return
}
glog.V(2).Infof("ADD %v", d)
// Multiple goroutines may be sending us processed messages.
c.Lock()
defer c.Unlock()
if cmd, ok := d[0].(string); ok {
if len(d) >= 2 {
if cmd == "connect" {
c.connect = d[2].(map[string]interface{})
} else if cmd == "play" {
c.play = d[2].(string)
}
}
}
if !c.complete && c.connect != nil && len(c.play) > 0 {
// Show command as soon as we have enough information.
c.complete = true
c.RTMPDumpCommand()
}
}
func (c *MessageFinalizer) exit() {
// This is called as the TCP stream goes away.
}