You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# TODO:: this will live in redis once testing is complete
require"./application"modulePlaceOS::ApiclassWebRTC < Applicationbase"/api/engine/v2/webrtc/"# TODO:: this will live in redis once testing is completeclassCallDetailsincludeJSON::Serializablegetterid : Stringgetterpeers : Hash(String,HTTP::WebSocket)
@[JSON::Field(converter: Time::EpochConverter)]gettercreated_at : Time
@[JSON::Field(converter: Time::EpochConverter)]propertyupdated_at : Timedefinitialize(@id : String)@peers={}ofString=>HTTP::WebSocket@updated_at=@created_at=Time.utcendendenumSignalTypeJoinParticipantListCandidateOfferAnswerPingLeaveendstructSessionSignalincludeJSON::Serializable# message id, generated by the sendergetterid : String# the unique id of the room to joingettersession_id : String# the type of messagepropertytype : SignalType# the id of the current userproperty!user_id : String# the id of the user we want to communicate withgetterto_user : String?# the payload, if any
@[JSON::Field(converter: String::RawConverter)]gettervalue : String?definitialize(@id,@session_id,@type,@user_id,@to_user,@value)endend# use a manager so the we can free the request context objectsclassManagerLog= ::Log.for(self)definitialize(@ice_config)end# authority_id => config stringprivategetterice_config : Hash(String,String)privategetterlock : Mutex=Mutex.newprivategettercalls={}ofString=>CallDetailsprivategettersockets={}ofHTTP::WebSocket=>SessionSignaldefhandle_session(websocket,request_id,user_id,auth_id)websocket.on_messagedo |message|
Log.context.set(request_id: request_id,user_id: user_id)Log.trace{{frame: "TEXT",text: message}}signal=SessionSignal.from_json(message)signal.user_id=user_idcasesignal.typewhen.join?on_join_signal(websocket,signal,auth_id)when.offer?,.answer?,.candidate?forward_signal(signal)elseLog.warn{"user #{user_id} sent unsupported signal #{signal.type}"}endifcall=calls[signal.session_id]?
call.updated_at=Time.utcendendwebsocket.on_closedo |_|
Log.trace{{request_id: request_id,frame: "CLOSE"}}ifconnect_details=sockets.deletewebsocketifcall=calls.delete(connect_details.session_id)call.peers.deleteconnect_details.user_idcall.updated_at=Time.utcconnect_details.type=:leavecall.peers.each_valuedo |ws|
send_signal(ws,connect_details)endendendendenddefcreate_new_call(signal) : CallDetailscalls[signal.session_id]=CallDetails.new(signal.session_id)enddefsend_signal(websocket,signal)Log.trace{"Sending signal #{signal.type} to #{signal.session_id}"}websocket.send(signal.to_json)rescue# we'll ignore websocket send failures, the user will be cleaned upenddefon_join_signal(websocket,signal,auth_id)call=calls[signal.session_id]? || create_new_call(signal)call.peers[signal.user_id]=websocketsockets[websocket]=signal# Return RTC configuration detailssend_signal(websocket,SessionSignal.new(id: "SIGNAL::#{Time.utc.to_unix_ms}+#{Random::Secure.hex(6)}",type: :join,session_id: signal.session_id,user_id: "SERVER::DATA",to_user: signal.user_id,value: ice_config[auth_id]))# Send participant listsend_signal(websocket,SessionSignal.new(id: "SIGNAL::#{Time.utc.to_unix_ms}+#{Random::Secure.hex(6)}",type: :participant_list,session_id: signal.session_id,user_id: "SERVER::DATA",to_user: signal.user_id,value: call.peers.keys.to_json))enddefforward_signal(signal)ifcall=calls[signal.session_id]?
ifto_user=call.peers[signal.to_user]?
send_signal(to_user,signal)endendendendICE_CONFIG={}ofString=>StringMANAGER=Manager.new(ICE_CONFIG)# WebRTC signaller endpoint, managing call participants
@[AC::Route::WebSocket("/signaller")]defsignaller(websocket) : NilLog.trace{{request_id: request_id,frame: "OPEN"}}authority=current_authority.not_nil!auth_id=authority.id.as(String)# https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServerICE_CONFIG[auth_id]=authority.internals["webrtc_ice"]?.try(&.to_json) || WEBRTC_DEFAULT_ICE_CONFIGMANAGER.handle_session(websocket,request_id,user_token.id,auth_id)endendend
The text was updated successfully, but these errors were encountered:
rest-api/src/placeos-rest-api/controllers/webrtc.cr
Line 7 in d1c330b
The text was updated successfully, but these errors were encountered: