<commit_before>$ = require 'jquery'
_ = require 'underscore'

module.exports =
class MediaConnection
  _.extend(@prototype, require 'event-emitter')

  constructor: (@remoteParticipant) ->
    @inboundStreamPromise = $.Deferred()

    @remoteParticipant.on 'add-ice-candidate', (candidate) =>
      @getOutboundStreamPromise().done =>
        @getPeerConnection().addIceCandidate(new RTCIceCandidate(candidate))

    @on 'connected', => @connected = true

  getInboundStreamPromise: -> @inboundStreamPromise

  getOutboundStreamPromise: ->
    @outboundStreamPromise ?= @createOutboundStreamPromise()

  createOutboundStreamPromise: ->
    deferred = $.Deferred()
    video = config.get('collaboration.video') ? mandatory: { maxWidth: 320, maxHeight: 240 }, optional: []
    audio = config.get('collaboration.audio') ? true
    success = (stream) =>
      @getPeerConnection().addStream(stream)
      deferred.resolve(stream)
    error = (args...) ->
      deferred.reject(args...)
    navigator.webkitGetUserMedia({video, audio}, success, error)
    deferred.promise()

  sendOffer: ->
    @getOutboundStreamPromise().done =>
      @getPeerConnection().createOffer (localDescription) =>
        @getPeerConnection().setLocalDescription(localDescription)
        @remoteParticipant.send('offer-media-connection', localDescription)
        @remoteParticipant.one 'answer-media-connection', (remoteDescription) =>
          @getPeerConnection().setRemoteDescription(new RTCSessionDescription(remoteDescription))
          @trigger 'connected'

  waitForOffer: ->
    @remoteParticipant.one 'offer-media-connection', (remoteDescription) =>
      @getOutboundStreamPromise().done =>
        @getPeerConnection().setRemoteDescription(new RTCSessionDescription(remoteDescription))
        @getPeerConnection().createAnswer (localDescription) =><commit_msg>Wrap webkitGetUserMedia in nextTick for reliability<commit_after>$ = require 'jquery'
_ = require 'underscore'

module.exports =
class MediaConnection
  _.extend(@prototype, require 'event-emitter')

  constructor: (@remoteParticipant) ->
    @inboundStreamPromise = $.Deferred()

    @remoteParticipant.on 'add-ice-candidate', (candidate) =>
      @getOutboundStreamPromise().done =>
        @getPeerConnection().addIceCandidate(new RTCIceCandidate(candidate))

    @on 'connected', => @connected = true

  getInboundStreamPromise: -> @inboundStreamPromise

  getOutboundStreamPromise: ->
    @outboundStreamPromise ?= @createOutboundStreamPromise()

  createOutboundStreamPromise: ->
    deferred = $.Deferred()

    _.nextTick =>
      video = config.get('collaboration.video') ? mandatory: { maxWidth: 320, maxHeight: 240 }, optional: []
      audio = config.get('collaboration.audio') ? true
      success = (stream) =>
        @getPeerConnection().addStream(stream)
        deferred.resolve(stream)
      error = (args...) ->
        deferred.reject(args...)
      navigator.webkitGetUserMedia({video, audio}, success, error)

    deferred.promise()

  sendOffer: ->
    @getOutboundStreamPromise().done =>
      @getPeerConnection().createOffer (localDescription) =>
        @getPeerConnection().setLocalDescription(localDescription)
        @remoteParticipant.send('offer-media-connection', localDescription)
        @remoteParticipant.one 'answer-media-connection', (remoteDescription) =>
          @getPeerConnection().setRemoteDescription(new RTCSessionDescription(remoteDescription))
          @trigger 'connected'

  waitForOffer: ->
    @remoteParticipant.one 'offer-media-connection', (remoteDescription) =>
      @getOutboundStreamPromise().done =>
        @getPeerConnection().setRemoteDescription(new RTCSessionDescription(remoteDescription))
        @getPeerConnection().createAnswer (localDescription) =>
{'fraction_non_alphanumeric': 0.12851086380498145, 'fraction_numerical': 0.003179650238473768, 'mean_word_length': 4.324400564174894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '3104348', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1000, 'n_words': 164}