Skip to content

Conversation

@skrashevich
Copy link
Contributor

Summary of Content:
This PR introduces an enhanced mechanism for caching keyframes within the (M)JPEG streaming service. The major changes include:

  1. Cache Structure Implementation:

    • A new CacheEntry structure that includes a byte slice for the frame and a timestamp for expiration was created.
    • A global keyframeCache with a read-write mutex for thread-safe operations was established, storing the cache entries in a map.
    • A placeholder cache duration (cacheDuration) has been defined, which is currently set to one minute but marked with a TODO to make it configurable in the future.
  2. Cache Utilization in Keyframe Handler:

    • Within the handlerKeyframe function, the commit implements a check to serve frames directly from the cache if they are found and haven't expired based on cacheDuration.
    • If a keyframe is not in the cache or has expired, it is fetched, transformed, and then updated in the cache with the current timestamp.
  3. Periodic Cache Cleanup:

    • A new cleanupCache goroutine function has been added to periodically clean up the expired keyframes from the cache. The cleanup runs in intervals equal to cacheDuration, ensuring that stale entries do not persist in memory.
  4. Helper Function:

    • Added writeJPEGResponse function to consolidate the response writing part with a correct setup for content-type and content-length in the HTTP header, promoting code reuse and clarity.

These modifications improve the efficiency of the JPEG streaming by reducing the need for frequent keyframe reconstructions, thus potentially lowering the processing overhead and speeding up the frame delivery. The implemented caching strategy enables the application to handle high-concurrency scenarios more effectively by reducing duplicate processing and serving cached results wherever applicable.

@AlexxIT AlexxIT self-assigned this Feb 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants