class KurentoWebsocketsController < WebsocketRails::BaseController public # just to see if this can make things work # def process_action(method, event) # puts "Calling process action with method #{method} and event #{event}" # super(method, event) # end def active_streams search_params = message.merge streaming: true streams = KurentoRailsVideoStream.where(search_params) trigger_success streams end def recorded_streams search_params = message.merge streaming: false streams = KurentoRailsVideoStream.where(search_params) trigger_success streams end # tell all users that a new video is available def broadcast stream = KurentoRailsVideoStream.new(stream_params(message)) if stream.save # notify any people watching active streams that this stream is available WebsocketRails[:video_streams].trigger 'new', stream # store the fact that this user was broadcasting connection_store[:streaming] = stream.id # and now trigger the success callback trigger_success stream else trigger_failure stream end end # tell all users that this broadcast is no longer streaming def stop_broadcasting stream = KurentoRailsVideoStream.find(connection_store[:streaming]) stream.streaming = false if stream.save # perform the relevant stop streaming actions stop_streaming stream # and finally, trigger the success callback trigger_success stream else trigger_failure stream end end # let a user know if a video is available for streaming or not def view stream = KurentoRailsVideoStream.find(stream_params(message)[:id]) # if this stream is active, great if stream.streaming trigger_success stream else trigger_failure stream end end # handle a user disconnecting from rails. # Usually, this would happen because the user refreshes the page def user_disconnect # check to see if the user was broadcasting video. If they were, then we need # to notify all users viewing their channel that they're no longer broadcasting if connection_store[:streaming] stream = KurentoRailsVideoStream.find(connection_store[:streaming]) stop_streaming stream end end private def stream_params(params) new_params = {} new_params[:id] = params[:id] new_params[:pipeline] = params[:pipeline] new_params[:file_url] = params[:file_url] new_params[:sender_rtc] = params permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name end def permit(original_hash, *allowed_fields) new_hash = {} if allowed_fields.nil? return {} end allowed_fields.each do |field| new_hash[field] = original_hash[field] end new_hash end def stop_streaming(stream) # Notify any people watching active streams that this stream isn't available any more WebsocketRails[:video_streams].trigger 'remove', stream # Also notify people watching this stream that it has ended WebsocketRails["stream-#{stream.id}"].trigger 'end-of-stream', stream # remove the stream from the connection store connection_store[:streaming] = nil end end