This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
forked from msach22/cordova-plugin-opentok
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathOTPublisher.coffee
160 lines (157 loc) · 6.72 KB
/
OTPublisher.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Publisher Object:
# Properties:
# id (String) — The ID of the DOM element through which the Publisher stream is displayed
# stream - The Stream object corresponding to the stream of the publisher
# session (Session) — The Session to which the Publisher is publishing a stream. If the Publisher is not publishing a stream to a Session, this property is set to null.
# replaceElementId (String) — The ID of the DOM element that was replaced when the Publisher video stream was inserted.
# Methods:
# destroy():Publisher - not yet implemented
# getImgData(callback)
# getStyle() : Object - not yet implemented
# off( type, listener )
# on( type, listener )
# publishAudio(Boolean) : publisher - change publishing state for Audio
# publishVideo(Boolean) : publisher - change publishing state for Video
# setStyle( style, value ) : publisher - not yet implemented
#
class TBPublisher
constructor: (one, two) ->
@sanitizeInputs(one, two)
pdebug "creating publisher", {}
position = getPosition(@pubElement)
name=""
publishAudio="true"
publishVideo="true"
cameraName = "front"
zIndex = TBGetZIndex(@pubElement)
ratios = TBGetScreenRatios()
audioFallbackEnabled = "true"
audioBitrate = 40000
audioSource = "true"
videoSource = "true"
frameRate = 30
resolution = "640X480"
insertMode = "replace"
if @properties?
width = @properties.width ? position.width
height = @properties.height ? position.height
name = @properties.name ? ""
cameraName = @properties.cameraName ? "front"
audioFallbackEnabled = @properties.audioFallbackEnabled ? audioFallbackEnabled
audioBitrate = @properties.audioBitrate ? audioBitrate
audioSource = @properties.audioSource ? audioSource
videoSource = @properties.videoSource ? videoSource
frameRate = @properties.frameRate ? frameRate
resolution = @properties.resolution ? resolution
if(@properties.publishAudio? and @properties.publishAudio==false)
publishAudio="false"
if(@properties.publishVideo? and @properties.publishVideo==false)
publishVideo="false"
if(@properties.audioFallbackEnabled? and @properties.audioFallbackEnabled==false)
audioFallbackEnabled="false"
if(@properties.audioSource? || @properties.audioSource==false)
audioSource="false"
if(@properties.videoSource? || @properties.videoSource==false)
videoSource="false"
insertMode = @properties.insertMode ? insertMode
if (not width?) or width == 0 or (not height?) or height==0
width = DefaultWidth
height = DefaultHeight
replaceWithVideoStream(@pubElement, PublisherStreamId, {width:width, height:height, insertMode:insertMode})
position = getPosition(@pubElement)
TBUpdateObjects()
OT.getHelper().eventing(@)
Cordova.exec(TBSuccess, TBError, OTPlugin, "initPublisher", [name, position.top, position.left, width, height, zIndex, publishAudio, publishVideo, cameraName, ratios.widthRatio, ratios.heightRatio, audioFallbackEnabled, audioBitrate, audioSource, videoSource, frameRate, resolution] )
Cordova.exec(@eventReceived, TBSuccess, OTPlugin, "addEvent", ["publisherEvents"] )
setSession: (session) =>
@session = session
eventReceived: (response) =>
@[response.eventType](response.data)
streamCreated: (event) =>
@stream = new TBStream( event.stream, @session.sessionConnected )
streamEvent = new TBEvent("streamCreated")
streamEvent.stream = @stream
@dispatchEvent(streamEvent)
return @
streamDestroyed: (event) =>
streamEvent = new TBEvent("streamDestroyed")
streamEvent.stream = @stream
streamEvent.reason = "clientDisconnected"
@dispatchEvent(streamEvent)
# remove stream DOM?
return @
removePublisherElement: =>
@pubElement.parentNode.removeChild(@pubElement)
@pubElement = false
destroy: ->
if(@pubElement)
Cordova.exec( @removePublisherElement, TBError, OTPlugin, "destroyPublisher", [])
getImgData: (callback) ->
errorCb = (error) -> callback(error)
successCb = (img) -> callback(null, img)
Cordova.exec(successCb, errorCb, OTPlugin, "getImgData", [PublisherStreamId]);
return @
getStyle: ->
return {}
publishAudio: (state) ->
@publishMedia( "publishAudio", state )
return @
publishVideo: (state) ->
@publishMedia( "publishVideo", state )
return @
setCameraPosition: (cameraPosition) ->
pdebug("setting camera position", cameraPosition: cameraPosition)
Cordova.exec(TBSuccess, TBError, OTPlugin, "setCameraPosition", [cameraPosition])
return @
setStyle: (style, value ) ->
return @
audioLevelUpdated: (event) ->
streamEvent = new TBEvent("audioLevelUpdated")
streamEvent.audioLevel = event.audioLevel
@dispatchEvent(streamEvent)
return @
publishMedia: (media, state) ->
if media not in ["publishAudio", "publishVideo"] then return
publishState = "true"
if state? and ( state == false or state == "false" )
publishState = "false"
pdebug "setting publishstate", {media: media, publishState: publishState}
Cordova.exec(TBSuccess, TBError, OTPlugin, media, [publishState] )
sanitizeInputs: (one, two) ->
if( two? )
# all 2 optional properties present: domId, properties
if one instanceof Element
@pubElement = one
@domId = @pubElement.id
else
@domId = one
@pubElement = document.getElementById(one)
@properties = two
else if( one? )
# only 1 property is present domId || properties
if one instanceof Element
@pubElement = one
@domId = @pubElement.id
else if( typeof(one) == "object" )
@properties = one
else
@domId = one
@pubElement = document.getElementById(one)
@properties = if( @properties and typeof( @properties == "object" )) then @properties else {}
# if domId does NOT exists and an element is provided, create a unique domId
if (!@domId and @pubElement)
@domId = "PubSub" + Date.now();
@pubElement.setAttribute('id', @domId)
# if domId exists but properties width or height is not specified, set properties
if( @domId and @pubElement )
if !@properties.width or !@properties.height
console.log "domId exists but properties width or height is not specified"
position = getPosition( @pubElement )
console.log " width: #{position.width} and height: #{position.height} for domId #{@domId}, and top: #{position.top}, left: #{position.left}"
if position.width > 0 and position.height > 0
@properties.width = position.width
@properties.height = position.height
else
@domId = TBGenerateDomHelper()
@pubElement = document.getElementById(@domId)
return @