예제 #1
0
        private async Task <JObject> SendAndReceive(StreamlabsOBSRequest request)
        {
            JObject result = new JObject();

            await this.idSempahoreLock.WaitAndRelease(async() =>
            {
                request.ID = this.currentID;
                this.currentID++;

                JObject requestJObj = JObject.FromObject(request);
                using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream(ConnectionString))
                {
                    string requestString = requestJObj.ToString(Formatting.None);
                    byte[] requestBytes  = Encoding.UTF8.GetBytes(requestString);

                    await Task.WhenAny(Task.Run(async() =>
                    {
                        namedPipeClient.Connect();
                        await namedPipeClient.WriteAsync(requestBytes, 0, requestBytes.Length);

                        byte[] responseBytes = new byte[1000000];
                        int count            = await namedPipeClient.ReadAsync(responseBytes, 0, responseBytes.Length);

                        string responseString = Encoding.ASCII.GetString(responseBytes, 0, count);
                        result = JObject.Parse(responseString);
                    }), Task.Delay(5000));
                }
            });

            return(result);
        }
예제 #2
0
        public async Task SetSourceDimensions(string sceneName, string sourceName, StreamingSourceDimensions dimensions)
        {
            StreamlabsOBSSceneItem sceneItem = await this.GetSceneItem(sceneName, sourceName);

            if (sceneItem != null)
            {
                StreamlabsOBSRequest request = new StreamlabsOBSRequest("setTransform", sceneItem.ResourceID);

                JObject positionJObj = new JObject();
                positionJObj["x"] = dimensions.X;
                positionJObj["y"] = dimensions.Y;

                JObject scaleObj = new JObject();
                scaleObj["x"] = dimensions.XScale;
                scaleObj["y"] = dimensions.YScale;

                JObject jobj = new JObject();
                jobj["position"] = positionJObj;
                jobj["scale"]    = scaleObj;
                jobj["rotation"] = dimensions.Rotation;

                request.Arguments.Add(jobj);
                await this.SendAndReceive(request);
            }
        }
예제 #3
0
        public async Task SetSourceVisibility(string sceneName, string sourceName, bool visibility)
        {
            StreamlabsOBSSceneItem sceneItem = await this.GetSceneItem(sceneName, sourceName);

            if (sceneItem != null)
            {
                StreamlabsOBSRequest request = new StreamlabsOBSRequest("setVisibility", sceneItem.ResourceID);
                request.Arguments.Add(visibility);
                await this.SendAndReceive(request);
            }
        }
예제 #4
0
        private async Task <T> GetResult <T>(StreamlabsOBSRequest request)
        {
            JObject responseJObj = await this.SendAndReceive(request);

            StreamlabsOBSResponse response = responseJObj.ToObject <StreamlabsOBSResponse>();

            if (response != null && response.Result != null)
            {
                return(response.Result.ToObject <T>());
            }
            return(default(T));
        }
예제 #5
0
        private async Task <List <T> > GetArrayResult <T>(StreamlabsOBSRequest request)
        {
            List <T> results      = new List <T>();
            JObject  responseJObj = await this.SendAndReceive(request);

            StreamlabsOBSArrayResponse response = responseJObj.ToObject <StreamlabsOBSArrayResponse>();

            if (response != null && response.Result != null)
            {
                foreach (JToken token in response.Result)
                {
                    results.Add(token.ToObject <T>());
                }
            }
            return(results);
        }
예제 #6
0
        public async Task SetWebBrowserSourceURL(string sceneName, string sourceName, string url)
        {
            StreamlabsOBSSceneItem sceneItem = await this.GetSceneItem(sceneName, sourceName);

            if (sceneItem != null)
            {
                StreamlabsOBSRequest getSourceRequest = new StreamlabsOBSRequest("getSource", sceneItem.ResourceID);
                getSourceRequest.Arguments.Add(sceneItem.SourceID);
                StreamlabsOBSSource source = await this.GetResult <StreamlabsOBSSource>(getSourceRequest);

                if (source != null && source.Type.Equals("browser_source"))
                {
                    IEnumerable <JObject> properties = await this.GetArrayResult <JObject>(new StreamlabsOBSRequest("getPropertiesFormData", source.ResourceID));

                    if (properties != null)
                    {
                        foreach (JObject property in properties)
                        {
                            if (property["name"] != null && property["name"].ToString().Equals("url"))
                            {
                                property["value"] = url;
                            }
                        }

                        StreamlabsOBSRequest setSourcePropertiesRequest = new StreamlabsOBSRequest("setPropertiesFormData", source.ResourceID);
                        JArray array = new JArray();
                        foreach (JObject property in properties)
                        {
                            array.Add(property);
                        }
                        setSourcePropertiesRequest.Arguments.Add(array);
                        await this.SendAndReceive(setSourcePropertiesRequest);
                    }
                }
            }
        }