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/. */
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)) || \
17 // For MEDIA_THREAD_STACK_SIZE
18 #include "nsIThreadManager.h"
20 #include "nsThreadUtils.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
;
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.
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
52 ReentrantMonitorConditionallyEnter(bool aEnter
,
53 ReentrantMonitor
&aReentrantMonitor
) :
54 mReentrantMonitor(nullptr)
56 MOZ_COUNT_CTOR(ReentrantMonitorConditionallyEnter
);
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
);
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
85 explicit ShutdownThreadEvent(nsIThread
* aThread
) : mThread(aThread
) {}
86 ~ShutdownThreadEvent() {}
87 NS_IMETHOD
Run() MOZ_OVERRIDE
{
93 nsCOMPtr
<nsIThread
> mThread
;
97 class DeleteObjectTask
: public nsRunnable
{
99 explicit DeleteObjectTask(nsAutoPtr
<T
>& aObject
)
104 NS_ASSERTION(NS_IsMainThread(), "Must be on main thread.");
109 nsAutoPtr
<T
> mObject
;
113 void DeleteOnMainThread(nsAutoPtr
<T
>& aObject
) {
114 NS_DispatchToMainThread(new DeleteObjectTask
<T
>(aObject
));
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)
176 // All other platforms use their system defaults.
177 #define MEDIA_THREAD_STACK_SIZE nsIThreadManager::DEFAULT_STACK_SIZE
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
,
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.
199 class AutoSetOnScopeExit
{
201 AutoSetOnScopeExit(T
& aVar
, T aValue
)
205 ~AutoSetOnScopeExit() {
213 class SharedThreadPool
;
215 // Returns the thread pool that is shared amongst all decoder state machines
216 // for decoding streams.
217 TemporaryRef
<SharedThreadPool
> GetMediaDecodeThreadPool();
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,
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
252 // Returns false on failure.
254 ExtractH264CodecDetails(const nsAString
& aCodecs
,
258 } // end namespace mozilla