Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
    <title>Cloudinary Live Streaming Example</title>
    <script src="https://unpkg.com/@cloudinary/js-streaming/dist/js-streaming.js"></script>
</head>

<body class="center-text">
<div class="container wrapper">
    <h1>Cloudinary Live Streaming Example</h1>
    <div class="video-wrapper">
        <video class="video" id="video" autoPlay muted="true" playsInline></video>
    </div>
    <div class="center">
        <div class="center-text">
            <button id="viewbtn" onclick="view();">Show camera</button>
            <button id="hidebtn" onclick="hide();">Hide camera</button>
        </div>
        <div class="center-text">
            <button id="initbtn" onclick="initialize();">Initialize stream</button>
            <button disabled id="startbtn" onclick="start();">Start stream</button>
            <button disabled id="stopbtn" onclick="stop();">Stop stream</button>
        </div>

        <div class="ui">
            <p>Status: <span id="status">Click on 'initialize Stream' to begin</span></p>
            <p>Public Id: <span id="publicid"></span></p>
            <p>File url: <a id="file_url" target="_blank"></a></p>
            <p>Stream url: <a id="stream_url" target="_blank"></a></p>
                      <p>Embed url: <a id="embed_url" target="_blank"></a></p>
        </div>
    </div>
</div>
</body>

</html>

              
            
!

CSS

              
                .container {
    max-width: 600px;
}

.info {
    height: 400px;
}

.ui {
    text-align: left;
    height: 400px;
}

.wrapper {
    display: inline-block;
}

.video-wrapper {
    width: 320px;
    margin-bottom: 15px;
    display: inline-block;
}

.video {
    width: 100%;
    border: 5px solid black;
}

.recording {
    border: 5px solid red;
}

.center-text {
    text-align: center;
}

.center {
    margin-left: auto;
    margin-right: auto;
    display: block
}

              
            
!

JS

              
                const {initLiveStream, attachCamera, detachCamera} = cloudinaryJsStreaming;
const CLOUD_NAME = '<cloud_name>';
const UPLOAD_PRESET = '<live_stream_preset>';
let liveStream, publicId, url, stream;

function setText(id, text) {
  document.getElementById(id).innerHTML = text;
}

function setStatus(status) {
  setText("status", status);
}

function toggleButton(id, enabled) {
  document.getElementById(id).disabled = !enabled;
}

function toggleBtns(init = false, start = false, stop = false) {
  toggleButton("initbtn", init);
  toggleButton("startbtn", start);
  toggleButton("stopbtn", stop);
}

function setUrl(url, cloudName, publicId) {
  const fileUrl = url + '.mp4';
  const streamUrl = url + '.m3u8';
  const embedUrl = 'https://player.cloudinary.com/embed/?cloud_name='+cloudName+'&public_id='+publicId+'&source%5BsourceTypes%5D%5B0%5D=hls';

  const file_link = document.getElementById('file_url');
  const stream_link = document.getElementById('stream_url');
  const embed_link = document.getElementById('embed_url');

  file_link.href = fileUrl;
  file_link.innerText = fileUrl;
  stream_link.href = streamUrl;
  stream_link.innerText = streamUrl;
  embed_link.href = embedUrl;
  embed_link.innerText = embedUrl;
}

function view(){
  attachCamera(document.getElementById("video")).then(c=>{
    console.log(c);
  })
}

function hide(){
  detachCamera(document.getElementById("video")).then(c=>{
    console.log(c);
  })
}

function start() {
  setStatus("starting...");
  toggleBtns();
  setText("publicid", stream.publicId);
  setUrl(stream.url, CLOUD_NAME, stream.publicId);
  liveStream.start(publicId);
}

function stop() {
  setStatus("stopping...");
  toggleBtns();
  liveStream.stop();
}

// call initLiveStream with the configuration parameters:
function initialize() {
  setStatus("initializing...");
  toggleBtns();

  initLiveStream({
    cloudName: CLOUD_NAME,
    uploadPreset: UPLOAD_PRESET,
    debug: "all",
    hlsTarget: true,
    fileTarget: true,
    events: {
      start: function (args) {
        setStatus("started");
        document.getElementById("video").className = "video recording";
        toggleBtns(false, false, true);
      },
      stop: function (args) {
        setStatus("stopped");
        document.getElementById("video").className = "video";
        toggleBtns(true, false, false);
      },
      error: function (error) {
        setStatus("error: " + error);
        toggleBtns(true, false, false);
      },
      local_stream: function (stream) {
        setStatus("local stream");
        // Attach the stream to a video element:
        liveStream.attach(document.getElementById("video"), stream);
      }
    }
  }).then(result => {
    // keep handle to instance to start/stop streaming
    liveStream = result;

    // Extract public id and url from result (publish the url for people to watch the stream):
    publicId = result.response.public_id;
    url = 'https://res.cloudinary.com/'+CLOUD_NAME+'/video/upload/' + publicId;

    setStatus("initialized");
    // setText("publicid", publicId);
    // setUrl(url, CLOUD_NAME, publicId);
    stream = { publicId: publicId, url: url }

    toggleBtns(false, true, false);
  });
}
              
            
!
999px

Console