Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / dom / media / fuzz / FuzzMedia.cpp
blobadd6e1448607435db5e752b3c9aa7ee507355db1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ADTSDemuxer.h"
7 #include "Benchmark.h"
8 #include "BufferMediaResource.h"
9 #include "FlacDemuxer.h"
10 #include "FuzzingInterface.h"
11 #include "mozilla/AbstractThread.h"
12 #include "mozilla/SpinEventLoopUntil.h"
13 #include "MP3Demuxer.h"
14 #include "MP4Demuxer.h"
15 #include "OggDemuxer.h"
16 #include "systemservices/MediaUtils.h"
17 #include "WaveDemuxer.h"
18 #include "WebMDemuxer.h"
20 using namespace mozilla;
22 class FuzzRunner {
23 public:
24 explicit FuzzRunner(Benchmark* aBenchmark) : mBenchmark(aBenchmark) {}
26 void Run() {
27 // Assert we're on the main thread, otherwise `done` must be synchronized.
28 MOZ_ASSERT(NS_IsMainThread());
29 bool done = false;
31 mBenchmark->Init();
32 mBenchmark->Run()->Then(
33 // Non DocGroup-version of AbstractThread::MainThread() is fine for
34 // testing.
35 AbstractThread::MainThread(), __func__,
36 [&](uint32_t aDecodeFps) { done = true; }, [&]() { done = true; });
38 // Wait until benchmark completes.
39 SpinEventLoopUntil("FuzzRunner::Run"_ns, [&]() { return done; });
40 return;
43 private:
44 RefPtr<Benchmark> mBenchmark;
47 #define MOZ_MEDIA_FUZZER(_name) \
48 static int FuzzingRunMedia##_name(const uint8_t* data, size_t size) { \
49 if (!size) { \
50 return 0; \
51 } \
52 RefPtr<BufferMediaResource> resource = \
53 new BufferMediaResource(data, size); \
54 FuzzRunner runner(new Benchmark(new _name##Demuxer(resource))); \
55 runner.Run(); \
56 return 0; \
57 } \
58 MOZ_FUZZING_INTERFACE_RAW(nullptr, FuzzingRunMedia##_name, Media##_name);
60 MOZ_MEDIA_FUZZER(ADTS);
61 MOZ_MEDIA_FUZZER(Flac);
62 MOZ_MEDIA_FUZZER(MP3);
63 MOZ_MEDIA_FUZZER(MP4);
64 MOZ_MEDIA_FUZZER(Ogg);
65 MOZ_MEDIA_FUZZER(WAV);
66 MOZ_MEDIA_FUZZER(WebM);