<commit_before>'use strict';

var spawn = require('child_process').spawn,
    debug = require('debug')('Camera:Sensr:MjpegToSrtpStream');

/**
 * Stream an MJPEG source to an SRTP destination
 * 
 * @param {any} streamOptions
 * @see {@link https://github.com/KhaosT/HAP-NodeJS/wiki/IP-Camera|HAP-NodeJS IP Camera Reference}
 * @see {@link https://github.com/KhaosT/homebridge-camera-ffmpeg/|Homebridge Camera FFMPEG}
 * @see {@link https://ffmpeg.org/ffmpeg.html|FFMPEG Reference}
 */
function MjpegToSrtpStream(streamOptions) {
    if (!streamOptions ||
        !streamOptions.sourceUrl ||
        !streamOptions.destinationUrl ||
        !streamOptions.destinationKey ||
        !streamOptions.width ||
        !streamOptions.height) {
        throw new Error('streamOptions must be specified with a sourceUrl, destinationUrl, destinationKey, width, and height.');
    }

    var self = this;
    self.sourceUrl = streamOptions.sourceUrl;
    //self.sourceUrl = 'http://192.168.123.181/video.cgi';
    self.destinationUrl = streamOptions.destinationUrl;
    self.destinationKey = streamOptions.destinationKey;
    self.width = streamOptions.width;
    self.height = streamOptions.height;
    self.fps = streamOptions.fps || 3;
    self.bitrate = streamOptions.bitrate || 300;

    return self.startStream();
}

MjpegToSrtpStream.prototype.startStream = function () {
    var self = this,
        ffmpegCommand = '';

    // GLOBAL OPTIONS
    ////////////////////////////////////////////
    ffmpegCommand += ' -loglevel debug';
    ffmpegCommand += ' -stats';

    // INPUT OPTIONS
    ////////////////////////////////////////////
    // set the input format explicitly
    ffmpegCommand += ' -f mjpeg';
    // Since we're using MJPEG, treat this as a "screen grabber"
    ffmpegCommand += ' -re';
    // starting with the input MJPEG URL
    ffmpegCommand += ' -i ' + self.sourceUrl;

    // OUTPUT OPTIONS
    ////////////////////////////////////////////
    // set the output codec
    ffmpegCommand += ' -c:v libx264';
    // set number of threads
    ffmpegCommand += ' -threads 0';
    // set the preset and tuning
    ffmpegCommand += ' -tune zerolatency';
    // set the constant rate (higher is lower quality)
    ffmpeg += ' -crf 18';
    // set a frame rate
    ffmpeg += ' -r ' + self.fps;
    // set the pixel format
    //ffmpeg += ' -pix_fmt yuv420p';
    // set a scaling
    ffmpegCommand += ' -vf scale=' + self.width + ':' + self.height;
    // there is no audio feed available
    ffmpegCommand += ' -an';
    // Bit rate and buffer
    ffmpegCommand += ' -b:v '+ self.bitrate +'k';
    ffmpegCommand += ' -bufsize '+ self.bitrate +'k';
    // there is no audio feed available
    ffmpegCommand += ' -ssrc 1 -payload_type 99';
    // setting destination output format to RTP
    ffmpegCommand += ' -f rtp';
    // specify the SRTP version
    ffmpegCommand += ' -srtp_out_suite AES_CM_128_HMAC_SHA1_80';
    // specify the SRTP key and destination for output
    ffmpegCommand += ' -srtp_out_params ' + self.destinationKey;
    // and now the output file
    ffmpegCommand +=' ' + self.destinationUrl;

    debug(ffmpegCommand);

    var ffmpeg = spawn('ffmpeg', ffmpegCommand.split(' '), {
        env: process.env
    });
    ffmpeg.stdout.on('data', debug);
    ffmpeg.stderr.setEncoding('utf8');
    ffmpeg.stderr.on('data', debug);
    ffmpeg.on('err', debug);
    ffmpeg.on('close', function (code) {
        debug('FFMPEG exiting with code ' + code);
    });

    return ffmpeg;
};

<commit_msg>Removed extra space as it was creating another output stream<commit_after>'use strict';

var spawn = require('child_process').spawn,
    debug = require('debug')('Camera:Sensr:MjpegToSrtpStream');

/**
 * Stream an MJPEG source to an SRTP destination
 * 
 * @param {any} streamOptions
 * @see {@link https://github.com/KhaosT/HAP-NodeJS/wiki/IP-Camera|HAP-NodeJS IP Camera Reference}
 * @see {@link https://github.com/KhaosT/homebridge-camera-ffmpeg/|Homebridge Camera FFMPEG}
 * @see {@link https://ffmpeg.org/ffmpeg.html|FFMPEG Reference}
 */
function MjpegToSrtpStream(streamOptions) {
    if (!streamOptions ||
        !streamOptions.sourceUrl ||
        !streamOptions.destinationUrl ||
        !streamOptions.destinationKey ||
        !streamOptions.width ||
        !streamOptions.height) {
        throw new Error('streamOptions must be specified with a sourceUrl, destinationUrl, destinationKey, width, and height.');
    }

    debug(streamOptions);

    var self = this;
    self.sourceUrl = streamOptions.sourceUrl;
    self.destinationUrl = streamOptions.destinationUrl;
    self.destinationKey = streamOptions.destinationKey;
    self.width = streamOptions.width;
    self.height = streamOptions.height;
    self.fps = streamOptions.fps || 3;
    self.bitrate = streamOptions.bitrate || 300;

    return self.startStream();
}

MjpegToSrtpStream.prototype.startStream = function () {
    var self = this,
        ffmpegCommand = '';

    // GLOBAL OPTIONS
    ////////////////////////////////////////////
    ffmpegCommand += '-loglevel debug';
    ffmpegCommand += ' -stats';

    // INPUT OPTIONS
    ////////////////////////////////////////////
    // set the input format explicitly
    ffmpegCommand += ' -f mjpeg';
    // Since we're using MJPEG, treat this as a "screen grabber"
    ffmpegCommand += ' -re';
    // starting with the input MJPEG URL
    ffmpegCommand += ' -i ' + self.sourceUrl;

    // OUTPUT OPTIONS
    ////////////////////////////////////////////
    // set the output codec
    ffmpegCommand += ' -c:v libx264';
    // set number of threads
    ffmpegCommand += ' -threads 0';
    // set the preset and tuning
    ffmpegCommand += ' -tune zerolatency';
    // set the constant rate (higher is lower quality)
    ffmpeg += ' -crf 8';
    // set a frame rate
    ffmpeg += ' -r ' + self.fps;
    // set the pixel format
    ffmpeg += ' -pix_fmt yuv420p';
    // set a scaling
    ffmpegCommand += ' -vf scale=' + self.width + ':' + self.height;
    // there is no audio feed available
    ffmpegCommand += ' -an';
    // Bit rate and buffer
    ffmpegCommand += ' -b:v '+ self.bitrate +'k';
    ffmpegCommand += ' -bufsize '+ self.bitrate +'k';
    // some defaults for RTP
    ffmpegCommand += ' -ssrc 1 -payload_type 99';
    // setting destination output format to RTP
    ffmpegCommand += ' -f rtp';
    // specify the SRTP version
    ffmpegCommand += ' -srtp_out_suite AES_CM_128_HMAC_SHA1_80';
    // specify the SRTP key and destination for output
    ffmpegCommand += ' -srtp_out_params ' + self.destinationKey;
    // and now the output file
    ffmpegCommand +=' ' + self.destinationUrl;

    debug(ffmpegCommand);

    var ffmpegParameters = ffmpegCommand.split(' '),
        ffmpeg = spawn('ffmpeg', ffmpegParameters, {
        env: process.env
    });

    ffmpeg.stdout.on('data', debug);
    ffmpeg.stderr.setEncoding('utf8');
    ffmpeg.stderr.on('data', debug);
    ffmpeg.on('err', debug);
    ffmpeg.on('close', function (code) {
        debug('FFMPEG exiting with code ' + code);
    });

    return ffmpeg;
};


{'fraction_non_alphanumeric': 0.1625810087348549, 'fraction_numerical': 0.008030431107354184, 'mean_word_length': 2.46630859375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7452955', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2192, 'n_words': 546}