Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let oryx support external redis and srs service #228

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions platform/dvr-local-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand Down Expand Up @@ -534,21 +536,16 @@ func (v *RecordWorker) Handle(ctx context.Context, handler *http.ServeMux) error
return nil
}

func (v *RecordWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) error {
func (v *RecordWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage, data []byte) error {
// Copy the ts file to temporary cache dir.
tsid := uuid.NewString()
tsfile := path.Join("record", fmt.Sprintf("%v.ts", tsid))

// Always use execFile when params contains user inputs, see https://auth0.com/blog/preventing-command-injection-attacks-in-node-js-apps/
// Note that should never use fs.copyFileSync(file, tsfile, fs.constants.COPYFILE_FICLONE_FORCE) which fails in macOS.
if err := exec.CommandContext(ctx, "cp", "-f", msg.File, tsfile).Run(); err != nil {
return errors.Wrapf(err, "copy file %v to %v", msg.File, tsfile)
}

// Get the file size.
stats, err := os.Stat(msg.File)
if err != nil {
return errors.Wrapf(err, "stat file %v", msg.File)
if file, err := os.Create(tsfile); err != nil {
return errors.Wrapf(err, "create file %v error", tsfile)
} else {
defer file.Close()
io.Copy(file, bytes.NewReader(data))
}

// Create a local ts file object.
Expand All @@ -557,7 +554,7 @@ func (v *RecordWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage)
URL: msg.URL,
SeqNo: msg.SeqNo,
Duration: msg.Duration,
Size: uint64(stats.Size()),
Size: uint64(len(data)),
File: tsfile,
}

Expand Down
22 changes: 9 additions & 13 deletions platform/dvr-tencent-cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strings"
"sync"
Expand Down Expand Up @@ -231,7 +232,7 @@ func (v *DvrWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
return nil
}

func (v *DvrWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) error {
func (v *DvrWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage, data []byte) error {
// Ignore for Tencent Cloud credentials not ready.
if !v.ready() {
return nil
Expand All @@ -241,16 +242,11 @@ func (v *DvrWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) er
tsid := uuid.NewString()
tsfile := path.Join("dvr", fmt.Sprintf("%v.ts", tsid))

// Always use execFile when params contains user inputs, see https://auth0.com/blog/preventing-command-injection-attacks-in-node-js-apps/
// Note that should never use fs.copyFileSync(file, tsfile, fs.constants.COPYFILE_FICLONE_FORCE) which fails in macOS.
if err := exec.CommandContext(ctx, "cp", "-f", msg.File, tsfile).Run(); err != nil {
return errors.Wrapf(err, "copy file %v to %v", msg.File, tsfile)
}

// Get the file size.
stats, err := os.Stat(msg.File)
if err != nil {
return errors.Wrapf(err, "stat file %v", msg.File)
if file, err := os.Create(tsfile); err != nil {
return errors.Wrapf(err, "create file %v error", tsfile)
} else {
defer file.Close()
io.Copy(file, bytes.NewReader(data))
}

// Create a local ts file object.
Expand All @@ -259,7 +255,7 @@ func (v *DvrWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) er
URL: msg.URL,
SeqNo: msg.SeqNo,
Duration: msg.Duration,
Size: uint64(stats.Size()),
Size: uint64(len(data)),
File: tsfile,
}

Expand Down
22 changes: 9 additions & 13 deletions platform/dvr-tencent-vod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
Expand Down Expand Up @@ -321,7 +322,7 @@ func (v *VodWorker) Handle(ctx context.Context, handler *http.ServeMux) error {
return nil
}

func (v *VodWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) error {
func (v *VodWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage, data []byte) error {
// Ignore for Tencent Cloud credentials not ready.
if !v.ready() {
return nil
Expand All @@ -331,16 +332,11 @@ func (v *VodWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) er
tsid := uuid.NewString()
tsfile := path.Join("vod", fmt.Sprintf("%v.ts", tsid))

// Always use execFile when params contains user inputs, see https://auth0.com/blog/preventing-command-injection-attacks-in-node-js-apps/
// Note that should never use fs.copyFileSync(file, tsfile, fs.constants.COPYFILE_FICLONE_FORCE) which fails in macOS.
if err := exec.CommandContext(ctx, "cp", "-f", msg.File, tsfile).Run(); err != nil {
return errors.Wrapf(err, "copy file %v to %v", msg.File, tsfile)
}

// Get the file size.
stats, err := os.Stat(msg.File)
if err != nil {
return errors.Wrapf(err, "stat file %v", msg.File)
if file, err := os.Create(tsfile); err != nil {
return errors.Wrapf(err, "create file %v error", tsfile)
} else {
defer file.Close()
io.Copy(file, bytes.NewReader(data))
}

// Create a local ts file object.
Expand All @@ -349,7 +345,7 @@ func (v *VodWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) er
URL: msg.URL,
SeqNo: msg.SeqNo,
Duration: msg.Duration,
Size: uint64(stats.Size()),
Size: uint64(len(data)),
File: tsfile,
}

Expand Down
5 changes: 5 additions & 0 deletions platform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/ossrs/go-oryx-lib/errors"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand Down Expand Up @@ -113,6 +114,10 @@ func doMain(ctx context.Context) error {
setEnvDefault("REDIS_PORT", "6379")
setEnvDefault("MGMT_LISTEN", "2022")

// SRS HOST
setEnvDefault("SRS_HOST", "127.0.0.1")
setEnvDefault("SRS_HTTP_STREAM_PORT", "8080")

// For HTTPS.
setEnvDefault("HTTPS_LISTEN", "2443")
setEnvDefault("AUTO_SELF_SIGNED_CERTIFICATE", "on")
Expand Down
51 changes: 9 additions & 42 deletions platform/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
Expand All @@ -39,17 +41,12 @@ type OCRWorker struct {
// The global OCR task, only support one OCR task.
task *OCRTask

// Use async goroutine to process on_hls messages.
msgs chan *SrsOnHlsMessage

// Got message from SRS, a new TS segment file is generated.
tsfiles chan *SrsOnHlsObject
}

func NewOCRWorker() *OCRWorker {
v := &OCRWorker{
// Message on_hls.
msgs: make(chan *SrsOnHlsMessage, 1024),
// TS files.
tsfiles: make(chan *SrsOnHlsObject, 1024),
}
Expand Down Expand Up @@ -547,16 +544,7 @@ func (v *OCRWorker) Enabled() bool {
return v.task.enabled()
}

func (v *OCRWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage) error {
select {
case <-ctx.Done():
case v.msgs <- msg:
}

return nil
}

func (v *OCRWorker) OnHlsTsMessageImpl(ctx context.Context, msg *SrsOnHlsMessage) error {
func (v *OCRWorker) OnHlsTsMessage(ctx context.Context, msg *SrsOnHlsMessage, data []byte) error {
// Ignore if not natch the task config.
if !v.task.match(msg) {
return nil
Expand All @@ -566,16 +554,11 @@ func (v *OCRWorker) OnHlsTsMessageImpl(ctx context.Context, msg *SrsOnHlsMessage
tsid := fmt.Sprintf("%v-org-%v", msg.SeqNo, uuid.NewString())
tsfile := path.Join("ocr", fmt.Sprintf("%v.ts", tsid))

// Always use execFile when params contains user inputs, see https://auth0.com/blog/preventing-command-injection-attacks-in-node-js-apps/
// Note that should never use fs.copyFileSync(file, tsfile, fs.constants.COPYFILE_FICLONE_FORCE) which fails in macOS.
if err := exec.CommandContext(ctx, "cp", "-f", msg.File, tsfile).Run(); err != nil {
return errors.Wrapf(err, "copy file %v to %v", msg.File, tsfile)
}

// Get the file size.
stats, err := os.Stat(msg.File)
if err != nil {
return errors.Wrapf(err, "stat file %v", msg.File)
if file, err := os.Create(tsfile); err != nil {
return errors.Wrapf(err, "create file %v error", tsfile)
} else {
defer file.Close()
io.Copy(file, bytes.NewReader(data))
}

// Create a local ts file object.
Expand All @@ -584,7 +567,7 @@ func (v *OCRWorker) OnHlsTsMessageImpl(ctx context.Context, msg *SrsOnHlsMessage
URL: msg.URL,
SeqNo: msg.SeqNo,
Duration: msg.Duration,
Size: uint64(stats.Size()),
Size: uint64(len(data)),
File: tsfile,
}

Expand Down Expand Up @@ -659,22 +642,6 @@ func (v *OCRWorker) Start(ctx context.Context) error {
}
}()

// Consume all on_hls messages.
wg.Add(1)
go func() {
defer wg.Done()

for ctx.Err() == nil {
select {
case <-ctx.Done():
case msg := <-v.msgs:
if err := v.OnHlsTsMessageImpl(ctx, msg); err != nil {
logger.Wf(ctx, "ocr: handle on hls message %v err %+v", msg.String(), err)
}
}
}
}()

// Consume all ts files by task.
wg.Add(1)
go func() {
Expand Down
15 changes: 9 additions & 6 deletions platform/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/joho/godotenv"

"github.com/ossrs/go-oryx-lib/errors"
ohttp "github.com/ossrs/go-oryx-lib/http"
"github.com/ossrs/go-oryx-lib/logger"

// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
"github.com/go-redis/redis/v8"
)
Expand Down Expand Up @@ -288,22 +291,22 @@ func handleHTTPService(ctx context.Context, handler *http.ServeMux) error {
handleMgmtStreamsKickoff(ctx, handler)
handleMgmtUI(ctx, handler)

proxy2023, err := httpCreateProxy("http://127.0.0.1:2023")
proxy2023, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":2023")
if err != nil {
return err
}

proxy1985, err := httpCreateProxy("http://127.0.0.1:1985")
proxy1985, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":1985")
if err != nil {
return err
}

proxyWhxp, err := httpCreateProxy("http://127.0.0.1:1985")
proxyWhxp, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":1985")
if err != nil {
return err
}

proxy8080, err := httpCreateProxy("http://127.0.0.1:8080")
proxy8080, err := httpCreateProxy("http://" + os.Getenv("SRS_HOST") + ":8080")
if err != nil {
return err
}
Expand Down Expand Up @@ -1690,7 +1693,7 @@ func handleMgmtStreamsKickoff(ctx context.Context, handler *http.ServeMux) {

// Whether client exists in SRS server.
var code int
clientURL := fmt.Sprintf("http://127.0.0.1:1985/api/v1/clients/%v", streamObject.Client)
clientURL := fmt.Sprintf("http://%v:1985/api/v1/clients/%v", os.Getenv("SRS_HOST"), streamObject.Client)
if r0, body, err := requestClient(ctx, clientURL, http.MethodGet); err != nil {
return errors.Wrapf(err, "http query client %v", clientURL)
} else if r0 != 0 && r0 != ErrorRtmpClientNotFound {
Expand Down
Loading
Loading