mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Tools: Update one to one demo
This commit is contained in:
parent
fc23b9e5f5
commit
74043b4153
5 changed files with 73 additions and 21 deletions
31
trunk/3rdparty/signaling/main.go
vendored
31
trunk/3rdparty/signaling/main.go
vendored
|
@ -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
|
var participants []*Participant
|
||||||
func() {
|
func() {
|
||||||
v.lock.RLock()
|
v.lock.RLock()
|
||||||
|
@ -112,12 +112,15 @@ func (v *Room) Notify(ctx context.Context, peer *Participant, event string) {
|
||||||
res := struct {
|
res := struct {
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
Event string `json:"event"`
|
Event string `json:"event"`
|
||||||
|
Param string `json:"param,omitempty"`
|
||||||
|
Data string `json:"data,omitempty"`
|
||||||
Room string `json:"room"`
|
Room string `json:"room"`
|
||||||
Self *Participant `json:"self"`
|
Self *Participant `json:"self"`
|
||||||
Peer *Participant `json:"peer"`
|
Peer *Participant `json:"peer"`
|
||||||
Participants []*Participant `json:"participants"`
|
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 {
|
b, err := json.Marshal(struct {
|
||||||
|
@ -228,7 +231,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var res interface{}
|
var res interface{}
|
||||||
var p *Participant
|
|
||||||
if action.Message.Action == "join" {
|
if action.Message.Action == "join" {
|
||||||
obj := struct {
|
obj := struct {
|
||||||
Message struct {
|
Message struct {
|
||||||
|
@ -241,7 +243,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
r, _ := rooms.LoadOrStore(obj.Message.Room, &Room{Name: obj.Message.Room})
|
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 {
|
if err := r.(*Room).Add(p); err != nil {
|
||||||
return errors.Wrapf(err, "join")
|
return errors.Wrapf(err, "join")
|
||||||
}
|
}
|
||||||
|
@ -258,7 +260,7 @@ func main() {
|
||||||
action.Message.Action, obj.Message.Room, p, r.(*Room).Participants,
|
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" {
|
} else if action.Message.Action == "publish" {
|
||||||
obj := struct {
|
obj := struct {
|
||||||
Message struct {
|
Message struct {
|
||||||
|
@ -276,7 +278,24 @@ func main() {
|
||||||
// Now, the peer is publishing.
|
// Now, the peer is publishing.
|
||||||
p.Publishing = true
|
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 {
|
} else {
|
||||||
return errors.Errorf("Invalid message %s", m)
|
return errors.Errorf("Invalid message %s", m)
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ function SrsRtcPublisherAsync() {
|
||||||
self.pc.addTransceiver("video", {direction: "sendonly"});
|
self.pc.addTransceiver("video", {direction: "sendonly"});
|
||||||
|
|
||||||
var stream = await navigator.mediaDevices.getUserMedia(
|
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
|
// @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
|
||||||
stream.getTracks().forEach(function (track) {
|
stream.getTracks().forEach(function (track) {
|
||||||
|
|
|
@ -106,6 +106,7 @@ function SrsRtcSignalingParse(location) {
|
||||||
host = host? host.split('&')[0] : location.hostname;
|
host = host? host.split('&')[0] : location.hostname;
|
||||||
|
|
||||||
let room = location.href.split('room=')[1];
|
let room = location.href.split('room=')[1];
|
||||||
|
room = room? room.split('&')[0] : null;
|
||||||
|
|
||||||
let display = location.href.split('display=')[1];
|
let display = location.href.split('display=')[1];
|
||||||
display = display? display.split('&')[0] : new Date().getTime().toString(16).substr(3);
|
display = display? display.split('&')[0] : new Date().getTime().toString(16).substr(3);
|
||||||
|
|
38
trunk/3rdparty/signaling/www/demos/one2one.html
vendored
38
trunk/3rdparty/signaling/www/demos/one2one.html
vendored
|
@ -45,19 +45,22 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="span5">
|
<div class="span4">
|
||||||
<label></label>
|
<label></label>
|
||||||
<video id="rtc_media_publisher" width="320" autoplay muted controls></video>
|
<video id="rtc_media_publisher" width="310" autoplay muted controls></video>
|
||||||
|
|
||||||
<label></label>
|
<label></label>
|
||||||
<span id='self'></span>
|
<span id='self'></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="span6">
|
<div class="span6">
|
||||||
<label></label>
|
<label></label>
|
||||||
<video id="rtc_media_player" width="320" autoplay muted controls></video>
|
<video id="rtc_media_player" width="310" autoplay muted controls></video>
|
||||||
|
|
||||||
<label></label>
|
<label></label>
|
||||||
<span id='peer'></span>
|
<span id='peer'></span>
|
||||||
|
<a href="javascript:control_refresh_peer()">Refresh</a>
|
||||||
|
<input type="text" id="txt_alert" class="input-medium" value="">
|
||||||
|
<a href="javascript:control_alert_peer()">Alert</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -65,6 +68,8 @@
|
||||||
var sig = null;
|
var sig = null;
|
||||||
var publisher = null;
|
var publisher = null;
|
||||||
var player = null;
|
var player = null;
|
||||||
|
var control_refresh_peer = null;
|
||||||
|
var control_alert_peer = null;
|
||||||
$(function(){
|
$(function(){
|
||||||
console.log('?wss=x to specify the websocket schema, ws or wss');
|
console.log('?wss=x to specify the websocket schema, ws or wss');
|
||||||
console.log('?wsh=x to specify the websocket server ip');
|
console.log('?wsh=x to specify the websocket server ip');
|
||||||
|
@ -85,13 +90,32 @@
|
||||||
sig = new SrsRtcSignalingAsync();
|
sig = new SrsRtcSignalingAsync();
|
||||||
sig.onmessage = function (msg) {
|
sig.onmessage = function (msg) {
|
||||||
console.log('Notify: ', msg);
|
console.log('Notify: ', msg);
|
||||||
msg.participants.forEach(function (participant) {
|
if (msg.event === 'publish') {
|
||||||
if (participant.display === display || !participant.publishing) return;
|
msg.participants.forEach(function (participant) {
|
||||||
startPlay(host, room, participant.display);
|
if (participant.display === display || !participant.publishing) return;
|
||||||
});
|
startPlay(host, room, participant.display);
|
||||||
|
});
|
||||||
|
} else if (msg.event === 'control') {
|
||||||
|
if (msg.param === 'refresh') {
|
||||||
|
setTimeout(function () {
|
||||||
|
window.location.href = window.location.href;
|
||||||
|
}, 500);
|
||||||
|
} else if (msg.param === 'alert') {
|
||||||
|
alert('From ' + msg.peer.display + ': ' + msg.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
await sig.connect(conf.wsSchema, conf.wsHost, room, display);
|
await sig.connect(conf.wsSchema, conf.wsHost, room, display);
|
||||||
|
|
||||||
|
control_refresh_peer = async function () {
|
||||||
|
let r1 = await sig.send({action:'control', room:room, display:display, call:'refresh'});
|
||||||
|
console.log('Signaling: control peer to refresh ok', r1);
|
||||||
|
};
|
||||||
|
control_alert_peer = async function () {
|
||||||
|
let r1 = await sig.send({action:'control', room:room, display:display, call:'alert', data:$('#txt_alert').val()});
|
||||||
|
console.log('Signaling: control peer to alert ok', r1);
|
||||||
|
};
|
||||||
|
|
||||||
let r0 = await sig.send({action:'join', room:room, display:display});
|
let r0 = await sig.send({action:'join', room:room, display:display});
|
||||||
console.log('Signaling: join ok', r0);
|
console.log('Signaling: join ok', r0);
|
||||||
|
|
||||||
|
|
|
@ -57,7 +57,7 @@ function SrsRtcPublisherAsync() {
|
||||||
self.pc.addTransceiver("video", {direction: "sendonly"});
|
self.pc.addTransceiver("video", {direction: "sendonly"});
|
||||||
|
|
||||||
var stream = await navigator.mediaDevices.getUserMedia(
|
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
|
// @see https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/addStream#Migrating_to_addTrack
|
||||||
stream.getTracks().forEach(function (track) {
|
stream.getTracks().forEach(function (track) {
|
||||||
|
@ -69,7 +69,8 @@ function SrsRtcPublisherAsync() {
|
||||||
var session = await new Promise(function (resolve, reject) {
|
var session = await new Promise(function (resolve, reject) {
|
||||||
// @see https://github.com/rtcdn/rtcdn-draft
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
var data = {
|
var data = {
|
||||||
api: conf.apiUrl, streamurl: conf.streamUrl, clientip: null, sdp: offer.sdp
|
api: conf.apiUrl, tid: conf.tid, streamurl: conf.streamUrl,
|
||||||
|
clientip: null, sdp: offer.sdp
|
||||||
};
|
};
|
||||||
console.log("Generated offer: ", data);
|
console.log("Generated offer: ", data);
|
||||||
|
|
||||||
|
@ -101,7 +102,7 @@ function SrsRtcPublisherAsync() {
|
||||||
|
|
||||||
// Close the publisher.
|
// Close the publisher.
|
||||||
self.close = function () {
|
self.close = function () {
|
||||||
self.pc.close();
|
self.pc && self.pc.close();
|
||||||
self.pc = null;
|
self.pc = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -141,7 +142,10 @@ function SrsRtcPublisherAsync() {
|
||||||
|
|
||||||
var streamUrl = urlObject.url;
|
var streamUrl = urlObject.url;
|
||||||
|
|
||||||
return {apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port};
|
return {
|
||||||
|
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
|
||||||
|
tid: new Date().getTime().toString(16)
|
||||||
|
};
|
||||||
},
|
},
|
||||||
parse: function (url) {
|
parse: function (url) {
|
||||||
// @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
|
// @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
|
||||||
|
@ -289,7 +293,8 @@ function SrsRtcPlayerAsync() {
|
||||||
var session = await new Promise(function(resolve, reject) {
|
var session = await new Promise(function(resolve, reject) {
|
||||||
// @see https://github.com/rtcdn/rtcdn-draft
|
// @see https://github.com/rtcdn/rtcdn-draft
|
||||||
var data = {
|
var data = {
|
||||||
api: conf.apiUrl, streamurl: conf.streamUrl, clientip: null, sdp: offer.sdp
|
api: conf.apiUrl, tid: conf.tid, streamurl: conf.streamUrl,
|
||||||
|
clientip: null, sdp: offer.sdp
|
||||||
};
|
};
|
||||||
console.log("Generated offer: ", data);
|
console.log("Generated offer: ", data);
|
||||||
|
|
||||||
|
@ -315,7 +320,7 @@ function SrsRtcPlayerAsync() {
|
||||||
|
|
||||||
// Close the player.
|
// Close the player.
|
||||||
self.close = function() {
|
self.close = function() {
|
||||||
self.pc.close();
|
self.pc && self.pc.close();
|
||||||
self.pc = null;
|
self.pc = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -354,7 +359,10 @@ function SrsRtcPlayerAsync() {
|
||||||
|
|
||||||
var streamUrl = urlObject.url;
|
var streamUrl = urlObject.url;
|
||||||
|
|
||||||
return {apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port};
|
return {
|
||||||
|
apiUrl: apiUrl, streamUrl: streamUrl, schema: schema, urlObject: urlObject, port: port,
|
||||||
|
tid: new Date().getTime().toString(16)
|
||||||
|
};
|
||||||
},
|
},
|
||||||
parse: function (url) {
|
parse: function (url) {
|
||||||
// @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
|
// @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue