Coder Social home page Coder Social logo

hugo-marques-m3 / cloudinary-js-streaming Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudinary/cloudinary-js-streaming

0.0 0.0 0.0 182 KB

Cloudinary Live Streaming Javascript SDK

Home Page: https://cloudinary.com/

License: MIT License

JavaScript 99.03% HTML 0.97%

cloudinary-js-streaming's Introduction

Cloudinary

Cloudinary is a cloud service that offers a solution to a web application's entire image and video management pipeline. Easily upload images and videos to the cloud. Automatically perform smart resizing, cropping and conversion without installing any complex software. Cloudinary offers comprehensive APIs and administration capabilities and is easy to integrate with any web application, existing or new.

Video live-stream

Cloudinary provides an end-to-end live video streaming solution, with on the fly video effects and transformations. When using the library in your website this is what happens:

  1. Video is streamed from the device's camera.
  2. The video is up-streamed through Cloudinary, providing the streaming user with a Cloudinary public id and resource url of the stream.
  3. The stream is now publicly available through that url, and can be fed into any streaming-supported video player.
  4. If any transformations and effects were added to the stream (during configuration, see below) all the viewers will see the modified stream.

Setup

  1. Sign up for a free account.

  2. Create an upload preset here, and enable the live-streaming setting. This is also the place to add any wanted effects and transformations under the 'Incoming transformations' section.

  3. fetch the library from npm:

    npm install @cloudinary/js-streaming

Usage

Streaming

After completing the setup, import the library and initialize it. There are two required parameters:

  • cloudName - this is the cloud name assigned to you when creating the Cloudinary free account.
  • uploadPreset - This is the name of the upload preset created in step two of the setup.

There are several optional parameters:

  • debug: Log level (disabled by default), one of, or array of ['trace', 'debug', 'vdebug', 'log', 'warn', 'error']. pass in 'all' to print all messages.
  • bandwidth: Bandwith, in bits. Default is 1Mbit/s (1024 * 1024).
  • hlsTarget: [true/false], When true, will stream live using hls protocol.
  • fileTarget: [true/false], When true, will save an mp4 file in your Cloudinary media library.
  • facebookUri: A Facebook streaming URI used to direct the stream to facebook. Supplied by facebook when configuring Facebook streaming.
  • youtubeUri: A Youtube streaming URI used to direct the stream to Youtube. Supplied by youtube when configuring Youtube streaming.
  • events: callback for events, supporting the following functions:
    • start: Called when the streaming starts. Includes the recording Id.
    • stop: Called when the streaming stops. Includes the recording Id.
    • error: Called when the library encounters an error. The error message is included in the callback.
    • local_stream: Called when the stream is available locally (stream is provided in the callback). This can be used to display to the user his own streaming as it up-streams.
import {initLiveStream} from '@cloudinary/js-streaming'

// ...

// configure your cloud name and the live-stream enabled upload-preset:
const cloudName = [your-cloud-name];
const uploadPreset = [your-upload-preset];
let liveStreamLibrary;

// ...

// call initLiveStream with the configuration parameters:
initLiveStream({
 cloudName: cloudName,
            uploadPreset: uploadPreset,
            debug: "all",
            hlsTarget: true,
            fileTarget: true,
            events: {
                start: function (args) {
                  // user code
                },
                stop: function (args) {
                  // user code
                },
                error: function(error){
                  // user code
                },
                local_stream: function (stream) {
                  // user code, typically attaching the stream to a video view:
                  liveStreamLibrary.attach($("#thevideo").get(0), stream);
                }
            }
}).then((result) => {
  // keep handle to instance to start/stop streaming 
  liveStreamLibrary = result;
  
  
  // Extract public id and url from result (publish the url for people to watch the stream):
  let publicId = result.response.public_id;
  let url = result.response.secure_url;
  
  // start the streaming:
  liveStreamLibrary.start(publicId);
})

Camera Control

The live streaming sdk contains some convenient camera functions:

  • attachCamera: Show camera in an html
  • detachCamera - Remove camera from an html
  • Streamer - a convenient wrapper for camera and streaming functions, it exposes functions for showing, hiding and streaming from a camera.

attachCamera & detachCamera

import {attachCamera, detachCamera} from '@cloudinary/js-streaming';
const video = document.getElementById("video");
const facingMode =  { exact: "user" };

// Show camera in an html <video> element
// facingMode is optional
attachCamera(video, facingMode).then(stream=>console.log(stream));

// Remove camera from an html <video> element
detachCamera(video).then(videoElement=>console.log(videoElement));

Streamer

import {Streamer} from '@cloudinary/js-streaming';
const video = document.getElementById("video");
const facingMode =  { exact: "user" };
const liveStreamOptions = {};
const streamer = new Streamer(video);

// Show camera in an html <video> element
// facingMode is optional
streamer.attachCamera(facingMode).then(stream=>console.log(stream));

// Remove camera from an html <video> element
streamer.detachCamera().then(videoElement=>console.log(videoElement));

// Initialize live-streaming using streaming configuration object
streamer.initLiveStream(liveStreamOptions).then(result => console.log(result));

Additional resources

Additional resources are available at:

Support

You can open an issue through GitHub.

Contact us https://cloudinary.com/contact

Stay tuned for updates, tips and tutorials: Blog, Twitter, Facebook.

Join the Community

Impact the product, hear updates, test drive new features and more! Join here.

License

Released under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.