2013-09-16 06:50:24 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "CaptureTask.h"
|
|
|
|
#include "mozilla/dom/ImageCapture.h"
|
|
|
|
#include "mozilla/dom/ImageCaptureError.h"
|
|
|
|
#include "mozilla/dom/ImageEncoder.h"
|
2016-01-22 06:26:29 +03:00
|
|
|
#include "mozilla/dom/MediaStreamTrack.h"
|
2013-09-16 06:50:24 +04:00
|
|
|
#include "mozilla/dom/VideoStreamTrack.h"
|
|
|
|
#include "gfxUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-05-31 08:53:49 +03:00
|
|
|
class CaptureTask::MediaStreamEventListener : public MediaStreamTrackListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit MediaStreamEventListener(CaptureTask* aCaptureTask)
|
|
|
|
: mCaptureTask(aCaptureTask) {};
|
|
|
|
|
|
|
|
// MediaStreamListener methods.
|
|
|
|
void NotifyEnded() override
|
|
|
|
{
|
|
|
|
if(!mCaptureTask->mImageGrabbedOrTrackEnd) {
|
|
|
|
mCaptureTask->PostTrackEndEvent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CaptureTask* mCaptureTask;
|
|
|
|
};
|
|
|
|
|
|
|
|
CaptureTask::CaptureTask(dom::ImageCapture* aImageCapture)
|
|
|
|
: mImageCapture(aImageCapture)
|
|
|
|
, mEventListener(new MediaStreamEventListener(this))
|
|
|
|
, mImageGrabbedOrTrackEnd(false)
|
|
|
|
, mPrincipalChanged(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-16 06:50:24 +04:00
|
|
|
nsresult
|
2015-05-12 15:09:51 +03:00
|
|
|
CaptureTask::TaskComplete(already_AddRefed<dom::Blob> aBlob, nsresult aRv)
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-22 07:29:54 +03:00
|
|
|
DetachTrack();
|
2013-09-16 06:50:24 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::Blob> blob(aBlob);
|
2014-10-08 20:15:22 +04:00
|
|
|
|
|
|
|
// We have to set the parent because the blob has been generated with a valid one.
|
|
|
|
if (blob) {
|
2015-05-12 15:09:51 +03:00
|
|
|
blob = dom::Blob::Create(mImageCapture->GetParentObject(), blob->Impl());
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
|
|
|
|
2013-09-16 06:50:24 +04:00
|
|
|
if (mPrincipalChanged) {
|
|
|
|
aRv = NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
IC_LOG("MediaStream principal should not change during TakePhoto().");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aRv)) {
|
|
|
|
rv = mImageCapture->PostBlobEvent(blob);
|
|
|
|
} else {
|
|
|
|
rv = mImageCapture->PostErrorEvent(dom::ImageCaptureError::PHOTO_ERROR, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure ImageCapture dereference on main thread here because the TakePhoto()
|
|
|
|
// sequences stopped here.
|
|
|
|
mImageCapture = nullptr;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-22 07:29:54 +03:00
|
|
|
CaptureTask::AttachTrack()
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-22 06:26:29 +03:00
|
|
|
dom::VideoStreamTrack* track = mImageCapture->GetVideoStreamTrack();
|
|
|
|
track->AddPrincipalChangeObserver(this);
|
2016-05-31 08:53:49 +03:00
|
|
|
track->AddListener(mEventListener.get());
|
|
|
|
track->AddDirectListener(this);
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-22 07:29:54 +03:00
|
|
|
CaptureTask::DetachTrack()
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-22 06:26:29 +03:00
|
|
|
dom::VideoStreamTrack* track = mImageCapture->GetVideoStreamTrack();
|
|
|
|
track->RemovePrincipalChangeObserver(this);
|
2016-05-31 08:53:49 +03:00
|
|
|
track->RemoveListener(mEventListener.get());
|
|
|
|
track->RemoveDirectListener(this);
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-22 06:26:29 +03:00
|
|
|
CaptureTask::PrincipalChanged(dom::MediaStreamTrack* aMediaStreamTrack)
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mPrincipalChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-31 08:53:49 +03:00
|
|
|
CaptureTask::SetCurrentFrames(const VideoSegment& aSegment)
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
if (mImageGrabbedOrTrackEnd) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Callback for encoding complete, it calls on main thread.
|
|
|
|
class EncodeComplete : public dom::EncodeCompleteCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-09-05 07:22:49 +04:00
|
|
|
explicit EncodeComplete(CaptureTask* aTask) : mTask(aTask) {}
|
2013-09-16 06:50:24 +04:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
nsresult ReceiveBlob(already_AddRefed<dom::Blob> aBlob) override
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::Blob> blob(aBlob);
|
2013-09-16 06:50:24 +04:00
|
|
|
mTask->TaskComplete(blob.forget(), NS_OK);
|
|
|
|
mTask = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CaptureTask> mTask;
|
2013-09-16 06:50:24 +04:00
|
|
|
};
|
|
|
|
|
2017-04-28 14:58:22 +03:00
|
|
|
for (VideoSegment::ConstChunkIterator iter(aSegment);
|
|
|
|
!iter.IsEnded(); iter.Next()) {
|
2016-01-22 07:29:54 +03:00
|
|
|
VideoChunk chunk = *iter;
|
2016-05-31 08:53:49 +03:00
|
|
|
|
2016-01-22 07:29:54 +03:00
|
|
|
// Extract the first valid video frame.
|
|
|
|
VideoFrame frame;
|
|
|
|
if (!chunk.IsNull()) {
|
|
|
|
RefPtr<layers::Image> image;
|
|
|
|
if (chunk.mFrame.GetForceBlack()) {
|
|
|
|
// Create a black image.
|
|
|
|
image = VideoFrame::CreateBlackImage(chunk.mFrame.GetIntrinsicSize());
|
|
|
|
} else {
|
|
|
|
image = chunk.mFrame.GetImage();
|
|
|
|
}
|
2017-04-28 14:58:22 +03:00
|
|
|
if (!image) {
|
|
|
|
MOZ_ASSERT(image);
|
|
|
|
continue;
|
|
|
|
}
|
2016-01-22 07:29:54 +03:00
|
|
|
mImageGrabbedOrTrackEnd = true;
|
|
|
|
|
|
|
|
// Encode image.
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoString type(NS_LITERAL_STRING("image/jpeg"));
|
|
|
|
nsAutoString options;
|
|
|
|
rv = dom::ImageEncoder::ExtractDataFromLayersImageAsync(
|
|
|
|
type,
|
|
|
|
options,
|
|
|
|
false,
|
|
|
|
image,
|
|
|
|
new EncodeComplete(this));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PostTrackEndEvent();
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
2016-01-22 07:29:54 +03:00
|
|
|
return;
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CaptureTask::PostTrackEndEvent()
|
|
|
|
{
|
|
|
|
mImageGrabbedOrTrackEnd = true;
|
|
|
|
|
|
|
|
// Got track end or finish event, stop the task.
|
2016-04-26 03:23:21 +03:00
|
|
|
class TrackEndRunnable : public Runnable
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-09-05 07:22:49 +04:00
|
|
|
explicit TrackEndRunnable(CaptureTask* aTask)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("TrackEndRunnable")
|
|
|
|
, mTask(aTask)
|
|
|
|
{
|
|
|
|
}
|
2013-09-16 06:50:24 +04:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2013-09-16 06:50:24 +04:00
|
|
|
{
|
|
|
|
mTask->TaskComplete(nullptr, NS_ERROR_FAILURE);
|
|
|
|
mTask = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CaptureTask> mTask;
|
2013-09-16 06:50:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
IC_LOG("Got MediaStream track removed or finished event.");
|
2017-03-13 13:04:27 +03:00
|
|
|
nsCOMPtr<nsIRunnable> event = new TrackEndRunnable(this);
|
|
|
|
SystemGroup::Dispatch("CaptureTask::TaskComplete",
|
|
|
|
TaskCategory::Other,
|
|
|
|
event.forget());
|
2013-09-16 06:50:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|