Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/webm/WebMDecoder.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "WebMDecoder.h"
8
#include "mozilla/Move.h"
9
#include "mozilla/Preferences.h"
10
#include "mozilla/StaticPrefs.h"
11
#ifdef MOZ_AV1
12
#include "AOMDecoder.h"
13
#endif
14
#include "MediaContainerType.h"
15
#include "PDMFactory.h"
16
#include "VideoUtils.h"
17
18
namespace mozilla {
19
20
/* static */ nsTArray<UniquePtr<TrackInfo>>
21
WebMDecoder::GetTracksInfo(const MediaContainerType& aType, MediaResult& aError)
22
0
{
23
0
  nsTArray<UniquePtr<TrackInfo>> tracks;
24
0
  const bool isVideo = aType.Type() == MEDIAMIMETYPE("video/webm");
25
0
26
0
  if (aType.Type() != MEDIAMIMETYPE("audio/webm") && !isVideo) {
27
0
    aError = MediaResult(
28
0
      NS_ERROR_DOM_MEDIA_FATAL_ERR,
29
0
      RESULT_DETAIL("Invalid type:%s", aType.Type().AsString().get()));
30
0
    return tracks;
31
0
  }
32
0
33
0
  aError = NS_OK;
34
0
35
0
  const MediaCodecs& codecs = aType.ExtendedType().Codecs();
36
0
  if (codecs.IsEmpty()) {
37
0
    return tracks;
38
0
  }
39
0
40
0
  for (const auto& codec : codecs.Range()) {
41
0
    if (codec.EqualsLiteral("opus") || codec.EqualsLiteral("vorbis")) {
42
0
      tracks.AppendElement(
43
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
44
0
          NS_LITERAL_CSTRING("audio/") + NS_ConvertUTF16toUTF8(codec), aType));
45
0
      continue;
46
0
    }
47
0
    if (isVideo) {
48
0
      UniquePtr<TrackInfo> trackInfo;
49
0
      if (IsVP9CodecString(codec)) {
50
0
        trackInfo = CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
51
0
          NS_LITERAL_CSTRING("video/vp9"), aType);
52
0
      } else if (IsVP8CodecString(codec)) {
53
0
        trackInfo = CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
54
0
          NS_LITERAL_CSTRING("video/vp8"), aType);
55
0
      }
56
0
      if (trackInfo) {
57
0
        uint8_t profile = 0;
58
0
        uint8_t level = 0;
59
0
        uint8_t bitDepth = 0;
60
0
        if (ExtractVPXCodecDetails(codec, profile, level, bitDepth)) {
61
0
          trackInfo->GetAsVideoInfo()->mBitDepth = bitDepth;
62
0
        }
63
0
        tracks.AppendElement(std::move(trackInfo));
64
0
        continue;
65
0
      }
66
0
    }
67
0
#ifdef MOZ_AV1
68
0
    if (StaticPrefs::MediaAv1Enabled() && IsAV1CodecString(codec)) {
69
0
      tracks.AppendElement(
70
0
        CreateTrackInfoWithMIMETypeAndContainerTypeExtraParameters(
71
0
          NS_LITERAL_CSTRING("video/av1"), aType));
72
0
      continue;
73
0
    }
74
0
#endif
75
0
    // Unknown codec
76
0
    aError =
77
0
      MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
78
0
                  RESULT_DETAIL("Unknown codec:%s",
79
0
                                NS_ConvertUTF16toUTF8(codec).get()));
80
0
  }
81
0
  return tracks;
82
0
}
83
84
/* static */
85
bool
86
WebMDecoder::IsSupportedType(const MediaContainerType& aContainerType)
87
0
{
88
0
  if (!StaticPrefs::MediaWebMEnabled()) {
89
0
    return false;
90
0
  }
91
0
92
0
  MediaResult rv = NS_OK;
93
0
  auto tracks = GetTracksInfo(aContainerType, rv);
94
0
95
0
  if (NS_FAILED(rv)) {
96
0
    return false;
97
0
  }
98
0
99
0
  if (tracks.IsEmpty()) {
100
0
    // WebM guarantees that the only codecs it contained are vp8, vp9, opus or vorbis.
101
0
    return true;
102
0
  }
103
0
104
0
  // Verify that we have a PDM that supports the whitelisted types, include
105
0
  // bitdepth
106
0
  RefPtr<PDMFactory> platform = new PDMFactory();
107
0
  for (const auto& track : tracks) {
108
0
    if (!track || !platform->Supports(*track, nullptr /* diagnostic */)) {
109
0
      return false;
110
0
    }
111
0
  }
112
0
113
0
  return true;
114
0
}
115
116
/* static */ nsTArray<UniquePtr<TrackInfo>>
117
WebMDecoder::GetTracksInfo(const MediaContainerType& aType)
118
0
{
119
0
  MediaResult rv = NS_OK;
120
0
  return GetTracksInfo(aType, rv);
121
0
}
122
123
} // namespace mozilla
124