2015-05-13 09:04:30 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "CanvasCaptureMediaStream.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
|
2015-05-13 09:04:30 +03:00
|
|
|
#include "DOMMediaStream.h"
|
|
|
|
#include "ImageContainer.h"
|
2019-10-02 13:23:02 +03:00
|
|
|
#include "MediaTrackGraph.h"
|
2019-10-02 13:22:53 +03:00
|
|
|
#include "Tracing.h"
|
|
|
|
#include "VideoSegment.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
2015-05-13 09:04:30 +03:00
|
|
|
#include "mozilla/dom/CanvasCaptureMediaStreamBinding.h"
|
2015-07-16 13:31:24 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2015-06-11 05:36:12 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-05-13 09:04:30 +03:00
|
|
|
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
OutputStreamDriver::OutputStreamDriver(SourceMediaTrack* aSourceStream,
|
2016-04-06 15:56:44 +03:00
|
|
|
const PrincipalHandle& aPrincipalHandle)
|
2015-09-17 07:36:57 +03:00
|
|
|
: FrameCaptureListener(),
|
|
|
|
mSourceStream(aSourceStream),
|
2019-03-22 14:42:10 +03:00
|
|
|
mPrincipalHandle(aPrincipalHandle) {
|
2015-09-17 07:36:57 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-05-13 09:04:41 +03:00
|
|
|
MOZ_ASSERT(mSourceStream);
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
// All CanvasCaptureMediaStreams shall at least get one frame.
|
|
|
|
mFrameCaptureRequested = true;
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
OutputStreamDriver::~OutputStreamDriver() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-03-22 14:42:10 +03:00
|
|
|
EndTrack();
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
2019-03-22 14:42:10 +03:00
|
|
|
void OutputStreamDriver::EndTrack() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2019-07-31 10:58:17 +03:00
|
|
|
if (!mSourceStream->IsDestroyed()) {
|
|
|
|
mSourceStream->Destroy();
|
|
|
|
}
|
2019-03-22 14:42:10 +03:00
|
|
|
}
|
2018-11-23 18:01:36 +03:00
|
|
|
|
2020-02-13 17:38:48 +03:00
|
|
|
void OutputStreamDriver::SetImage(RefPtr<layers::Image>&& aImage,
|
2017-01-13 13:56:03 +03:00
|
|
|
const TimeStamp& aTime) {
|
2019-03-22 14:42:10 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
TRACE_COMMENT("SourceMediaTrack %p", mSourceStream.get());
|
2019-03-22 14:42:10 +03:00
|
|
|
|
|
|
|
VideoSegment segment;
|
2020-02-13 17:38:48 +03:00
|
|
|
const auto size = aImage->GetSize();
|
|
|
|
segment.AppendFrame(aImage.forget(), size, mPrincipalHandle, false, aTime);
|
2019-10-02 13:22:53 +03:00
|
|
|
mSourceStream->AppendData(&segment);
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TimerDriver : public OutputStreamDriver {
|
|
|
|
public:
|
2019-10-02 13:23:02 +03:00
|
|
|
explicit TimerDriver(SourceMediaTrack* aSourceStream, const double& aFPS,
|
2016-04-06 15:56:44 +03:00
|
|
|
const PrincipalHandle& aPrincipalHandle)
|
2019-10-02 13:22:53 +03:00
|
|
|
: OutputStreamDriver(aSourceStream, aPrincipalHandle),
|
2015-05-13 09:04:41 +03:00
|
|
|
mFPS(aFPS),
|
|
|
|
mTimer(nullptr) {
|
2015-09-17 07:36:57 +03:00
|
|
|
if (mFPS == 0.0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-13 09:04:41 +03:00
|
|
|
|
2017-10-16 09:15:40 +03:00
|
|
|
NS_NewTimerWithFuncCallback(
|
|
|
|
getter_AddRefs(mTimer), &TimerTick, this, int(1000 / mFPS),
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK, "dom::TimerDriver::TimerDriver");
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
static void TimerTick(nsITimer* aTimer, void* aClosure) {
|
|
|
|
MOZ_ASSERT(aClosure);
|
|
|
|
TimerDriver* driver = static_cast<TimerDriver*>(aClosure);
|
2015-07-16 13:31:24 +03:00
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
driver->RequestFrameCapture();
|
|
|
|
}
|
2015-07-16 13:31:24 +03:00
|
|
|
|
2017-01-13 13:56:03 +03:00
|
|
|
void NewFrame(already_AddRefed<Image> aImage,
|
|
|
|
const TimeStamp& aTime) override {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = aImage;
|
2015-07-16 13:31:24 +03:00
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
if (!mFrameCaptureRequested) {
|
|
|
|
return;
|
2015-07-16 13:31:24 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
mFrameCaptureRequested = false;
|
2020-02-13 17:38:48 +03:00
|
|
|
SetImage(std::move(image), aTime);
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
void Forget() override {
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-03-04 18:39:20 +03:00
|
|
|
virtual ~TimerDriver() = default;
|
2015-05-13 09:04:41 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
const double mFPS;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2015-09-17 06:46:05 +03:00
|
|
|
class AutoDriver : public OutputStreamDriver {
|
|
|
|
public:
|
2019-10-02 13:23:02 +03:00
|
|
|
explicit AutoDriver(SourceMediaTrack* aSourceStream,
|
2016-04-06 15:56:44 +03:00
|
|
|
const PrincipalHandle& aPrincipalHandle)
|
2019-10-02 13:22:53 +03:00
|
|
|
: OutputStreamDriver(aSourceStream, aPrincipalHandle) {}
|
2015-09-17 06:46:05 +03:00
|
|
|
|
2017-01-13 13:56:03 +03:00
|
|
|
void NewFrame(already_AddRefed<Image> aImage,
|
|
|
|
const TimeStamp& aTime) override {
|
2015-09-17 06:46:05 +03:00
|
|
|
// Don't reset `mFrameCaptureRequested` since AutoDriver shall always have
|
|
|
|
// `mFrameCaptureRequested` set to true.
|
|
|
|
// This also means we should accept every frame as NewFrame is called only
|
|
|
|
// after something changed.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = aImage;
|
2020-02-13 17:38:48 +03:00
|
|
|
SetImage(std::move(image), aTime);
|
2015-09-17 06:46:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-03-04 18:39:20 +03:00
|
|
|
virtual ~AutoDriver() = default;
|
2015-09-17 06:46:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------
|
|
|
|
|
2015-05-13 09:04:30 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(CanvasCaptureMediaStream, DOMMediaStream,
|
|
|
|
mCanvas)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(CanvasCaptureMediaStream, DOMMediaStream)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CanvasCaptureMediaStream, DOMMediaStream)
|
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CanvasCaptureMediaStream)
|
2015-05-13 09:04:30 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream)
|
|
|
|
|
2016-01-05 05:16:20 +03:00
|
|
|
CanvasCaptureMediaStream::CanvasCaptureMediaStream(nsPIDOMWindowInner* aWindow,
|
|
|
|
HTMLCanvasElement* aCanvas)
|
2019-07-31 10:58:17 +03:00
|
|
|
: DOMMediaStream(aWindow), mCanvas(aCanvas) {}
|
2015-05-13 09:04:30 +03:00
|
|
|
|
|
|
|
CanvasCaptureMediaStream::~CanvasCaptureMediaStream() {
|
2015-05-13 09:04:41 +03:00
|
|
|
if (mOutputStreamDriver) {
|
2015-09-17 07:36:57 +03:00
|
|
|
mOutputStreamDriver->Forget();
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* CanvasCaptureMediaStream::WrapObject(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return dom::CanvasCaptureMediaStream_Binding::Wrap(aCx, this, aGivenProto);
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasCaptureMediaStream::RequestFrame() {
|
2015-05-13 09:04:41 +03:00
|
|
|
if (mOutputStreamDriver) {
|
2015-09-17 07:36:57 +03:00
|
|
|
mOutputStreamDriver->RequestFrameCapture();
|
2015-05-13 09:04:41 +03:00
|
|
|
}
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CanvasCaptureMediaStream::Init(const dom::Optional<double>& aFPS,
|
2016-04-06 15:56:44 +03:00
|
|
|
nsIPrincipal* aPrincipal) {
|
2019-10-02 13:23:02 +03:00
|
|
|
MediaTrackGraph* graph = MediaTrackGraph::GetInstance(
|
|
|
|
MediaTrackGraph::SYSTEM_THREAD_DRIVER, mWindow,
|
2020-03-16 15:56:20 +03:00
|
|
|
MediaTrackGraph::REQUEST_DEFAULT_SAMPLE_RATE,
|
|
|
|
MediaTrackGraph::DEFAULT_OUTPUT_DEVICE);
|
2019-10-02 13:23:02 +03:00
|
|
|
SourceMediaTrack* source = graph->CreateSourceTrack(MediaSegment::VIDEO);
|
2016-04-06 15:56:44 +03:00
|
|
|
PrincipalHandle principalHandle = MakePrincipalHandle(aPrincipal);
|
2015-05-13 09:04:30 +03:00
|
|
|
if (!aFPS.WasPassed()) {
|
2019-10-02 13:22:53 +03:00
|
|
|
mOutputStreamDriver = new AutoDriver(source, principalHandle);
|
2015-05-13 09:04:30 +03:00
|
|
|
} else if (aFPS.Value() < 0) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2015-05-13 09:04:41 +03:00
|
|
|
} else {
|
|
|
|
// Cap frame rate to 60 FPS for sanity
|
|
|
|
double fps = std::min(60.0, aFPS.Value());
|
2019-10-02 13:22:53 +03:00
|
|
|
mOutputStreamDriver = new TimerDriver(source, fps, principalHandle);
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
return NS_OK;
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
FrameCaptureListener* CanvasCaptureMediaStream::FrameCaptureListener() {
|
|
|
|
return mOutputStreamDriver;
|
|
|
|
}
|
|
|
|
|
2016-09-12 19:00:14 +03:00
|
|
|
void CanvasCaptureMediaStream::StopCapture() {
|
|
|
|
if (!mOutputStreamDriver) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-23 18:01:36 +03:00
|
|
|
mOutputStreamDriver->EndTrack();
|
2016-09-12 19:00:14 +03:00
|
|
|
mOutputStreamDriver->Forget();
|
|
|
|
mOutputStreamDriver = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-02 13:23:02 +03:00
|
|
|
SourceMediaTrack* CanvasCaptureMediaStream::GetSourceStream() const {
|
2019-07-31 10:58:17 +03:00
|
|
|
if (!mOutputStreamDriver) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mOutputStreamDriver->mSourceStream;
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:04:30 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|