diff --git a/dom/media/gtest/WaitFor.h b/dom/media/gtest/WaitFor.h index 96ecf3fa1f9e..09a775f759a4 100644 --- a/dom/media/gtest/WaitFor.h +++ b/dom/media/gtest/WaitFor.h @@ -6,9 +6,10 @@ #define WAITFOR_H_ #include "MediaEventSource.h" +#include "MediaUtils.h" #include "mozilla/Maybe.h" #include "mozilla/MozPromise.h" -#include "mozilla/Result.h" +#include "mozilla/ResultVariant.h" #include "mozilla/SpinEventLoopUntil.h" namespace mozilla { @@ -81,6 +82,36 @@ void WaitUntil(MediaEventSource& aEvent, const CallbackFunction& aF) { listener.Disconnect(); } +template +using TakeNPromise = MozPromise>, bool, true>; + +template +auto TakeN(MediaEventSourceImpl& aEvent, size_t aN) + -> RefPtr> { + using Storage = std::vector>; + using Promise = TakeNPromise; + using Values = media::Refcountable; + using Listener = media::Refcountable; + RefPtr values = MakeRefPtr(); + values->reserve(aN); + RefPtr listener = MakeRefPtr(); + auto promise = InvokeAsync( + AbstractThread::GetCurrent(), __func__, [values, aN]() mutable { + SpinEventLoopUntil( + "TakeN(MediaEventSourceImpl& aEvent, size_t aN)"_ns, + [&] { return values->size() == aN; }); + return Promise::CreateAndResolve(std::move(*values), __func__); + }); + *listener = aEvent.Connect(AbstractThread::GetCurrent(), + [values, listener, aN](Args... aValue) { + values->push_back({aValue...}); + if (values->size() == aN) { + listener->Disconnect(); + } + }); + return promise; +} + /** * Helper that, given that canonicals have just been updated on the current * thread, will block its execution until mirrors and their watchers have diff --git a/dom/media/gtest/moz.build b/dom/media/gtest/moz.build index 708b7d7704fd..d3798d85f082 100644 --- a/dom/media/gtest/moz.build +++ b/dom/media/gtest/moz.build @@ -10,6 +10,7 @@ DEFINES["ENABLE_SET_CUBEB_BACKEND"] = True LOCAL_INCLUDES += [ "/dom/media/mediasink", + "/dom/media/systemservices", "/dom/media/webrtc/common/", "/third_party/libwebrtc", "/third_party/libwebrtc/third_party/abseil-cpp", diff --git a/media/webrtc/signaling/gtest/moz.build b/media/webrtc/signaling/gtest/moz.build index 735e58ddb481..6592e9cd66cc 100644 --- a/media/webrtc/signaling/gtest/moz.build +++ b/media/webrtc/signaling/gtest/moz.build @@ -22,6 +22,7 @@ if ( LOCAL_INCLUDES += [ "/dom/media", "/dom/media/gtest", + "/dom/media/systemservices", "/dom/media/webrtc", "/dom/media/webrtc/common", "/dom/media/webrtc/common/time_profiling",