| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Unit test for VideoCaptureBufferPool. | 5 // Unit test for VideoCaptureBufferPool. |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 7 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
| 8 | 8 |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 scoped_refptr<media::VideoFrame> frame1 = pool->ReserveForProducer(0); | 28 scoped_refptr<media::VideoFrame> frame1 = pool->ReserveForProducer(0); |
| 29 ASSERT_TRUE(NULL != frame1.get()); | 29 ASSERT_TRUE(NULL != frame1.get()); |
| 30 ASSERT_EQ(size, frame1->coded_size()); | 30 ASSERT_EQ(size, frame1->coded_size()); |
| 31 scoped_refptr<media::VideoFrame> frame2 = pool->ReserveForProducer(0); | 31 scoped_refptr<media::VideoFrame> frame2 = pool->ReserveForProducer(0); |
| 32 ASSERT_TRUE(NULL != frame2.get()); | 32 ASSERT_TRUE(NULL != frame2.get()); |
| 33 ASSERT_EQ(size, frame2->coded_size()); | 33 ASSERT_EQ(size, frame2->coded_size()); |
| 34 scoped_refptr<media::VideoFrame> frame3 = pool->ReserveForProducer(0); | 34 scoped_refptr<media::VideoFrame> frame3 = pool->ReserveForProducer(0); |
| 35 ASSERT_TRUE(NULL != frame3.get()); | 35 ASSERT_TRUE(NULL != frame3.get()); |
| 36 | 36 |
| 37 // Touch the memory. | 37 // Touch the memory. |
| 38 media::FillYUV(frame1, 0x11, 0x22, 0x33); | 38 media::FillYUV(frame1.get(), 0x11, 0x22, 0x33); |
| 39 media::FillYUV(frame2, 0x44, 0x55, 0x66); | 39 media::FillYUV(frame2.get(), 0x44, 0x55, 0x66); |
| 40 media::FillYUV(frame3, 0x77, 0x88, 0x99); | 40 media::FillYUV(frame3.get(), 0x77, 0x88, 0x99); |
| 41 | 41 |
| 42 // Fourth frame should fail. | 42 // Fourth frame should fail. |
| 43 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 43 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 44 | 44 |
| 45 // Release 1st frame and retry; this should succeed. | 45 // Release 1st frame and retry; this should succeed. |
| 46 frame1 = NULL; | 46 frame1 = NULL; |
| 47 scoped_refptr<media::VideoFrame> frame4 = pool->ReserveForProducer(0); | 47 scoped_refptr<media::VideoFrame> frame4 = pool->ReserveForProducer(0); |
| 48 ASSERT_TRUE(NULL != frame4.get()); | 48 ASSERT_TRUE(NULL != frame4.get()); |
| 49 | 49 |
| 50 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 50 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 80 | 80 |
| 81 // First consumer finishes. | 81 // First consumer finishes. |
| 82 pool->RelinquishConsumerHold(buffer_id3, 1); | 82 pool->RelinquishConsumerHold(buffer_id3, 1); |
| 83 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 83 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 84 ASSERT_TRUE(pool->IsAnyBufferHeldForConsumers()); | 84 ASSERT_TRUE(pool->IsAnyBufferHeldForConsumers()); |
| 85 | 85 |
| 86 // Second consumer finishes. This should free that frame. | 86 // Second consumer finishes. This should free that frame. |
| 87 pool->RelinquishConsumerHold(buffer_id3, 1); | 87 pool->RelinquishConsumerHold(buffer_id3, 1); |
| 88 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); | 88 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); |
| 89 frame3 = pool->ReserveForProducer(0); | 89 frame3 = pool->ReserveForProducer(0); |
| 90 ASSERT_TRUE(NULL != frame3); | 90 ASSERT_TRUE(NULL != frame3.get()); |
| 91 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); | 91 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); |
| 92 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 92 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 93 | 93 |
| 94 // Now deliver & consume frame1, but don't release the VideoFrame. | 94 // Now deliver & consume frame1, but don't release the VideoFrame. |
| 95 int buffer_id1 = pool->RecognizeReservedBuffer(frame1); | 95 int buffer_id1 = pool->RecognizeReservedBuffer(frame1); |
| 96 ASSERT_NE(0, buffer_id1); | 96 ASSERT_NE(0, buffer_id1); |
| 97 pool->HoldForConsumers(frame1, buffer_id1, 5); | 97 pool->HoldForConsumers(frame1, buffer_id1, 5); |
| 98 ASSERT_TRUE(pool->IsAnyBufferHeldForConsumers()); | 98 ASSERT_TRUE(pool->IsAnyBufferHeldForConsumers()); |
| 99 pool->RelinquishConsumerHold(buffer_id1, 5); | 99 pool->RelinquishConsumerHold(buffer_id1, 5); |
| 100 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); | 100 ASSERT_FALSE(pool->IsAnyBufferHeldForConsumers()); |
| 101 | 101 |
| 102 // Even though the consumer is done with the buffer at |buffer_id1|, it cannot | 102 // Even though the consumer is done with the buffer at |buffer_id1|, it cannot |
| 103 // be re-allocated to the producer, because |frame1| still references it. But | 103 // be re-allocated to the producer, because |frame1| still references it. But |
| 104 // when |frame1| goes away, we should be able to re-reserve the buffer (and | 104 // when |frame1| goes away, we should be able to re-reserve the buffer (and |
| 105 // the ID ought to be the same). | 105 // the ID ought to be the same). |
| 106 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 106 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 107 frame1 = NULL; // Should free the frame. | 107 frame1 = NULL; // Should free the frame. |
| 108 frame2 = pool->ReserveForProducer(0); | 108 frame2 = pool->ReserveForProducer(0); |
| 109 ASSERT_TRUE(NULL != frame2); | 109 ASSERT_TRUE(NULL != frame2.get()); |
| 110 ASSERT_EQ(buffer_id1, pool->RecognizeReservedBuffer(frame2)); | 110 ASSERT_EQ(buffer_id1, pool->RecognizeReservedBuffer(frame2)); |
| 111 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 111 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 112 | 112 |
| 113 // For good measure, do one more cycle of free/realloc without delivery, now | 113 // For good measure, do one more cycle of free/realloc without delivery, now |
| 114 // that this buffer has been through the consumer-hold cycle. | 114 // that this buffer has been through the consumer-hold cycle. |
| 115 frame2 = NULL; | 115 frame2 = NULL; |
| 116 frame1 = pool->ReserveForProducer(0); | 116 frame1 = pool->ReserveForProducer(0); |
| 117 ASSERT_TRUE(NULL != frame1); | 117 ASSERT_TRUE(NULL != frame1.get()); |
| 118 ASSERT_EQ(buffer_id1, pool->RecognizeReservedBuffer(frame1)); | 118 ASSERT_EQ(buffer_id1, pool->RecognizeReservedBuffer(frame1)); |
| 119 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; | 119 ASSERT_EQ(NULL, pool->ReserveForProducer(0).get()) << "Pool should be empty"; |
| 120 | 120 |
| 121 // Tear down the pool, writing into the frames. The VideoFrame should | 121 // Tear down the pool, writing into the frames. The VideoFrame should |
| 122 // preserve the lifetime of the underlying memory. | 122 // preserve the lifetime of the underlying memory. |
| 123 frame3 = NULL; | 123 frame3 = NULL; |
| 124 pool = NULL; | 124 pool = NULL; |
| 125 | 125 |
| 126 // Touch the memory. | 126 // Touch the memory. |
| 127 media::FillYUV(frame1, 0x11, 0x22, 0x33); | 127 media::FillYUV(frame1.get(), 0x11, 0x22, 0x33); |
| 128 media::FillYUV(frame4, 0x44, 0x55, 0x66); | 128 media::FillYUV(frame4.get(), 0x44, 0x55, 0x66); |
| 129 | 129 |
| 130 frame1 = NULL; | 130 frame1 = NULL; |
| 131 | 131 |
| 132 media::FillYUV(frame4, 0x44, 0x55, 0x66); | 132 media::FillYUV(frame4.get(), 0x44, 0x55, 0x66); |
| 133 frame4 = NULL; | 133 frame4 = NULL; |
| 134 } | 134 } |
| 135 | 135 |
| 136 } // namespace content | 136 } // namespace content |
| OLD | NEW |