text: //
  //  Created by Mingliang Chen on 18/3/9.
  //  illuspas[a]gmail.com
  //  Copyright (c) 2018 Nodemedia. All rights reserved.
  //
  
  var SHA256 = require("crypto-js/sha256");;
  
  
  const Logger = require('./node_core_logger');
  
  const context = require('./node_core_ctx');
  
  const EventEmitter = require('events');
  const { spawn } = require('child_process');
  const dateFormat = require('dateformat');
  const mkdirp = require('mkdirp');
  const fs = require('fs');
  
  class NodeTransSession extends EventEmitter {
    constructor(conf) {
      super();
      this.conf = conf;
    }
  
    run() {
  
      if (this.conf.useHashedNames) {
        this.conf.streamName = SHA256(this.conf.streamName)
      }
  
  
      let vc = this.conf.vc || 'copy';
      let ac = this.conf.ac || 'copy';
      let inPath = 'rtmp://127.0.0.1:' + this.conf.rtmpPort + this.conf.streamPath;
      let ouPath = `${this.conf.mediaroot}/${this.conf.streamApp}/${this.conf.streamName}`;
      let mapStr = '';
  
  
      if (this.conf.rtmp && this.conf.rtmpApp) {
        if (this.conf.rtmpApp === this.conf.streamApp) {
          Logger.error('[Transmuxing RTMP] Cannot output to the same app.');
        } else {
          let rtmpOutput = `rtmp://127.0.0.1:${this.conf.rtmpPort}/${this.conf.rtmpApp}/${this.conf.streamName}`;
          mapStr += `[f=flv]${rtmpOutput}|`;
          Logger.log('[Transmuxing RTMP] ' + this.conf.streamPath + ' to ' + rtmpOutput);
        }
      }
      if (this.conf.mp4) {
        this.conf.mp4Flags = this.conf.mp4Flags ? this.conf.mp4Flags : '';
        let mp4FileName = dateFormat('yyyy-mm-dd-HH-MM') + '.mp4';
  
  
        let mp4OutPath = `${this.conf.mp4FileRoot}/recordings/${this.conf.streamName}`;
        let mapMp4 = `${this.conf.mp4Flags}${mp4OutPath}/${mp4FileName}|`;
        mapStr += mapMp4;
        Logger.log('[Transmuxing MP4] ' + this.conf.streamPath + ' to ' + mp4OutPath + '/' + mp4FileName);
  
        mkdirp.sync(mp4OutPath);
        let args = {mp4FileName: mp4FileName}
        context.nodeEvent.emit('savedRecording', this.conf.id, this.conf.streamPath, args);
      }
      if (this.conf.hls) {
        this.conf.hlsFlags = this.conf.hlsFlags ? this.conf.hlsFlags : '';
        let hlsFileName = 'index.m3u8';
        let mapHls = `${this.conf.hlsFlags}${ouPath}/${hlsFileName}|`;
        mapStr += mapHls;
        Logger.log('[Transmuxing HLS] ' + this.conf.streamPath + ' to ' + ouPath + '/' + hlsFileName);
      }
      if (this.conf.dash) {
        this.conf.dashFlags = this.conf.dashFlags ? this.conf.dashFlags : '';
        let dashFileName = 'index.mpd';
        let mapDash = `${this.conf.dashFlags}${ouPath}/${dashFileName}`;
        mapStr += mapDash;
        Logger.log('[Transmuxing DASH] ' + this.conf.streamPath + ' to ' + ouPath + '/' + dashFileName);
      }
      mkdirp.sync(ouPath);
      let argv = ['-y', '-fflags', 'nobuffer', '-i', inPath];
      Array.prototype.push.apply(argv, ['-c:v', vc]);
      Array.prototype.push.apply(argv, this.conf.vcParam);
      Array.prototype.push.apply(argv, ['-c:a', ac]);
      Array.prototype.push.apply(argv, this.conf.acParam);
    //  Array.prototype.push.apply(argv, ['-crf',]);
      Array.prototype.push.apply(argv, ['-f', 'tee', '-map', '0:a?', '-map', '0:v?', mapStr]);
      argv = argv.filter((n) => { return n }); //去空
      this.ffmpeg_exec = spawn(this.conf.ffmpeg, argv);
      this.ffmpeg_exec.on('error', (e) => {
        Logger.ffdebug(e);
      });
  
      this.ffmpeg_exec.stdout.on('data', (data) => {
        Logger.ffdebug(`FF输出：${data}`);
      });
  
      this.ffmpeg_exec.stderr.on('data', (data) => {
        Logger.ffdebug(`FF输出：${data}`);
      });
  
      this.ffmpeg_exec.on('close', (code) => {
        Logger.log('[Transmuxing end] ' + this.conf.streamPath);
        this.emit('end');
        fs.readdir(ouPath, function (err, files) {
          if (!err) {
            files.forEach((filename) => {
              if (filename.endsWith('.ts')
                || filename.endsWith('.m3u8')
                || filename.endsWith('.mpd')
                || filename.endsWith('.m4s')
                || filename.endsWith('.tmp')) {
                fs.unlinkSync(ouPath + '/' + filename);
              }
            })
          }
        });
      });
    }
  
    onSavedRecording(id, streamPath, args) {
  
    }
  
  
    end() {
      // this.ffmpeg_exec.kill();
    }
  }
  
  module.exports = NodeTransSession;
  