2010-04-01 20:03:07 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2010-04-27 01:53:44 -07:00
|
|
|
#ifndef VideoUtils_h
|
|
|
|
#define VideoUtils_h
|
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2012-05-14 12:50:20 -07:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2011-06-23 15:08:54 -07:00
|
|
|
#include "nsRect.h"
|
2011-07-26 20:28:03 -07:00
|
|
|
#include "nsIThreadManager.h"
|
2012-03-27 17:04:20 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2011-06-23 15:08:54 -07:00
|
|
|
|
2012-02-22 04:28:06 -08:00
|
|
|
using mozilla::CheckedInt64;
|
|
|
|
using mozilla::CheckedUint64;
|
|
|
|
using mozilla::CheckedInt32;
|
|
|
|
using mozilla::CheckedUint32;
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// This file contains stuff we'd rather put elsewhere, but which is
|
|
|
|
// dependent on other changes which we don't want to wait for. We plan to
|
|
|
|
// remove this file in the near future.
|
|
|
|
|
|
|
|
|
|
|
|
// This belongs in xpcom/monitor/Monitor.h, once we've made
|
|
|
|
// mozilla::Monitor non-reentrant.
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2011-04-29 12:21:57 -07:00
|
|
|
* ReentrantMonitorAutoExit
|
|
|
|
* Exit the ReentrantMonitor when it enters scope, and enters it when it leaves
|
2010-04-01 20:03:07 -07:00
|
|
|
* scope.
|
|
|
|
*
|
2011-04-29 12:21:57 -07:00
|
|
|
* MUCH PREFERRED to bare calls to ReentrantMonitor.Exit and Enter.
|
2010-04-01 20:03:07 -07:00
|
|
|
*/
|
2011-04-29 12:21:57 -07:00
|
|
|
class NS_STACK_CLASS ReentrantMonitorAutoExit
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* The constructor releases the given lock. The destructor
|
|
|
|
* acquires the lock. The lock must be held before constructing
|
|
|
|
* this object!
|
|
|
|
*
|
2011-04-29 12:21:57 -07:00
|
|
|
* @param aReentrantMonitor A valid mozilla::ReentrantMonitor*. It
|
|
|
|
* must be already locked.
|
2010-04-01 20:03:07 -07:00
|
|
|
**/
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit(ReentrantMonitor& aReentrantMonitor) :
|
|
|
|
mReentrantMonitor(&aReentrantMonitor)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
NS_ASSERTION(mReentrantMonitor, "null monitor");
|
|
|
|
mReentrantMonitor->AssertCurrentThreadIn();
|
|
|
|
mReentrantMonitor->Exit();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
~ReentrantMonitorAutoExit(void)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2011-04-29 12:21:57 -07:00
|
|
|
mReentrantMonitor->Enter();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitorAutoExit();
|
|
|
|
ReentrantMonitorAutoExit(const ReentrantMonitorAutoExit&);
|
|
|
|
ReentrantMonitorAutoExit& operator =(const ReentrantMonitorAutoExit&);
|
2010-04-01 20:03:07 -07:00
|
|
|
static void* operator new(size_t) CPP_THROW_NEW;
|
|
|
|
static void operator delete(void*);
|
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
ReentrantMonitor* mReentrantMonitor;
|
2010-04-01 20:03:07 -07:00
|
|
|
};
|
|
|
|
|
2012-03-27 17:04:20 -07:00
|
|
|
// Shuts down a thread asynchronously.
|
|
|
|
class ShutdownThreadEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
|
|
|
|
~ShutdownThreadEvent() {}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
mThread->Shutdown();
|
2012-07-30 07:20:58 -07:00
|
|
|
mThread = nullptr;
|
2012-03-27 17:04:20 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
};
|
|
|
|
|
2012-09-02 15:56:29 -07:00
|
|
|
class MediaResource;
|
2010-04-01 20:03:07 -07:00
|
|
|
} // namespace mozilla
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2012-09-02 15:56:29 -07:00
|
|
|
class nsTimeRanges;
|
|
|
|
|
|
|
|
// Estimates the buffered ranges of a MediaResource using a simple
|
|
|
|
// (byteOffset/length)*duration method. Probably inaccurate, but won't
|
|
|
|
// do file I/O, and can be used when we don't have detailed knowledge
|
|
|
|
// of the byte->time mapping of a resource. aDurationUsecs is the duration
|
|
|
|
// of the media in microseconds. Estimated buffered ranges are stored in
|
|
|
|
// aOutBuffered. Ranges are 0-normalized, i.e. in the range of (0,duration].
|
|
|
|
void GetEstimatedBufferedTimeRanges(mozilla::MediaResource* aStream,
|
|
|
|
int64_t aDurationUsecs,
|
|
|
|
nsTimeRanges* aOutBuffered);
|
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Converts from number of audio frames (aFrames) to microseconds, given
|
2011-09-29 16:34:37 -07:00
|
|
|
// the specified audio rate (aRate). Stores result in aOutUsecs. Returns true
|
|
|
|
// if the operation succeeded, or false if there was an integer overflow
|
2010-08-12 19:28:15 -07:00
|
|
|
// while calulating the conversion.
|
2012-08-22 08:56:38 -07:00
|
|
|
CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate);
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2011-09-26 20:31:18 -07:00
|
|
|
// Converts from microseconds (aUsecs) to number of audio frames, given the
|
|
|
|
// specified audio rate (aRate). Stores the result in aOutFrames. Returns
|
2011-09-29 16:34:37 -07:00
|
|
|
// true if the operation succeeded, or false if there was an integer
|
2010-08-12 19:28:15 -07:00
|
|
|
// overflow while calulating the conversion.
|
2012-08-22 08:56:38 -07:00
|
|
|
CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate);
|
2011-04-13 15:12:23 -07:00
|
|
|
|
|
|
|
// Number of microseconds per second. 1e6.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int64_t USECS_PER_S = 1000000;
|
2011-04-13 15:12:23 -07:00
|
|
|
|
|
|
|
// Number of microseconds per millisecond.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int64_t USECS_PER_MS = 1000;
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2011-06-23 15:08:54 -07:00
|
|
|
// The maximum height and width of the video. Used for
|
|
|
|
// sanitizing the memory allocation of the RGB buffer.
|
|
|
|
// The maximum resolution we anticipate encountering in the
|
|
|
|
// wild is 2160p - 3840x2160 pixels.
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t MAX_VIDEO_WIDTH = 4000;
|
|
|
|
static const int32_t MAX_VIDEO_HEIGHT = 3000;
|
2011-06-23 15:08:54 -07:00
|
|
|
|
|
|
|
// Scales the display rect aDisplay by aspect ratio aAspectRatio.
|
|
|
|
// Note that aDisplay must be validated by nsVideoInfo::ValidateVideoRegion()
|
|
|
|
// before being used!
|
|
|
|
void ScaleDisplayByAspectRatio(nsIntSize& aDisplay, float aAspectRatio);
|
|
|
|
|
2011-07-26 20:28:03 -07:00
|
|
|
// The amount of virtual memory reserved for thread stacks.
|
2012-06-03 12:43:09 -07:00
|
|
|
#if (defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)) && \
|
|
|
|
!defined(MOZ_ASAN)
|
2011-07-26 20:28:03 -07:00
|
|
|
#define MEDIA_THREAD_STACK_SIZE (128 * 1024)
|
|
|
|
#else
|
|
|
|
// All other platforms use their system defaults.
|
|
|
|
#define MEDIA_THREAD_STACK_SIZE nsIThreadManager::DEFAULT_STACK_SIZE
|
|
|
|
#endif
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
#endif
|