API Docs for:
Show:

File: src/local_peer.coffee

  1. Peer = require('./peer').Peer
  2. Stream = require('./stream').Stream
  3.  
  4. ###*
  5. # @module rtc
  6. ###
  7. ###*
  8. # Represents the local user of the room
  9. # @class rtc.LocalPeer
  10. # @extends rtc.Peer
  11. #
  12. # @constructor
  13. ###
  14. class exports.LocalPeer extends Peer
  15.  
  16. constructor: () ->
  17. ###*
  18. # Contains promises of the local streams offered to all remote peers
  19. # @property streams
  20. # @type Object
  21. ###
  22. @streams = {}
  23.  
  24. ###*
  25. # Contains all DataChannel configurations negotiated with all remote peers
  26. # @property channels
  27. # @type Object
  28. ###
  29. @channels = {}
  30.  
  31. @_status = {}
  32.  
  33.  
  34. ###*
  35. # Get an item of the status transferred to all remote peers
  36. # @method status
  37. # @param {String} key The key of the value. Will return
  38. # @return The value associated with the key
  39. ###
  40. ###*
  41. # Set an item of the status transferred to all remote peers
  42. # @method status
  43. # @param {String} key The key of the value. Will return
  44. # @param value The value to store
  45. ###
  46. status: (key, value) ->
  47. if value?
  48. @_status[key] = value
  49. @emit 'status_changed', @_status
  50. return
  51. else
  52. return @_status[key]
  53.  
  54.  
  55. ###*
  56. # Add data channel which will be negotiated with all remote peers
  57. # @method addDataChannel
  58. # @param {String} [name='data'] Name of the data channel
  59. # @param {Object} [desc={ordered: true}] Options passed to `RTCDataChannel.createDataChannel()`
  60. ###
  61. addDataChannel: (name, desc) ->
  62. if typeof name != 'string'
  63. desc = name
  64. name = @DEFAULT_CHANNEL
  65.  
  66. if not desc?
  67. # TODO: default handling
  68. desc = {
  69. ordered: true
  70. }
  71.  
  72. @channels[name] = desc
  73. @emit 'configuration_changed'
  74. return
  75.  
  76.  
  77. ###*
  78. # Add local stream to be sent to all remote peers
  79. # @method addStream
  80. # @param {String} [name='stream'] Name of the stream
  81. # @param {Promise -> rtc.Stream | rtc.Stream | Object} stream The stream, a promise to the stream or the configuration to create a stream with `rtc.Stream.createStream()`
  82. # @return {Promise -> rtc.Stream} Promise of the stream which was added
  83. ###
  84. addStream: (name, obj) ->
  85. # helper to actually save stream
  86. saveStream = (stream_p) =>
  87. # TODO: collision detection?
  88. @streams[name] = stream_p
  89. @emit 'configuration_changed'
  90. return stream_p
  91.  
  92. # name can be omitted ... once
  93. if typeof name != 'string'
  94. obj = name
  95. name = @DEFAULT_STREAM
  96.  
  97. if obj?.then?
  98. # it is a promise
  99. return saveStream(obj)
  100. else if obj instanceof Stream
  101. # it is the actual stream, turn into promise
  102. return saveStream(Promise.resolve(obj))
  103. else
  104. # we assume we can pass it on to create a stream
  105. stream_p = Stream.createStream(obj)
  106. return saveStream(stream_p)
  107.  
  108.  
  109. ###*
  110. # Get local stream
  111. # @method stream
  112. # @param {String} [name='stream'] Name of the stream
  113. # @return {Promise -> rtc.Stream} Promise of the stream
  114. ###
  115. stream: (name=@DEFAULT_STREAM) ->
  116. return @streams[name]
  117.  
  118.  
  119. ###*
  120. # Checks whether the peer is the local peer. Returns always `true` on this
  121. # class.
  122. # @method isLocal
  123. # @return {Boolean} Returns `true`
  124. ###
  125. isLocal: () ->
  126. return true
  127.