class KurentoWebsocketsController < WebsocketRails::BaseController before_action :set_stream, only: [:stop_broadcasting, :view, :start_viewing, :stop_viewing] public def active_streams puts "Started a request for get active streams" puts message streams = KurentoRailsVideoStream.live.where(stream_params(message)) puts streams trigger_success streams end def recorded_streams streams = KurentoRailsVideoStream.recorded.where(stream_params(message)) trigger_success streams end # tell all users that a new video is available def broadcast puts "Starting broadcast" puts message @stream = KurentoRailsVideoStream.new(stream_params(message)) if @stream && @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.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 is_live? # if this stream is active, great if @stream.streaming trigger_success @stream else trigger_failure @stream end end # increment the count def view if @stream.live? @stream.current_viewers = (@stream.current_viewers || 0) + 1 WebsocketRails["stream-#{@stream.id}"].trigger 'viewer-connected' end @stream.total_viewers = (@stream.total_viewers || 0) + 1 if @stream.save connection_store[:viewing] = @stream.id end end def stop_viewing on_viewer_stop @stream 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 they were viewing video, then we need to decrement our number of active viewers if connection_store[:streaming] stop_streaming @stream elsif connection_store[:viewing] on_viewer_stop @stream end end private def stream_params(params) permit params, :id, :pipeline, :file_url, :sender_rtc, :streaming, :name, :meeting_id end def permit(original_hash, *allowed_fields) new_hash = {} if allowed_fields.nil? || !original_hash.is_a?(Hash) return {} end allowed_fields.each do |field| new_hash[field] = original_hash[field] if original_hash[field] end return 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 def on_viewer_stop(stream) if stream && stream.live? stream.current_viewers -= 1 stream.save WebsocketRails["stream-#{stream.id}"].trigger 'viewer-disconnected' end connection_store[:viewing] = nil end def set_stream if message && stream_params(message).has_key?(:id) @stream = KurentoRailsVideoStream.find(stream_params(message)[:id]) elsif connection_store if connection_store.has_key?(:streaming) @stream = KurentoRailsVideoStream.find(connection_store[:streaming]) elsif connection_store.has_key?(:viewing) @stream = KurentoRailsVideoStream.find(connection_store[:viewing]) end end end end