diff options
author | Joe Rayhawk <jrayhawk@fairlystable.org> | 2022-11-19 22:05:41 -0800 |
---|---|---|
committer | Joe Rayhawk <jrayhawk@fairlystable.org> | 2022-11-19 22:05:41 -0800 |
commit | f9dd3cd0932ab4f2f19e8b8c62c4f646486c1e66 (patch) | |
tree | 598dbcebca8d6c69f00d997e5b62b5e98db05e36 /src/obswebsocket.cr | |
parent | 92d73a6f013c0b6d7867b1dfd19c8d009ffbe374 (diff) | |
download | crystal-obs-websocket-f9dd3cd0932ab4f2f19e8b8c62c4f646486c1e66.tar.gz crystal-obs-websocket-f9dd3cd0932ab4f2f19e8b8c62c4f646486c1e66.zip |
Rename OBSWebSocket::Server to OBS::WebSocket for brevity and clarity
Diffstat (limited to 'src/obswebsocket.cr')
-rw-r--r-- | src/obswebsocket.cr | 206 |
1 files changed, 103 insertions, 103 deletions
diff --git a/src/obswebsocket.cr b/src/obswebsocket.cr index da188bc..88b66b6 100644 --- a/src/obswebsocket.cr +++ b/src/obswebsocket.cr @@ -9,7 +9,7 @@ struct Nil end end -module OBSWebSocket +module OBS extend self def req( type : String, id : String, data : ( String | Nil | JSON::Any ) = nil ) request = JSON.build do |json| @@ -41,16 +41,16 @@ module OBSWebSocket class ResponseDataMissingException < Exception end - class Server + class WebSocket @@writer : Fiber | Nil @@reqchan = Channel( Tuple( ( Channel( JSON::Any ) | Nil ), String ) ).new - @scenecollection = OBSWebSocket::SceneCollection.new( @@reqchan, @@inputs ) - @@inputs = OBSWebSocket::Inputs.new( @@reqchan ) - @@scenes = OBSWebSocket::SceneList.new( @@reqchan, @@inputs ) - @sources = OBSWebSocket::Sources.new( @@reqchan, @@inputs, @@scenes ) - @outputs = OBSWebSocket::Outputs.new( @@reqchan ) - @video = OBSWebSocket::VideoSettings.new( @@reqchan ) - @stats = OBSWebSocket::Stats.new( @@reqchan ) + @scenecollection = OBS::SceneCollection.new( @@reqchan, @@inputs ) + @@inputs = OBS::Inputs.new( @@reqchan ) + @@scenes = OBS::SceneList.new( @@reqchan, @@inputs ) + @sources = OBS::Sources.new( @@reqchan, @@inputs, @@scenes ) + @outputs = OBS::Outputs.new( @@reqchan ) + @video = OBS::VideoSettings.new( @@reqchan ) + @stats = OBS::Stats.new( @@reqchan ) @negotiated = false @connecterror = 0 @shutdown = false @@ -87,7 +87,7 @@ module OBSWebSocket end def transition! reschan = Channel( JSON::Any ).new - send( reschan, OBSWebSocket.req( "TriggerStudioModeTransition", UUID.random.to_s ) ) + send( reschan, OBS.req( "TriggerStudioModeTransition", UUID.random.to_s ) ) return reschan.receive end # request channel, response channel @@ -146,7 +146,7 @@ module OBSWebSocket if @shutdown break end - @obs_pubsub = HTTP::WebSocket.new( URI.parse( uri ), HTTP::Headers{"Cookie" => "SESSIONID=1235", "Sec-WebSocket-Protocol" => "obswebsocket.json"} ) + @obs_pubsub = HTTP::WebSocket.new( URI.parse( uri ), HTTP::Headers{"Cookie" => "SESSIONID=1235", "Sec-WebSocket-Protocol" => "OBS.json"} ) @obs_pubsub.on_close do | message | # for some reason HTTP::WebSocket.closed? SIGSEGVs on a dangling pointer on the 1.6 runtime, so we hack around it with our own state variable for now. @closed = true end @@ -273,7 +273,7 @@ module OBSWebSocket # FIXME when "InputCreated" edata = d["eventData"] - @@inputs[edata["inputName"].to_s] = OBSWebSocket::Input.new( @@reqchan, edata ) + @@inputs[edata["inputName"].to_s] = OBS::Input.new( @@reqchan, edata ) when "InputNameChanged" edata = d["eventData"] @@inputs.renamecache( edata["oldInputName"].as_s, edata["inputName"].as_s ) @@ -385,13 +385,13 @@ module OBSWebSocket end sleep 10 # invalidate state on everything - @scenecollection = OBSWebSocket::SceneCollection.new( @@reqchan, @@inputs ) - @@inputs = OBSWebSocket::Inputs.new( @@reqchan ) - @@scenes = OBSWebSocket::SceneList.new( @@reqchan, @@inputs ) - @sources = OBSWebSocket::Sources.new( @@reqchan, @@inputs, @@scenes ) - @outputs = OBSWebSocket::Outputs.new( @@reqchan ) - @video = OBSWebSocket::VideoSettings.new( @@reqchan ) - @stats = OBSWebSocket::Stats.new( @@reqchan ) + @scenecollection = OBS::SceneCollection.new( @@reqchan, @@inputs ) + @@inputs = OBS::Inputs.new( @@reqchan ) + @@scenes = OBS::SceneList.new( @@reqchan, @@inputs ) + @sources = OBS::Sources.new( @@reqchan, @@inputs, @@scenes ) + @outputs = OBS::Outputs.new( @@reqchan ) + @video = OBS::VideoSettings.new( @@reqchan ) + @stats = OBS::Stats.new( @@reqchan ) retry || ( @shutdown = true ) end end @@ -405,18 +405,18 @@ module OBSWebSocket end class Outputs - @outputs = Hash( String, OBSWebSocket::Output ).new + @outputs = Hash( String, OBS::Output ).new def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ) ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetOutputList", UUID.random.to_s ) } ) + @reqchan.send( { reschan, OBS.req( "GetOutputList", UUID.random.to_s ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) STDERR.print( "OUTPUTS THREAD: " ) STDERR.print( rdata.pretty_inspect ) rdata["outputs"].as_a.each do | output | - @outputs[output["outputName"].as_s] = OBSWebSocket::Output.new( @reqchan, output ) + @outputs[output["outputName"].as_s] = OBS::Output.new( @reqchan, output ) end else raise ResponseDataMissingException.new( "#{d["requestType"].as_s} #{d["requestStatus"]["code"].as_i64}: #{d["requestStatus"]["comment"].as_s}" ) @@ -433,12 +433,12 @@ module OBSWebSocket end class Output - getter status : OBSWebSocket::OutputStatus + getter status : OBS::OutputStatus getter flags : Hash( String, Bool ) getter state : Hash( String, String | Bool | Int64 | Float64 | Hash( String, Bool ) ) def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), json : JSON::Any ) @flags = Hash( String, Bool ).from_json(json["outputFlags"].to_json) - @status = OBSWebSocket::OutputStatus.new( @reqchan, json["outputName"].as_s ) + @status = OBS::OutputStatus.new( @reqchan, json["outputName"].as_s ) @state = Hash(String, String | Bool | Int64 | Float64 | Hash( String, Bool ) ).from_json(json.to_json) end end @@ -451,7 +451,7 @@ module OBSWebSocket end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetOutputStatus", UUID.random.to_s, JSON.parse( { "outputName" => @name }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "GetOutputStatus", UUID.random.to_s, JSON.parse( { "outputName" => @name }.to_json ) ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @age = Time.utc.to_unix @@ -478,7 +478,7 @@ module OBSWebSocket end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetVideoSettings", UUID.random.to_s ) } ) + @reqchan.send( { reschan, OBS.req( "GetVideoSettings", UUID.random.to_s ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @settings = Hash(String, String | Bool | Int64 | Float64 ).from_json(rdata.to_json) @@ -507,7 +507,7 @@ module OBSWebSocket end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetStats", UUID.random.to_s ) } ) + @reqchan.send( { reschan, OBS.req( "GetStats", UUID.random.to_s ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @stats = Hash(String, String | Bool | Int64 | Float64 ).from_json(rdata.to_json) @@ -529,18 +529,18 @@ module OBSWebSocket class SceneCollection # Note: distinct from SceneList @current = String.new - @list = Hash( String, OBSWebSocket::SceneList ).new + @list = Hash( String, OBS::SceneList ).new @reqchan = Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ).new - def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBSWebSocket::Inputs ) + def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBS::Inputs ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetSceneCollectionList", UUID.random.to_s ) } ) + @reqchan.send( { reschan, OBS.req( "GetSceneCollectionList", UUID.random.to_s ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @current = rdata["currentSceneCollectionName"].as_s rdata["sceneCollections"].as_a.each{ | scenecollection | - @list[scenecollection.as_s] = OBSWebSocket::SceneList.new( @reqchan, @inputs ) + @list[scenecollection.as_s] = OBS::SceneList.new( @reqchan, @inputs ) } else raise ResponseDataMissingException.new( "#{d["requestType"].as_s} #{d["requestStatus"]["code"].as_i64}: #{d["requestStatus"]["comment"].as_s}" ) @@ -560,7 +560,7 @@ module OBSWebSocket end def deletecache() @current = String.new - @list = Hash( String, OBSWebSocket::SceneList ).new + @list = Hash( String, OBS::SceneList ).new end def currentcache( @current : String ) end @@ -568,14 +568,14 @@ module OBSWebSocket class SceneList # Note: distinct from SceneCollection @scenes_by_index = Array(String).new - @scenes = Hash(String, OBSWebSocket::Scene).new + @scenes = Hash(String, OBS::Scene).new @program = String.new @preview = String.new - def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBSWebSocket::Inputs ) + def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBS::Inputs ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetSceneList", UUID.random.to_s ) } ) + @reqchan.send( { reschan, OBS.req( "GetSceneList", UUID.random.to_s ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @program = rdata["currentProgramSceneName"].as_s @@ -583,7 +583,7 @@ module OBSWebSocket rdata["scenes"].as_a.each_with_index{ | scene, i | name = scene["sceneName"].as_s @scenes_by_index.push(name) - @scenes[name] = OBSWebSocket::Scene.new( @reqchan, name, self ) + @scenes[name] = OBS::Scene.new( @reqchan, name, self ) } else raise ResponseDataMissingException.new( "#{d["requestType"].as_s} #{d["requestStatus"]["code"].as_i64}: #{d["requestStatus"]["comment"].as_s}" ) @@ -593,7 +593,7 @@ module OBSWebSocket def invalidate STDERR.puts( "SCENELIST THREAD: scenes invalidated" ) @scenes_by_index = Array(String).new - @scenes = Hash(String, OBSWebSocket::Scene).new + @scenes = Hash(String, OBS::Scene).new @program = String.new @preview = String.new end @@ -615,53 +615,53 @@ module OBSWebSocket @program.empty? && populate @scenes[@program] end - def []( index : Int ) : OBSWebSocket::Scene + def []( index : Int ) : OBS::Scene @scenes_by_index[index]? || populate @scenes[@scenes_by_index[index]]? || populate @scenes[@scenes_by_index[index]] end - def []( index : String ) : OBSWebSocket::Scene + def []( index : String ) : OBS::Scene @scenes[index]? || populate @scenes[index] end - def []?( index : String ) : OBSWebSocket::Scene | Nil + def []?( index : String ) : OBS::Scene | Nil @scenes[index]? || populate @scenes[index]? end - def to_h : Hash( String, OBSWebSocket::Scene ) + def to_h : Hash( String, OBS::Scene ) @scenes.empty? && populate return @scenes end def deletecache() @scenes_by_index = Array(String).new - @scenes = Hash(String, OBSWebSocket::Scene).new + @scenes = Hash(String, OBS::Scene).new @program = String.new @preview = String.new end end class Scene - @items_by_id = Hash( Int64, OBSWebSocket::SceneItem ).new + @items_by_id = Hash( Int64, OBS::SceneItem ).new @items_by_name = Hash( String, Int64 ).new @items_by_index = Array( Int64 ).new - @subscenes = Array( OBSWebSocket::Scene ).new - getter filters : OBSWebSocket::SourceFilters + @subscenes = Array( OBS::Scene ).new + getter filters : OBS::SourceFilters getter name : String - getter parent : OBSWebSocket::SceneList # needed for reverse metasceneitem lookup + getter parent : OBS::SceneList # needed for reverse metasceneitem lookup @age = Int64.new( 0 ) - def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @name : String, @parent : OBSWebSocket::SceneList ) - @filters = OBSWebSocket::SourceFilters.new( @reqchan, @name ) + def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @name : String, @parent : OBS::SceneList ) + @filters = OBS::SourceFilters.new( @reqchan, @name ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetSceneItemList", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "GetSceneItemList", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) # handle "sourceType": "OBS_SOURCE_TYPE_SCENE" rdata["sceneItems"].as_a.each_with_index{ | item, i | sname = item["sourceName"].as_s siid = item["sceneItemId"].as_i64 - @items_by_id[siid] = OBSWebSocket::SceneItem.new( @reqchan, @name, item ) + @items_by_id[siid] = OBS::SceneItem.new( @reqchan, @name, item ) @items_by_index.push( siid ) @items_by_name[sname] = siid if item["sourceType"].as_s == "OBS_SOURCE_TYPE_SCENE" @@ -677,16 +677,16 @@ module OBSWebSocket # Sources may be present a scene via other scenes. # We can still look these up by source name. # Note: OBS refuses to create a referential loop - def metascene : Hash( String, Array( OBSWebSocket::SceneItem ) ) + def metascene : Hash( String, Array( OBS::SceneItem ) ) @subscenes.empty? && populate - metascene = Hash( String, Array( OBSWebSocket::SceneItem ) ).new + metascene = Hash( String, Array( OBS::SceneItem ) ).new newsubscenes = [ self ] while ( subscene = newsubscenes.shift? ) subscene.to_h.each do | siname, sceneitem | if sceneitem.type == "OBS_SOURCE_TYPE_SCENE" newsubscenes.push( parent[siname] ) end - metascene[ siname ]? || ( metascene[ siname ] = Array( OBSWebSocket::SceneItem ).new ) + metascene[ siname ]? || ( metascene[ siname ] = Array( OBS::SceneItem ).new ) metascene[ siname ].push( sceneitem ) # We can have the same scene included in the scene graph multiple times, which will duplicate all its sceneitems in the resulting metascene array. Might be a good idea to add a uniqueness constraint at some point? Not sure anyone would really care one way or the other. end @@ -696,7 +696,7 @@ module OBSWebSocket # returns first SceneItem it finds or Nil # less requests are generated with this method # but managing Nil unions is more annoying - def metasceneitem?( name : String ) : OBSWebSocket::SceneItem | Nil + def metasceneitem?( name : String ) : OBS::SceneItem | Nil result = nil newsubscenes = [ self ] while ( subscene = newsubscenes.shift? ) @@ -712,7 +712,7 @@ module OBSWebSocket end return result end - def []?( name : String ) : OBSWebSocket::SceneItem | Nil + def []?( name : String ) : OBS::SceneItem | Nil ( @items_by_name[name]? || ( @age > 0 ) ) || populate if @items_by_name[name]? ( @items_by_id[ @items_by_name[name] ]? || ( @age > 0 ) ) || populate @@ -721,12 +721,12 @@ module OBSWebSocket return nil end end - def []( name : String ) : OBSWebSocket::SceneItem + def []( name : String ) : OBS::SceneItem @items_by_name[name]? || populate @items_by_id[ @items_by_name[name] ]? || populate return @items_by_id[ @items_by_name[name] ] end - def []( id : Int64 ) : OBSWebSocket::SceneItem + def []( id : Int64 ) : OBS::SceneItem @items_by_id[id]? || populate return @items_by_id[id] end @@ -735,9 +735,9 @@ module OBSWebSocket @items_by_id[ @items_by_index[index] ]? || populate return @items_by_id[ @items_by_index[index] ] end - def to_h : Hash( String, OBSWebSocket::SceneItem ) + def to_h : Hash( String, OBS::SceneItem ) @items_by_id.empty? && populate - hash = Hash( String, OBSWebSocket::SceneItem ).new + hash = Hash( String, OBS::SceneItem ).new @items_by_name.each do | key, value | hash[key] = @items_by_id[value] end @@ -748,15 +748,15 @@ module OBSWebSocket end def program! reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "SetCurrentProgramScene", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "SetCurrentProgramScene", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) reschan.receive # we get weird behavior on transitions unless we block here end def preview! reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "SetCurrentPreviewScene", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "SetCurrentPreviewScene", UUID.random.to_s, JSON.parse( { "sceneName" => @name }.to_json ) ) } ) reschan.receive # we get weird behavior on transitions unless we block here end - def createinput( iname : String, kind : String, settings : OBSWebSocket::InputSettings | Hash( String, String | Bool | Int64 | Float64 ) | Nil = nil, enabled : Bool = true ) + def createinput( iname : String, kind : String, settings : OBS::InputSettings | Hash( String, String | Bool | Int64 | Float64 ) | Nil = nil, enabled : Bool = true ) reqdata = ( Hash( String, String | Bool | Int64 | Float64 | Hash( String, String | Bool | Int64 | Float64 ) ) ).new reqdata["sceneName"] = @name reqdata["inputName"] = iname @@ -769,7 +769,7 @@ module OBSWebSocket end reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "CreateInput", UUID.random.to_s, JSON.parse(reqdata.to_json) ) } ) + @reqchan.send( { reschan, OBS.req( "CreateInput", UUID.random.to_s, JSON.parse(reqdata.to_json) ) } ) d = reschan.receive # maybe block until the event comes in? # will have to depend on the EventSubscription state @@ -785,10 +785,10 @@ module OBSWebSocket end def deletecache( ) - @items_by_id = Hash( Int64, OBSWebSocket::SceneItem ).new + @items_by_id = Hash( Int64, OBS::SceneItem ).new @items_by_name = Hash( String, Int64 ).new @items_by_index = Array( Int64 ).new - @subscenes = Array( OBSWebSocket::Scene ).new + @subscenes = Array( OBS::Scene ).new end def deletecache( siid : Int64, siname : String ) @items_by_id.delete( siid ) @@ -806,7 +806,7 @@ module OBSWebSocket getter enabled : Bool getter blendmode : ( String | Nil ) getter locked : Bool - getter transform : OBSWebSocket::SceneItemTransform + getter transform : OBS::SceneItemTransform def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @scenename : String, json : JSON::Any ) @id = json["sceneItemId"].as_i64 @name = json["sourceName"].as_s @@ -815,7 +815,7 @@ module OBSWebSocket @locked = json["sceneItemLocked"].as_bool @enabled = json["sceneItemEnabled"].as_bool @blendmode = json["sceneItemBlendmode"]?.as_s? - @transform = OBSWebSocket::SceneItemTransform.new( @reqchan, json["sceneItemTransform"], @name, @id ) + @transform = OBS::SceneItemTransform.new( @reqchan, json["sceneItemTransform"], @name, @id ) end def enabledcache( v : Bool ) @enabled = v @@ -831,18 +831,18 @@ module OBSWebSocket end end def enable! - @reqchan.send( { nil, OBSWebSocket.req( "SetSceneItemEnabled", UUID.random.to_s, JSON.parse( { "sceneName" => @scenename, "sceneItemId" => @id, "sceneItemEnabled" => true }.to_json ) ) } ) + @reqchan.send( { nil, OBS.req( "SetSceneItemEnabled", UUID.random.to_s, JSON.parse( { "sceneName" => @scenename, "sceneItemId" => @id, "sceneItemEnabled" => true }.to_json ) ) } ) end def disable! - @reqchan.send( { nil, OBSWebSocket.req( "SetSceneItemEnabled", UUID.random.to_s, JSON.parse( { "sceneName" => @scenename, "sceneItemId" => @id, "sceneItemEnabled" => false }.to_json ) ) } ) + @reqchan.send( { nil, OBS.req( "SetSceneItemEnabled", UUID.random.to_s, JSON.parse( { "sceneName" => @scenename, "sceneItemId" => @id, "sceneItemEnabled" => false }.to_json ) ) } ) end - def transform( newtransform : OBSWebSocket::SceneItemTransform | Hash( String, String | Bool | Int64 | Float64 ) ) + def transform( newtransform : OBS::SceneItemTransform | Hash( String, String | Bool | Int64 | Float64 ) ) reqdata = ( Hash( String, String | Bool | Int64 | Float64 | Hash( String, String | Bool | Int64 | Float64 ) ) ).new reqdata["sceneItemId"] = @id.to_i64 reqdata["sceneName"] = @scenename reqdata["sceneItemTransform"] = Hash( String, String | Bool | Int64 | Float64).new.merge( newtransform.to_h ) reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "SetSceneItemTransform", UUID.random.to_s, JSON.parse(reqdata.to_json) ) } ) + @reqchan.send( { reschan, OBS.req( "SetSceneItemTransform", UUID.random.to_s, JSON.parse(reqdata.to_json) ) } ) d = reschan.receive end end @@ -854,7 +854,7 @@ module OBSWebSocket end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetSceneItemTransform", UUID.random.to_s, JSON.parse( { "sceneName" => @sceneitemname, "sceneItemId" => @sceneitemid }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "GetSceneItemTransform", UUID.random.to_s, JSON.parse( { "sceneName" => @sceneitemname, "sceneItemId" => @sceneitemid }.to_json ) ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @sceneitemtransform = Hash(String, String | Bool | Int64 | Float64 ).from_json(rdata["sceneItemTransform"].to_json) @@ -876,12 +876,12 @@ module OBSWebSocket end class Sources - getter scenes : OBSWebSocket::SceneList - getter inputs : OBSWebSocket::Inputs - @last_known_real_transform = Hash( String, OBSWebSocket::SceneItemTransform ).new - def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBSWebSocket::Inputs, @scenes : OBSWebSocket::SceneList ) + getter scenes : OBS::SceneList + getter inputs : OBS::Inputs + @last_known_real_transform = Hash( String, OBS::SceneItemTransform ).new + def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @inputs : OBS::Inputs, @scenes : OBS::SceneList ) end - def []?( index : String ) : OBSWebSocket::Scene | OBSWebSocket::Input | Nil + def []?( index : String ) : OBS::Scene | OBS::Input | Nil if @inputs[index]? return @inputs[index] elsif @scenes[index]? @@ -890,37 +890,37 @@ module OBSWebSocket return nil end end - def to_h() : Hash( String, OBSWebSocket::Scene | OBSWebSocket::Input ) + def to_h() : Hash( String, OBS::Scene | OBS::Input ) return @inputs.to_h.merge( @scenes.to_h ) end def last_known_real_transform_add( sourcename : String, transform : JSON::Any ) - @last_known_real_transform[ sourcename ] = OBSWebSocket::SceneItemTransform.new( @reqchan, transform, sourcename, 0.to_i64 ) + @last_known_real_transform[ sourcename ] = OBS::SceneItemTransform.new( @reqchan, transform, sourcename, 0.to_i64 ) end - def last_known_real_transform( sourcename : String ) : OBSWebSocket::SceneItemTransform + def last_known_real_transform( sourcename : String ) : OBS::SceneItemTransform @last_known_real_transform[ sourcename ] end - def last_known_real_transform?( sourcename : String ) : OBSWebSocket::SceneItemTransform | Nil + def last_known_real_transform?( sourcename : String ) : OBS::SceneItemTransform | Nil @last_known_real_transform[ sourcename ]? end end class Inputs - @inputs = Hash( String, OBSWebSocket::Input ).new + @inputs = Hash( String, OBS::Input ).new def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ) ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetInputList", UUID.random.to_s) } ) + @reqchan.send( { reschan, OBS.req( "GetInputList", UUID.random.to_s) } ) d = reschan.receive if ( rdata = d["responseData"]? ) rdata["inputs"].as_a.each{ | input | - @inputs[input["inputName"].as_s] = OBSWebSocket::Input.new( @reqchan, input ) + @inputs[input["inputName"].as_s] = OBS::Input.new( @reqchan, input ) } else raise ResponseDataMissingException.new( "#{d["requestType"].as_s} #{d["requestStatus"]["code"].as_i64}: #{d["requestStatus"]["comment"].as_s}" ) end end - def []?( index : String ) : OBSWebSocket::Input | Nil + def []?( index : String ) : OBS::Input | Nil @inputs[index]? || populate return @inputs[index]? end @@ -928,10 +928,10 @@ module OBSWebSocket @inputs[index]? || populate return @inputs[index] end - def []=( index : String, input = OBSWebSocket::Input ) + def []=( index : String, input = OBS::Input ) @inputs[index] = input end - def to_h : Hash( String, OBSWebSocket::Input ) + def to_h : Hash( String, OBS::Input ) @inputs.empty? && populate return @inputs end @@ -941,7 +941,7 @@ module OBSWebSocket @inputs[new].namecache( new ) end def deletecache() - @inputs = Hash( String, OBSWebSocket::Input ).new + @inputs = Hash( String, OBS::Input ).new end def deletecache( input : String ) @inputs.delete( input ) @@ -951,17 +951,17 @@ module OBSWebSocket class Input getter kind : String getter name : String - getter settings : OBSWebSocket::InputSettings - getter filters : OBSWebSocket::SourceFilters + getter settings : OBS::InputSettings + getter filters : OBS::SourceFilters def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), json : JSON::Any ) @kind = json["inputKind"].as_s @name = json["inputName"].as_s - @settings = OBSWebSocket::InputSettings.new( @reqchan, @name ) - @filters = OBSWebSocket::SourceFilters.new( @reqchan, @name ) + @settings = OBS::InputSettings.new( @reqchan, @name ) + @filters = OBS::SourceFilters.new( @reqchan, @name ) end def delete!( ) reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "RemoveInput", UUID.random.to_s, JSON.parse({ "inputName" => @name }.to_json) ) } ) + @reqchan.send( { reschan, OBS.req( "RemoveInput", UUID.random.to_s, JSON.parse({ "inputName" => @name }.to_json) ) } ) d = reschan.receive end def namecache( name : String ) @@ -984,7 +984,7 @@ module OBSWebSocket end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetInputSettings", UUID.random.to_s, JSON.parse( { "inputName" => @name }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "GetInputSettings", UUID.random.to_s, JSON.parse( { "inputName" => @name }.to_json ) ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) @inputsettings = Hash(String, String | Bool | Int64 | Float64 ).from_json(rdata["inputSettings"].to_json) @@ -1010,21 +1010,21 @@ module OBSWebSocket end end - class SourceFilters # Note: a property of both OBSWebSocket::Input and OBSWebSocket::Scene - @filters_by_name = Hash( String, OBSWebSocket::SourceFilter ).new + class SourceFilters # Note: a property of both OBS::Input and OBS::Scene + @filters_by_name = Hash( String, OBS::SourceFilter ).new @filters_by_index = Array( String ).new getter sourcename : String def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @sourcename : String ) end def populate reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "GetSourceFilterList", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "GetSourceFilterList", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename }.to_json ) ) } ) d = reschan.receive if ( rdata = d["responseData"]? ) rdata["filters"].as_a.each do | filter | name = filter["filterName"].as_s @filters_by_index.push( name ) - @filters_by_name[name] = OBSWebSocket::SourceFilter.new( @reqchan, @sourcename, filter ) + @filters_by_name[name] = OBS::SourceFilter.new( @reqchan, @sourcename, filter ) end else raise ResponseDataMissingException.new( "#{d["requestType"].as_s} #{d["requestStatus"]["code"].as_i64}: #{d["requestStatus"]["comment"].as_s}" ) @@ -1035,7 +1035,7 @@ module OBSWebSocket @filters_by_name[@filters_by_index[index]]? || populate @filters_by_name[@filters_by_index[index]] end - def []?( key : String ) : OBSWebSocket::SourceFilter | Nil + def []?( key : String ) : OBS::SourceFilter | Nil @filters_by_name[key]? || populate @filters_by_name[key]? end @@ -1053,13 +1053,13 @@ module OBSWebSocket getter name : String getter kind : String getter enabled : Bool - getter settings : OBSWebSocket::SourceFilterSettings + getter settings : OBS::SourceFilterSettings getter sourcename : String def initialize( @reqchan : Channel( Tuple( Channel( JSON::Any ) | Nil, String ) ), @sourcename, json : JSON::Any ) @kind = json["filterKind"].as_s @name = json["filterName"].as_s @enabled = json["filterEnabled"].as_bool - @settings = OBSWebSocket::SourceFilterSettings.new( @reqchan, @name, json["filterSettings"] ) + @settings = OBS::SourceFilterSettings.new( @reqchan, @name, json["filterSettings"] ) end def toggle! if @enabled @@ -1070,13 +1070,13 @@ module OBSWebSocket end def enable! reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "SetSourceFilterEnabled", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename, "filterName" => @name, "filterEnabled" => true }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "SetSourceFilterEnabled", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename, "filterName" => @name, "filterEnabled" => true }.to_json ) ) } ) d = reschan.receive return d["requestStatus"]["result"].as_bool end def disable! reschan = Channel( JSON::Any ).new - @reqchan.send( { reschan, OBSWebSocket.req( "SetSourceFilterEnabled", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename, "filterName" => @name, "filterEnabled" => false }.to_json ) ) } ) + @reqchan.send( { reschan, OBS.req( "SetSourceFilterEnabled", UUID.random.to_s, JSON.parse( { "sourceName" => @sourcename, "filterName" => @name, "filterEnabled" => false }.to_json ) ) } ) d = reschan.receive return d["requestStatus"]["result"].as_bool end |