Skip to content

richgoldmd/opentok-hardware-setup.js

 
 

Repository files navigation

Build Status Test Coverage

OpenTok Hardware Setup

The OpenTok Hardware Setup component provides a user interface for clients using the [OpenTok.js] 1 library to select the camera and microphone. The client can use the camera and microphone to publish a stream to an OpenTok.js session.

Demo

You can see the component running here.

Building the component

This project uses Node.js and gulp to build the distribution versions of the component.

Run the following to build the component:

npm install -g gulp
npm install
gulp

This creates a .js file that is useable with JavaScript module loaders and puts it in the ./dist directory, along with the .css file and a minified version of the .js file.

Copy the files in the ./dist directory to the appropriate paths on your web server.

Note that a pre-built version of this file is hosted at static.opentok.com. Load both the CSS and JavaScript files (in addition to the OpenTok.js library):

<link rel="stylesheet" type="text/css"
  href="https://static.opentok.com/hardware-setup/v1/css/opentok-hardware-setup.css">
<script
  src="https://static.opentok.com/hardware-setup/v1/js/opentok-hardware-setup.js"></script>

Browserify

You can copy the CSS in this example to public/css/hardware-setup.css automatically using NPM:

{
  "dependencies":{"opentok-hardware-setup.js":"~1.0.0"},
  "scripts":{
    "postinstall":"cp -r node_modules/opentok-hardware-setup.js/css/opentok-hardware-setup.css public/css/opentok-hardware-setup.css"
  }
}

Using the component

Use the component along with the [OpenTok.js] 1 library.

Important restrictions: Due to limitations in other browsers, the Hardware Setup component is only available in Chrome and only works on sites loaded via HTTPS.

createOpentokHardwareSetupComponent()

To initialize the Hardware Setup component, call the createOpentokHardwareSetupComponent() method. This method takes the following parameters:

  • targetElement -- The DOM element in which to insert the hardware setup component. (See the insertMode property of the next parameter, options.)

  • options -- An optional argument that specifies how the component will be inserted in the HTML DOM, in relation to the targetElement parameter. You can set this parameter to one of the following values:

    • "replace" -- The component replaces contents of the targetElement. This is the default.
    • "after" -- The component is a new element inserted after the targetElement in the HTML DOM. (Both the component and targetElement have the same parent element.)
    • "before" -- The component is a new element inserted before the targetElement in the HTML DOM. (Both the component and targetElement have the same parent element.)
    • "append" -- The component is a new element added as a child of the targetElement. If there are other child elements, the component is appended as the last child element of the targetElement.
  • completionHandler -- A function that is called is called when the component is rendered on the page or on error in calling to the method. Upon error, this function is passed an error object which has a message property, which can have one of the following values:

    • "No element provided to place component"
    • "This browser does not support getMediaDevices APIs"
    • "There are no audio or video devices available".

    When the component is rendered on the page successfully, the completion handler is called with no error object.

The method returns a HardwareSetup object, which has the following methods: audioSource(), videoSource(), and destroy().

Example:

// Replace this with the ID of the target DOM element for the component
var element = document.querySelector('#hardware-setup');

var options = {
  insertMode: 'append' // Or use another insertMode setting.
};

var component = createOpentokHardwareSetupComponent(element, options, function(error) {
  if (error) {
    console.error('Error: ', error);
    element.innerHTML = '<strong>Error getting devices</strong>: '
      error.message;
    return;
  }
  // Add a button to call component.destroy() to close the component.
});

HardwareSetup.audioSource()

Returns an object representing the selected audio source. This object has a deviceId property, which is the unique audio device ID (a string). You can store this string in a cookie for use in a future session. You can pass the audio source object or its deviceId property as a value for the audioSource property of the properties object passed into the OT.initPublisher() method.

Example:

// component is the object returned by createOpentokHardwareSetupComponent()
var publisherOptions = {
  audioSource: component.audioSource(),
  videoSource: component.videoSource()
};
OT.initPublisher(targetElement, publisherOptions);

HardwareSetup.videoSource()

Returns an object representing the selected video source. This object has a deviceId property, which is the unique video device ID (a string). You can store this string in a cookie for use in a future session. You can pass the video source object or its deviceId property as a value for the videoSource property of the properties object passed into the OT.initPublisher() method.

Example:

// component is the object returned by createOpentokHardwareSetupComponent()
var publisherOptions = {
  audioSource: component.audioSource(),
  videoSource: component.videoSource()
};
OT.initPublisher(targetElement, publisherOptions);

HardwareSetup.destroy()

Closes the hardware setup component (if visible) and removes it from the HTML DOM.

Example:

// component is the object returned by createOpentokHardwareSetupComponent()
component.destroy();

Sample

See the index.html file in this repository for sample code.

License

The MIT License (MIT)

Copyright (c) 2015 TokBox, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.4%
  • HTML 2.6%