summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/PlatformEncoderModule.cpp
blob: 525729e7563b2884c833e231b37ea67e21c1cc21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "PlatformEncoderModule.h"
#include "nsPrintfCString.h"
#include "mozilla/ToString.h"

namespace mozilla {

extern LazyLogModule sPEMLog;
#define LOGD(fmt, ...)                       \
  MOZ_LOG(sPEMLog, mozilla::LogLevel::Debug, \
          ("PEM: %s: " fmt, __func__, ##__VA_ARGS__))

// TODO: Automatically generate this (Bug 1865896)
const char* GetCodecTypeString(const CodecType& aCodecType) {
  switch (aCodecType) {
    case CodecType::_BeginVideo_:
      return "_BeginVideo_";
    case CodecType::H264:
      return "H264";
    case CodecType::VP8:
      return "VP8";
    case CodecType::VP9:
      return "VP9";
    case CodecType::AV1:
      return "AV1";
    case CodecType::_EndVideo_:  // CodecType::_BeginAudio_
      return "_EndVideo_/_BeginAudio_";
    case CodecType::Opus:
      return "Opus";
    case CodecType::Vorbis:
      return "Vorbis";
    case CodecType::Flac:
      return "Flac";
    case CodecType::AAC:
      return "AAC";
    case CodecType::PCM:
      return "PCM";
      break;
    case CodecType::G722:
      return "G722";
    case CodecType::_EndAudio_:
      return "_EndAudio_";
    case CodecType::Unknown:
      return "Unknown";
  }
  MOZ_ASSERT_UNREACHABLE("undefined codec type");
  return "Undefined";
}

RefPtr<PlatformEncoderModule::CreateEncoderPromise>
PlatformEncoderModule::AsyncCreateEncoder(const EncoderConfig& aEncoderConfig,
                                          const RefPtr<TaskQueue>& aTaskQueue) {
  RefPtr<MediaDataEncoder> encoder;
  MediaResult result = NS_OK;
  if (aEncoderConfig.IsAudio()) {
    encoder = CreateAudioEncoder(aEncoderConfig, aTaskQueue);
  } else if (aEncoderConfig.IsVideo()) {
    encoder = CreateVideoEncoder(aEncoderConfig, aTaskQueue);
  }
  if (!encoder) {
    if (NS_FAILED(result)) {
      return CreateEncoderPromise::CreateAndReject(result, __func__);
    }
    return CreateEncoderPromise::CreateAndReject(
        MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR,
                    nsPrintfCString("Error creating encoder for %d",
                                    static_cast<int>(aEncoderConfig.mCodec))
                        .get()),
        __func__);
  }
  return CreateEncoderPromise::CreateAndResolve(encoder, __func__);
}

template <typename T>
nsCString MaybeToString(const Maybe<T>& aMaybe) {
  return nsPrintfCString(
      "%s", aMaybe.isSome() ? ToString(aMaybe.value()).c_str() : "nothing");
}

struct ConfigurationChangeToString {
  nsCString operator()(const DimensionsChange& aDimensionsChange) {
    return nsPrintfCString("Dimensions: %dx%d", aDimensionsChange.get().width,
                           aDimensionsChange.get().height);
  }
  nsCString operator()(const DisplayDimensionsChange& aDisplayDimensionChange) {
    if (aDisplayDimensionChange.get().isNothing()) {
      return nsCString("Display dimensions: nothing");
    }
    gfx::IntSize displayDimensions = aDisplayDimensionChange.get().value();
    return nsPrintfCString("Display dimensions: %dx%d", displayDimensions.width,
                           displayDimensions.height);
  }
  nsCString operator()(const BitrateChange& aBitrateChange) {
    if (aBitrateChange.get().isSome()) {
      return nsLiteralCString("Bitrate: nothing");
    }
    return nsPrintfCString("Bitrate: %skbps",
                           MaybeToString(aBitrateChange.get()).get());
  }
  nsCString operator()(const FramerateChange& aFramerateChange) {
    if (aFramerateChange.get().isNothing()) {
      return nsCString("Framerate: nothing");
    }
    return nsPrintfCString("Framerate: %lfHz", aFramerateChange.get().value());
  }
  nsCString operator()(const BitrateModeChange& aBitrateModeChange) {
    return nsPrintfCString("Bitrate mode: %s",
                           aBitrateModeChange.get() == BitrateMode::Constant
                               ? "Constant"
                               : "Variable");
  }
  nsCString operator()(const UsageChange& aUsageChange) {
    return nsPrintfCString(
        "Usage mode: %s",
        aUsageChange.get() == Usage::Realtime ? "Realtime" : "Recoding");
  }
  nsCString operator()(const ContentHintChange& aContentHintChange) {
    return nsPrintfCString("Content hint: %s",
                           MaybeToString(aContentHintChange.get()).get());
  }
  nsCString operator()(const SampleRateChange& aSampleRateChange) {
    return nsPrintfCString("Sample rate %" PRIu32 "Hz",
                           aSampleRateChange.get());
  }
  nsCString operator()(const NumberOfChannelsChange& aNumberOfChannelsChange) {
    return nsPrintfCString("Channels: %" PRIu32 "Hz",
                           aNumberOfChannelsChange.get());
  }
};

nsString EncoderConfigurationChangeList::ToString() const {
  nsString rv(
      NS_LITERAL_STRING_FROM_CSTRING("EncoderConfigurationChangeList:"_ns));
  for (const EncoderConfigurationItem& change : mChanges) {
    nsCString str = change.match(ConfigurationChangeToString());
    rv.AppendPrintf("- %s\n", str.get());
  }
  return rv;
}

bool CanLikelyEncode(const EncoderConfig& aConfig) {
  if (aConfig.mCodec == CodecType::H264) {
    if (!aConfig.mCodecSpecific ||
        !aConfig.mCodecSpecific->is<H264Specific>()) {
      LOGD(
          "Error: asking for support codec for h264 without h264 specific "
          "config.");
      return false;
    }
    H264Specific specific = aConfig.mCodecSpecific->as<H264Specific>();
    int width = aConfig.mSize.width;
    int height = aConfig.mSize.height;
    if (width % 2 || !width) {
      LOGD("Invalid width of %d for h264", width);
      return false;
    }
    if (height % 2 || !height) {
      LOGD("Invalid height of %d for h264", height);
      return false;
    }
    if (specific.mProfile != H264_PROFILE_BASE &&
        specific.mProfile != H264_PROFILE_MAIN &&
        specific.mProfile != H264_PROFILE_HIGH) {
      LOGD("Invalid profile of %x for h264", specific.mProfile);
      return false;
    }
    if (width > 4096 || height > 4096) {
      LOGD("Invalid size of %dx%d for h264", width, height);
      return false;
    }
  }
  if (aConfig.mCodec == CodecType::VP8) {
    int width = aConfig.mSize.width;
    int height = aConfig.mSize.height;
    if (width > 2 << 13 || height > 2 << 13) {
      LOGD("Invalid size of %dx%d for VP8", width, height);
      return false;
    }
  }
  if (aConfig.mCodec == CodecType::VP9) {
    int width = aConfig.mSize.width;
    int height = aConfig.mSize.height;
    if (width > 2 << 15 || height > 2 << 15) {
      LOGD("Invalid size of %dx%d for VP9", width, height);
      return false;
    }
  }

  return true;
}

}  // namespace mozilla

#undef LOGD