Skip to content

Commit

Permalink
[sailfishos][media] Add NemoResourceHandler
Browse files Browse the repository at this point in the history
Original SHA1: b318b3c

Signed-off-by: Raine Makelainen <raine.makelainen@jolla.com>
  • Loading branch information
romaxa authored and rainemak committed May 27, 2020
1 parent f26d0a5 commit 9e2dc6c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
116 changes: 116 additions & 0 deletions dom/media/NemoResourceHandler.cpp
@@ -0,0 +1,116 @@
/* -*- 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 "NemoResourceHandler.h"

#include "nsThreadUtils.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "nsStringGlue.h"
#include "mozilla/Preferences.h"

namespace mozilla {

NemoResourceHandler* NemoResourceHandler::mGlobalHandler = nullptr;

void
NemoResourceHandler::AquireResources(void* aHolder)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
nsString data;
data.AppendPrintf("{ \"owner\" : \"%p\", \"state\": \"play\" }", aHolder);
obs->NotifyObservers(nullptr, "media-decoder-info", data.get());
}

if (Preferences::GetBool("media.resource_handler_disabled", false) == true) {
return;
}

if (mGlobalHandler == nullptr)
{
mGlobalHandler = new NemoResourceHandler();
}
mGlobalHandler->Aquire();
}

void
NemoResourceHandler::ReleaseResources(void* aHolder)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
nsString data;
data.AppendPrintf("{ \"owner\" : \"%p\", \"state\": \"pause\" }", aHolder);
obs->NotifyObservers(nullptr, "media-decoder-info", data.get());
}

if (Preferences::GetBool("media.resource_handler_disabled", false) == true) {
return;
}

if (!mGlobalHandler) {
return;
}

mGlobalHandler->Release();

if (mGlobalHandler->CanDestroy())
{
delete mGlobalHandler;
mGlobalHandler = nullptr;
}
}

void
NemoResourceHandler::MediaInfo(void* aHolder, bool aHasAudio, bool aHasVideo)
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
nsString data;
data.AppendPrintf("{ \"owner\" : \"%p\", \"state\": \"meta\", \"a\" : %i, \"v\" : %i }", aHolder, aHasAudio, aHasVideo);
obs->NotifyObservers(nullptr, "media-decoder-info", data.get());
}
}

void
NemoResourceHandler::Aquire()
{
mCounter++;
}

void
NemoResourceHandler::Release()
{
mCounter--;
}

bool
NemoResourceHandler::CanDestroy()
{
return mCounter <= 0;
}

NemoResourceHandler::NemoResourceHandler()
: mCounter(0)
{
MOZ_ASSERT(mGlobalHandler == nullptr);
mGlobalHandler = this;
}

NemoResourceHandler::~NemoResourceHandler()
{
MOZ_ASSERT(mGlobalHandler != nullptr);
mGlobalHandler = nullptr;
}

} // namespace mozilla

31 changes: 31 additions & 0 deletions dom/media/NemoResourceHandler.h
@@ -0,0 +1,31 @@
/* -*- 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/. */

#ifndef NEMO_RESOURCE_HANDLER
#define NEMO_RESOURCE_HANDLER

namespace mozilla {

class NemoResourceHandler
{
public:
static void AquireResources(void* aHolder);
static void ReleaseResources(void* aHolder);
static void MediaInfo(void* aHolder, bool aHasAudio, bool aHasVideo);
private:
NemoResourceHandler();
virtual ~NemoResourceHandler();
void Aquire();
void Release();
bool CanDestroy();

static NemoResourceHandler* mGlobalHandler;
int mCounter;
};

} // namespace mozilla

#endif // NEMO_RESOURCE_HANDLER
3 changes: 3 additions & 0 deletions dom/media/moz.build
Expand Up @@ -267,6 +267,9 @@ UNIFIED_SOURCES += [
'XiphExtradata.cpp',
]

SOURCES += [
'NemoResourceHandler.cpp',
]
if CONFIG['OS_TARGET'] == 'WINNT':
SOURCES += [ 'ThreadPoolCOMListener.cpp' ]

Expand Down

0 comments on commit 9e2dc6c

Please sign in to comment.