Skip to content

Commit

Permalink
SquashSRS4: Add demo for RTC
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed May 5, 2021
1 parent 206d958 commit becbe45
Show file tree
Hide file tree
Showing 34 changed files with 836 additions and 85 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ The ports used by SRS:

## V4 changes

* v4.0, 2021-05-04, Add video room demo. 4.0.98
* v4.0, 2021-05-03, Add RTC stream merging demo by FFmpeg. 4.0.97
* v4.0, 2021-05-02, Add one to one demo. 4.0.96
* v4.0, 2021-04-20, Support RTC2RTMP bridger and shared FastTimer. 4.0.95
* v4.0, 2021-04-20, Refine transcoder to support aac2opus and opus2aac. 4.0.94
Expand Down
22 changes: 22 additions & 0 deletions trunk/3rdparty/copy_to_gits.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

if [[ ! -d signaling ]]; then
cd 3rdparty
fi
if [[ ! -d signaling ]]; then
echo "no 3rdparty"
exit -1
fi
if [[ ! -d ~/git/signaling ]]; then
echo "no signaling"
exit -1
fi

echo "Copy signaling"
cp -R signaling/* ~/git/signaling/ && (cd ~/git/signaling && git st)

echo "Copy httpx-static"
cp -R httpx-static/* ~/git/go-oryx/httpx-static/ && (cd ~/git/go-oryx && git st)

echo "Copy srs-bench"
cp -R srs-bench/* ~/git/srs-bench/ && (cd ~/git/srs-bench && git st)
21 changes: 20 additions & 1 deletion trunk/3rdparty/httpx-static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $GOPATH/bin/httpx-static -https 8443 -root `pwd`/html

Open https://localhost:8443/ in browser.

> Remark: Click `ADVANCED` => `Proceed to localhost (unsafe)`.
> Remark: Click `ADVANCED` => `Proceed to localhost (unsafe)`, or type `thisisunsafe` in page.
*HTTPS proxy*: Proxy http as https

Expand All @@ -44,6 +44,25 @@ $GOPATH/bin/httpx-static -https 8443 -root `pwd`/html -proxy http://ossrs.net:19

Open https://localhost:8443/api/v1/summaries in browser.

## Docker

Run httpx-static in docker:

```bash
docker run --rm -p 80:80 -p 443:443 registry.cn-hangzhou.aliyuncs.com/ossrs/httpx:v1.0.2
```

> Note: More images and version is [here](https://cr.console.aliyun.com/repository/cn-hangzhou/ossrs/httpx/images).
To proxy to other dockers, in macOS:

```bash
CANDIDATE=$(ifconfig en0 inet| grep 'inet '|awk '{print $2}') &&
docker run --rm -p 80:80 -p 443:443 registry.cn-hangzhou.aliyuncs.com/ossrs/httpx:v1.0.5 \
./bin/httpx-static -http 80 -https 443 -ssk ./etc/server.key -ssc ./etc/server.crt \
-proxy http://$CANDIDATE:8080/
```

## History

* v0.0.3, 2017-11-03, Support multiple proxy HTTP to HTTPS.
Expand Down
24 changes: 24 additions & 0 deletions trunk/3rdparty/signaling/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

############################################################
# build
############################################################
FROM registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev AS build

COPY . /tmp/signaling
RUN cd /tmp/signaling && make
RUN cp /tmp/signaling/objs/signaling /usr/local/bin/signaling
RUN cp -R /tmp/signaling/www /usr/local/

############################################################
# dist
############################################################
FROM centos:7 AS dist

# HTTP/1989
EXPOSE 1989
# SRS binary, config files and srs-console.
COPY --from=build /usr/local/bin/signaling /usr/local/bin/
COPY --from=build /usr/local/www /usr/local/www
# Default workdir and command.
WORKDIR /usr/local
CMD ["./bin/signaling"]
48 changes: 43 additions & 5 deletions trunk/3rdparty/signaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,59 @@ WebRTC signaling for https://github.com/ossrs/srs

## Usage

Build and [run SRS](https://github.com/ossrs/srs/tree/4.0release#usage):
[Run SRS](https://github.com/ossrs/srs/tree/4.0release#usage) in docker:

```bash
git clone -b 4.0release https://gitee.com/ossrs/srs.git srs &&
cd srs/trunk && ./configure && make && ./objs/srs -c conf/rtc.conf
docker run --rm --env CANDIDATE=$(ifconfig en0 inet| grep 'inet '|awk '{print $2}') \
-p 1935:1935 -p 8080:8080 -p 1985:1985 -p 8000:8000/udp \
registry.cn-hangzhou.aliyuncs.com/ossrs/srs:v4.0.95 \
objs/srs -c conf/rtc.conf
```

Build and run signaling:
> Note: More images and version is [here](https://cr.console.aliyun.com/repository/cn-hangzhou/ossrs/srs/images).
Run signaling in docker:

```bash
cd srs/trunk/3rdparty/signaling && make && ./objs/signaling
docker run --rm -p 1989:1989 registry.cn-hangzhou.aliyuncs.com/ossrs/signaling:v1.0.4
```

> Note: More images and version is [here](https://cr.console.aliyun.com/repository/cn-hangzhou/ossrs/signaling/images).
Open the H5 demos:

* [WebRTC: One to One over SFU(SRS)](http://localhost:1989/demos/one2one.html?autostart=true)

## Build from source

Build and [run SRS](https://github.com/ossrs/srs/tree/4.0release#usage):

```bash
cd ~/git && git clone -b 4.0release https://gitee.com/ossrs/srs.git srs &&
cd ~/git/srs/trunk && ./configure && make && ./objs/srs -c conf/rtc.conf
```

Build and run signaling:

```bash
cd ~/git/srs/trunk/3rdparty/signaling && make && ./objs/signaling
```

Open demos by localhost: http://localhost:1989/demos

Build and run httpx-static for HTTPS/WSS:

```bash
cd ~/git/srs/trunk/3rdparty/httpx-static && make &&
./objs/httpx-static -http 80 -https 443 -ssk server.key -ssc server.crt \
-proxy http://127.0.0.1:1989/sig -proxy http://127.0.0.1:1985/rtc \
-proxy http://127.0.0.1:8080/
```

Open demos by HTTPS or IP:

* http://localhost/demos/
* https://localhost/demos/
* https://192.168.3.6/demos/

Winlin 2021.05
70 changes: 70 additions & 0 deletions trunk/3rdparty/signaling/auto/release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

SRS_GIT=$HOME/git/signaling
SRS_TAG=

# linux shell color support.
RED="\\033[31m"
GREEN="\\033[32m"
YELLOW="\\033[33m"
BLACK="\\033[0m"

function NICE() {
echo -e "${GREEN}$@${BLACK}"
}

function TRACE() {
echo -e "${BLACK}$@${BLACK}"
}

function WARN() {
echo -e "${YELLOW}$@${BLACK}"
}

function ERROR() {
echo -e "${RED}$@${BLACK}"
}

##################################################################################
##################################################################################
##################################################################################
if [[ -z $SRS_TAG ]]; then
SRS_TAG=`(cd $SRS_GIT && git describe --tags --abbrev=0 --exclude release-* 2>/dev/null)`
if [[ $? -ne 0 ]]; then
echo "Invalid tag $SRS_TAG of $SRS_FILTER in $SRS_GIT"
exit -1
fi
fi

NICE "Build docker for $SRS_GIT, tag is $SRS_TAG"

git ci -am "Release $SRS_TAG"

# For aliyun hub.
NICE "aliyun hub release-v$SRS_TAG"

echo "git push aliyun"
git push aliyun

git tag -d release-v$SRS_TAG 2>/dev/null
echo "Cleanup tag $SRS_TAG for aliyun"

git tag release-v$SRS_TAG; git push -f aliyun release-v$SRS_TAG
echo "Create new tag $SRS_TAG for aliyun"
echo ""

NICE "aliyun hub release-vlatest"
git tag -d release-vlatest 2>/dev/null
echo "Cleanup tag latest for aliyun"

git tag release-vlatest; git push -f aliyun release-vlatest
echo "Create new tag latest for aliyun"

# For github.com
echo "git push origin"
git push origin

echo "git push origin $SRS_TAG"
git push origin $SRS_TAG

NICE "Update github ok"
43 changes: 34 additions & 9 deletions trunk/3rdparty/signaling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (v *Room) Remove(p *Participant) {
}
}

func (v *Room) Notify(ctx context.Context, peer *Participant, event string) {
func (v *Room) Notify(ctx context.Context, peer *Participant, event, param, data string) {
var participants []*Participant
func() {
v.lock.RLock()
Expand All @@ -112,12 +112,15 @@ func (v *Room) Notify(ctx context.Context, peer *Participant, event string) {
res := struct {
Action string `json:"action"`
Event string `json:"event"`
Param string `json:"param,omitempty"`
Data string `json:"data,omitempty"`
Room string `json:"room"`
Self *Participant `json:"self"`
Peer *Participant `json:"peer"`
Participants []*Participant `json:"participants"`
}{
"notify", event, v.Name, r, peer, participants,
"notify", event, param, data,
v.Name, r, peer, participants,
}

b, err := json.Marshal(struct {
Expand Down Expand Up @@ -187,10 +190,16 @@ func main() {
var self *Participant
go func() {
<-ctx.Done()
if self != nil {
self.Room.Remove(self)
logger.Tf(ctx, "Remove client %v", self)
if self == nil {
return
}

// Notify other peers that we're quiting.
// @remark The ctx(of self) is done, so we must use a new context.
go self.Room.Notify(context.Background(), self, "leave", "", "")

self.Room.Remove(self)
logger.Tf(ctx, "Remove client %v", self)
}()

inMessages := make(chan []byte, 0)
Expand Down Expand Up @@ -228,7 +237,6 @@ func main() {
}

var res interface{}
var p *Participant
if action.Message.Action == "join" {
obj := struct {
Message struct {
Expand All @@ -241,7 +249,7 @@ func main() {
}

r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room})
p = &Participant{Room: r.(*Room), Display: obj.Message.Display, Out: outMessages}
p := &Participant{Room: r.(*Room), Display: obj.Message.Display, Out: outMessages}
if err := r.(*Room).Add(p); err != nil {
return errors.Wrapf(err, "join")
}
Expand All @@ -258,7 +266,7 @@ func main() {
action.Message.Action, obj.Message.Room, p, r.(*Room).Participants,
}

go r.(*Room).Notify(ctx, p, action.Message.Action)
go r.(*Room).Notify(ctx, p, action.Message.Action, "", "")
} else if action.Message.Action == "publish" {
obj := struct {
Message struct {
Expand All @@ -276,7 +284,24 @@ func main() {
// Now, the peer is publishing.
p.Publishing = true

go r.(*Room).Notify(ctx, p, action.Message.Action)
go r.(*Room).Notify(ctx, p, action.Message.Action, "", "")
} else if action.Message.Action == "control" {
obj := struct {
Message struct {
Room string `json:"room"`
Display string `json:"display"`
Call string `json:"call"`
Data string `json:"data"`
} `json:"msg"`
}{}
if err := json.Unmarshal(m, &obj); err != nil {
return errors.Wrapf(err, "Unmarshal %s", m)
}

r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room})
p := r.(*Room).Get(obj.Message.Display)

go r.(*Room).Notify(ctx, p, action.Message.Action, obj.Message.Call, obj.Message.Data)
} else {
return errors.Errorf("Invalid message %s", m)
}
Expand Down
16 changes: 14 additions & 2 deletions trunk/3rdparty/signaling/www/demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
<body>
<h3><a href="https://github.com/ossrs/signaling">Signaling</a> works!</h3>
<p>
Run demo for <a href="one2one.html">WebRTC: One to One over SFU(SRS)</a><br/>
点击进入<a href="one2one.html">SRS一对一通话演示</a>
Run demo for <a class="srs_demo" href="one2one.html?autostart=true">WebRTC: One to One over SFU(SRS)</a><br/>
点击进入<a class="srs_demo" href="one2one.html?autostart=true">SRS一对一通话演示</a>
</p>
<p>
Run demo for <a class="srs_demo" href="room.html?autostart=true">WebRTC: Video Room over SFU(SRS)</a><br/>
点击进入<a class="srs_demo" href="room.html?autostart=true">SRS多人通话演示</a>
</p>
<script>
let roomName = Number(new Date().getTime() + parseInt(String(Math.random() * 10000000000))).toString(16).substr(3);
let elems = document.getElementsByClassName('srs_demo');
for (var i = 0; i < elems.length; i++) {
let elem = elems.item(i);
elem.setAttribute('href', elem.getAttribute('href') + '&room=' + roomName);
}
</script>
</body>
6 changes: 3 additions & 3 deletions trunk/3rdparty/signaling/www/demos/js/srs.sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function SrsRtcPublisherAsync() {
self.pc.addTransceiver("video", {direction: "sendonly"});

var stream = await navigator.mediaDevices.getUserMedia(
{audio: true, video: {height: {max: 320}}}
{audio: true, video: {width: {max: 320}}}
);
// @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
stream.getTracks().forEach(function (track) {
Expand Down Expand Up @@ -144,7 +144,7 @@ function SrsRtcPublisherAsync() {

return {
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
tid: new Date().getTime().toString(16)
tid: Number(new Date().getTime() + parseInt(String(Math.random() * 10000000000))).toString(16)
};
},
parse: function (url) {
Expand Down Expand Up @@ -361,7 +361,7 @@ function SrsRtcPlayerAsync() {

return {
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
tid: new Date().getTime().toString(16)
tid: Number(new Date().getTime() + parseInt(String(Math.random() * 10000000000))).toString(16)
};
},
parse: function (url) {
Expand Down
Loading

0 comments on commit becbe45

Please sign in to comment.