OpenTok 1:1 对话的问题
Troubles with OpenTok 1:1 conversations
我正在尝试跨浏览器进行 1:1 opentok 对话。
有两个角色:
一位邀请的发布者和一位被邀请者。目的是让这两个人进行 1:1 对话。
会话正确进行,被邀请者连接到流。受邀者接收发布者的视频流并将其添加到其容器中。然后他开始发布到同一个会话。
因此,受邀者现在可以看到两个视频。(yay)...
但是,发布者只能看到他自己的视频。我根本无法检测到受邀者发布的流。
发布者函数如下:
function startVideoHost(sessionId, apiKey, token) {
var replacementElementId = "localVideo";
if (OT.checkSystemRequirements() == 1) {
publisher = OT.initPublisher(replacementElementId);
publisher.on({
streamCreated: function (event) {
$('.videoWindow').show();
console.log("Publisher started streaming.");
},
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
// this is kinda where id hope the publisher would detect the guest stream
session = OT.initSession(apiKey, sessionId);
session.on("streamCreated", function(event) {
session.subscribe(event.stream, remoteVideos);
});
session.connect(token, function (error) {
if (session.capabilities.publish == 1) {
session.publish(publisher);
} else {
console.log("You cannot publish an audio-video stream.");
}
});
} else {
console.log("The client does not support WebRTC.");
}
}
这里是受邀者函数:
function startVideoGuest(sessionId, apiKey, token) {
if (OT.checkSystemRequirements() == 1) {
var session = OT.initSession(apiKey, sessionId);
session.on("streamCreated", function(event) {
session.subscribe(event.stream, remoteVideos);
});
session.connect(token, function(error) {
if (error) {
console.log("Error connecting: ", error.code, error.message);
} else {
$('.videoWindow').show();
var replacementElementId = "localVideo";
publisher = OT.initPublisher(replacementElementId);
publisher.on({
streamCreated: function (event) {
console.log("Publisher started streaming.");
},
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
console.log("Connected to the session.");
}
});
}
}
有谁知道我为什么不将受邀视频流返回给发布者?
我看到的一个问题是 'invitee' 调用 OT.initPublisher()
会触发被邀请者的网络摄像头开始录制视频并在被邀请者的 DOM 中显示该流,但被邀请者从未实际上将该网络摄像头发布到会话中。这就是为什么受邀者可以在他们的浏览器中看到视频,但 'host' 永远不会在会话中看到该视频。
一个解决方案是在 function startVideoGuest
的 session.connect
回调中调用 session.publish(publisher)
。我已经在下面修改了您的代码:
//.....
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
console.log("Connected to the session.");
//I have added the line below
session.publish(publisher);
//......
一个澄清:只要会话中的客户端发送视频 and/or 音频,他们就是发布者。由于您似乎需要 'invitee' 将视频发送到 'host','invitee' 也是发布者,因此必须调用 session.publish
。客户端可以同时是发布者和订阅者。它们是可选属性,不是独占角色。
我正在尝试跨浏览器进行 1:1 opentok 对话。
有两个角色:
一位邀请的发布者和一位被邀请者。目的是让这两个人进行 1:1 对话。
会话正确进行,被邀请者连接到流。受邀者接收发布者的视频流并将其添加到其容器中。然后他开始发布到同一个会话。
因此,受邀者现在可以看到两个视频。(yay)...
但是,发布者只能看到他自己的视频。我根本无法检测到受邀者发布的流。
发布者函数如下:
function startVideoHost(sessionId, apiKey, token) {
var replacementElementId = "localVideo";
if (OT.checkSystemRequirements() == 1) {
publisher = OT.initPublisher(replacementElementId);
publisher.on({
streamCreated: function (event) {
$('.videoWindow').show();
console.log("Publisher started streaming.");
},
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
// this is kinda where id hope the publisher would detect the guest stream
session = OT.initSession(apiKey, sessionId);
session.on("streamCreated", function(event) {
session.subscribe(event.stream, remoteVideos);
});
session.connect(token, function (error) {
if (session.capabilities.publish == 1) {
session.publish(publisher);
} else {
console.log("You cannot publish an audio-video stream.");
}
});
} else {
console.log("The client does not support WebRTC.");
}
}
这里是受邀者函数:
function startVideoGuest(sessionId, apiKey, token) {
if (OT.checkSystemRequirements() == 1) {
var session = OT.initSession(apiKey, sessionId);
session.on("streamCreated", function(event) {
session.subscribe(event.stream, remoteVideos);
});
session.connect(token, function(error) {
if (error) {
console.log("Error connecting: ", error.code, error.message);
} else {
$('.videoWindow').show();
var replacementElementId = "localVideo";
publisher = OT.initPublisher(replacementElementId);
publisher.on({
streamCreated: function (event) {
console.log("Publisher started streaming.");
},
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
console.log("Connected to the session.");
}
});
}
}
有谁知道我为什么不将受邀视频流返回给发布者?
我看到的一个问题是 'invitee' 调用 OT.initPublisher()
会触发被邀请者的网络摄像头开始录制视频并在被邀请者的 DOM 中显示该流,但被邀请者从未实际上将该网络摄像头发布到会话中。这就是为什么受邀者可以在他们的浏览器中看到视频,但 'host' 永远不会在会话中看到该视频。
一个解决方案是在 function startVideoGuest
的 session.connect
回调中调用 session.publish(publisher)
。我已经在下面修改了您的代码:
//.....
streamDestroyed: function (event) {
console.log("Publisher stopped streaming. Reason: "
+ event.reason);
}
});
console.log("Connected to the session.");
//I have added the line below
session.publish(publisher);
//......
一个澄清:只要会话中的客户端发送视频 and/or 音频,他们就是发布者。由于您似乎需要 'invitee' 将视频发送到 'host','invitee' 也是发布者,因此必须调用 session.publish
。客户端可以同时是发布者和订阅者。它们是可选属性,不是独占角色。