<commit_before>/**
 * Expose the MediaStream class.
 */
module.exports = MediaStream;

/**
 * Spec: http://w3c.github.io/mediacapture-main/#mediastream
 */

/**
 * Dependencies.
 */
var
	debug = require('debug')('iosrtc:MediaStream'),
	exec = require('cordova/exec'),
	EventTarget = require('./EventTarget'),
	MediaStreamTrack = require('./MediaStreamTrack'),

/**
 * Local variables.
 */

	// Dictionary of MediaStreams (provided via getMediaStreams() class method).
	// - key: MediaStream blobId.
	// - value: MediaStream.
	mediaStreams;

// TODO longer UUID like native call
// - "4021904575-2849079001-3048689102-1644344044-4021904575-2849079001-3048689102-1644344044"
function newMediaStreamId() {
   return window.crypto.getRandomValues(new Uint32Array(4)).join('-');
}

// Save original MediaStream, use Blob if not available
var originalMediaStream = window.MediaStream || window.Blob;

/**
 * Expose the MediaStream class.
 * Make MediaStream be a Blob so it can be consumed by URL.createObjectURL().
 */
function MediaStream(arg, id) {
	debug('new MediaStream(arg) | [arg:%o]', arg);

	// new MediaStream(new MediaStream()) // stream
	// new MediaStream([]) // tracks
	// new MediaStream() // empty

	// TODO detect originalMediaStream usage and skip SHIM
	// TODO attempt CustomMediaStream extend.

	// Extend returned MediaTream with custom MediaStream
	var stream = new (Function.prototype.bind.apply(originalMediaStream.bind(this), [])); // jshint ignore:line
	Object.defineProperties(stream, Object.getOwnPropertyDescriptors(MediaStream.prototype));

	// Make it an EventTarget.
	EventTarget.call(stream);

	// Public atributes.
	stream._id = id || newMediaStreamId();
	stream.active = true;

	// Init Stream by Id
	exec(null, null, 'iosrtcPlugin', 'MediaStream_init', [stream.id]);

	// Public but internal attributes.
	stream.connected = false;

	// Private attributes.
	stream._audioTracks = {};
	stream._videoTracks = {};

	// Store the stream into the dictionary.
	stream._blobId = 'MediaStream_' + stream.id;
	mediaStreams[stream._blobId] = stream;

	// Convert arg to array of tracks if possible
	if (
		(arg instanceof MediaStream) || <commit_msg>detect Native MediaStream constructor usage and only use Native MediaStream when detected<commit_after>/**
 * Expose the MediaStream class.
 */
module.exports = MediaStream;

/**
 * Spec: http://w3c.github.io/mediacapture-main/#mediastream
 */

/**
 * Dependencies.
 */
var
	debug = require('debug')('iosrtc:MediaStream'),
	exec = require('cordova/exec'),
	EventTarget = require('./EventTarget'),
	MediaStreamTrack = require('./MediaStreamTrack'),

/**
 * Local variables.
 */

	// Dictionary of MediaStreams (provided via getMediaStreams() class method).
	// - key: MediaStream blobId.
	// - value: MediaStream.
	mediaStreams;

// TODO longer UUID like native call
// - "4021904575-2849079001-3048689102-1644344044-4021904575-2849079001-3048689102-1644344044"
function newMediaStreamId() {
   return window.crypto.getRandomValues(new Uint32Array(4)).join('-');
}

// Save original MediaStream
var originalMediaStream = window.MediaStream;
var originalMediaStreamTrack = MediaStreamTrack.originalMediaStreamTrack;

/**
 * Expose the MediaStream class.
 * Make MediaStream be a Blob so it can be consumed by URL.createObjectURL().
 */
function MediaStream(arg, id) {
	debug('new MediaStream(arg) | [arg:%o]', arg);

	// Detect native MediaStream usage	
	// new MediaStream(originalMediaStream) // stream
	// new MediaStream(originalMediaStreamTrack[]) // tracks		
	if (
		arg instanceof originalMediaStream || 
			(Array.isArray(arg) && arg[0] instanceof originalMediaStreamTrack)
	) {
		return new originalMediaStream(arg);
	}

	// new MediaStream(MediaStream) // stream
	// new MediaStream(MediaStreamTrack[]) // tracks
	// new MediaStream() // empty

	// TODO attempt CustomMediaStream extend.
	// Extend returned MediaTream with custom MediaStream
	var stream = new (Function.prototype.bind.apply(originalMediaStream.bind(this), [])); // jshint ignore:line
	Object.defineProperties(stream, Object.getOwnPropertyDescriptors(MediaStream.prototype));

	// Make it an EventTarget.
	EventTarget.call(stream);

	// Public atributes.
	stream._id = id || newMediaStreamId();
	stream.active = true;

	// Init Stream by Id
	exec(null, null, 'iosrtcPlugin', 'MediaStream_init', [stream.id]);

	// Public but internal attributes.
	stream.connected = false;

	// Private attributes.
	stream._audioTracks = {};
	stream._videoTracks = {};

	// Store the stream into the dictionary.
	stream._blobId = 'MediaStream_' + stream.id;
	mediaStreams[stream._blobId] = stream;

	// Convert arg to array of tracks if possible
	if (
		(arg instanceof MediaStream) || 
{'fraction_non_alphanumeric': 0.13215952481968604, 'fraction_numerical': 0.03585065761561307, 'mean_word_length': 4.2330743618202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5128624', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1414, 'n_words': 363}