daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Component, OnInit, ViewChild} from '@angular/core';
  2. import {DomSanitizer} from '@angular/platform-browser';
  3.  
  4. import {WindowRefService} from './WindowRefService';
  5.  
  6. import * as io from 'socket.io-client';
  7.  
  8. const async = require('async');
  9.  
  10. @Component({
  11.   selector: 'app-video',
  12.   templateUrl: './video.component.html',
  13.   styleUrls: ['./video.component.css']
  14. })
  15. export class VideoComponent implements OnInit {
  16.  
  17.   @ViewChild('video') video: any;
  18.  
  19.   private mediaSource: MediaSource;
  20.   private sourceBuffer: SourceBuffer;
  21.  
  22.   private window: Window;
  23.   private src: any;
  24.  
  25.   constructor(private domSanitizer: DomSanitizer) {
  26.     this.window = WindowRefService.nativeWindow;
  27.     this.mediaSource = new MediaSource();
  28.   }
  29.  
  30.   ngOnInit(): void {
  31.  
  32.     let objectURL = this.window.URL.createObjectURL(this.mediaSource);
  33.     this.src = this.domSanitizer.bypassSecurityTrustResourceUrl(objectURL);
  34.  
  35.     if (!this.sourceBuffer && this.mediaSource.readyState === 'open') {
  36.       this.sourceBuffer = this.mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
  37.     }
  38.  
  39.     let chunks: any[] = [];
  40.  
  41.     this.invoke(this, chunks);
  42.   }
  43.  
  44.   invoke(context: VideoComponent, chunks: any[]) {
  45.  
  46.     async.waterfall([
  47.       getChunkData,
  48.       appendChunkData
  49.     ], function (err: any, result: any) {
  50.       // TODO: implement main callback
  51.       console.log('finished streaming all chunks')
  52.     });
  53.  
  54.     function getChunkData(callback: any): void {
  55.  
  56.       let socket = io.connect('http://localhost:8080');
  57.  
  58.       socket.emit('video_stream_req', false);
  59.  
  60.       socket.on('vs', function (data: any) {
  61.         chunks.push(data);
  62.       });
  63.  
  64.       socket.on('finish', function () {
  65.         callback(null);
  66.       });
  67.     }
  68.  
  69.     function appendChunkData(callback: any): void {
  70.  
  71.       for (let i in chunks) {
  72.         console.log('streaming chunk #' + i);
  73.         context.stream(chunks[i], context);
  74.       }
  75.  
  76.       callback(null, 'done');
  77.     }
  78.   }
  79.  
  80.   stream(chunk: any, context: VideoComponent): void {
  81.  
  82.     async.waterfall([
  83.       validate,
  84.       addSourceBuffer,
  85.       append
  86.     ], function (err: any, result: any) {
  87.       // TODO: implement main callback
  88.       console.log('finished appending chunk to buffer')
  89.     });
  90.  
  91.     function validate(callback: any): void {
  92.  
  93.       console.log('validating chunk');
  94.       if (chunk === null) {
  95.         callback('null chunk');
  96.       } else {
  97.         callback(null);
  98.       }
  99.     }
  100.  
  101.     function addSourceBuffer(callback: any): void {
  102.  
  103.       if (!context.sourceBuffer && context.mediaSource.readyState === 'open') {
  104.         console.log('adding source buffer');
  105.         context.sourceBuffer = context.mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
  106.       }
  107.  
  108.       callback(null);
  109.     }
  110.  
  111.     function append(callback: any): void {
  112.  
  113.       try {
  114.         console.log('appending chunk to buffer');
  115.         context.sourceBuffer.appendBuffer(chunk);
  116.  
  117.         context.sourceBuffer.addEventListener('updateend', function () {
  118.  
  119.           callback(null, 'done');
  120.         });
  121.       } catch (e) {
  122.         if (e.name === 'QuotaExceededError') {
  123.           console.log(e.name);
  124.         }
  125.       }
  126.     }
  127.   }
  128.  
  129. }
RAW Paste Data
Top