<commit_before>var CameraView = Backbone.View.extend({
  el: '#camera-view',
  template: _.template( $("#camera-watch-page-template").html() ),
  serverUrl: null,
  localSessionId: null,
  streamingPlugIn: null,
  events: {
	'click .buttons .columns .success': 'startStreaming',
	'click .buttons .columns .secondary': 'stopStreaming'
  },
  initialize: function(options)
  {
	this.serverUrl = "http://" + window.location.hostname + ":8088/janus";
	startStreaming = this.startStreaming;
	this.initJanus();

	// Initialize the library (all console debuggers enabled)
	Janus.init({debug: "all", callback: function() {
		console.log('Janus Initialized')
	}});

	this.render();
	
	if(Janus.isWebrtcSupported()) {
		this.setState('ready');
	} else {
		this.setState('nowebrtc');
	}
  },
  render: function() {
	this.$el.html(this.template());
  },
  initJanus: function(){
	this.sessionId = null;
  },
  setState: function(state)
  {
	this.$el.removeClass('preparing error nowebrtc streaming ready').addClass(state)  
  },
  startStreaming: function(e){	  
	this.setState('preparing');
	$.when( 
			$.getJSON(API_BASEURL + 'settings/camera/streaming'),
			$.ajax({
				url: API_BASEURL + "camera/init-janus",
				type: "POST",
				dataType: "json",
				contentType: "application/json; charset=UTF-8",
				data: ''
			})
	)
	.done(_.bind(function(settings, session){
		console.log('-2');
		if(!this.$('#remotevideo').is(':visible')) {
			console.log('-1');
			// Create session
			var janus = new Janus({
				server: this.serverUrl,
				apisecret: 'd5faa25fe8e3438d826efb1cd3369a50',
				success: _.bind(function() {
					console.log('0');
					$.ajax({
						url: API_BASEURL + "camera/start-peer-session",
						type: "POST",
						dataType: "json",
						contentType: "application/json; charset=UTF-8",
						data: JSON.stringify({
							clientId: new String(Math.random()*10000000000000000000)
						})
					}).done(_.bind(function(response){
						console.log('1');
						this.localSessionId = response.sessionId;},this));

					
                        var streamingPlugIn = null;
						var selectedStream = settings.encoding == 'h264' ? 1 : 2;
						var sizeVideo = settings.size;

						//Attach to streaming plugin
						janus.attach({
							plugin: "janus.plugin.streaming",
							success: _.bind(function(pluginHandle) {
								this.streamingPlugIn = pluginHandle;
								console.log('streamingPlugIn');
								console.log(this.streamingPlugIn);
								console.log(this);
								
								this.streamingPlugIn.oncleanup = _.bind(function(){
									$.ajax({
										//url: API_BASEURL + "camera/stop-janus",
					                    url: API_BASEURL + "camera/close-peer-session",
					                    type: "POST",
					                    dataType: "json",
					                    contentType: "application/json; charset=UTF-8",
					                    data: JSON.stringify({
					                    	sessionId: this.localSessionId
					                     })
					                })
				                    .done(_.bind(function(){
				                    	this.setstate('ready');
				                    },this))
				                    .always(_.bind(this.initJanus, this))
				                    .fail(_.bind(function(){this.setState('error');},this))
				                },);

				                var body = { "request": "watch", id: selectedStream };
				                this.streamingPlugIn.send({"message": body});
							},this),
							error: function(error) {
								console.error(error);
								noty({text: "Error communicating with the WebRTC system.", timeout: 3000});
							},
							onmessage: _.bind(function(msg, jsep) {
								console.log(this);
								console.log(" ::: Got a message :::");
								console.log(JSON.stringify(msg));
								console.log('streamingPlugIn on attach');
								console.log(this.streamingPlugIn);
								var result = msg["result"];
									if(result !== null && result !== undefined) {
										if(result["status"] !== undefined && result["status"] !== null) {
											var status = result["status"];
											if(status === 'stopped')
												this.stopStreaming();
										}
									} else if(msg["error"] !== undefined && msg["error"] !== null) {
										console.error(msg["error"]);<commit_msg>source code cleaned and performed<commit_after>var CameraView = Backbone.View.extend({
  el: '#camera-view',
  template: _.template( $("#camera-watch-page-template").html() ),
  serverUrl: null,
  localSessionId: null,
  streamingPlugIn: null,
  events: {
	'click .buttons .columns .success': 'startStreaming',
	'click .buttons .columns .secondary': 'stopStreaming'
  },
  initialize: function(options)
  {
	this.serverUrl = "http://" + window.location.hostname + ":8088/janus";
	startStreaming = this.startStreaming;
	this.initJanus();

	// Initialize the library (all console debuggers enabled)
	Janus.init({debug: "all", callback: function() {
		console.log('Janus Initialized')
	}});

	this.render();
	
	if(Janus.isWebrtcSupported()) {
		this.setState('ready');
	} else {
		this.setState('nowebrtc');
	}
  },
  render: function() {
	this.$el.html(this.template());
  },
  initJanus: function(){
  	this.localSessionId = null;
	this.streamingPlugIn = null;
  },
  setState: function(state)
  {
	this.$el.removeClass('preparing error nowebrtc streaming ready').addClass(state)  
  },
  startStreaming: function(e){	  
	this.setState('preparing');
	$.when( 
			$.getJSON(API_BASEURL + 'settings/camera/streaming'),
			$.ajax({
				url: API_BASEURL + "camera/init-janus",
				type: "POST",
				dataType: "json",
				contentType: "application/json; charset=UTF-8",
				data: ''
			})
	)
	.done(_.bind(function(settings, session){
		console.log('-2');
		if(!this.$('#remotevideo').is(':visible')) {
			console.log('-1');
			// Create session
			var janus = new Janus({
				server: this.serverUrl,
				apisecret: 'd5faa25fe8e3438d826efb1cd3369a50',
				success: _.bind(function() {
					console.log('0');
					$.ajax({
						url: API_BASEURL + "camera/start-peer-session",
						type: "POST",
						dataType: "json",
						contentType: "application/json; charset=UTF-8",
						data: JSON.stringify({
							clientId: new String(Math.random()*10000000000000000000)
						})
					}).done(_.bind(function(response){
						console.log('1');
						this.localSessionId = response.sessionId;},this));

					
                        var streamingPlugIn = null;
						var selectedStream = settings.encoding == 'h264' ? 1 : 2;
						var sizeVideo = settings.size;

						//Attach to streaming plugin
						janus.attach({
							plugin: "janus.plugin.streaming",
							success: _.bind(function(pluginHandle) {
								this.streamingPlugIn = pluginHandle;
								console.log('streamingPlugIn');
								console.log(this.streamingPlugIn);
								console.log(this);
								
								this.streamingPlugIn.oncleanup = _.bind(function(){
									$.ajax({
										//url: API_BASEURL + "camera/stop-janus",
					                    url: API_BASEURL + "camera/close-peer-session",
					                    type: "POST",
					                    dataType: "json",
					                    contentType: "application/json; charset=UTF-8",
					                    data: JSON.stringify({
					                    	sessionId: this.localSessionId
					                     })
					                })
				                    .done(_.bind(function(){
				                    	this.setState('ready');
				                    	janus.sessionId = null;
				                    },this))
				                    .always(_.bind(this.initJanus, this))
				                    .fail(_.bind(function(){this.setState('error');},this))
				                },this);

				                var body = { "request": "watch", id: selectedStream };
				                this.streamingPlugIn.send({"message": body});
							},this),
							error: function(error) {
								console.error(error);
								noty({text: "Error communicating with the WebRTC system.", timeout: 3000});
							},
							onmessage: _.bind(function(msg, jsep) {
								console.log(this);
								console.log(" ::: Got a message :::");
								console.log(JSON.stringify(msg));
								console.log('streamingPlugIn on attach');
								console.log(this.streamingPlugIn);
								var result = msg["result"];
									if(result !== null && result !== undefined) {
										if(result["status"] !== undefined && result["status"] !== null) {
											var status = result["status"];
											if(status === 'stopped')
												this.stopStreaming();
										}
									} else if(msg["error"] !== undefined && msg["error"] !== null) {
										console.error(msg["error"]);
{'fraction_non_alphanumeric': 0.15692590888173033, 'fraction_numerical': 0.013345605154164749, 'mean_word_length': 2.254586297266941, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5619188', 'n_tokens_mistral': 3519, 'n_tokens_neox': 2553, 'n_words': 470}