1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/shared_memory.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/shared_impl/media_stream_buffer_manager.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using base::SharedMemory
;
12 using base::SharedMemoryCreateOptions
;
16 scoped_ptr
<SharedMemory
> CreateSharedMemory(int32_t buffer_size
,
17 int32_t number_of_buffers
) {
18 scoped_ptr
<SharedMemory
> shared_memory(new SharedMemory());
19 SharedMemoryCreateOptions options
;
20 options
.size
= buffer_size
* number_of_buffers
;
21 options
.executable
= false;
22 EXPECT_TRUE(shared_memory
->Create(options
));
23 return shared_memory
.Pass();
30 class MockDelegate
: public MediaStreamBufferManager::Delegate
{
32 MockDelegate() : new_buffer_enqueue_counter_(0) {}
33 void OnNewBufferEnqueued() override
{
34 new_buffer_enqueue_counter_
++;
37 int32_t new_buffer_enqueue_counter_
;
40 TEST(MediaStreamBufferManager
, General
) {
42 const int32_t kNumberOfBuffers
= 5;
43 const int32_t kBufferSize
= 128;
44 MockDelegate delegate
;
45 MediaStreamBufferManager
manager(&delegate
);
46 scoped_ptr
<SharedMemory
> shared_memory
=
47 CreateSharedMemory(kBufferSize
, kNumberOfBuffers
);
48 // SetBuffers with enqueue_all_buffers = true;
49 EXPECT_TRUE(manager
.SetBuffers(kNumberOfBuffers
,
54 int8_t* memory
= reinterpret_cast<int8_t*>(manager
.GetBufferPointer(0));
55 EXPECT_NE(static_cast<int8_t*>(NULL
), memory
);
57 EXPECT_EQ(kNumberOfBuffers
, manager
.number_of_buffers());
58 EXPECT_EQ(kBufferSize
, manager
.buffer_size());
60 // Test DequeueBuffer() and GetBufferPointer()
61 for (int32_t i
= 0; i
< kNumberOfBuffers
; ++i
) {
62 EXPECT_EQ(i
, manager
.DequeueBuffer());
63 EXPECT_EQ(reinterpret_cast<MediaStreamBuffer
*>(memory
+ i
* kBufferSize
),
64 manager
.GetBufferPointer(i
));
67 manager
.EnqueueBuffer(0);
68 manager
.EnqueueBuffer(4);
69 manager
.EnqueueBuffer(3);
70 manager
.EnqueueBuffer(1);
71 manager
.EnqueueBuffer(2);
72 EXPECT_EQ(5, delegate
.new_buffer_enqueue_counter_
);
74 EXPECT_EQ(0, manager
.DequeueBuffer());
75 EXPECT_EQ(4, manager
.DequeueBuffer());
76 EXPECT_EQ(3, manager
.DequeueBuffer());
77 EXPECT_EQ(1, manager
.DequeueBuffer());
78 EXPECT_EQ(2, manager
.DequeueBuffer());
79 EXPECT_EQ(PP_ERROR_FAILED
, manager
.DequeueBuffer());
80 EXPECT_EQ(PP_ERROR_FAILED
, manager
.DequeueBuffer());
82 // Returns NULL for invalid index to GetBufferPointer()
83 EXPECT_EQ(NULL
, manager
.GetBufferPointer(-1));
84 EXPECT_EQ(NULL
, manager
.GetBufferPointer(kNumberOfBuffers
));
86 // Test crash for passing invalid index to EnqueueBuffer().
87 // String arguments aren't passed to CHECK() in official builds.
88 #if defined(OFFICIAL_BUILD) && defined(NDEBUG)
89 EXPECT_DEATH(manager
.EnqueueBuffer(-1), "");
90 EXPECT_DEATH(manager
.EnqueueBuffer(kNumberOfBuffers
), "");
92 EXPECT_DEATH(manager
.EnqueueBuffer(-1), ".*Check failed: index >= 0.*");
93 EXPECT_DEATH(manager
.EnqueueBuffer(kNumberOfBuffers
),
94 ".*Check failed: index < number_of_buffers_.*");
99 const int32_t kNumberOfBuffers
= 5;
100 const int32_t kBufferSize
= 128;
101 MockDelegate delegate
;
102 MediaStreamBufferManager
manager(&delegate
);
103 scoped_ptr
<SharedMemory
> shared_memory
=
104 CreateSharedMemory(kBufferSize
, kNumberOfBuffers
);
105 // SetBuffers with enqueue_all_buffers = false;
106 EXPECT_TRUE(manager
.SetBuffers(kNumberOfBuffers
,
108 shared_memory
.Pass(),
111 int8_t* memory
= reinterpret_cast<int8_t*>(manager
.GetBufferPointer(0));
112 EXPECT_NE(static_cast<int8_t*>(NULL
), memory
);
114 EXPECT_EQ(kNumberOfBuffers
, manager
.number_of_buffers());
115 EXPECT_EQ(kBufferSize
, manager
.buffer_size());
117 // Test DequeueBuffer() and GetBufferPointer()
118 for (int32_t i
= 0; i
< kNumberOfBuffers
; ++i
) {
119 EXPECT_EQ(PP_ERROR_FAILED
, manager
.DequeueBuffer());
120 EXPECT_EQ(reinterpret_cast<MediaStreamBuffer
*>(memory
+ i
* kBufferSize
),
121 manager
.GetBufferPointer(i
));
126 TEST(MediaStreamBufferManager
, ResetBuffers
) {
127 const int32_t kNumberOfBuffers1
= 5;
128 const int32_t kBufferSize1
= 128;
129 const int32_t kNumberOfBuffers2
= 8;
130 const int32_t kBufferSize2
= 256;
131 MockDelegate delegate
;
132 MediaStreamBufferManager
manager(&delegate
);
134 scoped_ptr
<SharedMemory
> shared_memory(new SharedMemory());
135 SharedMemoryCreateOptions options
;
136 options
.size
= kBufferSize1
* kNumberOfBuffers1
;
137 options
.executable
= false;
139 EXPECT_TRUE(shared_memory
->Create(options
));
141 // SetBuffers with enqueue_all_buffers = true;
142 EXPECT_TRUE(manager
.SetBuffers(kNumberOfBuffers1
,
144 shared_memory
.Pass(),
147 int8_t* memory
= reinterpret_cast<int8_t*>(manager
.GetBufferPointer(0));
148 EXPECT_NE(static_cast<int8_t*>(NULL
), memory
);
150 EXPECT_EQ(kNumberOfBuffers1
, manager
.number_of_buffers());
151 EXPECT_EQ(kBufferSize1
, manager
.buffer_size());
153 // Test DequeueBuffer() and GetBufferPointer()
154 for (int32_t i
= 0; i
< kNumberOfBuffers1
; ++i
) {
155 EXPECT_EQ(i
, manager
.DequeueBuffer());
156 EXPECT_EQ(reinterpret_cast<MediaStreamBuffer
*>(memory
+ i
* kBufferSize1
),
157 manager
.GetBufferPointer(i
));
162 scoped_ptr
<SharedMemory
> shared_memory
=
163 CreateSharedMemory(kBufferSize2
, kNumberOfBuffers2
);
164 // SetBuffers with enqueue_all_buffers = true;
165 EXPECT_TRUE(manager
.SetBuffers(kNumberOfBuffers2
,
167 shared_memory
.Pass(),
170 int8_t* memory
= reinterpret_cast<int8_t*>(manager
.GetBufferPointer(0));
171 EXPECT_NE(static_cast<int8_t*>(NULL
), memory
);
173 EXPECT_EQ(kNumberOfBuffers2
, manager
.number_of_buffers());
174 EXPECT_EQ(kBufferSize2
, manager
.buffer_size());
176 // Test DequeueBuffer() and GetBufferPointer()
177 for (int32_t i
= 0; i
< kNumberOfBuffers2
; ++i
) {
178 EXPECT_EQ(i
, manager
.DequeueBuffer());
179 EXPECT_EQ(reinterpret_cast<MediaStreamBuffer
*>(memory
+ i
* kBufferSize2
),
180 manager
.GetBufferPointer(i
));