[
  {
    "path": ".gitignore",
    "content": ".DS_STORE\n.vsCode/\nnode_modules"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2017 Diogo Santos\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE."
  },
  {
    "path": "README.md",
    "content": "This is a basic sample of how to do video streaming using Node.js and HTML5\n\n# Install\n\n- git clone\n- npm install\n- npm start\n- open browser in `localhost:3000`"
  },
  {
    "path": "index.htm",
    "content": "<html>\n  <head>\n    <title>Video stream sample</title>\n  </head>\n  <body>\n    <video id=\"videoPlayer\" controls muted=\"muted\" autoplay> \n      <source src=\"/video\" type=\"video/mp4\">\n    </video>\n  </body>\n</html>"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"video-stream-sample\",\n  \"version\": \"1.0.0\",\n  \"description\": \"Streaming video example\",\n  \"main\": \"server.js\",\n  \"engines\": {\n    \"node\": \">=6.2.0\"\n  },\n  \"scripts\": {\n    \"start\": \"nodemon server.js\"\n  },\n  \"author\": \"daspinola\",\n  \"license\": \"MIT\",\n  \"dependencies\": {\n    \"express\": \"^4.15.3\",\n    \"nodemon\": \"^1.11.0\"\n  }\n}\n"
  },
  {
    "path": "public/index.js",
    "content": "document.addEventListener('DOMContentLoaded', init, false);\n\n/** \n* You can manipulate the video here\n* For example: Uncomment the code below and in the index to get a Start/Stop button\n*/\nfunction init() {\n  // const VP = document.getElementById('videoPlayer')\n  // const VPToggle = document.getElementById('toggleButton')\n\n  // VPToggle.addEventListener('click', function() {\n  //   if (VP.paused) VP.play()\n  //   else VP.pause()\n  // })\n}\n"
  },
  {
    "path": "server.js",
    "content": "const express = require('express')\nconst fs = require('fs')\nconst path = require('path')\nconst app = express()\n\napp.use(express.static(path.join(__dirname, 'public')))\n\napp.get('/', function(req, res) {\n  res.sendFile(path.join(__dirname + '/index.htm'))\n})\n\napp.get('/video', function(req, res) {\n  const path = 'assets/sample.mp4'\n  const stat = fs.statSync(path)\n  const fileSize = stat.size\n  const range = req.headers.range\n\n  if (range) {\n    const parts = range.replace(/bytes=/, \"\").split(\"-\")\n    const start = parseInt(parts[0], 10)\n    const end = parts[1]\n      ? parseInt(parts[1], 10)\n      : fileSize-1\n\n    if(start >= fileSize) {\n      res.status(416).send('Requested range not satisfiable\\n'+start+' >= '+fileSize);\n      return\n    }\n    \n    const chunksize = (end-start)+1\n    const file = fs.createReadStream(path, {start, end})\n    const head = {\n      'Content-Range': `bytes ${start}-${end}/${fileSize}`,\n      'Accept-Ranges': 'bytes',\n      'Content-Length': chunksize,\n      'Content-Type': 'video/mp4',\n    }\n\n    res.writeHead(206, head)\n    file.pipe(res)\n  } else {\n    const head = {\n      'Content-Length': fileSize,\n      'Content-Type': 'video/mp4',\n    }\n    res.writeHead(200, head)\n    fs.createReadStream(path).pipe(res)\n  }\n})\n\napp.listen(3000, function () {\n  console.log('Listening on port 3000!')\n})\n"
  }
]