8000 Merge remote-tracking branch 'origin/master' · invisiblecoder99/livego@e0f4213 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0f4213

Browse files
author
吴浩麟
committed
Merge remote-tracking branch 'origin/master'
2 parents 6f30580 + 21175a2 commit e0f4213

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+846
-647
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Created by .ignore support plugin (hsz.mobi)
22
.idea
33
dist
4-
room_keys.json
54
.vscode
6-
.tmp
7-
vendor
5+
tmp
6+
vendor
7+
livego

CHANGELOG.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- JSON Web Token support.
1111
``` json
12-
// .livego.json
12+
// livego.json
1313
{
1414
"jwt": {
1515
"secret": "testing",
16-
"algorithm": "HS256s"
16+
"algorithm": "HS256"
1717
},
1818
"server": [
1919
{
2020
"appname": "live",
21-
"liveon": "on",
22-
"hlson": "on"
21+
"live": true,
22+
"hls": true
2323
}
2424
]
2525
}
2626
```
2727
- Use redis for store room keys
2828
``` json
29-
// .livego.json
29+
// livego.json
3030
{
3131
"redis_addr": "localhost:6379",
3232
"server": [
3333
{
3434
"appname": "live",
35-
"liveon": "on",
36-
"hlson": "on"
35+
"live": true,
36+
"hls": true
3737
}
3838
]
3939
}
4040
```
41+
- Makefile
4142

4243
### Changed
4344
- Show `players`.
44-
- Show `stream_id`.
45+
- Show `stream_id`.
46+
- Deleted keys saved in physical file, now the keys are in cached using `go-cache` by default.
47+
- Using `logrus` like log system.
48+
- Using method `.Get(queryParamName)` to get an url query param.
49+
- Replaced `errors.New(...)` to `fmt.Errorf(...)`.
50+
- Replaced types string on config params `liveon` and `hlson` to booleans `live: true/false` and `hls: true/false`
51+
- Using viper for config, allow use file, cloud providers, environment vars or flags.
52+
- Using yaml config by default.

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
FROM golang:latest as builder
22
WORKDIR /app
3+
ENV GOPROXY https://goproxy.io
34
COPY go.mod go.sum ./
45
RUN go mod download
56
COPY . .

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
GOCMD ?= go
2+
GOBUILD = $(GOCMD) build
3+
GOCLEAN = $(GOCMD) clean
4+
GOTEST = $(GOCMD) test
5+
GOGET = $(GOCMD) get
6+
BINARY_NAME = livego
7+
BINARY_UNIX = $(BINARY_NAME)_unix
8+
9+
DOCKER_ACC ?= gwuhaolin
10+
DOCKER_REPO ?= livego
11+
12+
TAG ?= $(shell git describe --tags --abbrev=0 2>/dev/null)
13+
14+
default: all
15+
16+
all: test build dockerize
17+
build:
18+
$(GOBUILD) -o $(BINARY_NAME) -v -ldflags="-X main.VERSION=$(TAG)"
19+
20+
test:
21+
$(GOTEST) -v ./...
22+
23+
clean:
24+
$(GOCLEAN)
25+
rm -f $(BINARY_NAME)
26+
rm -f $(BINARY_UNIX)
27+
28+
run: build
29+
./$(BINARY_NAME)
30+
31+
build-linux:
32+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_UNIX) -v
33+
34+
dockerize:
35+
docker build -t $(DOCKER_ACC)/$(DOCKER_REPO):$(TAG) .
36+
docker push $(DOCKER_ACC)/$(DOCKER_REPO):$(TAG)

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Simple and efficient live broadcast server:
2020
#### Supported encoding formats
2121
- H264
2222
- AAC
23-
- sMP3
23+
- MP3
2424

2525
## Installation
2626
After directly downloading the compiled [binary file](https://github.com/gwuhaolin/livego/releases), execute it on the command line.
@@ -30,12 +30,27 @@ Run `docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gwuhaoli
3030

3131
#### Compile from source
3232
1. Download the source code `git clone https://github.com/gwuhaolin/livego.git`
33-
2. Go to the livego directory and execute `go build`
33+
2. Go to the livego directory and execute `go build` or `make build`
3434

3535
## Use
36-
2. Start the service: execute the livego binary file to start the livego service;
37-
3. Upstream push: Push the video stream to `rtmp://localhost:1935/live/movie` through the` RTMP` protocol, for example, use `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost:1935/live/movie` push;
38-
4. Downstream playback: The following three playback protocols are supported, and the playback address is as follows:
36+
```bash
37+
./livego -h
38+
Usage of ./livego:
39+
--api_addr string HTTP manage interface server listen address (default ":8090")
40+
--config_file string configure filename (default "livego.yaml")
41+
--flv_dir string output flv file at flvDir/APP/KEY_TIME.flv (default "tmp")
42+
--gop_num int gop num (default 1)
43+
--hls_addr string HLS server listen address (default ":7002")
44+
--httpflv_addr string HTTP-FLV server listen address (default ":7001")
45+
--level string Log level (default "info")
46+
--read_timeout int read time out (default 10)
47+
--rtmp_addr string RTMP server listen address (default ":1935")
48+
--write_timeout int write time out (default 10)
49+
```
50+
2. Start the service: execute the livego binary file or `make run` to start the livego service;
51+
3. Get a channelkey `curl http://localhost:8090/control/get?room=movie` and copy data like your channelkey.
52+
4. Upstream push: Push the video stream to `rtmp://localhost:1935/live/movie`(`rtmp://localhost:1935/{appname}/{channelkey}`) through the` RTMP` protocol, for example, use `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost:1935/live/movie` push;
53+
5. Downstream playback: The following three playback protocols are supported, and the playback address is as follows:
3954
-`RTMP`:`rtmp://localhost:1935/live/movie`
4055
-`FLV`:`http://127.0.0.1:7001/live/movie.flv`
4156
-`HLS`:`http://127.0.0.1:7002/live/movie.m3u8`

configure/channel.go

Lines changed: 47 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,58 @@
11
package configure
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"io/ioutil"
7-
"log"
8-
"math/rand"
9-
"sync"
10-
"time"
5+
6+
"livego/utils/uid"
117

128
"github.com/go-redis/redis/v7"
9+
"github.com/patrickmn/go-cache"
10+
log "github.com/sirupsen/logrus"
1311
)
1412

15-
var RoomKeys = LoadRoomKey(*GetKeyFile())
13+
type RoomKeysType struct {
14+
redisCli *redis.Client
15+
localCache *cache.Cache
16+
}
1617

17-
var roomUpdated = false
18+
var RoomKeys = &RoomKeysType{
19+
localCache: cache.New(cache.NoExpiration, 0),
20+
}
1821

19-
var saveInFile = true
20-
var redisCli *redis.Client
22+
var saveInLocal = true
2123

2224
func Init() {
23-
saveInFile = GetRedisAddr() == nil
24-
25-
rand.Seed(time.Now().UnixNano())
26-
if saveInFile {
27-
go func() {
28-
for {
29-
time.Sleep(15 * time.Second)
30-
if roomUpdated {
31-
RoomKeys.Save(*roomKeySaveFile)
32-
roomUpdated = false
33-
}
34-
}
35-
}()
36-
25+
saveInLocal = len(Config.GetString("redis_addr")) == 0
26+
if saveInLocal {
3727
return
3828
}
3929

40-
redisCli = redis.NewClient(&redis.Options{
41-
Addr: *GetRedisAddr(),
42-
Password: *GetRedisPwd(),
30+
RoomKeys.redisCli = redis.NewClient(&redis.Options{
31+
Addr: Config.GetString("redis_addr"),
32+
Password: Config.GetString("redis_pwd"),
4333
DB: 0,
4434
})
4535

46-
_, err := redisCli.Ping().Result()
47-
if err != nil {
48-
panic(err)
49-
}
50-
51-
log.Printf("Redis connected")
52-
}
53-
54-
type RoomKeysType struct {
55-
mapChanKey sync.Map
56-
mapKeyChan sync.Map
57-
}
58-
59-
func LoadRoomKey(f string) *RoomKeysType {
60-
result := &RoomKeysType{
61-
mapChanKey: sync.Map{},
62-
mapKeyChan: sync.Map{},
63-
}
64-
raw := map[string]string{}
65-
content, err := ioutil.ReadFile(f)
36+
_, err := RoomKeys.redisCli.Ping().Result()
6637
if err != nil {
67-
log.Printf("Failed to read file %s for room keys", f)
68-
return result
69-
}
70-
if json.Unmarshal(content, &raw) != nil {
71-
log.Printf("Failed to unmarshal file %s for room keys", f)
72-
return result
38+
log.Panic("Redis: ", err)
7339
}
74-
for room, key := range raw {
75-
result.mapChanKey.Store(room, key)
76-
result.mapKeyChan.Store(key, room)
77-
}
78-
return result
79-
}
8040

81-
func (r *RoomKeysType) Save(f string) {
82-
raw := map[string]string{}
83-
r.mapChanKey.Range(func(channel, key interface{}) bool {
84-
raw[channel.(string)] = key.(string)
85-
return true
86-
})
87-
content, err := json.Marshal(raw)
88-
if err != nil {
89-
log.Println("Failed to marshal room keys")
90-
return
91-
}
92-
if ioutil.WriteFile(f, content, 0644) != nil {
93-
log.Println("Failed to save room keys")
94-
return
95-
}
41+
log.Info("Redis connected")
9642
}
9743

9844
// set/reset a random key for channel
9945
func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
100-
if !saveInFile {
46+
if !saveInLocal {
10147
for {
102-
key = randStringRunes(48)
103-
if _, err = redisCli.Get(key).Result(); err == redis.Nil {
104-
err = redisCli.Set(channel, key, 0).Err()
48+
key = uid.RandStringRunes(48)
49+
if _, err = r.redisCli.Get(key).Result(); err == redis.Nil {
50+
err = r.redisCli.Set(channel, key, 0).Err()
10551
if err != nil {
10652
return
10753
}
10854

109-
err = redisCli.Set(key, channel, 0).Err()
55+
err = r.redisCli.Set(key, channel, 0).Err()
11056
return
11157
} else if err != nil {
11258
return
@@ -115,22 +61,21 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
11561
}
11662

11763
for {
118-
key = randStringRunes(48)
119-
if _, found := r.mapKeyChan.Load(key); !found {
120-
r.mapChanKey.Store(channel, key)
121-
r.mapKeyChan.Store(key, channel)
64+
key = uid.RandStringRunes(48)
65+
if _, found := r.localCache.Get(key); !found {
66+
r.localCache.SetDefault(channel, key)
67+
r.localCache.SetDefault(key, channel)
12268
break
12369
}
12470
}
125-
roomUpdated = true
12671
return
12772
}
12873

12974
func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
130-
if !saveInFile {
131-
if newKey, err = redisCli.Get(channel).Result(); err == redis.Nil {
75+
if !saveInLocal {
76+
if newKey, err = r.redisCli.Get(channel).Result(); err == redis.Nil {
13277
newKey, err = r.SetKey(channel)
133-
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
78+
log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
13479
return
13580
}
13681

@@ -139,20 +84,20 @@ func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
13984

14085
var key interface{}
14186
var found bool
142-
if key, found = r.mapChanKey.Load(channel); found {
87+
if key, found = r.localCache.Get(channel); found {
14388
return key.(string), nil
14489
}
14590
newKey, err = r.SetKey(channel)
146-
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
91+
log.Debugf("[KEY] new channel [%s]: %s", channel, newKey)
14792
return
14893
}
14994

15095
func (r *RoomKeysType) GetChannel(key string) (channel string, err error) {
151-
if !saveInFile {
152-
return redisCli.Get(key).Result()
96+
if !saveInLocal {
97+
return r.redisCli.Get(key).Result()
15398
}
15499

155-
chann, found := r.mapKeyChan.Load(key)
100+
chann, found := r.localCache.Get(key)
156101
if found {
157102
return chann.(string), nil
158103
} else {
@@ -161,40 +106,29 @@ func (r *RoomKeysType) GetChannel(key string) (channel string, err error) {
161106
}
162107

163108
func (r *RoomKeysType) DeleteChannel(channel string) bool {
164-
if !saveInFile {
165-
return redisCli.Del(channel).Err() != nil
109+
if !saveInLocal {
110+
return r.redisCli.Del(channel).Err() != nil
166111
}
167112

168-
key, ok := r.mapChanKey.Load(channel)
113+
key, ok := r.localCache.Get(channel)
169114
if ok {
170-
r.mapChanKey.Delete(channel)
171-
r.mapKeyChan.Delete(key)
115+
r.localCache.Delete(channel)
116+
r.localCache.Delete(key.(string))
172117
return true
173118
}
174119
return false
175120
}
176121

177122
func (r *RoomKeysType) DeleteKey(key string) bool {
178-
if !saveInFile {
179-
return redisCli.Del(key).Err() != nil
123+
if !saveInLocal {
124+
return r.redisCli.Del(key).Err() != nil
180125
}
181126

182-
channel, ok := r.mapKeyChan.Load(key)
127+
channel, ok := r.localCache.Get(key)
183128
if ok {
184-
r.mapChanKey.Delete(channel)
185-
r.mapKeyChan.Delete(key)
129+
r.localCache.Delete(channel.(string))
130+
r.localCache.Delete(key)
186131
return true
187132
}
188133
return false
189134
}
190-
191-
// helpers
192-
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
193-
194-
func randStringRunes(n int) string {
195-
b := make([]rune, n)
196-
for i := range b {
197-
b[i] = letterRunes[rand.Intn(len(letterRunes))]
198-
}
199-
return string(b)
200-
}

0 commit comments

Comments
 (0)
0