obsproject / obs-websocket

Remote-control of OBS Studio through WebSocket
GNU General Public License v2.0
3.84k stars 704 forks source link

Feature Request: Analysis of GetSourceScreenshot and SaveSourceScreenshot #1104

Closed Cyranejro closed 1 year ago

Cyranejro commented 1 year ago

Feature Request Type

Other

Feature Request Type (Other)

No response

Requested Feature

Analysis of GetSourceScreenshot and SaveSourceScreenshot

Introduction

While working on my project, I came across two functions in OBS Studio's OBSWebsocket API: GetSourceScreenshot and SaveSourceScreenshot. After testing both of these functions, I discovered that SaveSourceScreenshot executed in less than 100 ms, while GetSourceScreenshot executed in over 3000 ms. This led me to investigate further into the reasons for this performance difference.

Findings

Through my investigation, I discovered that SaveSourceScreenshot simply saves the current source's screenshot to a file on the local disk, while GetSourceScreenshot actually retrieves the source's texture data and then generates a screenshot from that data. This process is much more resource-intensive, leading to the significant time difference between the two functions.

Why does SaveSourceScreenshot execute faster?

The reason SaveSourceScreenshotexecutes faster than GetSourceScreenshotis that it doesn't have to transfer the image data over the network. When you call GetSourceScreenshot, OBS has to capture the screenshot, encode it as a base64-encoded string, and then send it over the network to your application. This process takes time, especially if the image size is large or the network connection is slow. In contrast, SaveSourceScreenshotsimply saves the image data to a file on your hard drive, which is much faster than sending it over the network.

I hope this analysis of the OBSWebsocket API in OBS Studio is helpful for others who are working with it. I would like to know if there are any improvements that can be made to my approach or if this is the expected performance.

Requested Feature Usage Scenario

I want to load data into memory, not save on disk but GetSourceScreenshot is too slow.

tt2468 commented 1 year ago

This appears to be neither a feature request nor a bug report. Indeed it is more complicated to send a screenshot over the network than it is to save it to disk. GetSourceScreenshot has the additional steps of encoding the image data to base64 and sending it over the WebSocket transport.

Your 3000ms number does sound off though. I would recommend checking over your client implementation, as obs-websocket absolutely does not take that long to do everything it needs to on its side. It sounds like your obs-websocket client is slow.