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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
/*
* Copyright (C) 2023 Igalia, S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if USE(GSTREAMER)
#include "GStreamerTest.h"
#include "Test.h"
#include <WebCore/GStreamerCommon.h>
#include <WebCore/GStreamerElementHarness.h>
#include <wtf/FileSystem.h>
using namespace WebCore;
namespace TestWebKitAPI {
TEST_F(GStreamerTest, harnessBasic)
{
GRefPtr<GstElement> element = gst_element_factory_make("identity", nullptr);
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [](auto&, auto&&) { });
// The identity element has a single source pad. Fetch the corresponding stream.
ASSERT_FALSE(harness->outputStreams().isEmpty());
auto& stream = harness->outputStreams().first();
// Harness has not started processing data yet.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
// Push a sample and expect initial events and an output buffer.
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
gst_buffer_memset(buffer.get(), 0, 2, 64);
GstMappedBuffer mappedInputBuffer(buffer.get(), GST_MAP_READ);
ASSERT_TRUE(mappedInputBuffer);
EXPECT_EQ(mappedInputBuffer.size(), 64);
auto caps = adoptGRef(gst_caps_new_empty_simple("foo"));
auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
EXPECT_TRUE(harness->pushSample(WTFMove(sample)));
auto event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "stream-start");
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "caps");
GstCaps* eventCaps;
gst_event_parse_caps(event.get(), &eventCaps);
ASSERT_TRUE(gst_caps_is_equal(eventCaps, caps.get()));
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "segment");
ASSERT_TRUE(gst_caps_is_equal(stream->outputCaps().get(), caps.get()));
// The harnessed element is identity, so output buffers should be the same as input buffers.
auto outputSample = stream->pullSample();
auto outputBuffer = gst_sample_get_buffer(outputSample.get());
GstMappedBuffer mappedOutputBuffer(outputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedOutputBuffer);
EXPECT_EQ(mappedOutputBuffer.size(), 64);
EXPECT_EQ(memcmp(mappedInputBuffer.data(), mappedOutputBuffer.data(), 64), 0);
// Harness is now empty.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
}
TEST_F(GStreamerTest, harnessManualStart)
{
GRefPtr<GstElement> element = gst_element_factory_make("identity", nullptr);
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [](auto&, auto&&) { });
// The identity element has a single source pad. Fetch the corresponding stream.
ASSERT_FALSE(harness->outputStreams().isEmpty());
auto& stream = harness->outputStreams().first();
// Harness has not started processing data yet.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
// Pushing a buffer before start is not allowed.
EXPECT_FALSE(harness->pushBuffer(adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr))));
// Start the harness and expect initial events.
auto caps = adoptGRef(gst_caps_new_empty_simple("foo"));
harness->start(WTFMove(caps));
auto event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "stream-start");
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "caps");
GstCaps* eventCaps;
gst_event_parse_caps(event.get(), &eventCaps);
const auto* structure = gst_caps_get_structure(eventCaps, 0);
ASSERT_STREQ(gst_structure_get_name(structure), "foo");
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "segment");
// Push a buffer and expect an output buffer.
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
gst_buffer_memset(buffer.get(), 0, 2, 64);
GstMappedBuffer mappedInputBuffer(buffer.get(), GST_MAP_READ);
ASSERT_TRUE(mappedInputBuffer);
EXPECT_EQ(mappedInputBuffer.size(), 64);
EXPECT_TRUE(harness->pushBuffer(GRefPtr<GstBuffer>(buffer.get())));
// The harnessed element is identity, so output buffers should be the same as input buffers.
auto outputSample = stream->pullSample();
auto outputBuffer = gst_sample_get_buffer(outputSample.get());
GstMappedBuffer mappedOutputBuffer(outputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedOutputBuffer);
EXPECT_EQ(mappedOutputBuffer.size(), 64);
EXPECT_EQ(memcmp(mappedInputBuffer.data(), mappedOutputBuffer.data(), 64), 0);
// Harness is now empty.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
}
TEST_F(GStreamerTest, harnessBufferProcessing)
{
GRefPtr<GstElement> element = gst_element_factory_make("identity", nullptr);
unsigned counter = 0;
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [&counter](auto&, auto&& outputSample) mutable {
auto outputBuffer = gst_sample_get_buffer(outputSample.get());
GstMappedBuffer mappedOutputBuffer(outputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedOutputBuffer);
EXPECT_EQ(mappedOutputBuffer.size(), 64);
EXPECT_EQ(mappedOutputBuffer.data()[0], counter);
counter++;
});
// The identity element has a single source pad. Fetch the corresponding stream.
ASSERT_FALSE(harness->outputStreams().isEmpty());
auto& stream = harness->outputStreams().first();
// Harness has not started processing data yet.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
ASSERT_EQ(counter, 0);
// Push a batch of samples and expect they were all processed using the supplied harness callback.
auto caps = adoptGRef(gst_caps_new_empty_simple("foo"));
for (unsigned i = 0; i < 3; i++) {
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
gst_buffer_memset(buffer.get(), 0, i, 64);
auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
EXPECT_TRUE(harness->pushSample(WTFMove(sample)));
}
harness->processOutputSamples();
ASSERT_EQ(counter, 3);
}
TEST_F(GStreamerTest, harnessFlush)
{
GRefPtr<GstElement> element = gst_element_factory_make("identity", nullptr);
unsigned counter = 0;
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [&counter](auto&, auto&& outputSample) mutable {
auto outputBuffer = gst_sample_get_buffer(outputSample.get());
GstMappedBuffer mappedOutputBuffer(outputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedOutputBuffer);
EXPECT_EQ(mappedOutputBuffer.size(), 64);
EXPECT_EQ(mappedOutputBuffer.data()[0], 2);
counter++;
});
// The identity element has a single source pad. Fetch the corresponding stream.
ASSERT_FALSE(harness->outputStreams().isEmpty());
auto& stream = harness->outputStreams().first();
// Harness has not started processing data yet.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
ASSERT_EQ(counter, 0);
// Push a batch of 3 samples, manually process the first two output buffers and flush. The last
// sample should be processed during the flush using the harness callback.
auto caps = adoptGRef(gst_caps_new_empty_simple("foo"));
for (unsigned i = 0; i < 3; i++) {
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
gst_buffer_memset(buffer.get(), 0, i, 64);
auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
EXPECT_TRUE(harness->pushSample(WTFMove(sample)));
}
auto firstOutputSample = stream->pullSample();
auto firstOutputBuffer = gst_sample_get_buffer(firstOutputSample.get());
GstMappedBuffer mappedFirstOutputBuffer(firstOutputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedFirstOutputBuffer);
EXPECT_EQ(mappedFirstOutputBuffer.size(), 64);
EXPECT_EQ(mappedFirstOutputBuffer.data()[0], 0);
auto secondOutputSample = stream->pullSample();
auto secondOutputBuffer = gst_sample_get_buffer(secondOutputSample.get());
GstMappedBuffer mappedSecondOutputBuffer(secondOutputBuffer, GST_MAP_READ);
ASSERT_TRUE(mappedSecondOutputBuffer);
EXPECT_EQ(mappedSecondOutputBuffer.size(), 64);
EXPECT_EQ(mappedSecondOutputBuffer.data()[0], 1);
harness->flush();
ASSERT_EQ(counter, 1);
// Flushed harness is empty.
ASSERT_NULL(stream->pullSample());
ASSERT_NULL(stream->pullEvent());
}
TEST_F(GStreamerTest, harnessParseMP4)
{
GRefPtr<GstElement> element = gst_element_factory_make("parsebin", nullptr);
struct BufferTracker {
uint64_t totalAudioBuffers { 0 };
uint64_t totalVideoBuffers { 0 };
} bufferTracker;
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [&bufferTracker](auto& stream, auto&&) mutable {
auto gstStream = adoptGRef(gst_pad_get_stream(stream.pad().get()));
auto streamType = gst_stream_get_stream_type(gstStream.get());
if (streamType == GST_STREAM_TYPE_AUDIO)
bufferTracker.totalAudioBuffers++;
else if (streamType == GST_STREAM_TYPE_VIDEO)
bufferTracker.totalVideoBuffers++;
else {
ASSERT_NOT_REACHED();
return;
}
});
// Feed the contents of a MP4 file to the harnessed parsebin.
GUniquePtr<char> filePath(g_build_filename(WEBKIT_SRC_DIR, "Tools", "TestWebKitAPI", "Tests", "WebKit", "test.mp4", nullptr));
auto handle = FileSystem::openFile(unsafeSpan(filePath.get()), FileSystem::FileOpenMode::Read);
size_t totalRead = 0;
auto size = FileSystem::fileSize(handle).value_or(0);
auto caps = adoptGRef(gst_caps_new_empty_simple("video/quicktime"));
while (totalRead < size) {
size_t bytesToRead = 1024;
if (totalRead + bytesToRead > size)
bytesToRead = size - totalRead;
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, bytesToRead, nullptr));
{
GstMappedBuffer mappedBuffer(buffer.get(), GST_MAP_WRITE);
FileSystem::readFromFile(handle, mappedBuffer.mutableSpan<uint8_t>());
}
auto sample = adoptGRef(gst_sample_new(buffer.get(), caps.get(), nullptr, nullptr));
EXPECT_TRUE(harness->pushSample(WTFMove(sample)));
totalRead += bytesToRead;
}
// The entire file was processed by parsebin, the output streams should have been created. Check
// the first events on each.
EXPECT_EQ(harness->outputStreams().size(), 2);
for (auto& stream : harness->outputStreams()) {
// Since GStreamer 1.24.9 the stream collection is stored on the pad and not forwarded by parsebin.
// https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/7609
auto event = adoptGRef(gst_pad_get_sticky_event(stream->pad().get(), GST_EVENT_STREAM_COLLECTION, 0));
ASSERT_NOT_NULL(event.get());
GRefPtr<GstStreamCollection> collection;
gst_event_parse_stream_collection(event.get(), &collection.outPtr());
ASSERT_EQ(gst_stream_collection_get_size(collection.get()), 2);
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "stream-start");
event = stream->pullEvent();
ASSERT_NOT_NULL(event.get());
EXPECT_STREQ(GST_EVENT_TYPE_NAME(event.get()), "caps");
GstCaps* eventCaps;
gst_event_parse_caps(event.get(), &eventCaps);
ASSERT_TRUE(gst_caps_is_equal(stream->outputCaps().get(), eventCaps));
}
// We haven't pulled any buffer yet, so our buffer tracker should report empty metrics.
ASSERT_EQ(bufferTracker.totalAudioBuffers, 0);
ASSERT_EQ(bufferTracker.totalVideoBuffers, 0);
// Flush all internal queues, the harness should trigger our buffer tracker now.
harness->flush();
ASSERT_EQ(bufferTracker.totalAudioBuffers, 260);
ASSERT_EQ(bufferTracker.totalVideoBuffers, 182);
}
TEST_F(GStreamerTest, harnessDecodeMP4Video)
{
// Process an MP4 file, expecting non-video streams will be discarded.
GRefPtr<GstElement> element = gst_element_factory_make("decodebin3", nullptr);
// Disable internal buffering in decodebin3.
for (auto child : GstIteratorAdaptor<GstElement>(GUniquePtr<GstIterator>(gst_bin_iterate_recurse(GST_BIN_CAST(element.get()))))) {
if (g_str_has_prefix(GST_OBJECT_NAME(GST_OBJECT_CAST(child)), "multiqueue")) {
g_object_set(child, "max-size-buffers", 1, nullptr);
break;
}
}
struct BufferTracker {
uint64_t totalVideoBuffers { 0 };
} bufferTracker;
auto harness = WebCore::GStreamerElementHarness::create(WTFMove(element), [&bufferTracker](auto& stream, auto&&) mutable {
auto gstStream = adoptGRef(gst_pad_get_stream(stream.pad().get()));
auto streamType = gst_stream_get_stream_type(gstStream.get());
if (streamType == GST_STREAM_TYPE_VIDEO)
bufferTracker.totalVideoBuffers++;
else {
ASSERT_NOT_REACHED();
return;
}
});
auto caps = adoptGRef(gst_caps_new_empty_simple("video/quicktime"));
harness->start(WTFMove(caps));
// Feed the contents of a MP4 file to the harnessed decodebin3, until it is able to figure out
// the stream topology.
GUniquePtr<char> filePath(g_build_filename(WEBKIT_SRC_DIR, "Tools", "TestWebKitAPI", "Tests", "WebKit", "test.mp4", nullptr));
auto handle = FileSystem::openFile(unsafeSpan(filePath.get()), FileSystem::FileOpenMode::Read);
size_t totalRead = 0;
auto size = FileSystem::fileSize(handle).value_or(0);
GRefPtr<GstStreamCollection> collection;
RefPtr<GStreamerElementHarness::Stream> harnessedStream;
while (totalRead < size) {
size_t bytesToRead = 512;
if (totalRead + bytesToRead > size)
bytesToRead = size - totalRead;
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, bytesToRead, nullptr));
{
GstMappedBuffer mappedBuffer(buffer.get(), GST_MAP_WRITE);
FileSystem::readFromFile(handle, mappedBuffer.mutableSpan<uint8_t>());
}
EXPECT_TRUE(harness->pushBuffer(WTFMove(buffer)));
totalRead += bytesToRead;
// Process events, if a stream collection is received, interrupt the buffer feed.
for (auto& stream : harness->outputStreams()) {
while (auto event = stream->pullEvent()) {
if (GST_EVENT_TYPE(event.get()) == GST_EVENT_STREAM_COLLECTION) {
gst_event_parse_stream_collection(event.get(), &collection.outPtr());
harnessedStream = stream;
break;
}
}
if (collection)
break;
}
if (collection)
break;
}
// Process the stream collection, discard all non-video streams.
ASSERT(collection);
unsigned collectionSize = gst_stream_collection_get_size(collection.get());
GList* streams = nullptr;
for (unsigned i = 0; i < collectionSize; i++) {
auto stream = gst_stream_collection_get_stream(collection.get(), i);
auto streamType = gst_stream_get_stream_type(stream);
if (streamType == GST_STREAM_TYPE_VIDEO) {
streams = g_list_append(streams, const_cast<char*>(gst_stream_get_stream_id(stream)));
break;
}
}
// Instruct decodebin3 to reconfigure according to the streams we selected.
if (streams) {
harnessedStream->sendEvent(gst_event_new_select_streams(streams));
g_list_free(streams);
}
// Feed the remaining bytes of the MP4 file to decodebin3.
while (totalRead < size) {
size_t bytesToRead = 512;
if (totalRead + bytesToRead > size)
bytesToRead = size - totalRead;
auto buffer = adoptGRef(gst_buffer_new_allocate(nullptr, bytesToRead, nullptr));
{
GstMappedBuffer mappedBuffer(buffer.get(), GST_MAP_WRITE);
FileSystem::readFromFile(handle, mappedBuffer.mutableSpan<uint8_t>());
}
EXPECT_TRUE(harness->pushBuffer(WTFMove(buffer)));
totalRead += bytesToRead;
}
// The entire file was processed by decodebin3, the output streams should have been created. We
// should only have one output stream at this point.
EXPECT_EQ(harness->outputStreams().size(), 1);
// We haven't pulled any buffer yet, so our buffer tracker should report empty metrics.
ASSERT_EQ(bufferTracker.totalVideoBuffers, 0);
// Flush all internal queues, the harness should trigger our buffer tracker now.
harness->flush();
ASSERT_GT(bufferTracker.totalVideoBuffers, 100);
}
} // namespace TestWebKitAPI
#endif // USE(GSTREAMER)
|