Hello,
I have built a draft peer-to-peer chat system using example codes provided on your website.
The transfer works fine in one direction but I can not make it to transfer audio/video in other direction.
I was trying to transfer audio/video in both directions using the same token/session IDs but it did not work so then I used two pairs of token/session IDs.
One pair is used to transmit (sender --> receiver) and this one works fine. Another pair supposed to be used to transmit (receiver --> sender).
But this one also has issues - seems that web browser allows only for one open flash session (?), I'm not sure how to explain this.
Could someone possibly explain to me how to implement audio/video transmission in both directions?
I will appreciate a help
Robert
I have placed my code here:
The sender code:
<?php
require_once 'opentok/API_Config.php';
require_once 'opentok/OpenTokSDK.php';
$apiObj_one = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);
$apiObj_two = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);
$ApiKey = constant("API_Config::API_KEY");
$session_one = $apiObj_one->create_session($_SERVER["REMOTE_ADDR"], array(SessionPropertyConstants::P2P_PREFERENCE=>"enabled"));
$session_two = $apiObj_two->create_session($_SERVER["REMOTE_ADDR"], array(SessionPropertyConstants::P2P_PREFERENCE=>"enabled"));
$CustomSessionID_one = $session_one->getSessionId();
$CustomTokenID_one = $apiObj_one->generate_token($CustomSessionID_one, RoleConstants::PUBLISHER);
$CustomSessionID_two = $session_two->getSessionId();
$CustomTokenID_two = $apiObj_two->generate_token($CustomSessionID_two, RoleConstants::PUBLISHER);
?>
<html>
<head>
<title>Pet end node</title>
<script src="http://static.opentok.com/v0.91/js/TB.min.js"></script>
<script type="text/javascript">
var apiKey = "<?php echo $ApiKey; ?>";
var sessionId_one = "<?php echo $CustomSessionID_one; ?>";
var token_one = "<?php echo $CustomTokenID_one; ?>";
TB.setLogLevel(TB.DEBUG); // Set this for helpful debugging messages in console
if (TB.checkSystemRequirements() != TB.HAS_REQUIREMENTS) {
alert('Minimum System Requirements not met!');
}
var session_one;
session_one = TB.initSession(sessionId_one);
session_one.addEventListener('sessionConnected', sessionConnectedHandler);
session_one.connect(apiKey, token_one);
var publisher_one;
var publishProps_one;
function sessionConnectedHandler(event) {
publishProps_one = {height:240, width:320, name:"Pet's stream"};
publisher_one = TB.initPublisher(apiKey, 'myPublisherDiv', publishProps_one);
session_one.publish(publisher_one);
}
</script>
</head>
<body>
<div id="myPublisherDiv"></div>
</body>
</html>
Received code:
<?php
$session_one = $_GET['SessionID_one'];
$token_one = $_GET['TokenID_one'];
$session_two = $_GET['SessionID_two'];
$token_two = $_GET['TokenID_two'];
$api = $_GET['ApiID'];
?>
<html>
<head>
<title>Client and node</title>
<script src="http://static.opentok.com/v0.91/js/TB.min.js"></script>
<script type="text/javascript">
var apiKey = "<?php echo $api; ?>";
var sessionId_one = "<?php echo $session_one; ?>";
var token_one = "<?php echo $token_one; ?>";
TB.setLogLevel(TB.DEBUG);
var session_one = TB.initSession(sessionId_one);
session_one.addEventListener('sessionConnected', sessionConnectedHandler);
session_one.addEventListener('streamCreated', streamCreatedHandler);
session_one.connect(apiKey, token_one);
function sessionConnectedHandler(event) {
subscribeToStreams(event.streams);
}
function streamCreatedHandler(event) {
// Subscribe to any new streams that are created
subscribeToStreams(event.streams);
}
function subscribeToStreams(streams) {
for (var i = 0; i < streams.length; i++) {
if (streams[i].connection.connectionId == session_one.connection.connectionId) {
return;
}
// Create the div to put the subscriber element in to
var div = document.createElement('div');
div.setAttribute('id', 'stream' + streams[i].streamId);
document.body.appendChild(div);
// Subscribe to the stream
session_one.subscribe(streams[i], div.id);
}
}
</script>
</head>
<body>
<div id="myPublisherDiv"></div>
</body>
</html>