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