| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <math.h> | 11 #include <math.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 | 13 |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "webrtc/base/bind.h" | 15 #include "webrtc/base/bind.h" |
| 16 #include "webrtc/base/scoped_ptr.h" | 16 #include "webrtc/base/scoped_ptr.h" |
| 17 #include "webrtc/test/fake_texture_frame.h" | 17 #include "webrtc/test/fake_texture_frame.h" |
| 18 #include "webrtc/test/frame_utils.h" |
| 18 #include "webrtc/video_frame.h" | 19 #include "webrtc/video_frame.h" |
| 19 | 20 |
| 20 namespace webrtc { | 21 namespace webrtc { |
| 21 | 22 |
| 22 bool EqualPlane(const uint8_t* data1, | |
| 23 const uint8_t* data2, | |
| 24 int stride, | |
| 25 int width, | |
| 26 int height); | |
| 27 int ExpectedSize(int plane_stride, int image_height, PlaneType type); | 23 int ExpectedSize(int plane_stride, int image_height, PlaneType type); |
| 28 | 24 |
| 29 TEST(TestVideoFrame, InitialValues) { | 25 TEST(TestVideoFrame, InitialValues) { |
| 30 VideoFrame frame; | 26 VideoFrame frame; |
| 31 EXPECT_TRUE(frame.IsZeroSize()); | 27 EXPECT_TRUE(frame.IsZeroSize()); |
| 32 EXPECT_EQ(kVideoRotation_0, frame.rotation()); | 28 EXPECT_EQ(kVideoRotation_0, frame.rotation()); |
| 33 } | 29 } |
| 34 | 30 |
| 35 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { | 31 TEST(TestVideoFrame, CopiesInitialFrameWithoutCrashing) { |
| 36 VideoFrame frame; | 32 VideoFrame frame; |
| 37 VideoFrame frame2; | 33 VideoFrame frame2; |
| 38 frame2.CopyFrame(frame); | 34 frame2.CopyFrame(frame); |
| 39 } | 35 } |
| 40 | 36 |
| 41 TEST(TestVideoFrame, WidthHeightValues) { | 37 TEST(TestVideoFrame, WidthHeightValues) { |
| 42 VideoFrame frame; | 38 VideoFrame frame; |
| 43 const int valid_value = 10; | 39 const int valid_value = 10; |
| 44 EXPECT_EQ(0, frame.CreateEmptyFrame(10, 10, 10, 14, 90)); | 40 frame.CreateEmptyFrame(10, 10, 10, 14, 90); |
| 45 EXPECT_EQ(valid_value, frame.width()); | 41 EXPECT_EQ(valid_value, frame.width()); |
| 46 EXPECT_EQ(valid_value, frame.height()); | 42 EXPECT_EQ(valid_value, frame.height()); |
| 47 frame.set_timestamp(123u); | 43 frame.set_timestamp(123u); |
| 48 EXPECT_EQ(123u, frame.timestamp()); | 44 EXPECT_EQ(123u, frame.timestamp()); |
| 49 frame.set_ntp_time_ms(456); | 45 frame.set_ntp_time_ms(456); |
| 50 EXPECT_EQ(456, frame.ntp_time_ms()); | 46 EXPECT_EQ(456, frame.ntp_time_ms()); |
| 51 frame.set_render_time_ms(789); | 47 frame.set_render_time_ms(789); |
| 52 EXPECT_EQ(789, frame.render_time_ms()); | 48 EXPECT_EQ(789, frame.render_time_ms()); |
| 53 } | 49 } |
| 54 | 50 |
| 55 TEST(TestVideoFrame, SizeAllocation) { | 51 TEST(TestVideoFrame, SizeAllocation) { |
| 56 VideoFrame frame; | 52 VideoFrame frame; |
| 57 EXPECT_EQ(0, frame. CreateEmptyFrame(10, 10, 12, 14, 220)); | 53 frame. CreateEmptyFrame(10, 10, 12, 14, 220); |
| 58 int height = frame.height(); | 54 int height = frame.height(); |
| 59 int stride_y = frame.stride(kYPlane); | 55 int stride_y = frame.stride(kYPlane); |
| 60 int stride_u = frame.stride(kUPlane); | 56 int stride_u = frame.stride(kUPlane); |
| 61 int stride_v = frame.stride(kVPlane); | 57 int stride_v = frame.stride(kVPlane); |
| 62 // Verify that allocated size was computed correctly. | 58 // Verify that allocated size was computed correctly. |
| 63 EXPECT_EQ(ExpectedSize(stride_y, height, kYPlane), | 59 EXPECT_EQ(ExpectedSize(stride_y, height, kYPlane), |
| 64 frame.allocated_size(kYPlane)); | 60 frame.allocated_size(kYPlane)); |
| 65 EXPECT_EQ(ExpectedSize(stride_u, height, kUPlane), | 61 EXPECT_EQ(ExpectedSize(stride_u, height, kUPlane), |
| 66 frame.allocated_size(kUPlane)); | 62 frame.allocated_size(kUPlane)); |
| 67 EXPECT_EQ(ExpectedSize(stride_v, height, kVPlane), | 63 EXPECT_EQ(ExpectedSize(stride_v, height, kVPlane), |
| 68 frame.allocated_size(kVPlane)); | 64 frame.allocated_size(kVPlane)); |
| 69 } | 65 } |
| 70 | 66 |
| 71 TEST(TestVideoFrame, CopyFrame) { | 67 TEST(TestVideoFrame, CopyFrame) { |
| 72 uint32_t timestamp = 1; | 68 uint32_t timestamp = 1; |
| 73 int64_t ntp_time_ms = 2; | 69 int64_t ntp_time_ms = 2; |
| 74 int64_t render_time_ms = 3; | 70 int64_t render_time_ms = 3; |
| 75 int stride_y = 15; | 71 int stride_y = 15; |
| 76 int stride_u = 10; | 72 int stride_u = 10; |
| 77 int stride_v = 10; | 73 int stride_v = 10; |
| 78 int width = 15; | 74 int width = 15; |
| 79 int height = 15; | 75 int height = 15; |
| 80 // Copy frame. | 76 // Copy frame. |
| 81 VideoFrame small_frame; | 77 VideoFrame small_frame; |
| 82 EXPECT_EQ(0, small_frame.CreateEmptyFrame(width, height, | 78 small_frame.CreateEmptyFrame(width, height, |
| 83 stride_y, stride_u, stride_v)); | 79 stride_y, stride_u, stride_v); |
| 84 small_frame.set_timestamp(timestamp); | 80 small_frame.set_timestamp(timestamp); |
| 85 small_frame.set_ntp_time_ms(ntp_time_ms); | 81 small_frame.set_ntp_time_ms(ntp_time_ms); |
| 86 small_frame.set_render_time_ms(render_time_ms); | 82 small_frame.set_render_time_ms(render_time_ms); |
| 87 const int kSizeY = 400; | 83 const int kSizeY = 400; |
| 88 const int kSizeU = 100; | 84 const int kSizeU = 100; |
| 89 const int kSizeV = 100; | 85 const int kSizeV = 100; |
| 90 const VideoRotation kRotation = kVideoRotation_270; | 86 const VideoRotation kRotation = kVideoRotation_270; |
| 91 uint8_t buffer_y[kSizeY]; | 87 uint8_t buffer_y[kSizeY]; |
| 92 uint8_t buffer_u[kSizeU]; | 88 uint8_t buffer_u[kSizeU]; |
| 93 uint8_t buffer_v[kSizeV]; | 89 uint8_t buffer_v[kSizeV]; |
| 94 memset(buffer_y, 16, kSizeY); | 90 memset(buffer_y, 16, kSizeY); |
| 95 memset(buffer_u, 8, kSizeU); | 91 memset(buffer_u, 8, kSizeU); |
| 96 memset(buffer_v, 4, kSizeV); | 92 memset(buffer_v, 4, kSizeV); |
| 97 VideoFrame big_frame; | 93 VideoFrame big_frame; |
| 98 EXPECT_EQ(0, | 94 big_frame.CreateFrame(buffer_y, buffer_u, buffer_v, |
| 99 big_frame.CreateFrame(buffer_y, buffer_u, buffer_v, | 95 width + 5, height + 5, stride_y + 5, |
| 100 width + 5, height + 5, stride_y + 5, | 96 stride_u, stride_v, kRotation); |
| 101 stride_u, stride_v, kRotation)); | |
| 102 // Frame of smaller dimensions. | 97 // Frame of smaller dimensions. |
| 103 EXPECT_EQ(0, small_frame.CopyFrame(big_frame)); | 98 small_frame.CopyFrame(big_frame); |
| 104 EXPECT_TRUE(small_frame.EqualsFrame(big_frame)); | 99 EXPECT_TRUE(test::FramesEqual(small_frame, big_frame)); |
| 105 EXPECT_EQ(kRotation, small_frame.rotation()); | 100 EXPECT_EQ(kRotation, small_frame.rotation()); |
| 106 | 101 |
| 107 // Frame of larger dimensions. | 102 // Frame of larger dimensions. |
| 108 EXPECT_EQ(0, small_frame.CreateEmptyFrame(width, height, | 103 small_frame.CreateEmptyFrame(width, height, |
| 109 stride_y, stride_u, stride_v)); | 104 stride_y, stride_u, stride_v); |
| 110 memset(small_frame.buffer(kYPlane), 1, small_frame.allocated_size(kYPlane)); | 105 memset(small_frame.buffer(kYPlane), 1, small_frame.allocated_size(kYPlane)); |
| 111 memset(small_frame.buffer(kUPlane), 2, small_frame.allocated_size(kUPlane)); | 106 memset(small_frame.buffer(kUPlane), 2, small_frame.allocated_size(kUPlane)); |
| 112 memset(small_frame.buffer(kVPlane), 3, small_frame.allocated_size(kVPlane)); | 107 memset(small_frame.buffer(kVPlane), 3, small_frame.allocated_size(kVPlane)); |
| 113 EXPECT_EQ(0, big_frame.CopyFrame(small_frame)); | 108 big_frame.CopyFrame(small_frame); |
| 114 EXPECT_TRUE(small_frame.EqualsFrame(big_frame)); | 109 EXPECT_TRUE(test::FramesEqual(small_frame, big_frame)); |
| 115 } | 110 } |
| 116 | 111 |
| 117 TEST(TestVideoFrame, ShallowCopy) { | 112 TEST(TestVideoFrame, ShallowCopy) { |
| 118 uint32_t timestamp = 1; | 113 uint32_t timestamp = 1; |
| 119 int64_t ntp_time_ms = 2; | 114 int64_t ntp_time_ms = 2; |
| 120 int64_t render_time_ms = 3; | 115 int64_t render_time_ms = 3; |
| 121 int stride_y = 15; | 116 int stride_y = 15; |
| 122 int stride_u = 10; | 117 int stride_u = 10; |
| 123 int stride_v = 10; | 118 int stride_v = 10; |
| 124 int width = 15; | 119 int width = 15; |
| 125 int height = 15; | 120 int height = 15; |
| 126 | 121 |
| 127 const int kSizeY = 400; | 122 const int kSizeY = 400; |
| 128 const int kSizeU = 100; | 123 const int kSizeU = 100; |
| 129 const int kSizeV = 100; | 124 const int kSizeV = 100; |
| 130 const VideoRotation kRotation = kVideoRotation_270; | 125 const VideoRotation kRotation = kVideoRotation_270; |
| 131 uint8_t buffer_y[kSizeY]; | 126 uint8_t buffer_y[kSizeY]; |
| 132 uint8_t buffer_u[kSizeU]; | 127 uint8_t buffer_u[kSizeU]; |
| 133 uint8_t buffer_v[kSizeV]; | 128 uint8_t buffer_v[kSizeV]; |
| 134 memset(buffer_y, 16, kSizeY); | 129 memset(buffer_y, 16, kSizeY); |
| 135 memset(buffer_u, 8, kSizeU); | 130 memset(buffer_u, 8, kSizeU); |
| 136 memset(buffer_v, 4, kSizeV); | 131 memset(buffer_v, 4, kSizeV); |
| 137 VideoFrame frame1; | 132 VideoFrame frame1; |
| 138 EXPECT_EQ(0, frame1.CreateFrame(buffer_y, buffer_u, buffer_v, width, height, | 133 frame1.CreateFrame(buffer_y, buffer_u, buffer_v, width, height, |
| 139 stride_y, stride_u, stride_v, kRotation)); | 134 stride_y, stride_u, stride_v, kRotation); |
| 140 frame1.set_timestamp(timestamp); | 135 frame1.set_timestamp(timestamp); |
| 141 frame1.set_ntp_time_ms(ntp_time_ms); | 136 frame1.set_ntp_time_ms(ntp_time_ms); |
| 142 frame1.set_render_time_ms(render_time_ms); | 137 frame1.set_render_time_ms(render_time_ms); |
| 143 VideoFrame frame2; | 138 VideoFrame frame2; |
| 144 frame2.ShallowCopy(frame1); | 139 frame2.ShallowCopy(frame1); |
| 145 | 140 |
| 146 // To be able to access the buffers, we need const pointers to the frames. | 141 // To be able to access the buffers, we need const pointers to the frames. |
| 147 const VideoFrame* const_frame1_ptr = &frame1; | 142 const VideoFrame* const_frame1_ptr = &frame1; |
| 148 const VideoFrame* const_frame2_ptr = &frame2; | 143 const VideoFrame* const_frame2_ptr = &frame2; |
| 149 | 144 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 165 frame2.set_rotation(kVideoRotation_90); | 160 frame2.set_rotation(kVideoRotation_90); |
| 166 | 161 |
| 167 EXPECT_NE(frame2.timestamp(), frame1.timestamp()); | 162 EXPECT_NE(frame2.timestamp(), frame1.timestamp()); |
| 168 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms()); | 163 EXPECT_NE(frame2.ntp_time_ms(), frame1.ntp_time_ms()); |
| 169 EXPECT_NE(frame2.render_time_ms(), frame1.render_time_ms()); | 164 EXPECT_NE(frame2.render_time_ms(), frame1.render_time_ms()); |
| 170 EXPECT_NE(frame2.rotation(), frame1.rotation()); | 165 EXPECT_NE(frame2.rotation(), frame1.rotation()); |
| 171 } | 166 } |
| 172 | 167 |
| 173 TEST(TestVideoFrame, Reset) { | 168 TEST(TestVideoFrame, Reset) { |
| 174 VideoFrame frame; | 169 VideoFrame frame; |
| 175 ASSERT_EQ(frame.CreateEmptyFrame(5, 5, 5, 5, 5), 0); | 170 frame.CreateEmptyFrame(5, 5, 5, 5, 5); |
| 176 frame.set_ntp_time_ms(1); | 171 frame.set_ntp_time_ms(1); |
| 177 frame.set_timestamp(2); | 172 frame.set_timestamp(2); |
| 178 frame.set_render_time_ms(3); | 173 frame.set_render_time_ms(3); |
| 179 ASSERT_TRUE(frame.video_frame_buffer() != NULL); | 174 ASSERT_TRUE(frame.video_frame_buffer() != NULL); |
| 180 | 175 |
| 181 frame.Reset(); | 176 frame.Reset(); |
| 182 EXPECT_EQ(0u, frame.ntp_time_ms()); | 177 EXPECT_EQ(0u, frame.ntp_time_ms()); |
| 183 EXPECT_EQ(0u, frame.render_time_ms()); | 178 EXPECT_EQ(0u, frame.render_time_ms()); |
| 184 EXPECT_EQ(0u, frame.timestamp()); | 179 EXPECT_EQ(0u, frame.timestamp()); |
| 185 EXPECT_TRUE(frame.video_frame_buffer() == NULL); | 180 EXPECT_TRUE(frame.video_frame_buffer() == NULL); |
| 186 } | 181 } |
| 187 | 182 |
| 188 TEST(TestVideoFrame, CopyBuffer) { | 183 TEST(TestVideoFrame, CopyBuffer) { |
| 189 VideoFrame frame1, frame2; | 184 VideoFrame frame1, frame2; |
| 190 int width = 15; | 185 int width = 15; |
| 191 int height = 15; | 186 int height = 15; |
| 192 int stride_y = 15; | 187 int stride_y = 15; |
| 193 int stride_uv = 10; | 188 int stride_uv = 10; |
| 194 const int kSizeY = 225; | 189 const int kSizeY = 225; |
| 195 const int kSizeUv = 80; | 190 const int kSizeUv = 80; |
| 196 EXPECT_EQ(0, frame2.CreateEmptyFrame(width, height, | 191 frame2.CreateEmptyFrame(width, height, |
| 197 stride_y, stride_uv, stride_uv)); | 192 stride_y, stride_uv, stride_uv); |
| 198 uint8_t buffer_y[kSizeY]; | 193 uint8_t buffer_y[kSizeY]; |
| 199 uint8_t buffer_u[kSizeUv]; | 194 uint8_t buffer_u[kSizeUv]; |
| 200 uint8_t buffer_v[kSizeUv]; | 195 uint8_t buffer_v[kSizeUv]; |
| 201 memset(buffer_y, 16, kSizeY); | 196 memset(buffer_y, 16, kSizeY); |
| 202 memset(buffer_u, 8, kSizeUv); | 197 memset(buffer_u, 8, kSizeUv); |
| 203 memset(buffer_v, 4, kSizeUv); | 198 memset(buffer_v, 4, kSizeUv); |
| 204 frame2.CreateFrame(buffer_y, buffer_u, buffer_v, | 199 frame2.CreateFrame(buffer_y, buffer_u, buffer_v, |
| 205 width, height, stride_y, stride_uv, stride_uv); | 200 width, height, stride_y, stride_uv, stride_uv, |
| 201 kVideoRotation_0); |
| 206 // Expect exactly the same pixel data. | 202 // Expect exactly the same pixel data. |
| 207 EXPECT_TRUE(EqualPlane(buffer_y, frame2.buffer(kYPlane), stride_y, 15, 15)); | 203 EXPECT_TRUE( |
| 208 EXPECT_TRUE(EqualPlane(buffer_u, frame2.buffer(kUPlane), stride_uv, 8, 8)); | 204 test::EqualPlane(buffer_y, frame2.buffer(kYPlane), stride_y, 15, 15)); |
| 209 EXPECT_TRUE(EqualPlane(buffer_v, frame2.buffer(kVPlane), stride_uv, 8, 8)); | 205 EXPECT_TRUE( |
| 206 test::EqualPlane(buffer_u, frame2.buffer(kUPlane), stride_uv, 8, 8)); |
| 207 EXPECT_TRUE( |
| 208 test::EqualPlane(buffer_v, frame2.buffer(kVPlane), stride_uv, 8, 8)); |
| 210 | 209 |
| 211 // Compare size. | 210 // Compare size. |
| 212 EXPECT_LE(kSizeY, frame2.allocated_size(kYPlane)); | 211 EXPECT_LE(kSizeY, frame2.allocated_size(kYPlane)); |
| 213 EXPECT_LE(kSizeUv, frame2.allocated_size(kUPlane)); | 212 EXPECT_LE(kSizeUv, frame2.allocated_size(kUPlane)); |
| 214 EXPECT_LE(kSizeUv, frame2.allocated_size(kVPlane)); | 213 EXPECT_LE(kSizeUv, frame2.allocated_size(kVPlane)); |
| 215 } | 214 } |
| 216 | 215 |
| 217 TEST(TestVideoFrame, ReuseAllocation) { | 216 TEST(TestVideoFrame, ReuseAllocation) { |
| 218 VideoFrame frame; | 217 VideoFrame frame; |
| 219 frame.CreateEmptyFrame(640, 320, 640, 320, 320); | 218 frame.CreateEmptyFrame(640, 320, 640, 320, 320); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 250 EXPECT_EQ(10, frame.render_time_ms()); | 249 EXPECT_EQ(10, frame.render_time_ms()); |
| 251 EXPECT_EQ(handle, frame.native_handle()); | 250 EXPECT_EQ(handle, frame.native_handle()); |
| 252 | 251 |
| 253 frame.set_timestamp(200); | 252 frame.set_timestamp(200); |
| 254 EXPECT_EQ(200u, frame.timestamp()); | 253 EXPECT_EQ(200u, frame.timestamp()); |
| 255 frame.set_render_time_ms(20); | 254 frame.set_render_time_ms(20); |
| 256 EXPECT_EQ(20, frame.render_time_ms()); | 255 EXPECT_EQ(20, frame.render_time_ms()); |
| 257 } | 256 } |
| 258 | 257 |
| 259 } // namespace webrtc | 258 } // namespace webrtc |
| OLD | NEW |