Skip to content

Commit

Permalink
browser(firefox): use 16-byte long uid instead of ordinal as screenca…
Browse files Browse the repository at this point in the history
…st id (#4147)
  • Loading branch information
yury-s authored Oct 14, 2020
1 parent e9f5477 commit 3c32c16
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
4 changes: 2 additions & 2 deletions browser_patches/firefox/BUILD_NUMBER
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1190
Changed: [email protected] Tue 13 Oct 2020 09:08:57 AM PDT
1191
Changed: [email protected] Wed Oct 14 16:12:19 PDT 2020
2 changes: 1 addition & 1 deletion browser_patches/firefox/juggler/protocol/PageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class PageHandler {

_onScreencastStarted() {
const info = this._pageTarget.screencastInfo();
this._session.emitEvent('Page.screencastStarted', { screencastId: '' + info.videoSessionId, file: info.file });
this._session.emitEvent('Page.screencastStarted', { screencastId: info.videoSessionId, file: info.file });
}

_onDialogOpened(dialog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ interface nsIDocShell;
[scriptable, uuid(d8c4d9e0-9462-445e-9e43-68d3872ad1de)]
interface nsIScreencastService : nsISupports
{
long startVideoRecording(in nsIDocShell docShell, in ACString fileName, in uint32_t width, in uint32_t height, in double scale, in int32_t offset_top);
AString startVideoRecording(in nsIDocShell docShell, in ACString fileName, in uint32_t width, in uint32_t height, in double scale, in int32_t offset_top);

/**
* Will emit 'juggler-screencast-stopped' when the video file is saved.
*/
void stopVideoRecording(in long sessionId);
void stopVideoRecording(in AString sessionId);
};
45 changes: 33 additions & 12 deletions browser_patches/firefox/juggler/screencast/nsScreencastService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mozilla/StaticPtr.h"
#include "nsIDocShell.h"
#include "nsIObserverService.h"
#include "nsIRandomGenerator.h"
#include "nsISupportsPrimitives.h"
#include "nsThreadManager.h"
#include "nsView.h"
Expand All @@ -34,7 +35,7 @@ namespace {

StaticRefPtr<nsScreencastService> gScreencastService;

rtc::scoped_refptr<webrtc::VideoCaptureModule> CreateWindowCapturer(nsIWidget* widget, int sessionId) {
rtc::scoped_refptr<webrtc::VideoCaptureModule> CreateWindowCapturer(nsIWidget* widget) {
if (gfxPlatform::IsHeadless()) {
HeadlessWidget* headlessWidget = static_cast<HeadlessWidget*>(widget);
return HeadlessWindowCapturer::Create(headlessWidget);
Expand All @@ -47,19 +48,35 @@ rtc::scoped_refptr<webrtc::VideoCaptureModule> CreateWindowCapturer(nsIWidget* w
nsCString windowId;
windowId.AppendPrintf("%" PRIuPTR, rawWindowId);
bool captureCursor = false;
return webrtc::DesktopCaptureImpl::Create(sessionId, windowId.get(), webrtc::CaptureDeviceType::Window, captureCursor);
static int moduleId = 0;
return webrtc::DesktopCaptureImpl::Create(++moduleId, windowId.get(), webrtc::CaptureDeviceType::Window, captureCursor);
}

void NotifyScreencastStopped(int32_t sessionId) {
void NotifyScreencastStopped(const nsString& sessionId) {
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
if (!observerService) {
fprintf(stderr, "NotifyScreencastStopped error: no observer service\n");
return;
}

nsString id;
id.AppendPrintf("%" PRIi32, sessionId);
observerService->NotifyObservers(nullptr, "juggler-screencast-stopped", id.get());
observerService->NotifyObservers(nullptr, "juggler-screencast-stopped", sessionId.get());
}

nsresult generateUid(nsString& uid) {
nsresult rv = NS_OK;
nsCOMPtr<nsIRandomGenerator> rg = do_GetService("@mozilla.org/security/random-generator;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);

uint8_t* buffer;
const int kLen = 16;
rv = rg->GenerateRandomBytes(kLen, &buffer);
NS_ENSURE_SUCCESS(rv, rv);

for (int i = 0; i < kLen; i++) {
uid.AppendPrintf("%" PRIx8, buffer[i]);
}
free(buffer);
return rv;
}
}

Expand Down Expand Up @@ -123,9 +140,8 @@ nsScreencastService::nsScreencastService() = default;
nsScreencastService::~nsScreencastService() {
}

nsresult nsScreencastService::StartVideoRecording(nsIDocShell* aDocShell, const nsACString& aFileName, uint32_t width, uint32_t height, double scale, int32_t offsetTop, int32_t* sessionId) {
nsresult nsScreencastService::StartVideoRecording(nsIDocShell* aDocShell, const nsACString& aFileName, uint32_t width, uint32_t height, double scale, int32_t offsetTop, nsAString& sessionId) {
MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Screencast service must be started on the Main thread.");
*sessionId = -1;

PresShell* presShell = aDocShell->GetPresShell();
if (!presShell)
Expand All @@ -138,8 +154,7 @@ nsresult nsScreencastService::StartVideoRecording(nsIDocShell* aDocShell, const
return NS_ERROR_UNEXPECTED;
nsIWidget* widget = view->GetWidget();

*sessionId = ++mLastSessionId;
rtc::scoped_refptr<webrtc::VideoCaptureModule> capturer = CreateWindowCapturer(widget, *sessionId);
rtc::scoped_refptr<webrtc::VideoCaptureModule> capturer = CreateWindowCapturer(widget);
if (!capturer)
return NS_ERROR_FAILURE;

Expand Down Expand Up @@ -171,11 +186,17 @@ nsresult nsScreencastService::StartVideoRecording(nsIDocShell* aDocShell, const
if (!session->Start())
return NS_ERROR_FAILURE;

mIdToSession.emplace(*sessionId, std::move(session));
nsString uid;
nsresult rv = generateUid(uid);
NS_ENSURE_SUCCESS(rv, rv);

sessionId = uid;
mIdToSession.emplace(uid, std::move(session));
return NS_OK;
}

nsresult nsScreencastService::StopVideoRecording(int32_t sessionId) {
nsresult nsScreencastService::StopVideoRecording(const nsAString& aSessionId) {
nsString sessionId(aSessionId);
auto it = mIdToSession.find(sessionId);
if (it == mIdToSession.end())
return NS_ERROR_INVALID_ARG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once

#include <memory>
#include <unordered_map>
#include <map>
#include "nsIScreencastService.h"

namespace mozilla {
Expand All @@ -23,8 +23,7 @@ class nsScreencastService final : public nsIScreencastService {
~nsScreencastService();

class Session;
int mLastSessionId = 0;
std::unordered_map<int, std::unique_ptr<Session>> mIdToSession;
std::map<nsString, std::unique_ptr<Session>> mIdToSession;
};

} // namespace mozilla

0 comments on commit 3c32c16

Please sign in to comment.