Skip to content

Commit

Permalink
Bug 1795507 - part2 : remove FakeDecodedDataCreator and let the engin…
Browse files Browse the repository at this point in the history
…e audio stream to return its own fake data. r=jolin

Differential Revision: https://phabricator.services.mozilla.com/D159538
  • Loading branch information
alastor0325 committed Nov 14, 2022
1 parent 7c4873d commit e41cb52
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 69 deletions.
17 changes: 17 additions & 0 deletions dom/media/platforms/wmf/MFMediaEngineAudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ MFMediaEngineAudioStream* MFMediaEngineAudioStream::Create(
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);

Expand Down Expand Up @@ -74,6 +75,22 @@ bool MFMediaEngineAudioStream::HasEnoughRawData() const {
return mRawDataQueueForFeedingEngine.Duration() >= AMPLE_AUDIO_USECS;
}

already_AddRefed<MediaData> MFMediaEngineAudioStream::OutputData() {
if (mRawDataQueueForGeneratingOutput.GetSize() == 0) {
LOGV("Hasn't got raw data for generating output yet");
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();
}

#undef LOGV

} // namespace mozilla
5 changes: 5 additions & 0 deletions dom/media/platforms/wmf/MFMediaEngineAudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class MFMediaEngineAudioStream final : public MFMediaEngineStream {
return TrackInfo::TrackType::kAudioTrack;
}

already_AddRefed<MediaData> OutputData() override;

private:
HRESULT CreateMediaType(const TrackInfo& aInfo,
IMFMediaType** aMediaType) override;
Expand All @@ -35,6 +37,9 @@ class MFMediaEngineAudioStream final : public MFMediaEngineStream {

// For MF_MT_USER_DATA. Currently only used for AAC.
nsTArray<BYTE> mAACUserData;

// Set when `CreateMediaType()` is called.
AudioInfo mAudioInfo;
};

} // namespace mozilla
Expand Down
53 changes: 9 additions & 44 deletions dom/media/platforms/wmf/MFMediaEngineStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ RefPtr<MediaDataDecoder::DecodePromise> MFMediaEngineStreamWrapper::Decode(
"MFMediaEngineStreamWrapper::Decode",
[sample = RefPtr{aSample}, stream]() { stream->NotifyNewData(sample); }));

// TODO : Generalize this part by using `OutputData()` for audio as well and
// remove `mFakeDataCreator`.
if (mStream->TrackType() == TrackInfo::TrackType::kVideoTrack) {
if (RefPtr<MediaData> outputData = mStream->OutputData()) {
return DecodePromise::CreateAndResolve(DecodedData{std::move(outputData)},
__func__);
} else {
return DecodePromise::CreateAndResolve(DecodedData{}, __func__);
}
}
// The stream don't support returning output, all data would be processed
// inside the media engine. We return an empty data back instead.
MOZ_ASSERT(mFakeDataCreator->Type() == mStream->TrackType());
return mFakeDataCreator->Decode(aSample);
RefPtr<MediaData> outputData = mStream->OutputData();
return outputData ? DecodePromise::CreateAndResolve(
DecodedData{std::move(outputData)}, __func__)
: DecodePromise::CreateAndResolve(DecodedData{}, __func__);
}

RefPtr<MediaDataDecoder::DecodePromise> MFMediaEngineStreamWrapper::Drain() {
Expand All @@ -75,14 +65,12 @@ RefPtr<MediaDataDecoder::DecodePromise> MFMediaEngineStreamWrapper::Drain() {
MediaResult(NS_ERROR_FAILURE, "MFMediaEngineStreamWrapper is shutdown"),
__func__);
}
// TODO : Generalize this part by using `OutputData()` for audio as well.

DecodedData outputs;
if (mStream->TrackType() == TrackInfo::TrackType::kVideoTrack) {
RefPtr<MediaData> outputData = mStream->OutputData();
while (outputData) {
outputs.AppendElement(outputData);
outputData = mStream->OutputData();
}
RefPtr<MediaData> outputData = mStream->OutputData();
while (outputData) {
outputs.AppendElement(outputData);
outputData = mStream->OutputData();
}
return DecodePromise::CreateAndResolve(std::move(outputs), __func__);
}
Expand All @@ -94,7 +82,6 @@ RefPtr<MediaDataDecoder::FlushPromise> MFMediaEngineStreamWrapper::Flush() {
MediaResult(NS_ERROR_FAILURE, "MFMediaEngineStreamWrapper is shutdown"),
__func__);
}
mFakeDataCreator->Flush();
return InvokeAsync(mTaskQueue, mStream.Get(), __func__,
&MFMediaEngineStream::Flush);
}
Expand All @@ -110,7 +97,6 @@ RefPtr<ShutdownPromise> MFMediaEngineStreamWrapper::Shutdown() {
}
mStream = nullptr;
mTaskQueue = nullptr;
mFakeDataCreator = nullptr;
return ShutdownPromise::CreateAndResolve(true, __func__);
}

Expand All @@ -124,27 +110,6 @@ MFMediaEngineStreamWrapper::NeedsConversion() const {
: MediaDataDecoder::ConversionRequired::kNeedNone;
}

MFMediaEngineStreamWrapper::FakeDecodedDataCreator::FakeDecodedDataCreator(
const CreateDecoderParams& aParams) {
if (aParams.mConfig.IsVideo()) {
const VideoInfo& config = aParams.VideoConfig();
mDummyDecoder = new DummyMediaDataDecoder(
MakeUnique<BlankVideoDataCreator>(config.mDisplay.width,
config.mDisplay.height,
aParams.mImageContainer),
"blank video data decoder for media engine"_ns, aParams);
mType = TrackInfo::TrackType::kVideoTrack;
} else if (aParams.mConfig.IsAudio()) {
const AudioInfo& config = aParams.AudioConfig();
mDummyDecoder = new DummyMediaDataDecoder(
MakeUnique<BlankAudioDataCreator>(config.mChannels, config.mRate),
"blank audio data decoder for media engine"_ns, aParams);
mType = TrackInfo::TrackType::kAudioTrack;
} else {
MOZ_ASSERT_UNREACHABLE("unexpected config type");
}
}

MFMediaEngineStream::MFMediaEngineStream()
: mIsShutdown(false), mIsSelected(false), mReceivedEOS(false) {
MOZ_COUNT_CTOR(MFMediaEngineStream);
Expand Down
27 changes: 2 additions & 25 deletions dom/media/platforms/wmf/MFMediaEngineStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class MFMediaEngineStream

// Overwrite this method to support returning decoded data. Called on
// MediaPDecoder thread (wrapper thread).
// TODO : make this function pure virtual and let audio stream to generate its
// own fake data as well.
virtual already_AddRefed<MediaData> OutputData() { return nullptr; }
virtual already_AddRefed<MediaData> OutputData() = 0;

virtual MediaDataDecoder::ConversionRequired NeedsConversion() const {
return MediaDataDecoder::ConversionRequired::kNeedNone;
Expand Down Expand Up @@ -182,12 +180,9 @@ class MFMediaEngineStreamWrapper : public MediaDataDecoder {
MFMediaEngineStreamWrapper(MFMediaEngineStream* aStream,
TaskQueue* aTaskQueue,
const CreateDecoderParams& aParams)
: mStream(aStream),
mTaskQueue(aTaskQueue),
mFakeDataCreator(new FakeDecodedDataCreator(aParams)) {
: mStream(aStream), mTaskQueue(aTaskQueue) {
MOZ_ASSERT(mStream);
MOZ_ASSERT(mTaskQueue);
MOZ_ASSERT(mFakeDataCreator);
}

// Methods for MediaDataDecoder, they are all called on the remote
Expand All @@ -202,25 +197,7 @@ class MFMediaEngineStreamWrapper : public MediaDataDecoder {

private:
Microsoft::WRL::ComPtr<MFMediaEngineStream> mStream;
// We use this to generate fake decoded outputs, as the real data is handled
// inside the media engine. Audio output is not possible to get, the video
// output would be output via DCOMP.
class FakeDecodedDataCreator final {
public:
explicit FakeDecodedDataCreator(const CreateDecoderParams& aParams);
RefPtr<MediaDataDecoder::DecodePromise> Decode(MediaRawData* aSample) {
return mDummyDecoder->Decode(aSample);
}
void Flush() { Unused << mDummyDecoder->Flush(); }

TrackInfo::TrackType Type() const { return mType; }

private:
RefPtr<MediaDataDecoder> mDummyDecoder;
TrackInfo::TrackType mType;
};
RefPtr<TaskQueue> mTaskQueue;
UniquePtr<FakeDecodedDataCreator> mFakeDataCreator;
};

} // namespace mozilla
Expand Down

0 comments on commit e41cb52

Please sign in to comment.