DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (56e7b9127e89)

VCS Links

Line Code
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "MediaMIMETypes.h"

#include "nsContentTypeParser.h"
#include "mozilla/dom/MediaCapabilitiesBinding.h"

namespace mozilla {

template <int N>
static bool
StartsWith(const nsACString& string, const char (&prefix)[N])
{
    if (N - 1 > string.Length()) {
      return false;
    }
    return memcmp(string.Data(), prefix, N - 1) == 0;
}

bool
MediaMIMEType::HasApplicationMajorType() const
{
  return StartsWith(mMIMEType, "application/");
}

bool
MediaMIMEType::HasAudioMajorType() const
{
  return StartsWith(mMIMEType, "audio/");
}

bool
MediaMIMEType::HasVideoMajorType() const
{
  return StartsWith(mMIMEType, "video/");
}

size_t
MediaMIMEType::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
  return mMIMEType.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}

MediaMIMEType::MediaMIMEType(const nsACString& aType)
  : mMIMEType(aType)
{
}

Maybe<MediaMIMEType>
MakeMediaMIMEType(const nsAString& aType)
{
  nsContentTypeParser parser(aType);
  nsAutoString mime;
  nsresult rv = parser.GetType(mime);
  if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
    return Nothing();
  }

  NS_ConvertUTF16toUTF8 mime8{mime};
  if (!IsMediaMIMEType(mime8)) {
    return Nothing();
  }

  return Some(MediaMIMEType(mime8));
}

Maybe<MediaMIMEType>
MakeMediaMIMEType(const nsACString& aType)
{
  return MakeMediaMIMEType(NS_ConvertUTF8toUTF16(aType));
}

Maybe<MediaMIMEType>
MakeMediaMIMEType(const char* aType)
{
  if (!aType) {
    return Nothing();
  }
  return MakeMediaMIMEType(nsDependentCString(aType));
}

bool
MediaCodecs::Contains(const nsAString& aCodec) const
{
  for (const auto& myCodec : Range()) {
    if (myCodec == aCodec) {
      return true;
    }
  }
  return false;
}

bool
MediaCodecs::ContainsAll(const MediaCodecs& aCodecs) const
{
  const auto& codecsToTest = aCodecs.Range();
  for (const auto& codecToTest : codecsToTest) {
    if (!Contains(codecToTest)) {
      return false;
    }
  }
  return true;
}

bool
MediaCodecs::ContainsPrefix(const nsAString& aCodecPrefix) const
{
  const size_t prefixLength = aCodecPrefix.Length();
  for (const auto& myCodec : Range()) {
    if (myCodec.Length() >= prefixLength &&
        memcmp(myCodec.Data(), aCodecPrefix.Data(), prefixLength) == 0) {
      return true;
    }
  }
  return false;
}

size_t
MediaCodecs::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
  return mCodecs.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
}


static int32_t
GetParameterAsNumber(const nsContentTypeParser& aParser,
                     const char* aParameter,
                     const int32_t aErrorReturn)
{
  nsAutoString parameterString;
  nsresult rv = aParser.GetParameter(aParameter, parameterString);
  if (NS_FAILED_impl(rv)) {
    return aErrorReturn;
  }
  int32_t number = parameterString.ToInteger(&rv);
  if (MOZ_UNLIKELY(NS_FAILED_impl(rv))) {
    return aErrorReturn;
  }
  return number;
}

MediaExtendedMIMEType::MediaExtendedMIMEType(const nsACString& aOriginalString,
                                             const nsACString& aMIMEType,
                                             bool aHaveCodecs,
                                             const nsAString& aCodecs,
                                             int32_t aWidth,
                                             int32_t aHeight,
                                             double aFramerate,
                                             int32_t aBitrate)
  : mOriginalString(aOriginalString)
  , mMIMEType(aMIMEType)
  , mHaveCodecs(aHaveCodecs)
  , mCodecs(aCodecs)
  , mWidth(aWidth)
  , mHeight(aHeight)
  , mFramerate(aFramerate)
  , mBitrate(aBitrate)
{
}

MediaExtendedMIMEType::MediaExtendedMIMEType(const nsACString& aOriginalString,
                                             const nsACString& aMIMEType,
                                             bool aHaveCodecs,
                                             const nsAString& aCodecs,
                                             int32_t aChannels,
                                             int32_t aSamplerate,
                                             int32_t aBitrate)
  : mOriginalString(aOriginalString)
  , mMIMEType(aMIMEType)
  , mHaveCodecs(aHaveCodecs)
  , mCodecs(aCodecs)
  , mChannels(aChannels)
  , mSamplerate(aSamplerate)
  , mBitrate(aBitrate)
{
}

MediaExtendedMIMEType::MediaExtendedMIMEType(const MediaMIMEType& aType)
  : mOriginalString(aType.AsString())
  , mMIMEType(aType)
{
}

MediaExtendedMIMEType::MediaExtendedMIMEType(MediaMIMEType&& aType)
  : mOriginalString(aType.AsString())
  , mMIMEType(std::move(aType))
{
}

/* static */ Maybe<double>
MediaExtendedMIMEType::ComputeFractionalString(const nsAString& aFrac)
{
  nsAutoString frac(aFrac);
  nsresult error;
  double result = frac.ToDouble(&error);
  if (NS_SUCCEEDED(error)) {
    if (result <= 0) {
      return Nothing();
    }
    return Some(result);
  }

  int32_t slashPos = frac.Find(NS_LITERAL_STRING("/"));
  if (slashPos == kNotFound) {
    return Nothing();
  }

  nsAutoString firstPart(Substring(frac, 0, slashPos - 1));
  double first = firstPart.ToDouble(&error);
  if (NS_FAILED(error)) {
    return Nothing();
  }
  nsAutoString secondPart(Substring(frac, slashPos + 1));
  double second = secondPart.ToDouble(&error);
  if (NS_FAILED(error) || second == 0) {
    return Nothing();
  }

  result = first / second;
  if (result <= 0) {
    return Nothing();
  }

  return Some(result);
}

Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const nsAString& aType)
{
  nsContentTypeParser parser(aType);
  nsAutoString mime;
  nsresult rv = parser.GetType(mime);
  if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
    return Nothing();
  }

  NS_ConvertUTF16toUTF8 mime8{mime};
  if (!IsMediaMIMEType(mime8)) {
    return Nothing();
  }

  nsAutoString codecs;
  rv = parser.GetParameter("codecs", codecs);
  bool haveCodecs = NS_SUCCEEDED(rv);

  int32_t width = GetParameterAsNumber(parser, "width", -1);
  int32_t height = GetParameterAsNumber(parser, "height", -1);
  double framerate = GetParameterAsNumber(parser, "framerate", -1);
  int32_t bitrate = GetParameterAsNumber(parser, "bitrate", -1);

  return Some(MediaExtendedMIMEType(NS_ConvertUTF16toUTF8(aType),
                                    mime8,
                                    haveCodecs, codecs,
                                    width, height,
                                    framerate, bitrate));
}

Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const dom::VideoConfiguration& aConfig)
{
  MOZ_ASSERT(aConfig.mContentType.WasPassed() &&
             aConfig.mFramerate.WasPassed() &&
             aConfig.mWidth.WasPassed() &&
             aConfig.mHeight.WasPassed() &&
             aConfig.mBitrate.WasPassed(),
             "All dictionary members must be present");
  if (aConfig.mContentType.Value().IsEmpty()) {
    return Nothing();
  }
  nsContentTypeParser parser(aConfig.mContentType.Value());
  nsAutoString mime;
  nsresult rv = parser.GetType(mime);
  if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
    return Nothing();
  }

  NS_ConvertUTF16toUTF8 mime8{ mime };
  if (!IsMediaMIMEType(mime8)) {
    return Nothing();
  }

  nsAutoString codecs;
  rv = parser.GetParameter("codecs", codecs);
  bool haveCodecs = NS_SUCCEEDED(rv);

  auto framerate =
    MediaExtendedMIMEType::ComputeFractionalString(aConfig.mFramerate.Value());
  if (!framerate) {
    return Nothing();
  }

  return Some(
    MediaExtendedMIMEType(NS_ConvertUTF16toUTF8(aConfig.mContentType.Value()),
                          mime8,
                          haveCodecs,
                          codecs,
                          aConfig.mWidth.Value(),
                          aConfig.mHeight.Value(),
                          framerate.ref(),
                          aConfig.mBitrate.Value()));
}

Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const dom::AudioConfiguration& aConfig)
{
  if (!aConfig.mContentType.WasPassed() ||
      aConfig.mContentType.Value().IsEmpty()) {
    return Nothing();
  }
  nsContentTypeParser parser(aConfig.mContentType.Value());
  nsAutoString mime;
  nsresult rv = parser.GetType(mime);
  if (!NS_SUCCEEDED(rv) || mime.IsEmpty()) {
    return Nothing();
  }

  NS_ConvertUTF16toUTF8 mime8{ mime };
  if (!IsMediaMIMEType(mime8)) {
    return Nothing();
  }

  nsAutoString codecs;
  rv = parser.GetParameter("codecs", codecs);
  bool haveCodecs = NS_SUCCEEDED(rv);

  int32_t channels = 2; // use a stereo config if not known.
  if (aConfig.mChannels.WasPassed()) {
    // A channels string was passed. Make sure it is valid.
    nsresult error;
    double value = aConfig.mChannels.Value().ToDouble(&error);
    if (NS_FAILED(error)) {
      return Nothing();
    }
    // Value is a channel configuration such as 5.1. We want to treat this as 6.
    channels = value;
    double fp = value - channels;
     // round up as .1 and .2 aren't exactly expressible in binary.
    channels += (fp * 10) + .5;
  }

  return Some(MediaExtendedMIMEType(
    NS_ConvertUTF16toUTF8(aConfig.mContentType.Value()),
    mime8,
    haveCodecs,
    codecs,
    channels,
    aConfig.mSamplerate.WasPassed() ? aConfig.mSamplerate.Value() : 48000,
    aConfig.mBitrate.WasPassed() ? aConfig.mBitrate.Value() : 131072));
}

size_t
MediaExtendedMIMEType::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
  return mOriginalString.SizeOfExcludingThisIfUnshared(aMallocSizeOf)
         + mMIMEType.SizeOfExcludingThis(aMallocSizeOf)
         + mCodecs.SizeOfExcludingThis(aMallocSizeOf);
}

Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const nsACString& aType)
{
  return MakeMediaExtendedMIMEType(NS_ConvertUTF8toUTF16(aType));
}

Maybe<MediaExtendedMIMEType>
MakeMediaExtendedMIMEType(const char* aType)
{
  if (!aType) {
    return Nothing();
  }
  return MakeMediaExtendedMIMEType(nsDependentCString(aType));
}

} // namespace mozilla