1/*
2 * Copyright (C) 2018 Igalia, S.L.
3 * Copyright (C) 2018 Metrological Group B.V.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24 * THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28
29#if USE(GSTREAMER)
30
31#include "GStreamerTest.h"
32#include "Test.h"
33#include <WebCore/GStreamerCommon.h>
34#include <WebCore/SharedBuffer.h>
35
36using namespace WebCore;
37
38namespace TestWebKitAPI {
39
40TEST_F(GStreamerTest, mappedBufferBasics)
41{
42 GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
43 auto mappedBuf = GstMappedBuffer::create(buf.get(), GST_MAP_READ);
44 ASSERT_TRUE(mappedBuf);
45 EXPECT_EQ(mappedBuf->size(), 64);
46
47 auto mappedBuf2 = GstMappedBuffer::create(buf.get(), GST_MAP_READ);
48 ASSERT_TRUE(mappedBuf2);
49 EXPECT_EQ(*mappedBuf, *mappedBuf2);
50 EXPECT_EQ(buf.get(), *mappedBuf);
51 EXPECT_EQ(*mappedBuf2, buf.get());
52}
53
54TEST_F(GStreamerTest, mappedBufferReadSanity)
55{
56 gpointer memory = g_malloc(16);
57 memset(memory, 'x', 16);
58 GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_wrapped(memory, 16));
59 auto mappedBuf = GstMappedBuffer::create(buf.get(), GST_MAP_READ);
60 ASSERT_TRUE(mappedBuf);
61 EXPECT_EQ(mappedBuf->size(), 16);
62 EXPECT_EQ(memcmp(memory, mappedBuf->data(), 16), 0);
63 EXPECT_EQ(memcmp(memory, mappedBuf->createSharedBuffer()->data(), 16), 0);
64}
65
66TEST_F(GStreamerTest, mappedBufferWriteSanity)
67{
68 gpointer memory = g_malloc(16);
69 memset(memory, 'x', 16);
70 GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_wrapped(memory, 16));
71
72 auto mappedBuf = GstMappedBuffer::create(buf.get(), GST_MAP_WRITE);
73 ASSERT_TRUE(mappedBuf);
74 EXPECT_EQ(mappedBuf->size(), 16);
75 memset(mappedBuf->data(), 'y', mappedBuf->size());
76
77 EXPECT_EQ(memcmp(memory, mappedBuf->data(), 16), 0);
78}
79
80TEST_F(GStreamerTest, mappedBufferCachesSharedBuffers)
81{
82 GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new_allocate(nullptr, 64, nullptr));
83 auto mappedBuf = GstMappedBuffer::create(buf.get(), GST_MAP_READ);
84 ASSERT_TRUE(mappedBuf);
85 auto sharedBuf = mappedBuf->createSharedBuffer();
86 // We expect the same data pointer wrapped by shared buffer, no
87 // copies need to be made.
88 EXPECT_EQ(sharedBuf->data(), mappedBuf->createSharedBuffer()->data());
89}
90
91TEST_F(GStreamerTest, mappedBufferDoesNotAddExtraRefs)
92{
93 // It is important not to ref the passed GStreamer buffers, if we
94 // do so, then in the case of writable buffers, they can become
95 // unwritable, even though we are the sole owners. We also don't
96 // want to take ownership of the buffer from the user-code, since
97 // for transformInPlace use-cases, that would break.
98 GRefPtr<GstBuffer> buf = adoptGRef(gst_buffer_new());
99
100 ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
101
102 auto mappedBuf = GstMappedBuffer::create(buf.get(), GST_MAP_READWRITE);
103 ASSERT_TRUE(mappedBuf);
104
105 ASSERT_EQ(GST_OBJECT_REFCOUNT(buf.get()), 1);
106}
107
108} // namespace TestWebKitAPI
109
110#endif // USE(GSTREAMER)
111