summaryrefslogtreecommitdiffstats
path: root/dom/media/platforms/wmf/MFMediaEngineAudioStream.cpp
blob: 4acf26e0417c85013e4dcf0454aca8107a404200 (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
/* 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 "MFMediaEngineAudioStream.h"

#include <mferror.h>
#include <mfapi.h>

#include "MFMediaEngineUtils.h"
#include "WMFUtils.h"
#include "mozilla/StaticPrefs_media.h"

namespace mozilla {

#define LOG(msg, ...)                           \
  MOZ_LOG(gMFMediaEngineLog, LogLevel::Debug,   \
          ("MFMediaStream=%p (%s), " msg, this, \
           this->GetDescriptionName().get(), ##__VA_ARGS__))

using Microsoft::WRL::ComPtr;
using Microsoft::WRL::MakeAndInitialize;

/* static */
MFMediaEngineAudioStream* MFMediaEngineAudioStream::Create(
    uint64_t aStreamId, const TrackInfo& aInfo, MFMediaSource* aParentSource) {
  MOZ_ASSERT(aInfo.IsAudio());
  MFMediaEngineAudioStream* stream;
  if (FAILED(MakeAndInitialize<MFMediaEngineAudioStream>(
          &stream, aStreamId, aInfo, aParentSource))) {
    return nullptr;
  }
  return stream;
}

HRESULT MFMediaEngineAudioStream::CreateMediaType(const TrackInfo& aInfo,
                                                  IMFMediaType** aMediaType) {
  const AudioInfo& info = *aInfo.GetAsAudioInfo();
  mAudioInfo = info;
  GUID subType = AudioMimeTypeToMediaFoundationSubtype(info.mMimeType);
  NS_ENSURE_TRUE(subType != GUID_NULL, MF_E_TOPO_CODEC_NOT_FOUND);

  // https://docs.microsoft.com/en-us/windows/win32/medfound/media-type-attributes
  ComPtr<IMFMediaType> mediaType;
  RETURN_IF_FAILED(wmf::MFCreateMediaType(&mediaType));
  RETURN_IF_FAILED(mediaType->SetGUID(MF_MT_SUBTYPE, subType));
  RETURN_IF_FAILED(mediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));
  RETURN_IF_FAILED(
      mediaType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, info.mChannels));
  RETURN_IF_FAILED(
      mediaType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, info.mRate));
  uint64_t bitDepth = info.mBitDepth != 0 ? info.mBitDepth : 16;
  RETURN_IF_FAILED(mediaType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bitDepth));
  if (subType == MFAudioFormat_AAC) {
    if (mAACUserData.IsEmpty()) {
      MOZ_ASSERT(info.mCodecSpecificConfig.is<AacCodecSpecificData>() ||
                 info.mCodecSpecificConfig.is<AudioCodecSpecificBinaryBlob>());
      RefPtr<MediaByteBuffer> blob;
      if (info.mCodecSpecificConfig.is<AacCodecSpecificData>()) {
        blob = info.mCodecSpecificConfig.as<AacCodecSpecificData>()
                   .mDecoderConfigDescriptorBinaryBlob;
      } else {
        blob = info.mCodecSpecificConfig.as<AudioCodecSpecificBinaryBlob>()
                   .mBinaryBlob;
      }
      AACAudioSpecificConfigToUserData(info.mExtendedProfile, blob->Elements(),
                                       blob->Length(), mAACUserData);
      LOG("Generated AAC user data");
    }
    RETURN_IF_FAILED(
        mediaType->SetUINT32(MF_MT_AAC_PAYLOAD_TYPE, 0x0));  // Raw AAC packet
    RETURN_IF_FAILED(mediaType->SetBlob(
        MF_MT_USER_DATA, mAACUserData.Elements(), mAACUserData.Length()));
  }
  LOG("Created audio type, subtype=%s, channel=%" PRIu32 ", rate=%" PRIu32
      ", bitDepth=%" PRIu64 ", encrypted=%d",
      GUIDToStr(subType), info.mChannels, info.mRate, bitDepth,
      mAudioInfo.mCrypto.IsEncrypted());

  if (IsEncrypted()) {
    ComPtr<IMFMediaType> protectedMediaType;
    RETURN_IF_FAILED(wmf::MFWrapMediaType(mediaType.Get(),
                                          MFMediaType_Protected, subType,
                                          protectedMediaType.GetAddressOf()));
    LOG("Wrap MFMediaType_Audio into MFMediaType_Protected");
    *aMediaType = protectedMediaType.Detach();
  } else {
    *aMediaType = mediaType.Detach();
  }
  return S_OK;
}

bool MFMediaEngineAudioStream::HasEnoughRawData() const {
  // If more than this much raw audio is queued, we'll hold off request more
  // audio.
  return mRawDataQueueForFeedingEngine.Duration() >=
         StaticPrefs::media_wmf_media_engine_raw_data_threshold_audio();
}

already_AddRefed<MediaData> MFMediaEngineAudioStream::OutputDataInternal() {
  AssertOnTaskQueue();
  if (mRawDataQueueForGeneratingOutput.GetSize() == 0) {
    return nullptr;
  }
  // The media engine doesn't provide a way to allow us to access decoded audio
  // frames, and the audio playback will be handled internally inside the media
  // engine. So we simply return fake audio data.
  RefPtr<MediaRawData> input = mRawDataQueueForGeneratingOutput.PopFront();
  RefPtr<MediaData> output =
      new AudioData(input->mOffset, input->mTime, AlignedAudioBuffer{},
                    mAudioInfo.mChannels, mAudioInfo.mRate);
  return output.forget();
}

nsCString MFMediaEngineAudioStream::GetCodecName() const {
  WMFStreamType type = GetStreamTypeFromMimeType(mAudioInfo.mMimeType);
  switch (type) {
    case WMFStreamType::MP3:
      return "mp3"_ns;
    case WMFStreamType::AAC:
      return "aac"_ns;
    case WMFStreamType::OPUS:
      return "opus"_ns;
    case WMFStreamType::VORBIS:
      return "vorbis"_ns;
    default:
      return "unknown"_ns;
  }
}

bool MFMediaEngineAudioStream::IsEncrypted() const {
  return mAudioInfo.mCrypto.IsEncrypted();
}

#undef LOG

}  // namespace mozilla