Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/tools/desktop/screen-capturer.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Key methods (inherited from `DesktopCapturer`):
- `void selectSource(DesktopSource source)` – choose the screen to capture
- `void setFocusSelectedSource(boolean focus)` – try to focus the selected source during capture
- `void setMaxFrameRate(int maxFrameRate)` – cap the capture FPS
- `void start(DesktopCaptureCallback callback)` – begin capturing; frames are delivered via callback
- `void captureFrame()` – request a single frame (manual capture)
- `void start(DesktopCaptureCallback callback)` – begin capturing; frames are delivered via callback. A capturer can be started once, a second call throws `IllegalStateException`
- `void captureFrame()` – request a single frame (manual capture) after `start`; without a capture backend, as on Linux without a display, the callback gets `ERROR_PERMANENT`
- `void dispose()` – release resources

## Typical usage: enumerate screens
Expand Down
4 changes: 2 additions & 2 deletions docs/tools/desktop/window-capturer.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Key methods (inherited from `DesktopCapturer`):
- `void selectSource(DesktopSource source)` – choose which window to capture
- `void setFocusSelectedSource(boolean focus)` – attempt to focus/raise the selected window
- `void setMaxFrameRate(int maxFrameRate)` – limit the capture frame rate
- `void start(DesktopCaptureCallback callback)` – start capture, frames via callback
- `void captureFrame()` – request a single capture
- `void start(DesktopCaptureCallback callback)` – start capture, frames via callback. A capturer can be started once, a second call throws `IllegalStateException`
- `void captureFrame()` – request a single capture after `start`; without a capture backend, as on Linux without a display, the callback gets `ERROR_PERMANENT`
- `void dispose()` – release resources

## Typical usage: enumerate windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,31 @@

#include <jni.h>
#include <memory>
#include <mutex>

namespace jni
{
class DesktopCaptureCallback;

class DesktopCapturer
{
public:
explicit DesktopCapturer(bool screenCapturer);
~DesktopCapturer();

// Starts capturing and keeps the callback until this capturer is
// destroyed. Returns false if it has been started before: WebRTC's
// capturers take a single callback.
bool Start(std::unique_ptr<DesktopCaptureCallback> callback);

// Captures a frame. Without a capture backend, the callback gets
// ERROR_PERMANENT. Returns false if there is no callback yet, which
// a capturer of WebRTC would dereference.
bool CaptureFrame();

// webrtc::DesktopCapturer implementations.
void Start(webrtc::DesktopCapturer::Callback * callback);
void SetMaxFrameRate(uint32_t max_frame_rate);
void SetSharedMemoryFactory(std::unique_ptr<webrtc::SharedMemoryFactory> factory);
void CaptureFrame();
void SetExcludedWindow(webrtc::WindowId window);
bool GetSourceList(webrtc::DesktopCapturer::SourceList * sources);
bool SelectSource(webrtc::DesktopCapturer::SourceId id);
Expand All @@ -47,6 +58,11 @@ namespace jni
bool IsOccluded(const webrtc::DesktopVector & pos);

protected:
std::mutex startMutex;

// Declared before the capturer, which uses it until destroyed.
std::unique_ptr<DesktopCaptureCallback> captureCallback;

std::unique_ptr<webrtc::DesktopCapturer> capturer;

bool focusSelectedSource;
Expand Down
27 changes: 7 additions & 20 deletions webrtc-jni/src/main/cpp/src/JNI_DesktopCapturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture
return;
}

auto callback = GetHandle<jni::DesktopCaptureCallback>(env, caller, "callbackHandle");

SetHandle<std::nullptr_t>(env, caller, "callbackHandle", nullptr);

// A captureFrame() running on another thread, or the one whose callback
// disposes the capturer, still uses both.
jni::ReleaseHandle(env, capturer, [capturer, callback](bool) {
// disposes the capturer, still uses it.
jni::ReleaseHandle(env, capturer, [capturer](bool) {
delete capturer;
delete callback;
});
});
}
Expand Down Expand Up @@ -129,15 +124,10 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture
jni::NativeLease<jni::DesktopCapturer> capturer(env, caller);
CHECK_HANDLE(capturer);

auto callback = new jni::DesktopCaptureCallback(env, jni::JavaGlobalRef<jobject>(env, jcallback));

try {
SetHandle(env, caller, "callbackHandle", callback);
auto callback = std::make_unique<jni::DesktopCaptureCallback>(env, jni::JavaGlobalRef<jobject>(env, jcallback));

capturer->Start(callback);
}
catch (...) {
ThrowCxxJavaException(env);
if (!capturer->Start(std::move(callback))) {
ThrowJavaException(env, "java/lang/IllegalStateException", "Capture has been started already");
}
});
}
Expand All @@ -149,11 +139,8 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_desktop_DesktopCapture
jni::NativeLease<jni::DesktopCapturer> capturer(env, caller);
CHECK_HANDLE(capturer);

try {
capturer->CaptureFrame();
}
catch (...) {
ThrowCxxJavaException(env);
if (!capturer->CaptureFrame()) {
ThrowJavaException(env, "java/lang/IllegalStateException", "Capture has not been started");
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "media/video/desktop/DesktopCapturer.h"
#include "media/video/desktop/DesktopCaptureCallback.h"

#include "modules/desktop_capture/desktop_capturer.h"
#include "modules/desktop_capture/desktop_capture_options.h"
Expand Down Expand Up @@ -56,7 +57,8 @@ namespace jni
"session without PipeWire support, or snap confinement."
: "DesktopCapturer: CreateWindowCapturer returned null. "
"Possible causes: missing X11/XFIXES extension or snap confinement.");
// Leave capturer as nullptr; callers check via GetSourceList returning false.
// Leave capturer as nullptr: GetSourceList returns false, and
// CaptureFrame reports ERROR_PERMANENT.
return;
}

Expand All @@ -68,15 +70,25 @@ namespace jni
capturer.reset();
}

void DesktopCapturer::Start(webrtc::DesktopCapturer::Callback * callback)
bool DesktopCapturer::Start(std::unique_ptr<DesktopCaptureCallback> callback)
{
if (!capturer) return;
std::lock_guard<std::mutex> lock(startMutex);

if (captureCallback) {
return false;
}

capturer->Start(callback);
if (capturer) {
capturer->Start(callback.get());

if (focusSelectedSource) {
capturer->FocusOnSelectedSource();
if (focusSelectedSource) {
capturer->FocusOnSelectedSource();
}
}

captureCallback = std::move(callback);

return true;
}

void DesktopCapturer::SetMaxFrameRate(uint32_t max_frame_rate)
Expand All @@ -91,15 +103,34 @@ namespace jni
capturer->SetSharedMemoryFactory(std::move(factory));
}

void DesktopCapturer::CaptureFrame()
bool DesktopCapturer::CaptureFrame()
{
if (!capturer) return;
DesktopCaptureCallback * callback = nullptr;

{
// Not held while capturing: the result may call back into this
// capturer on the same thread.
std::lock_guard<std::mutex> lock(startMutex);

callback = captureCallback.get();
}

if (callback == nullptr) {
return false;
}

if (!capturer) {
callback->OnCaptureResult(webrtc::DesktopCapturer::Result::ERROR_PERMANENT, nullptr);
return true;
}

#if defined(WEBRTC_MAC)
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
#endif

capturer->CaptureFrame();

return true;
}

void DesktopCapturer::SetExcludedWindow(webrtc::WindowId window)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public FrameType getFrameType() {
}

/**
* @return The rotation of the frame in degrees, a multiple of 90.
* @return The rotation of the frame in degrees: 0, 90, 180 or 270.
*/
public int getRotation() {
return rotation;
Expand Down Expand Up @@ -301,7 +301,7 @@ public Builder setFrameType(FrameType frameType) {

/**
* @param rotation The rotation of the frame in degrees, a multiple of
* 90.
* 90. It is normalized to 0, 90, 180 or 270.
*
* @return This builder.
*/
Expand Down Expand Up @@ -343,8 +343,12 @@ public EncodedImage build() {
throw new IllegalArgumentException("Size must not be negative");
}

// Native code takes it for a webrtc::VideoRotation, which has only
// these four values.
int normalizedRotation = ((rotation % 360) + 360) % 360;

return new EncodedImage(buffer, encodedWidth, encodedHeight,
captureTimeNs, frameType, rotation, qp);
captureTimeNs, frameType, normalizedRotation, qp);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public enum Result {
}


/**
* The DesktopCapturer doesn't take ownership of the callback. When the
* DesktopCapturer is disposed, the callback will be disposed using this
* handle.
*/
private long callbackHandle;


@Override
public native void dispose();

Expand Down Expand Up @@ -110,15 +102,22 @@ public enum Result {
public native void setMaxFrameRate(int maxFrameRate);

/**
* Starts the desktop capture process with the provided callback.
* Starts the desktop capture process with the provided callback. A capturer
* can be started once and keeps its callback until it is disposed.
*
* @param callback The callback that receives capture events and frames.
*
* @throws IllegalStateException If the capturer has been started before.
*/
public native void start(DesktopCaptureCallback callback);

/**
* Captures a single frame manually.
* The capture result will be delivered via the callback provided in {@link #start}.
* Where the platform offers no capture backend, for example on Linux without
* a display, the result is {@link Result#ERROR_PERMANENT}.
*
* @throws IllegalStateException If the capturer has not been started.
*/
public native void captureFrame();

Expand Down
16 changes: 16 additions & 0 deletions webrtc/src/test/java/dev/onvoid/webrtc/VideoCodecFactoryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,22 @@ void encodedImageBuilderValidates() {
assertEquals(7, (int) image.getQp());
}

@Test
void encodedImageRotationIsNormalized() {
assertEquals(270, encodedImageRotatedBy(-90));
assertEquals(0, encodedImageRotatedBy(360));
assertEquals(90, encodedImageRotatedBy(450));
assertEquals(180, encodedImageRotatedBy(180));
}

private static int encodedImageRotatedBy(int rotation) {
return EncodedImage.builder()
.setBuffer(ByteBuffer.allocate(1))
.setRotation(rotation)
.build()
.getRotation();
}

private void assertFramesReceived(PeerConnectionFactory built, String codec) throws Exception {
CountDownLatch received = new CountDownLatch(10);

Expand Down
Loading
Loading