1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "VideoUtils.h"
10 #include "CubebUtils.h"
11 #include "ImageContainer.h"
12 #include "MediaContainerType.h"
13 #include "MediaResource.h"
14 #include "TimeUnits.h"
15 #include "VorbisUtils.h"
16 #include "mozilla/Base64.h"
17 #include "mozilla/dom/ContentChild.h"
18 #include "mozilla/SchedulerGroup.h"
19 #include "mozilla/SharedThreadPool.h"
20 #include "mozilla/StaticPrefs_accessibility.h"
21 #include "mozilla/StaticPrefs_media.h"
22 #include "mozilla/TaskCategory.h"
23 #include "mozilla/TaskQueue.h"
24 #include "mozilla/Telemetry.h"
25 #include "nsCharSeparatedTokenizer.h"
26 #include "nsContentTypeParser.h"
27 #include "nsIConsoleService.h"
28 #include "nsINetworkLinkService.h"
29 #include "nsIRandomGenerator.h"
30 #include "nsMathUtils.h"
32 #include "nsServiceManagerUtils.h"
33 #include "nsThreadUtils.h"
34 #include "AudioStream.h"
38 using gfx::ColorRange
;
39 using gfx::CICP::ColourPrimaries
;
40 using gfx::CICP::MatrixCoefficients
;
41 using gfx::CICP::TransferCharacteristics
;
42 using layers::PlanarYCbCrImage
;
43 using media::TimeUnit
;
45 double ToMicrosecondResolution(double aSeconds
) {
47 modf(aSeconds
* USECS_PER_S
, &integer
);
48 return integer
/ USECS_PER_S
;
51 CheckedInt64
SaferMultDiv(int64_t aValue
, uint64_t aMul
, uint64_t aDiv
) {
52 if (aMul
> INT64_MAX
|| aDiv
> INT64_MAX
) {
53 return CheckedInt64(INT64_MAX
) + 1; // Return an invalid checked int.
57 int64_t major
= aValue
/ div
;
58 int64_t remainder
= aValue
% div
;
59 return CheckedInt64(remainder
) * mul
/ div
+ CheckedInt64(major
) * mul
;
62 // Converts from number of audio frames to microseconds, given the specified
64 CheckedInt64
FramesToUsecs(int64_t aFrames
, uint32_t aRate
) {
65 return SaferMultDiv(aFrames
, USECS_PER_S
, aRate
);
68 // Converts from microseconds to number of audio frames, given the specified
70 CheckedInt64
UsecsToFrames(int64_t aUsecs
, uint32_t aRate
) {
71 return SaferMultDiv(aUsecs
, aRate
, USECS_PER_S
);
74 // Format TimeUnit as number of frames at given rate.
75 CheckedInt64
TimeUnitToFrames(const TimeUnit
& aTime
, uint32_t aRate
) {
76 return aTime
.IsValid() ? UsecsToFrames(aTime
.ToMicroseconds(), aRate
)
77 : CheckedInt64(INT64_MAX
) + 1;
80 nsresult
SecondsToUsecs(double aSeconds
, int64_t& aOutUsecs
) {
81 if (aSeconds
* double(USECS_PER_S
) > double(INT64_MAX
)) {
82 return NS_ERROR_FAILURE
;
84 aOutUsecs
= int64_t(aSeconds
* double(USECS_PER_S
));
88 static int32_t ConditionDimension(float aValue
) {
89 // This will exclude NaNs and too-big values.
90 if (aValue
> 1.0 && aValue
<= float(INT32_MAX
)) {
91 return int32_t(NS_round(aValue
));
96 void ScaleDisplayByAspectRatio(gfx::IntSize
& aDisplay
, float aAspectRatio
) {
97 if (aAspectRatio
> 1.0) {
98 // Increase the intrinsic width
99 aDisplay
.width
= ConditionDimension(aAspectRatio
* aDisplay
.width
);
101 // Increase the intrinsic height
102 aDisplay
.height
= ConditionDimension(aDisplay
.height
/ aAspectRatio
);
106 static int64_t BytesToTime(int64_t offset
, int64_t length
, int64_t durationUs
) {
107 NS_ASSERTION(length
> 0, "Must have positive length");
108 double r
= double(offset
) / double(length
);
112 return int64_t(double(durationUs
) * r
);
115 media::TimeIntervals
GetEstimatedBufferedTimeRanges(
116 mozilla::MediaResource
* aStream
, int64_t aDurationUsecs
) {
117 media::TimeIntervals buffered
;
118 // Nothing to cache if the media takes 0us to play.
119 if (aDurationUsecs
<= 0 || !aStream
) {
123 // Special case completely cached files. This also handles local files.
124 if (aStream
->IsDataCachedToEndOfResource(0)) {
125 buffered
+= media::TimeInterval(TimeUnit::Zero(),
126 TimeUnit::FromMicroseconds(aDurationUsecs
));
130 int64_t totalBytes
= aStream
->GetLength();
132 // If we can't determine the total size, pretend that we have nothing
133 // buffered. This will put us in a state of eternally-low-on-undecoded-data
134 // which is not great, but about the best we can do.
135 if (totalBytes
<= 0) {
139 int64_t startOffset
= aStream
->GetNextCachedData(0);
140 while (startOffset
>= 0) {
141 int64_t endOffset
= aStream
->GetCachedDataEnd(startOffset
);
142 // Bytes [startOffset..endOffset] are cached.
143 NS_ASSERTION(startOffset
>= 0, "Integer underflow in GetBuffered");
144 NS_ASSERTION(endOffset
>= 0, "Integer underflow in GetBuffered");
146 int64_t startUs
= BytesToTime(startOffset
, totalBytes
, aDurationUsecs
);
147 int64_t endUs
= BytesToTime(endOffset
, totalBytes
, aDurationUsecs
);
148 if (startUs
!= endUs
) {
149 buffered
+= media::TimeInterval(TimeUnit::FromMicroseconds(startUs
),
150 TimeUnit::FromMicroseconds(endUs
));
152 startOffset
= aStream
->GetNextCachedData(endOffset
);
157 void DownmixStereoToMono(mozilla::AudioDataValue
* aBuffer
, uint32_t aFrames
) {
159 const int channels
= 2;
160 for (uint32_t fIdx
= 0; fIdx
< aFrames
; ++fIdx
) {
161 #ifdef MOZ_SAMPLE_TYPE_FLOAT32
166 // The sample of the buffer would be interleaved.
167 sample
= (aBuffer
[fIdx
* channels
] + aBuffer
[fIdx
* channels
+ 1]) * 0.5f
;
168 aBuffer
[fIdx
* channels
] = aBuffer
[fIdx
* channels
+ 1] = sample
;
172 uint32_t DecideAudioPlaybackChannels(const AudioInfo
& info
) {
173 if (StaticPrefs::accessibility_monoaudio_enable()) {
177 if (StaticPrefs::media_forcestereo_enabled()) {
181 return info
.mChannels
;
184 uint32_t DecideAudioPlaybackSampleRate(const AudioInfo
& aInfo
,
185 bool aShouldResistFingerprinting
) {
186 bool resampling
= StaticPrefs::media_resampling_enabled();
192 } else if (aInfo
.mRate
>= 44100) {
193 // The original rate is of good quality and we want to minimize unecessary
194 // resampling, so we let cubeb decide how to resample (if needed).
197 // We will resample all data to match cubeb's preferred sampling rate.
198 rate
= CubebUtils::PreferredSampleRate(aShouldResistFingerprinting
);
200 // bogus rate, fall back to something else;
204 MOZ_DIAGNOSTIC_ASSERT(rate
, "output rate can't be 0.");
209 bool IsDefaultPlaybackDeviceMono() {
210 return CubebUtils::MaxNumberOfChannels() == 1;
213 bool IsVideoContentType(const nsCString
& aContentType
) {
214 constexpr auto video
= "video"_ns
;
215 return FindInReadable(video
, aContentType
);
218 bool IsValidVideoRegion(const gfx::IntSize
& aFrame
,
219 const gfx::IntRect
& aPicture
,
220 const gfx::IntSize
& aDisplay
) {
221 return aFrame
.width
> 0 && aFrame
.width
<= PlanarYCbCrImage::MAX_DIMENSION
&&
223 aFrame
.height
<= PlanarYCbCrImage::MAX_DIMENSION
&&
224 aFrame
.width
* aFrame
.height
<= MAX_VIDEO_WIDTH
* MAX_VIDEO_HEIGHT
&&
225 aPicture
.width
> 0 &&
226 aPicture
.width
<= PlanarYCbCrImage::MAX_DIMENSION
&&
227 aPicture
.x
< PlanarYCbCrImage::MAX_DIMENSION
&&
228 aPicture
.x
+ aPicture
.width
< PlanarYCbCrImage::MAX_DIMENSION
&&
229 aPicture
.height
> 0 &&
230 aPicture
.height
<= PlanarYCbCrImage::MAX_DIMENSION
&&
231 aPicture
.y
< PlanarYCbCrImage::MAX_DIMENSION
&&
232 aPicture
.y
+ aPicture
.height
< PlanarYCbCrImage::MAX_DIMENSION
&&
233 aPicture
.width
* aPicture
.height
<=
234 MAX_VIDEO_WIDTH
* MAX_VIDEO_HEIGHT
&&
235 aDisplay
.width
> 0 &&
236 aDisplay
.width
<= PlanarYCbCrImage::MAX_DIMENSION
&&
237 aDisplay
.height
> 0 &&
238 aDisplay
.height
<= PlanarYCbCrImage::MAX_DIMENSION
&&
239 aDisplay
.width
* aDisplay
.height
<= MAX_VIDEO_WIDTH
* MAX_VIDEO_HEIGHT
;
242 already_AddRefed
<SharedThreadPool
> GetMediaThreadPool(MediaThreadType aType
) {
244 uint32_t threads
= 4;
246 case MediaThreadType::PLATFORM_DECODER
:
247 name
= "MediaPDecoder";
249 case MediaThreadType::WEBRTC_CALL_THREAD
:
250 name
= "WebrtcCallThread";
253 case MediaThreadType::WEBRTC_WORKER
:
254 name
= "WebrtcWorker";
256 case MediaThreadType::MDSM
:
257 name
= "MediaDecoderStateMachine";
260 case MediaThreadType::PLATFORM_ENCODER
:
261 name
= "MediaPEncoder";
264 MOZ_FALLTHROUGH_ASSERT("Unexpected MediaThreadType");
265 case MediaThreadType::SUPERVISOR
:
266 name
= "MediaSupervisor";
270 RefPtr
<SharedThreadPool
> pool
=
271 SharedThreadPool::Get(nsDependentCString(name
), threads
);
273 // Ensure a larger stack for platform decoder threads
274 if (aType
== MediaThreadType::PLATFORM_DECODER
) {
275 const uint32_t minStackSize
= 512 * 1024;
277 MOZ_ALWAYS_SUCCEEDS(pool
->GetThreadStackSize(&stackSize
));
278 if (stackSize
< minStackSize
) {
279 MOZ_ALWAYS_SUCCEEDS(pool
->SetThreadStackSize(minStackSize
));
283 return pool
.forget();
286 bool ExtractVPXCodecDetails(const nsAString
& aCodec
, uint8_t& aProfile
,
287 uint8_t& aLevel
, uint8_t& aBitDepth
) {
288 uint8_t dummyChromaSubsampling
= 1;
289 VideoColorSpace dummyColorspace
;
290 return ExtractVPXCodecDetails(aCodec
, aProfile
, aLevel
, aBitDepth
,
291 dummyChromaSubsampling
, dummyColorspace
);
294 bool ExtractVPXCodecDetails(const nsAString
& aCodec
, uint8_t& aProfile
,
295 uint8_t& aLevel
, uint8_t& aBitDepth
,
296 uint8_t& aChromaSubsampling
,
297 VideoColorSpace
& aColorSpace
) {
298 // Assign default value.
299 aChromaSubsampling
= 1;
300 auto splitter
= aCodec
.Split(u
'.');
301 auto fieldsItr
= splitter
.begin();
302 auto fourCC
= *fieldsItr
;
304 if (!fourCC
.EqualsLiteral("vp09") && !fourCC
.EqualsLiteral("vp08")) {
309 uint8_t primary
, transfer
, matrix
, range
;
310 uint8_t* fields
[] = {&aProfile
, &aLevel
, &aBitDepth
, &aChromaSubsampling
,
311 &primary
, &transfer
, &matrix
, &range
};
314 for (; fieldsItr
!= splitter
.end(); ++fieldsItr
, ++fieldsCount
) {
315 if (fieldsCount
> 7) {
316 // No more than 8 fields are expected.
319 *(fields
[fieldsCount
]) =
320 static_cast<uint8_t>((*fieldsItr
).ToInteger(&rv
, 10));
321 // We got invalid field value, parsing error.
322 NS_ENSURE_SUCCESS(rv
, false);
325 // <sample entry 4CC>.<profile>.<level>.<bitDepth>.
327 // <chromaSubsampling>.<colourPrimaries>.<transferCharacteristics>.
328 // <matrixCoefficients>.<videoFullRangeFlag>
329 // First three fields are mandatory(we have parsed 4CC).
330 if (fieldsCount
< 3) {
331 // Invalid number of fields.
334 // Start to validate the parsing value.
336 // profile should be 0,1,2 or 3.
337 // See https://www.webmproject.org/vp9/profiles/
343 // level, See https://www.webmproject.org/vp9/mp4/#semantics_1
365 if (aBitDepth
!= 8 && aBitDepth
!= 10 && aBitDepth
!= 12) {
370 if (fieldsCount
== 3) {
375 // chromaSubsampling should be 0,1,2,3...4~7 are reserved.
376 if (aChromaSubsampling
> 3) {
380 if (fieldsCount
== 4) {
385 // It is an integer that is defined by the "Colour primaries"
386 // section of ISO/IEC 23001-8:2016 Table 2.
387 // We treat reserved value as false case.
388 if (primary
== 0 || primary
== 3 || primary
> 22) {
392 if (primary
> 12 && primary
< 22) {
393 // 13~21 are reserved values.
396 aColorSpace
.mPrimaries
= static_cast<ColourPrimaries
>(primary
);
398 if (fieldsCount
== 5) {
403 // It is an integer that is defined by the
404 // "Transfer characteristics" section of ISO/IEC 23001-8:2016 Table 3.
405 // We treat reserved value as false case.
406 if (transfer
== 0 || transfer
== 3 || transfer
> 18) {
410 aColorSpace
.mTransfer
= static_cast<TransferCharacteristics
>(transfer
);
412 if (fieldsCount
== 6) {
417 // It is an integer that is defined by the
418 // "Matrix coefficients" section of ISO/IEC 23001-8:2016 Table 4.
419 // We treat reserved value as false case.
420 if (matrix
== 3 || matrix
> 11) {
423 aColorSpace
.mMatrix
= static_cast<MatrixCoefficients
>(matrix
);
425 // If matrixCoefficients is 0 (RGB), then chroma subsampling MUST be 3
427 if (aColorSpace
.mMatrix
== MatrixCoefficients::MC_IDENTITY
&&
428 aChromaSubsampling
!= 3) {
432 if (fieldsCount
== 7) {
437 // videoFullRangeFlag indicates the black level and range of the luma and
438 // chroma signals. 0 = legal range (e.g. 16-235 for 8 bit sample depth);
439 // 1 = full range (e.g. 0-255 for 8-bit sample depth).
440 aColorSpace
.mRange
= static_cast<ColorRange
>(range
);
444 bool ExtractH264CodecDetails(const nsAString
& aCodec
, uint8_t& aProfile
,
445 uint8_t& aConstraint
, uint8_t& aLevel
) {
446 // H.264 codecs parameters have a type defined as avcN.PPCCLL, where
447 // N = avc type. avc3 is avcc with SPS & PPS implicit (within stream)
448 // PP = profile_idc, CC = constraint_set flags, LL = level_idc.
449 // We ignore the constraint_set flags, as it's not clear from any
450 // documentation what constraints the platform decoders support.
452 // http://blog.pearce.org.nz/2013/11/what-does-h264avc1-codecs-parameters.html
454 if (aCodec
.Length() != strlen("avc1.PPCCLL")) {
458 // Verify the codec starts with "avc1." or "avc3.".
459 const nsAString
& sample
= Substring(aCodec
, 0, 5);
460 if (!sample
.EqualsASCII("avc1.") && !sample
.EqualsASCII("avc3.")) {
464 // Extract the profile_idc, constraint_flags and level_idc.
466 aProfile
= Substring(aCodec
, 5, 2).ToInteger(&rv
, 16);
467 NS_ENSURE_SUCCESS(rv
, false);
469 // Constraint flags are stored on the 6 most significant bits, first two bits
470 // are reserved_zero_2bits.
471 aConstraint
= Substring(aCodec
, 7, 2).ToInteger(&rv
, 16);
472 NS_ENSURE_SUCCESS(rv
, false);
474 aLevel
= Substring(aCodec
, 9, 2).ToInteger(&rv
, 16);
475 NS_ENSURE_SUCCESS(rv
, false);
478 aLevel
= H264_LEVEL_1_b
;
479 } else if (aLevel
<= 5) {
486 bool ExtractAV1CodecDetails(const nsAString
& aCodec
, uint8_t& aProfile
,
487 uint8_t& aLevel
, uint8_t& aTier
, uint8_t& aBitDepth
,
488 bool& aMonochrome
, bool& aSubsamplingX
,
489 bool& aSubsamplingY
, uint8_t& aChromaSamplePosition
,
490 VideoColorSpace
& aColorSpace
) {
491 auto fourCC
= Substring(aCodec
, 0, 4);
493 if (!fourCC
.EqualsLiteral("av01")) {
499 // av01.N.NN[MH].NN.B.BBN.NN.NN.NN.B
502 // [] = single character
505 // <sample entry 4CC>.<profile>.<level><tier>.<bitDepth>
506 // [.<monochrome>.<chromaSubsampling>
507 // .<colorPrimaries>.<transferCharacteristics>.<matrixCoefficients>
508 // .<videoFullRangeFlag>]
510 // If any optional field is found, all the rest must be included.
512 // Parsing stops but does not fail upon encountering unexpected characters
513 // at the end of an otherwise well-formed string.
515 // See https://aomediacodec.github.io/av1-isobmff/#codecsparam
527 AV1Field fields
[] = {{&aProfile
, 1},
529 // parsing loop skips tier
538 auto splitter
= aCodec
.Split(u
'.');
539 auto iter
= splitter
.begin();
541 size_t fieldCount
= 0;
542 while (iter
!= splitter
.end()) {
543 // Exit if there are too many fields.
544 if (fieldCount
>= 9) {
548 AV1Field
& field
= fields
[fieldCount
];
549 auto fieldStr
= *iter
;
551 if (field
.field
== &aLevel
) {
552 // Parse tier and remove it from the level field.
553 if (fieldStr
.Length() < 3) {
556 auto tier
= fieldStr
[2];
567 fieldStr
.SetLength(2);
570 if (fieldStr
.Length() < field
.length
) {
574 // Manually parse values since nsString.ToInteger silently stops parsing
575 // upon encountering unknown characters.
577 for (size_t i
= 0; i
< field
.length
; i
++) {
578 uint8_t oldValue
= value
;
579 char16_t character
= fieldStr
[i
];
580 if ('0' <= character
&& character
<= '9') {
581 value
= (value
* 10) + (character
- '0');
585 if (value
< oldValue
) {
586 // Overflow is possible on the 3-digit subsampling field.
591 *field
.field
= value
;
596 // Field had extra characters, exit early.
597 if (fieldStr
.Length() > field
.length
) {
598 // Disallow numbers as unexpected characters.
599 char16_t character
= fieldStr
[field
.length
];
600 if ('0' <= character
&& character
<= '9') {
607 // Spec requires profile, level/tier, bitdepth, or for all possible fields to
609 if (fieldCount
!= 3 && fieldCount
!= 9) {
613 // Valid profiles are: Main (0), High (1), Professional (2).
614 // Levels range from 0 to 23, or 31 to remove level restrictions.
615 if (aProfile
> 2 || (aLevel
> 23 && aLevel
!= 31)) {
619 if (fieldCount
== 3) {
620 // If only required fields are included, set to the spec defaults for the
621 // rest and continue validating.
623 aSubsamplingX
= true;
624 aSubsamplingY
= true;
625 aChromaSamplePosition
= 0;
626 aColorSpace
.mPrimaries
= ColourPrimaries::CP_BT709
;
627 aColorSpace
.mTransfer
= TransferCharacteristics::TC_BT709
;
628 aColorSpace
.mMatrix
= MatrixCoefficients::MC_BT709
;
629 aColorSpace
.mRange
= ColorRange::LIMITED
;
631 // Extract the individual values for the remaining fields, and check for
632 // valid values for each.
634 // Monochrome is a boolean.
635 if (monochrome
> 1) {
638 aMonochrome
= !!monochrome
;
640 // Extract individual digits of the subsampling field.
641 // Subsampling is two binary digits for x and y
642 // and one enumerated sample position field of
643 // Unknown (0), Vertical (1), Colocated (2).
644 uint8_t subsamplingX
= (subsampling
/ 100) % 10;
645 uint8_t subsamplingY
= (subsampling
/ 10) % 10;
646 if (subsamplingX
> 1 || subsamplingY
> 1) {
649 aSubsamplingX
= !!subsamplingX
;
650 aSubsamplingY
= !!subsamplingY
;
651 aChromaSamplePosition
= subsampling
% 10;
652 if (aChromaSamplePosition
> 2) {
656 // We can validate the color space values using CICP enums, as the values
657 // are standardized in Rec. ITU-T H.273.
658 aColorSpace
.mPrimaries
= static_cast<ColourPrimaries
>(primary
);
659 aColorSpace
.mTransfer
= static_cast<TransferCharacteristics
>(transfer
);
660 aColorSpace
.mMatrix
= static_cast<MatrixCoefficients
>(matrix
);
661 if (gfx::CICP::IsReserved(aColorSpace
.mPrimaries
) ||
662 gfx::CICP::IsReserved(aColorSpace
.mTransfer
) ||
663 gfx::CICP::IsReserved(aColorSpace
.mMatrix
)) {
666 // Range is a boolean, true meaning full and false meaning limited range.
670 aColorSpace
.mRange
= static_cast<ColorRange
>(range
);
673 // Begin validating all parameter values:
675 // Only Levels 8 and above (4.0 and greater) can specify Tier.
676 // See: 5.5.1. General sequence header OBU syntax,
677 // if ( seq_level_idx[ i ] > 7 ) seq_tier[ i ] = f(1)
678 // https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=42
679 // Also: Annex A, A.3. Levels, columns MainMbps and HighMbps
680 // at https://aomediacodec.github.io/av1-spec/av1-spec.pdf#page=652
681 if (aLevel
< 8 && aTier
> 0) {
685 // Supported bit depths are 8, 10 and 12.
686 if (aBitDepth
!= 8 && aBitDepth
!= 10 && aBitDepth
!= 12) {
689 // Profiles 0 and 1 only support 8-bit and 10-bit.
690 if (aProfile
< 2 && aBitDepth
== 12) {
694 // x && y subsampling is used to specify monochrome 4:0:0 as well
695 bool is420or400
= aSubsamplingX
&& aSubsamplingY
;
696 bool is422
= aSubsamplingX
&& !aSubsamplingY
;
697 bool is444
= !aSubsamplingX
&& !aSubsamplingY
;
699 // Profile 0 only supports 4:2:0.
700 if (aProfile
== 0 && !is420or400
) {
703 // Profile 1 only supports 4:4:4.
704 if (aProfile
== 1 && !is444
) {
707 // Profile 2 only allows 4:2:2 at 10 bits and below.
708 if (aProfile
== 2 && aBitDepth
< 12 && !is422
) {
711 // Chroma sample position can only be specified with 4:2:0.
712 if (aChromaSamplePosition
!= 0 && !is420or400
) {
716 // When video is monochrome, subsampling must be 4:0:0.
717 if (aMonochrome
&& (aChromaSamplePosition
!= 0 || !is420or400
)) {
720 // Monochrome can only be signaled when profile is 0 or 2.
721 // Note: This check is redundant with the above subsampling check,
722 // as profile 1 only supports 4:4:4.
723 if (aMonochrome
&& aProfile
!= 0 && aProfile
!= 2) {
727 // Identity matrix requires 4:4:4 subsampling.
728 if (aColorSpace
.mMatrix
== MatrixCoefficients::MC_IDENTITY
&&
729 (aSubsamplingX
|| aSubsamplingY
||
730 aColorSpace
.mRange
!= gfx::ColorRange::FULL
)) {
737 nsresult
GenerateRandomName(nsCString
& aOutSalt
, uint32_t aLength
) {
739 nsCOMPtr
<nsIRandomGenerator
> rg
=
740 do_GetService("@mozilla.org/security/random-generator;1", &rv
);
745 // For each three bytes of random data we will get four bytes of ASCII.
746 const uint32_t requiredBytesLength
=
747 static_cast<uint32_t>((aLength
+ 3) / 4 * 3);
750 rv
= rg
->GenerateRandomBytes(requiredBytesLength
, &buffer
);
756 nsDependentCSubstring
randomData(reinterpret_cast<const char*>(buffer
),
757 requiredBytesLength
);
758 rv
= Base64Encode(randomData
, temp
);
765 aOutSalt
= std::move(temp
);
769 nsresult
GenerateRandomPathName(nsCString
& aOutSalt
, uint32_t aLength
) {
770 nsresult rv
= GenerateRandomName(aOutSalt
, aLength
);
775 // Base64 characters are alphanumeric (a-zA-Z0-9) and '+' and '/', so we need
776 // to replace illegal characters -- notably '/'
777 aOutSalt
.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS
, '_');
781 already_AddRefed
<TaskQueue
> CreateMediaDecodeTaskQueue(const char* aName
) {
782 RefPtr
<TaskQueue
> queue
= TaskQueue::Create(
783 GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER
), aName
);
784 return queue
.forget();
787 void SimpleTimer::Cancel() {
790 nsCOMPtr
<nsIEventTarget
> target
;
791 mTimer
->GetTarget(getter_AddRefs(target
));
793 nsresult rv
= target
->IsOnCurrentThread(&onCurrent
);
794 MOZ_ASSERT(NS_SUCCEEDED(rv
) && onCurrent
);
803 SimpleTimer::Notify(nsITimer
* timer
) {
804 RefPtr
<SimpleTimer
> deathGrip(this);
813 SimpleTimer::GetName(nsACString
& aName
) {
814 aName
.AssignLiteral("SimpleTimer");
818 nsresult
SimpleTimer::Init(nsIRunnable
* aTask
, uint32_t aTimeoutMs
,
819 nsIEventTarget
* aTarget
) {
822 // Get target thread first, so we don't have to cancel the timer if it fails.
823 nsCOMPtr
<nsIEventTarget
> target
;
827 target
= GetMainThreadSerialEventTarget();
829 return NS_ERROR_NOT_AVAILABLE
;
833 rv
= NS_NewTimerWithCallback(getter_AddRefs(mTimer
), this, aTimeoutMs
,
834 nsITimer::TYPE_ONE_SHOT
, target
);
843 NS_IMPL_ISUPPORTS(SimpleTimer
, nsITimerCallback
, nsINamed
)
845 already_AddRefed
<SimpleTimer
> SimpleTimer::Create(nsIRunnable
* aTask
,
847 nsIEventTarget
* aTarget
) {
848 RefPtr
<SimpleTimer
> t(new SimpleTimer());
849 if (NS_FAILED(t
->Init(aTask
, aTimeoutMs
, aTarget
))) {
855 void LogToBrowserConsole(const nsAString
& aMsg
) {
856 if (!NS_IsMainThread()) {
858 nsCOMPtr
<nsIRunnable
> task
= NS_NewRunnableFunction(
859 "LogToBrowserConsole", [msg
]() { LogToBrowserConsole(msg
); });
860 SchedulerGroup::Dispatch(TaskCategory::Other
, task
.forget());
863 nsCOMPtr
<nsIConsoleService
> console(
864 do_GetService("@mozilla.org/consoleservice;1"));
866 NS_WARNING("Failed to log message to console.");
869 nsAutoString
msg(aMsg
);
870 console
->LogStringMessage(msg
.get());
873 bool ParseCodecsString(const nsAString
& aCodecs
,
874 nsTArray
<nsString
>& aOutCodecs
) {
876 bool expectMoreTokens
= false;
877 nsCharSeparatedTokenizer
tokenizer(aCodecs
, ',');
878 while (tokenizer
.hasMoreTokens()) {
879 const nsAString
& token
= tokenizer
.nextToken();
880 expectMoreTokens
= tokenizer
.separatorAfterCurrentToken();
881 aOutCodecs
.AppendElement(token
);
883 if (expectMoreTokens
) {
884 // Last codec name was empty
890 bool ParseMIMETypeString(const nsAString
& aMIMEType
,
891 nsString
& aOutContainerType
,
892 nsTArray
<nsString
>& aOutCodecs
) {
893 nsContentTypeParser
parser(aMIMEType
);
894 nsresult rv
= parser
.GetType(aOutContainerType
);
900 parser
.GetParameter("codecs", codecsStr
);
901 return ParseCodecsString(codecsStr
, aOutCodecs
);
905 static bool StartsWith(const nsACString
& string
, const char (&prefix
)[N
]) {
906 if (N
- 1 > string
.Length()) {
909 return memcmp(string
.Data(), prefix
, N
- 1) == 0;
912 bool IsH264CodecString(const nsAString
& aCodec
) {
914 uint8_t constraint
= 0;
916 return ExtractH264CodecDetails(aCodec
, profile
, constraint
, level
);
919 bool IsAACCodecString(const nsAString
& aCodec
) {
920 return aCodec
.EqualsLiteral("mp4a.40.2") || // MPEG4 AAC-LC
921 aCodec
.EqualsLiteral(
922 "mp4a.40.02") || // MPEG4 AAC-LC(for compatibility)
923 aCodec
.EqualsLiteral("mp4a.40.5") || // MPEG4 HE-AAC
924 aCodec
.EqualsLiteral(
925 "mp4a.40.05") || // MPEG4 HE-AAC(for compatibility)
926 aCodec
.EqualsLiteral("mp4a.67") || // MPEG2 AAC-LC
927 aCodec
.EqualsLiteral("mp4a.40.29"); // MPEG4 HE-AACv2
930 bool IsVP8CodecString(const nsAString
& aCodec
) {
933 uint8_t bitDepth
= 0;
934 return aCodec
.EqualsLiteral("vp8") || aCodec
.EqualsLiteral("vp8.0") ||
935 (StartsWith(NS_ConvertUTF16toUTF8(aCodec
), "vp08") &&
936 ExtractVPXCodecDetails(aCodec
, profile
, level
, bitDepth
));
939 bool IsVP9CodecString(const nsAString
& aCodec
) {
942 uint8_t bitDepth
= 0;
943 return aCodec
.EqualsLiteral("vp9") || aCodec
.EqualsLiteral("vp9.0") ||
944 (StartsWith(NS_ConvertUTF16toUTF8(aCodec
), "vp09") &&
945 ExtractVPXCodecDetails(aCodec
, profile
, level
, bitDepth
));
948 bool IsAV1CodecString(const nsAString
& aCodec
) {
949 uint8_t profile
, level
, tier
, bitDepth
, chromaPosition
;
950 bool monochrome
, subsamplingX
, subsamplingY
;
951 VideoColorSpace colorSpace
;
952 return aCodec
.EqualsLiteral("av1") ||
953 (StartsWith(NS_ConvertUTF16toUTF8(aCodec
), "av01") &&
954 ExtractAV1CodecDetails(aCodec
, profile
, level
, tier
, bitDepth
,
955 monochrome
, subsamplingX
, subsamplingY
,
956 chromaPosition
, colorSpace
));
959 UniquePtr
<TrackInfo
> CreateTrackInfoWithMIMEType(
960 const nsACString
& aCodecMIMEType
) {
961 UniquePtr
<TrackInfo
> trackInfo
;
962 if (StartsWith(aCodecMIMEType
, "audio/")) {
963 trackInfo
.reset(new AudioInfo());
964 trackInfo
->mMimeType
= aCodecMIMEType
;
965 } else if (StartsWith(aCodecMIMEType
, "video/")) {
966 trackInfo
.reset(new VideoInfo());
967 trackInfo
->mMimeType
= aCodecMIMEType
;
972 UniquePtr
<TrackInfo
> CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
973 const nsACString
& aCodecMIMEType
,
974 const MediaContainerType
& aContainerType
) {
975 UniquePtr
<TrackInfo
> trackInfo
= CreateTrackInfoWithMIMEType(aCodecMIMEType
);
977 VideoInfo
* videoInfo
= trackInfo
->GetAsVideoInfo();
979 Maybe
<int32_t> maybeWidth
= aContainerType
.ExtendedType().GetWidth();
980 if (maybeWidth
&& *maybeWidth
> 0) {
981 videoInfo
->mImage
.width
= *maybeWidth
;
982 videoInfo
->mDisplay
.width
= *maybeWidth
;
984 Maybe
<int32_t> maybeHeight
= aContainerType
.ExtendedType().GetHeight();
985 if (maybeHeight
&& *maybeHeight
> 0) {
986 videoInfo
->mImage
.height
= *maybeHeight
;
987 videoInfo
->mDisplay
.height
= *maybeHeight
;
989 } else if (trackInfo
->GetAsAudioInfo()) {
990 AudioInfo
* audioInfo
= trackInfo
->GetAsAudioInfo();
991 Maybe
<int32_t> maybeChannels
=
992 aContainerType
.ExtendedType().GetChannels();
993 if (maybeChannels
&& *maybeChannels
> 0) {
994 audioInfo
->mChannels
= *maybeChannels
;
996 Maybe
<int32_t> maybeSamplerate
=
997 aContainerType
.ExtendedType().GetSamplerate();
998 if (maybeSamplerate
&& *maybeSamplerate
> 0) {
999 audioInfo
->mRate
= *maybeSamplerate
;
1006 bool OnCellularConnection() {
1007 uint32_t linkType
= nsINetworkLinkService::LINK_TYPE_UNKNOWN
;
1008 if (XRE_IsContentProcess()) {
1009 mozilla::dom::ContentChild
* cpc
=
1010 mozilla::dom::ContentChild::GetSingleton();
1012 NS_WARNING("Can't get ContentChild singleton in content process!");
1015 linkType
= cpc
->NetworkLinkType();
1018 nsCOMPtr
<nsINetworkLinkService
> nls
=
1019 do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID
, &rv
);
1020 if (NS_FAILED(rv
)) {
1021 NS_WARNING("Can't get nsINetworkLinkService.");
1025 rv
= nls
->GetLinkType(&linkType
);
1026 if (NS_FAILED(rv
)) {
1027 NS_WARNING("Can't get network link type.");
1033 case nsINetworkLinkService::LINK_TYPE_UNKNOWN
:
1034 case nsINetworkLinkService::LINK_TYPE_ETHERNET
:
1035 case nsINetworkLinkService::LINK_TYPE_USB
:
1036 case nsINetworkLinkService::LINK_TYPE_WIFI
:
1038 case nsINetworkLinkService::LINK_TYPE_WIMAX
:
1039 case nsINetworkLinkService::LINK_TYPE_MOBILE
:
1046 } // end namespace mozilla