Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / content / media / VideoUtils.h
blobe4217330e62241296ef6e757a7d035922cc6298d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef VideoUtils_h
8 #define VideoUtils_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ReentrantMonitor.h"
12 #include "mozilla/CheckedInt.h"
13 #include "nsIThread.h"
15 #if !(defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)) || \
16 defined(MOZ_ASAN)
17 // For MEDIA_THREAD_STACK_SIZE
18 #include "nsIThreadManager.h"
19 #endif
20 #include "nsThreadUtils.h"
21 #include "prtime.h"
22 #include "AudioSampleFormat.h"
23 #include "mozilla/RefPtr.h"
25 using mozilla::CheckedInt64;
26 using mozilla::CheckedUint64;
27 using mozilla::CheckedInt32;
28 using mozilla::CheckedUint32;
30 struct nsIntSize;
31 struct nsIntRect;
33 // This file contains stuff we'd rather put elsewhere, but which is
34 // dependent on other changes which we don't want to wait for. We plan to
35 // remove this file in the near future.
38 // This belongs in xpcom/monitor/Monitor.h, once we've made
39 // mozilla::Monitor non-reentrant.
40 namespace mozilla {
42 /**
43 * ReentrantMonitorConditionallyEnter
45 * Enters the supplied monitor only if the conditional value |aEnter| is true.
46 * E.g. Used to allow unmonitored read access on the decode thread,
47 * and monitored access on all other threads.
49 class MOZ_STACK_CLASS ReentrantMonitorConditionallyEnter
51 public:
52 ReentrantMonitorConditionallyEnter(bool aEnter,
53 ReentrantMonitor &aReentrantMonitor) :
54 mReentrantMonitor(nullptr)
56 MOZ_COUNT_CTOR(ReentrantMonitorConditionallyEnter);
57 if (aEnter) {
58 mReentrantMonitor = &aReentrantMonitor;
59 NS_ASSERTION(mReentrantMonitor, "null monitor");
60 mReentrantMonitor->Enter();
63 ~ReentrantMonitorConditionallyEnter(void)
65 if (mReentrantMonitor) {
66 mReentrantMonitor->Exit();
68 MOZ_COUNT_DTOR(ReentrantMonitorConditionallyEnter);
70 private:
71 // Restrict to constructor and destructor defined above.
72 ReentrantMonitorConditionallyEnter();
73 ReentrantMonitorConditionallyEnter(const ReentrantMonitorConditionallyEnter&);
74 ReentrantMonitorConditionallyEnter& operator =(const ReentrantMonitorConditionallyEnter&);
75 static void* operator new(size_t) CPP_THROW_NEW;
76 static void operator delete(void*);
78 ReentrantMonitor* mReentrantMonitor;
81 // Shuts down a thread asynchronously.
82 class ShutdownThreadEvent : public nsRunnable
84 public:
85 explicit ShutdownThreadEvent(nsIThread* aThread) : mThread(aThread) {}
86 ~ShutdownThreadEvent() {}
87 NS_IMETHOD Run() MOZ_OVERRIDE {
88 mThread->Shutdown();
89 mThread = nullptr;
90 return NS_OK;
92 private:
93 nsCOMPtr<nsIThread> mThread;
96 template<class T>
97 class DeleteObjectTask: public nsRunnable {
98 public:
99 explicit DeleteObjectTask(nsAutoPtr<T>& aObject)
100 : mObject(aObject)
103 NS_IMETHOD Run() {
104 NS_ASSERTION(NS_IsMainThread(), "Must be on main thread.");
105 mObject = nullptr;
106 return NS_OK;
108 private:
109 nsAutoPtr<T> mObject;
112 template<class T>
113 void DeleteOnMainThread(nsAutoPtr<T>& aObject) {
114 NS_DispatchToMainThread(new DeleteObjectTask<T>(aObject));
117 class MediaResource;
119 namespace dom {
120 class TimeRanges;
123 // Estimates the buffered ranges of a MediaResource using a simple
124 // (byteOffset/length)*duration method. Probably inaccurate, but won't
125 // do file I/O, and can be used when we don't have detailed knowledge
126 // of the byte->time mapping of a resource. aDurationUsecs is the duration
127 // of the media in microseconds. Estimated buffered ranges are stored in
128 // aOutBuffered. Ranges are 0-normalized, i.e. in the range of (0,duration].
129 void GetEstimatedBufferedTimeRanges(mozilla::MediaResource* aStream,
130 int64_t aDurationUsecs,
131 mozilla::dom::TimeRanges* aOutBuffered);
133 // Converts from number of audio frames (aFrames) to microseconds, given
134 // the specified audio rate (aRate). Stores result in aOutUsecs. Returns true
135 // if the operation succeeded, or false if there was an integer overflow
136 // while calulating the conversion.
137 CheckedInt64 FramesToUsecs(int64_t aFrames, uint32_t aRate);
139 // Converts from microseconds (aUsecs) to number of audio frames, given the
140 // specified audio rate (aRate). Stores the result in aOutFrames. Returns
141 // true if the operation succeeded, or false if there was an integer
142 // overflow while calulating the conversion.
143 CheckedInt64 UsecsToFrames(int64_t aUsecs, uint32_t aRate);
145 // Number of microseconds per second. 1e6.
146 static const int64_t USECS_PER_S = 1000000;
148 // Number of microseconds per millisecond.
149 static const int64_t USECS_PER_MS = 1000;
151 // Converts seconds to milliseconds.
152 #define MS_TO_SECONDS(s) ((double)(s) / (PR_MSEC_PER_SEC))
154 // Converts from seconds to microseconds. Returns failure if the resulting
155 // integer is too big to fit in an int64_t.
156 nsresult SecondsToUsecs(double aSeconds, int64_t& aOutUsecs);
158 // The maximum height and width of the video. Used for
159 // sanitizing the memory allocation of the RGB buffer.
160 // The maximum resolution we anticipate encountering in the
161 // wild is 2160p - 3840x2160 pixels.
162 static const int32_t MAX_VIDEO_WIDTH = 4000;
163 static const int32_t MAX_VIDEO_HEIGHT = 3000;
165 // Scales the display rect aDisplay by aspect ratio aAspectRatio.
166 // Note that aDisplay must be validated by IsValidVideoRegion()
167 // before being used!
168 void ScaleDisplayByAspectRatio(nsIntSize& aDisplay, float aAspectRatio);
170 // The amount of virtual memory reserved for thread stacks.
171 #if (defined(XP_WIN) || defined(LINUX)) && !defined(MOZ_ASAN)
172 #define MEDIA_THREAD_STACK_SIZE (128 * 1024)
173 #elif defined(XP_MACOSX) && !defined(MOZ_ASAN)
174 #define MEDIA_THREAD_STACK_SIZE (256 * 1024)
175 #else
176 // All other platforms use their system defaults.
177 #define MEDIA_THREAD_STACK_SIZE nsIThreadManager::DEFAULT_STACK_SIZE
178 #endif
180 // Downmix multichannel Audio samples to Stereo.
181 // Input are the buffer contains multichannel data,
182 // the number of channels and the number of frames.
183 int DownmixAudioToStereo(mozilla::AudioDataValue* buffer,
184 int channels,
185 uint32_t frames);
187 bool IsVideoContentType(const nsCString& aContentType);
189 // Returns true if it's safe to use aPicture as the picture to be
190 // extracted inside a frame of size aFrame, and scaled up to and displayed
191 // at a size of aDisplay. You should validate the frame, picture, and
192 // display regions before using them to display video frames.
193 bool IsValidVideoRegion(const nsIntSize& aFrame, const nsIntRect& aPicture,
194 const nsIntSize& aDisplay);
196 // Template to automatically set a variable to a value on scope exit.
197 // Useful for unsetting flags, etc.
198 template<typename T>
199 class AutoSetOnScopeExit {
200 public:
201 AutoSetOnScopeExit(T& aVar, T aValue)
202 : mVar(aVar)
203 , mValue(aValue)
205 ~AutoSetOnScopeExit() {
206 mVar = mValue;
208 private:
209 T& mVar;
210 const T mValue;
213 class SharedThreadPool;
215 // Returns the thread pool that is shared amongst all decoder state machines
216 // for decoding streams.
217 TemporaryRef<SharedThreadPool> GetMediaDecodeThreadPool();
219 enum H264_PROFILE {
220 H264_PROFILE_UNKNOWN = 0,
221 H264_PROFILE_BASE = 0x42,
222 H264_PROFILE_MAIN = 0x4D,
223 H264_PROFILE_EXTENDED = 0x58,
224 H264_PROFILE_HIGH = 0x64,
227 enum H264_LEVEL {
228 H264_LEVEL_1 = 10,
229 H264_LEVEL_1_b = 11,
230 H264_LEVEL_1_1 = 11,
231 H264_LEVEL_1_2 = 12,
232 H264_LEVEL_1_3 = 13,
233 H264_LEVEL_2 = 20,
234 H264_LEVEL_2_1 = 21,
235 H264_LEVEL_2_2 = 22,
236 H264_LEVEL_3 = 30,
237 H264_LEVEL_3_1 = 31,
238 H264_LEVEL_3_2 = 32,
239 H264_LEVEL_4 = 40,
240 H264_LEVEL_4_1 = 41,
241 H264_LEVEL_4_2 = 42,
242 H264_LEVEL_5 = 50,
243 H264_LEVEL_5_1 = 51,
244 H264_LEVEL_5_2 = 52
247 // Extracts the H.264/AVC profile and level from an H.264 codecs string.
248 // H.264 codecs parameters have a type defined as avc1.PPCCLL, where
249 // PP = profile_idc, CC = constraint_set flags, LL = level_idc.
250 // See http://blog.pearce.org.nz/2013/11/what-does-h264avc1-codecs-parameters.html
251 // for more details.
252 // Returns false on failure.
253 bool
254 ExtractH264CodecDetails(const nsAString& aCodecs,
255 int16_t& aProfile,
256 int16_t& aLevel);
258 } // end namespace mozilla
260 #endif