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
|
/*
* Copyright (C) 2018 Igalia, S.L.
* Copyright (C) 2018 Metrological Group B.V.
*
* 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/SharedBuffer.h>
using namespace WebCore;
namespace TestWebKitAPI {
TEST_F(GStreamerTest, mappedBufferBasics)
{
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
GstMappedBuffer mappedBuf(buf, GST_MAP_READ);
ASSERT_TRUE(mappedBuf);
EXPECT_EQ(mappedBuf.size(), 64);
GstMappedBuffer mappedBuf2(buf, GST_MAP_READ);
ASSERT_TRUE(mappedBuf2);
EXPECT_EQ(mappedBuf, mappedBuf2);
EXPECT_EQ(buf.get(), mappedBuf);
EXPECT_EQ(mappedBuf2, buf.get());
}
TEST_F(GStreamerTest, mappedBufferReadSanity)
{
gpointer memory = g_malloc(16);
memset(memory, 'x', 16);
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_wrapped(memory, 16));
GstMappedBuffer mappedBuf(buf, GST_MAP_READ);
ASSERT_TRUE(mappedBuf);
EXPECT_EQ(mappedBuf.size(), 16);
EXPECT_EQ(memcmp(memory, mappedBuf.data(), 16), 0);
}
TEST_F(GStreamerTest, mappedBufferWriteSanity)
{
gpointer memory = g_malloc(16);
memset(memory, 'x', 16);
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_wrapped(memory, 16));
GstMappedBuffer mappedBuf(buf.get(), GST_MAP_WRITE);
ASSERT_TRUE(mappedBuf);
EXPECT_EQ(mappedBuf.size(), 16);
memset(mappedBuf.data(), 'y', mappedBuf.size());
EXPECT_EQ(memcmp(memory, mappedBuf.data(), 16), 0);
}
TEST_F(GStreamerTest, mappedBufferDoesNotAddExtraRefs)
{
// It is important not to ref the passed GStreamer buffers, if we
// do so, then in the case of writable buffers, they can become
// unwritable, even though we are the sole owners. We also don't
// want to take ownership of the buffer from the user-code, since
// for transformInPlace use-cases, that would break.
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new());
ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
GstMappedBuffer mappedBuf(buf, GST_MAP_READWRITE);
ASSERT_TRUE(mappedBuf);
ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
}
TEST_F(GStreamerTest, mappedBufferValidityUnmapEarly)
{
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new());
GstMappedBuffer mappedBuf(buf, GST_MAP_READ);
ASSERT_TRUE(mappedBuf);
mappedBuf.unmapEarly();
ASSERT_FALSE(mappedBuf);
}
TEST_F(GStreamerTest, mappedOwnedBufferReadSanity)
{
gpointer memory = g_malloc(16);
memset(memory, 'x', 16);
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_wrapped(memory, 16));
auto mappedBuf = GstMappedOwnedBuffer::create(buf);
ASSERT_TRUE(mappedBuf);
EXPECT_EQ(mappedBuf->size(), 16);
EXPECT_EQ(memcmp(memory, mappedBuf->data(), 16), 0);
EXPECT_EQ(memcmp(memory, mappedBuf->createSharedBuffer()->data(), 16), 0);
}
TEST_F(GStreamerTest, mappedOwnedBufferCachesSharedBuffers)
{
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
auto mappedBuf = GstMappedOwnedBuffer::create(buf);
ASSERT_TRUE(mappedBuf);
auto sharedBuf = mappedBuf->createSharedBuffer();
// We expect the same data pointer wrapped by shared buffer, no
// copies need to be made.
EXPECT_EQ(sharedBuf->data(), mappedBuf->createSharedBuffer()->data());
}
TEST_F(GStreamerTest, mappedOwnedBufferDoesAddsExtraRefs)
{
// GstMappedOwnedBuffer needs to bump the buffer reference count
// so that it is sure that the buffer outlives the mapped buffer.
GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new());
ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
{
auto mappedBuf = GstMappedOwnedBuffer::create(buf);
ASSERT_TRUE(mappedBuf);
ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 2);
}
ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
}
TEST_F(GStreamerTest, mappedOwnedBufferDestruction)
{
GstBuffer* buffer = gst_buffer_new();
ASSERT_EQ(GST_OBJECT_REFCOUNT(buffer), 1);
auto mappedBuffer = GstMappedOwnedBuffer::create(buffer);
ASSERT_TRUE(mappedBuffer);
ASSERT_EQ(GST_OBJECT_REFCOUNT(buffer), 2);
// After this we should not use the buffer anymore but it should
// be still alive inside the mapped buffer, so we still can check
// the refcount.
gst_buffer_unref(buffer);
ASSERT_EQ(GST_OBJECT_REFCOUNT(buffer), 1);
// Now we are going to check if the buffer really outlives the
// mapped buffer. For that we check if there is no stderr output
// when we unref the mapped buffer. We can't check the ref count
// because it should be 0 and the object should be invalid, what
// could lead to a crash or other bad things.
char capturedStderrBuffer[BUFSIZ];
capturedStderrBuffer[0] = 0;
setbuf(stderr, capturedStderrBuffer);
mappedBuffer = nullptr;
setbuf(stderr, nullptr);
ASSERT_EQ(capturedStderrBuffer[0], 0);
}
} // namespace TestWebKitAPI
#endif // USE(GSTREAMER)
|