1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/VideoUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef VideoUtils_h 1.11 +#define VideoUtils_h 1.12 + 1.13 +#include "mozilla/Attributes.h" 1.14 +#include "mozilla/ReentrantMonitor.h" 1.15 +#include "mozilla/CheckedInt.h" 1.16 + 1.17 +#if !(defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)) || \ 1.18 + defined(MOZ_ASAN) 1.19 +// For MEDIA_THREAD_STACK_SIZE 1.20 +#include "nsIThreadManager.h" 1.21 +#endif 1.22 +#include "nsThreadUtils.h" 1.23 +#include "prtime.h" 1.24 +#include "AudioSampleFormat.h" 1.25 + 1.26 +using mozilla::CheckedInt64; 1.27 +using mozilla::CheckedUint64; 1.28 +using mozilla::CheckedInt32; 1.29 +using mozilla::CheckedUint32; 1.30 + 1.31 +struct nsIntSize; 1.32 +struct nsIntRect; 1.33 + 1.34 +// This file contains stuff we'd rather put elsewhere, but which is 1.35 +// dependent on other changes which we don't want to wait for. We plan to 1.36 +// remove this file in the near future. 1.37 + 1.38 + 1.39 +// This belongs in xpcom/monitor/Monitor.h, once we've made 1.40 +// mozilla::Monitor non-reentrant. 1.41 +namespace mozilla { 1.42 + 1.43 +/** 1.44 + * ReentrantMonitorConditionallyEnter 1.45 + * 1.46 + * Enters the supplied monitor only if the conditional value |aEnter| is true. 1.47 + * E.g. Used to allow unmonitored read access on the decode thread, 1.48 + * and monitored access on all other threads. 1.49 + */ 1.50 +class MOZ_STACK_CLASS ReentrantMonitorConditionallyEnter 1.51 +{ 1.52 +public: 1.53 + ReentrantMonitorConditionallyEnter(bool aEnter, 1.54 + ReentrantMonitor &aReentrantMonitor) : 1.55 + mReentrantMonitor(nullptr) 1.56 + { 1.57 + MOZ_COUNT_CTOR(ReentrantMonitorConditionallyEnter); 1.58 + if (aEnter) { 1.59 + mReentrantMonitor = &aReentrantMonitor; 1.60 + NS_ASSERTION(mReentrantMonitor, "null monitor"); 1.61 + mReentrantMonitor->Enter(); 1.62 + } 1.63 + } 1.64 + ~ReentrantMonitorConditionallyEnter(void) 1.65 + { 1.66 + if (mReentrantMonitor) { 1.67 + mReentrantMonitor->Exit(); 1.68 + } 1.69 + MOZ_COUNT_DTOR(ReentrantMonitorConditionallyEnter); 1.70 + } 1.71 +private: 1.72 + // Restrict to constructor and destructor defined above. 1.73 + ReentrantMonitorConditionallyEnter(); 1.74 + ReentrantMonitorConditionallyEnter(const ReentrantMonitorConditionallyEnter&); 1.75 + ReentrantMonitorConditionallyEnter& operator =(const ReentrantMonitorConditionallyEnter&); 1.76 + static void* operator new(size_t) CPP_THROW_NEW; 1.77 + static void operator delete(void*); 1.78 + 1.79 + ReentrantMonitor* mReentrantMonitor; 1.80 +}; 1.81 + 1.82 +// Shuts down a thread asynchronously. 1.83 +class ShutdownThreadEvent : public nsRunnable 1.84 +{ 1.85 +public: 1.86 + ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {} 1.87 + ~ShutdownThreadEvent() {} 1.88 + NS_IMETHOD Run() MOZ_OVERRIDE { 1.89 + mThread->Shutdown(); 1.90 + mThread = nullptr; 1.91 + return NS_OK; 1.92 + } 1.93 +private: 1.94 + nsCOMPtr<nsIThread> mThread; 1.95 +}; 1.96 + 1.97 +class MediaResource; 1.98 + 1.99 +namespace dom { 1.100 +class TimeRanges; 1.101 +} 1.102 + 1.103 +// Estimates the buffered ranges of a MediaResource using a simple 1.104 +// (byteOffset/length)*duration method. Probably inaccurate, but won't 1.105 +// do file I/O, and can be used when we don't have detailed knowledge 1.106 +// of the byte->time mapping of a resource. aDurationUsecs is the duration 1.107 +// of the media in microseconds. Estimated buffered ranges are stored in 1.108 +// aOutBuffered. Ranges are 0-normalized, i.e. in the range of (0,duration]. 1.109 +void GetEstimatedBufferedTimeRanges(mozilla::MediaResource* aStream, 1.110 + int64_t aDurationUsecs, 1.111 + mozilla::dom::TimeRanges* aOutBuffered); 1.112 + 1.113 +// Converts from number of audio frames (aFrames) to microseconds, given 1.114 +// the specified audio rate (aRate). Stores result in aOutUsecs. Returns true 1.115 +// if the operation succeeded, or false if there was an integer overflow 1.116 +// while calulating the conversion. 1.117 +CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate); 1.118 + 1.119 +// Converts from microseconds (aUsecs) to number of audio frames, given the 1.120 +// specified audio rate (aRate). Stores the result in aOutFrames. Returns 1.121 +// true if the operation succeeded, or false if there was an integer 1.122 +// overflow while calulating the conversion. 1.123 +CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate); 1.124 + 1.125 +// Number of microseconds per second. 1e6. 1.126 +static const int64_t USECS_PER_S = 1000000; 1.127 + 1.128 +// Number of microseconds per millisecond. 1.129 +static const int64_t USECS_PER_MS = 1000; 1.130 + 1.131 +// Converts seconds to milliseconds. 1.132 +#define MS_TO_SECONDS(s) ((double)(s) / (PR_MSEC_PER_SEC)) 1.133 + 1.134 +// Converts from seconds to microseconds. Returns failure if the resulting 1.135 +// integer is too big to fit in an int64_t. 1.136 +nsresult SecondsToUsecs(double aSeconds, int64_t& aOutUsecs); 1.137 + 1.138 +// The maximum height and width of the video. Used for 1.139 +// sanitizing the memory allocation of the RGB buffer. 1.140 +// The maximum resolution we anticipate encountering in the 1.141 +// wild is 2160p - 3840x2160 pixels. 1.142 +static const int32_t MAX_VIDEO_WIDTH = 4000; 1.143 +static const int32_t MAX_VIDEO_HEIGHT = 3000; 1.144 + 1.145 +// Scales the display rect aDisplay by aspect ratio aAspectRatio. 1.146 +// Note that aDisplay must be validated by IsValidVideoRegion() 1.147 +// before being used! 1.148 +void ScaleDisplayByAspectRatio(nsIntSize& aDisplay, float aAspectRatio); 1.149 + 1.150 +// The amount of virtual memory reserved for thread stacks. 1.151 +#if (defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)) && \ 1.152 + !defined(MOZ_ASAN) 1.153 +#define MEDIA_THREAD_STACK_SIZE (128 * 1024) 1.154 +#else 1.155 +// All other platforms use their system defaults. 1.156 +#define MEDIA_THREAD_STACK_SIZE nsIThreadManager::DEFAULT_STACK_SIZE 1.157 +#endif 1.158 + 1.159 +// Downmix multichannel Audio samples to Stereo. 1.160 +// Input are the buffer contains multichannel data, 1.161 +// the number of channels and the number of frames. 1.162 +int DownmixAudioToStereo(mozilla::AudioDataValue* buffer, 1.163 + int channels, 1.164 + uint32_t frames); 1.165 + 1.166 +bool IsVideoContentType(const nsCString& aContentType); 1.167 + 1.168 +// Returns true if it's safe to use aPicture as the picture to be 1.169 +// extracted inside a frame of size aFrame, and scaled up to and displayed 1.170 +// at a size of aDisplay. You should validate the frame, picture, and 1.171 +// display regions before using them to display video frames. 1.172 +bool IsValidVideoRegion(const nsIntSize& aFrame, const nsIntRect& aPicture, 1.173 + const nsIntSize& aDisplay); 1.174 + 1.175 +} // end namespace mozilla 1.176 + 1.177 +#endif