Skip to content

Commit

Permalink
basic video-feed with data-packet testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ezraezraezra committed Mar 20, 2012
1 parent 00727d9 commit 8f4e67e
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README
Expand Up @@ -6,12 +6,13 @@
* Title: AwesomeSauce URL coming soon
* Description: Coming soon
*
* Tech: Facebook API, JavaScript, jQuery, MySQL, PHP
* Last Update: March 12th, 2012
* Tech: Facebook API, JavaScript, jQuery, MySQL, OpenTok API, PHP
* Last Update: March 19th, 2012
***********************************************************************/

NOTE: *** 3rd PARTY LIBRARIES HAVE BEEN OMITTED FROM THE PUBLIC REPO ***

Libraries Omitted: jQuery http://www.jquery.com
FBook PHP SDK https://github.com/facebook/facebook-php-sdk
OpenTok http://www.tokbox.com
Timepicker http://trentrichardson.com/examples/timepicker/
160 changes: 160 additions & 0 deletions js/tb.js
@@ -0,0 +1,160 @@
/**
* @author Ezra Velazquez
*/

console.log("hello world");

var OpenTok = function() {
var apiKey = 13249262; // OpenTok sample API key. Replace with your own API key.
var sessionId = '153975e9d3ecce1d11baddd2c9d8d3c9d147df18'; // Replace with your session ID.
var token = 'devtoken'; // Should not be hard-coded.
// Add to the page using the OpenTok server-side libraries.
var session;
var publisher;
var subscribers = {};

//--------------------------------------
// LINK CLICK HANDLERS
//--------------------------------------
function connect() {
console.log(token);
session.connect(apiKey, token);
}

function disconnect() {
session.disconnect();
}

// Called when user wants to start publishing to the session
function startPublishing() {
if (!publisher) {
var parentDiv = document.getElementById("video_feed_instructor");
var publisherDiv = document.createElement('div'); // Create a div for the publisher to replace
publisherDiv.setAttribute('id', 'opentok_publisher');
parentDiv.appendChild(publisherDiv);
publisher = session.publish(publisherDiv.id); // Pass the replacement div id to the publish method
}
}

function stopPublishing() {
if (publisher) {
session.unpublish(publisher);
}
publisher = null;
}

//--------------------------------------
// OPENTOK EVENT HANDLERS
//--------------------------------------
function sessionConnectedHandler(event) {
startPublishing();

// Subscribe to all streams currently in the Session
for (var i = 0; i < event.streams.length; i++) {
addStream(event.streams[i]);
}
}

function streamCreatedHandler(event) {
// Subscribe to the newly created streams
for (var i = 0; i < event.streams.length; i++) {
addStream(event.streams[i]);
}
}

function streamDestroyedHandler(event) {
// This signals that a stream was destroyed. Any Subscribers will automatically be removed.
// This default behaviour can be prevented using event.preventDefault()
}

function sessionDisconnectedHandler(event) {
// This signals that the user was disconnected from the Session. Any subscribers and publishers
// will automatically be removed. This default behaviour can be prevented using event.preventDefault()
publisher = null;
}

function connectionDestroyedHandler(event) {
// This signals that connections were destroyed
}

function connectionCreatedHandler(event) {
// This signals new connections have been created.
}

/*
If you un-comment the call to TB.addEventListener("exception", exceptionHandler) above, OpenTok calls the
exceptionHandler() method when exception events occur. You can modify this method to further process exception events.
If you un-comment the call to TB.setLogLevel(), above, OpenTok automatically displays exception event messages.
*/
function exceptionHandler(event) {
alert("Exception: " + event.code + "::" + event.message);
}

//--------------------------------------
// HELPER METHODS
//--------------------------------------

function addStream(stream) {

// For testing purposes, it all goes to instructor currently!
//console.log(stream);
connection_data = getConnectionData(stream['connection']);
console.log(connection_data);
$(".instructor_name.classroom_labels").html(connection_data['name']);

// Check if this is the stream that I am publishing, and if so do not publish.
if (stream.connection.connectionId == session.connection.connectionId) {
return;
}
var subscriberDiv = document.createElement('div'); // Create a div for the subscriber to replace
subscriberDiv.setAttribute('id', stream.streamId); // Give the replacement div the id of the stream as its id.
document.getElementById("subscribers").appendChild(subscriberDiv);
subscribers[stream.streamId] = session.subscribe(stream, subscriberDiv.id);
}

function show(id) {
document.getElementById(id).style.display = 'block';
}

function hide(id) {
document.getElementById(id).style.display = 'none';
}

function getConnectionData(connection) {
try {
connectionData = JSON.parse(connection.data);
} catch(error) {
connectionData = eval("(" + connection.data + ")" );
}
return connectionData;
}

return {
init : function() {
$.get('../php/server_ajax.php', {
"o" : "classroom"
}, function(data) {
console.log(data);
sessionId = data.session[0];
token = data.token;

if (TB.checkSystemRequirements() != TB.HAS_REQUIREMENTS) {
alert("You don't have the minimum requirements to run this application."
+ "Please upgrade to the latest version of Flash.");
} else {
session = TB.initSession(sessionId); // Initialize session

// Add event listeners to the session
session.addEventListener('sessionConnected', sessionConnectedHandler);
session.addEventListener('sessionDisconnected', sessionDisconnectedHandler);
session.addEventListener('connectionCreated', connectionCreatedHandler);
session.addEventListener('connectionDestroyed', connectionDestroyedHandler);
session.addEventListener('streamCreated', streamCreatedHandler);
session.addEventListener('streamDestroyed', streamDestroyedHandler);

connect();
}
});
}
}
}();
11 changes: 10 additions & 1 deletion php/classroom.php
@@ -1,12 +1,21 @@
<?php
?>
<script src="http://staging.tokbox.com/v0.91/js/TB.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="../js/tb.js"></script>
<script type="text/javascript">
$(document).ready(function() {
OpenTok.init();
//OpenTok.setData();
});

</script>

<div id="container_content" class="container_bodies">
<div class="classroom_container">
<div class="classroom_left_container classroom_inner_container">
<div class="instructor_container">
<span class="instructor_name classroom_labels">Sir George Martin</span>
<div class="video_feed_instructor"></div>
<div class="video_feed_instructor" id="video_feed_instructor"></div>
<div class="instructor_button_container">
<div class="instructor_rate_button instructor_good_img button">75%</div>
<div class="instructor_rate_button instructor_bad_img button">25%</div>
Expand Down
27 changes: 27 additions & 0 deletions php/opentok.php
@@ -0,0 +1,27 @@
<?php
require_once 'lib/API_Config.php';
require_once 'lib/OpenTokSDK.php';

class OpenTok {

function OpenTok() {

}

function generate($name) {
$apiObj = new OpenTokSDK(API_Config::API_KEY, API_Config::API_SECRET);

// Pull this from the server when wanting the workshop
$session = $apiObj->create_session($_SERVER["REMOTE_ADDR"]);

$role = RoleConstants::PUBLISHER; // Or set to the correct role for the user.
// Get username from the FB server?
$metadata = '{"name": "'.$name.'"}';
$token_user = $apiObj->generate_token($session->getSessionId(), $role, NULL, $metadata); // Replace with the correct session ID
$session_id = $session->getSessionId();

$arr = array('session'=>$session_id, 'token'=>$token_user);
return $arr;
}
}
?>
11 changes: 11 additions & 0 deletions php/server_ajax.php
@@ -1,6 +1,7 @@
<?php
header('Content-type: application/json; charset=utf-8');
require('server.php');
require('opentok.php');

$server = new Server();
$server->startApp();
Expand All @@ -25,6 +26,16 @@
//$response = array('test'=>$date);

$response =$server->addWorkshop($title, $tech, $description, $date, $fb_id, $name);
break;
case 'classroom':
require('fbook.php');
// Get fb id from db
$facebook_array = $facebook->api('/1088730508', 'GET');
$facebook_name = $facebook_array['name'];

$opentok = new OpenTok();
$response = $opentok->generate($facebook_name);

break;
default:
$response = array('status'=>'400','message'=>'command not known');
Expand Down

0 comments on commit 8f4e67e

Please sign in to comment.