Tools: Update one to one demo

pull/2357/head
winlin 4 years ago
parent fc23b9e5f5
commit 74043b4153

@ -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()
@ -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 {
@ -228,7 +231,6 @@ func main() {
}
var res interface{}
var p *Participant
if action.Message.Action == "join" {
obj := struct {
Message struct {
@ -241,7 +243,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")
}
@ -258,7 +260,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 {
@ -276,7 +278,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)
}

@ -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) {

@ -106,6 +106,7 @@ function SrsRtcSignalingParse(location) {
host = host? host.split('&')[0] : location.hostname;
let room = location.href.split('room=')[1];
room = room? room.split('&')[0] : null;
let display = location.href.split('display=')[1];
display = display? display.split('&')[0] : new Date().getTime().toString(16).substr(3);

@ -45,19 +45,22 @@
</div>
<div class="row">
<div class="span5">
<div class="span4">
<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>
<span id='self'></span>
</div>
<div class="span6">
<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>
<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>
@ -65,6 +68,8 @@
var sig = null;
var publisher = null;
var player = null;
var control_refresh_peer = null;
var control_alert_peer = null;
$(function(){
console.log('?wss=x to specify the websocket schema, ws or wss');
console.log('?wsh=x to specify the websocket server ip');
@ -85,13 +90,32 @@
sig = new SrsRtcSignalingAsync();
sig.onmessage = function (msg) {
console.log('Notify: ', msg);
msg.participants.forEach(function (participant) {
if (participant.display === display || !participant.publishing) return;
startPlay(host, room, participant.display);
});
if (msg.event === 'publish') {
msg.participants.forEach(function (participant) {
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);
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});
console.log('Signaling: join ok', r0);

@ -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) {
@ -69,7 +69,8 @@ function SrsRtcPublisherAsync() {
var session = await new Promise(function (resolve, reject) {
// @see https://github.com/rtcdn/rtcdn-draft
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);
@ -101,7 +102,7 @@ function SrsRtcPublisherAsync() {
// Close the publisher.
self.close = function () {
self.pc.close();
self.pc && self.pc.close();
self.pc = null;
};
@ -141,7 +142,10 @@ function SrsRtcPublisherAsync() {
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) {
// @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) {
// @see https://github.com/rtcdn/rtcdn-draft
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);
@ -315,7 +320,7 @@ function SrsRtcPlayerAsync() {
// Close the player.
self.close = function() {
self.pc.close();
self.pc && self.pc.close();
self.pc = null;
};
@ -354,7 +359,10 @@ function SrsRtcPlayerAsync() {
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) {
// @see: http://stackoverflow.com/questions/10469575/how-to-use-location-object-to-parse-url-without-redirecting-the-page-in-javascri

Loading…
Cancel
Save