niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/utility/source/video_frames_queue.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #ifdef WEBRTC_MODULE_UTILITY_VIDEO |
| 14 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 15 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 17 | #include "webrtc/common_video/interface/texture_video_frame.h" |
pbos@webrtc.org | 8b06200 | 2013-07-12 08:28:10 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/interface/module_common_types.h" |
| 19 | #include "webrtc/system_wrappers/interface/tick_util.h" |
| 20 | #include "webrtc/system_wrappers/interface/trace.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | VideoFramesQueue::VideoFramesQueue() |
| 24 | : _incomingFrames(), |
| 25 | _renderDelayMs(10) |
| 26 | { |
| 27 | } |
| 28 | |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 29 | VideoFramesQueue::~VideoFramesQueue() { |
| 30 | while (!_incomingFrames.Empty()) { |
| 31 | ListItem* item = _incomingFrames.First(); |
| 32 | if (item) { |
| 33 | I420VideoFrame* ptrFrame = static_cast<I420VideoFrame*>(item->GetItem()); |
| 34 | assert(ptrFrame != NULL); |
| 35 | delete ptrFrame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 37 | _incomingFrames.Erase(item); |
| 38 | } |
| 39 | while (!_emptyFrames.Empty()) { |
| 40 | ListItem* item = _emptyFrames.First(); |
| 41 | if (item) { |
| 42 | I420VideoFrame* ptrFrame = |
| 43 | static_cast<I420VideoFrame*>(item->GetItem()); |
| 44 | assert(ptrFrame != NULL); |
| 45 | delete ptrFrame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 47 | _emptyFrames.Erase(item); |
| 48 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 51 | int32_t VideoFramesQueue::AddFrame(const I420VideoFrame& newFrame) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 52 | if (newFrame.native_handle() != NULL) { |
| 53 | _incomingFrames.PushBack(new TextureVideoFrame( |
| 54 | static_cast<NativeHandle*>(newFrame.native_handle()), |
| 55 | newFrame.width(), |
| 56 | newFrame.height(), |
| 57 | newFrame.timestamp(), |
| 58 | newFrame.render_time_ms())); |
| 59 | return 0; |
| 60 | } |
| 61 | |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 62 | I420VideoFrame* ptrFrameToAdd = NULL; |
| 63 | // Try to re-use a VideoFrame. Only allocate new memory if it is necessary. |
| 64 | if (!_emptyFrames.Empty()) { |
| 65 | ListItem* item = _emptyFrames.First(); |
| 66 | if (item) { |
| 67 | ptrFrameToAdd = static_cast<I420VideoFrame*>(item->GetItem()); |
| 68 | _emptyFrames.Erase(item); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 70 | } |
| 71 | if (!ptrFrameToAdd) { |
| 72 | if (_emptyFrames.GetSize() + _incomingFrames.GetSize() > |
| 73 | KMaxNumberOfFrames) { |
| 74 | WEBRTC_TRACE(kTraceWarning, kTraceVideoRenderer, -1, |
| 75 | "%s: too many frames, limit: %d", __FUNCTION__, |
| 76 | KMaxNumberOfFrames); |
| 77 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 79 | |
| 80 | WEBRTC_TRACE(kTraceMemory, kTraceVideoRenderer, -1, |
| 81 | "%s: allocating buffer %d", __FUNCTION__, |
| 82 | _emptyFrames.GetSize() + _incomingFrames.GetSize()); |
| 83 | |
| 84 | ptrFrameToAdd = new I420VideoFrame(); |
| 85 | if (!ptrFrameToAdd) { |
| 86 | WEBRTC_TRACE(kTraceError, kTraceVideoRenderer, -1, |
| 87 | "%s: could not create new frame for", __FUNCTION__); |
| 88 | return -1; |
| 89 | } |
| 90 | } |
| 91 | ptrFrameToAdd->CopyFrame(newFrame); |
| 92 | _incomingFrames.PushBack(ptrFrameToAdd); |
| 93 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Find the most recent frame that has a VideoFrame::RenderTimeMs() that is |
| 97 | // lower than current time in ms (TickTime::MillisecondTimestamp()). |
| 98 | // Note _incomingFrames is sorted so that the oldest frame is first. |
| 99 | // Recycle all frames that are older than the most recent frame. |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 100 | I420VideoFrame* VideoFramesQueue::FrameToRecord() { |
| 101 | I420VideoFrame* ptrRenderFrame = NULL; |
| 102 | ListItem* item = _incomingFrames.First(); |
| 103 | while(item) { |
| 104 | I420VideoFrame* ptrOldestFrameInList = |
| 105 | static_cast<I420VideoFrame*>(item->GetItem()); |
| 106 | if (ptrOldestFrameInList->render_time_ms() <= |
| 107 | TickTime::MillisecondTimestamp() + _renderDelayMs) { |
| 108 | if (ptrRenderFrame) { |
| 109 | // List is traversed beginning to end. If ptrRenderFrame is not |
| 110 | // NULL it must be the first, and thus oldest, VideoFrame in the |
| 111 | // queue. It can be recycled. |
| 112 | ReturnFrame(ptrRenderFrame); |
| 113 | _incomingFrames.PopFront(); |
| 114 | } |
| 115 | item = _incomingFrames.Next(item); |
| 116 | ptrRenderFrame = ptrOldestFrameInList; |
| 117 | } else { |
| 118 | // All VideoFrames following this one will be even newer. No match |
| 119 | // will be found. |
| 120 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 122 | } |
| 123 | return ptrRenderFrame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | } |
| 125 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 126 | int32_t VideoFramesQueue::ReturnFrame(I420VideoFrame* ptrOldFrame) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 127 | // No need to reuse texture frames because they do not allocate memory. |
| 128 | if (ptrOldFrame->native_handle() == NULL) { |
| 129 | ptrOldFrame->set_timestamp(0); |
| 130 | ptrOldFrame->set_width(0); |
| 131 | ptrOldFrame->set_height(0); |
| 132 | ptrOldFrame->set_render_time_ms(0); |
| 133 | ptrOldFrame->ResetSize(); |
| 134 | _emptyFrames.PushBack(ptrOldFrame); |
| 135 | } else { |
| 136 | delete ptrOldFrame; |
| 137 | } |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 138 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
pbos@webrtc.org | c75102e | 2013-04-09 13:32:55 +0000 | [diff] [blame] | 141 | int32_t VideoFramesQueue::SetRenderDelay(uint32_t renderDelay) { |
mikhal@webrtc.org | c381a84 | 2012-10-30 18:22:02 +0000 | [diff] [blame] | 142 | _renderDelayMs = renderDelay; |
| 143 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | } |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 145 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | #endif // WEBRTC_MODULE_UTILITY_VIDEO |