Skip to content

Commit 4ac1658

Browse files
add gb_test func:TestGbPublishWithApi
1 parent ba42a40 commit 4ac1658

File tree

4 files changed

+107
-51
lines changed

4 files changed

+107
-51
lines changed

trunk/3rdparty/srs-bench/gb28181/gb28181.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ func Parse(ctx context.Context) interface{} {
8888
os.Exit(0)
8989
}
9090

91-
// showHelp := c.sipConfig.String() == ""
92-
// if showHelp {
93-
// fl.Usage()
94-
// os.Exit(-1)
95-
// }
91+
showHelp := c.sipConfig.String() == "" && c.psConfig.addr == ""
92+
if showHelp {
93+
fl.Usage()
94+
os.Exit(-1)
95+
}
9696

9797
summaryDesc := ""
9898
if c.sipConfig.addr != "" {

trunk/3rdparty/srs-bench/gb28181/gb_test.go

+79-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// The MIT License (MIT)
22
//
3-
// Copyright (c) 2022 Winlin
3+
// # Copyright (c) 2022 Winlin
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy of
66
// this software and associated documentation files (the "Software"), to deal in
@@ -23,11 +23,18 @@ package gb28181
2323
import (
2424
"context"
2525
"fmt"
26+
"io"
27+
"math/rand"
28+
"strconv"
29+
"sync"
30+
"testing"
31+
"time"
32+
33+
"github.com/ossrs/srs-bench/srs"
34+
2635
"github.com/ghettovoice/gosip/sip"
2736
"github.com/ossrs/go-oryx-lib/errors"
2837
"github.com/ossrs/go-oryx-lib/logger"
29-
"testing"
30-
"time"
3138
)
3239

3340
func TestGbPublishRegularly(t *testing.T) {
@@ -523,3 +530,72 @@ func TestGbPublishUnregister(t *testing.T) {
523530
t.Errorf("err %+v", err)
524531
}
525532
}
533+
534+
func TestGbPublishWithApi(t *testing.T) {
535+
ctx := logger.WithContext(context.Background())
536+
ctx, cancel := context.WithTimeout(ctx, time.Duration(*srsTimeout)*time.Millisecond)
537+
defer cancel()
538+
539+
psConfig := PSConfig{
540+
video: *srsPublishVideo,
541+
fps: *srsPublishVideoFps,
542+
audio: *srsPublishAudio,
543+
}
544+
545+
var r0, r1, r2 error
546+
r0 = func() error {
547+
// generate a random ssrc, and use it as the stream name.
548+
ssrc := rand.Intn(900000000) + 1000000000
549+
stream := strconv.FormatUint(uint64(ssrc), 10)
550+
551+
port, err := apiGbPublishRequest(ctx, stream, strconv.Itoa(ssrc))
552+
if err != nil {
553+
return errors.Wrapf(err, "apiGbPublishRequest stream=%v", stream)
554+
}
555+
556+
ingester := NewPSIngester(&IngesterConfig{
557+
psConfig: psConfig,
558+
ssrc: uint32(ssrc),
559+
clockRate: 90000,
560+
payloadType: 96,
561+
})
562+
defer ingester.Close()
563+
564+
var wg sync.WaitGroup
565+
defer wg.Wait()
566+
567+
// Check the stream is publishing.
568+
wg.Add(1)
569+
go func() {
570+
defer wg.Done()
571+
for ctx.Err() == nil {
572+
stat := srs.NewStatApi(ctx).Streams().FilterByStreamSuffix(stream)
573+
logger.Tf(ctx, "Check publishing, stream=%v", stat.Stream())
574+
if stat.Stream() != nil {
575+
return
576+
}
577+
time.Sleep(1 * time.Second)
578+
}
579+
r1 = ctx.Err()
580+
}()
581+
582+
// Ingest the stream.
583+
wg.Add(1)
584+
go func() {
585+
defer wg.Done()
586+
ingester.conf.serverAddr = "tcp://localhost:" + strconv.Itoa(port)
587+
if err := ingester.Ingest(ctx); err != nil {
588+
if errors.Cause(err) == io.EOF {
589+
logger.Tf(ctx, "EOF, video=%v, audio=%v", psConfig.video, psConfig.audio)
590+
return
591+
}
592+
r2 = err
593+
}
594+
}()
595+
return err
596+
}()
597+
598+
if err := filterTestError(ctx.Err(), r0, r1, r2); err != nil {
599+
t.Errorf("err %+v", err)
600+
}
601+
}

trunk/3rdparty/srs-bench/gb28181/util.go

+11-43
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@ package gb28181
2323
import (
2424
"bufio"
2525
"context"
26-
"encoding/json"
2726
"flag"
2827
"fmt"
2928
"io"
30-
"io/ioutil"
3129
"net"
32-
"net/http"
3330
"net/url"
3431
"os"
3532
"path"
@@ -39,7 +36,7 @@ import (
3936
"github.com/ghettovoice/gosip/sip"
4037
"github.com/ossrs/go-oryx-lib/aac"
4138
"github.com/ossrs/go-oryx-lib/errors"
42-
"github.com/ossrs/go-oryx-lib/logger"
39+
"github.com/ossrs/srs-bench/srs"
4340
"github.com/yapingcat/gomedia/mpeg2"
4441
)
4542

@@ -365,52 +362,23 @@ func (v *AACReader) NextADTSFrame() ([]byte, error) {
365362
return adts, nil
366363
}
367364

368-
// Request SRS GB API, the apiPath like "/gb/v1/publish",
369-
//
370-
// Return the response of answer in string.
371-
func apiGbRequest(ctx context.Context, apiPath, host, stream, ssrc string) (string, error) {
372-
api := host
373-
if !strings.HasPrefix(apiPath, "/") {
374-
api += "/"
375-
}
376-
api += apiPath
377-
378-
if !strings.HasSuffix(apiPath, "/") {
379-
api += "/"
380-
}
381-
382-
// Build JSON body.
383-
reqBody := struct {
365+
func apiGbPublishRequest(ctx context.Context, stream, ssrc string) (int, error) {
366+
req := struct {
384367
ClientIP string `json:"clientip"`
385368
Stream string `json:"stream"`
386369
SSRC string `json:"ssrc"`
387370
}{
388-
host, stream, ssrc,
389-
}
390-
391-
b, err := json.Marshal(reqBody)
392-
if err != nil {
393-
return "", errors.Wrapf(err, "Marshal body %v", reqBody)
371+
"", stream, ssrc,
394372
}
395-
logger.If(ctx, "Request url api=%v with %v", api, string(b))
396-
logger.Tf(ctx, "Request url api=%v with %v bytes", api, len(b))
397373

398-
req, err := http.NewRequest("POST", api, strings.NewReader(string(b)))
399-
if err != nil {
400-
return "", errors.Wrapf(err, "HTTP request %v", string(b))
401-
}
374+
res := struct {
375+
Code int `json:"code"`
376+
Port int `json:"port"`
377+
}{}
402378

403-
res, err := http.DefaultClient.Do(req.WithContext(ctx))
404-
if err != nil {
405-
return "", errors.Wrapf(err, "Do HTTP request %v", string(b))
406-
}
407-
408-
b2, err := ioutil.ReadAll(res.Body)
409-
if err != nil {
410-
return "", errors.Wrapf(err, "Read response for %v", string(b))
379+
if err := srs.ApiRequest(ctx, "http://localhost:1985/gb/v1/publish/", req, &res); err != nil {
380+
return 0, errors.Wrapf(err, "gb/v1/publish")
411381
}
412-
logger.If(ctx, "Response from %v is %v", api, string(b2))
413-
logger.Tf(ctx, "Response from %v is %v bytes", api, len(b2))
414382

415-
return string(b2), nil
383+
return res.Port, nil
416384
}

trunk/3rdparty/srs-bench/srs/api.go

+12
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func apiRequest(ctx context.Context, r string, req interface{}, res interface{})
8787
return nil
8888
}
8989

90+
func ApiRequest(ctx context.Context, r string, req interface{}, res interface{}) error {
91+
return apiRequest(ctx, r, req, res)
92+
}
93+
9094
// The SRS HTTP statistic API.
9195
type statAPI struct {
9296
ctx context.Context
@@ -98,6 +102,10 @@ func newStatAPI(ctx context.Context) *statAPI {
98102
return &statAPI{ctx: ctx}
99103
}
100104

105+
func NewStatApi(ctx context.Context) *statAPI {
106+
return newStatAPI(ctx)
107+
}
108+
101109
type statGeneral struct {
102110
Code int `json:"code"`
103111
Server string `json:"server"`
@@ -152,3 +160,7 @@ func (v *statAPI) FilterByStreamSuffix(suffix string) *statAPI {
152160
}
153161
return v
154162
}
163+
164+
func (v *statAPI) Stream() *statStream {
165+
return v.stream
166+
}

0 commit comments

Comments
 (0)