blob: d3d37bec2d0401be7420a89dfa238a7eb2e4fd4e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.org8b062002013-07-12 08:28:10 +000011#include "webrtc/modules/utility/source/video_frames_queue.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#ifdef WEBRTC_MODULE_UTILITY_VIDEO
14
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
wu@webrtc.org9dba5252013-08-05 20:36:57 +000017#include "webrtc/common_video/interface/texture_video_frame.h"
pbos@webrtc.org8b062002013-07-12 08:28:10 +000018#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.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
23VideoFramesQueue::VideoFramesQueue()
24 : _incomingFrames(),
25 _renderDelayMs(10)
26{
27}
28
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +000029VideoFramesQueue::~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.com470e71d2011-07-07 08:21:25 +000036 }
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +000037 _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.com470e71d2011-07-07 08:21:25 +000046 }
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +000047 _emptyFrames.Erase(item);
48 }
niklase@google.com470e71d2011-07-07 08:21:25 +000049}
50
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000051int32_t VideoFramesQueue::AddFrame(const I420VideoFrame& newFrame) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +000052 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.orgc381a842012-10-30 18:22:02 +000062 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.com470e71d2011-07-07 08:21:25 +000069 }
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +000070 }
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.com470e71d2011-07-07 08:21:25 +000078 }
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +000079
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.com470e71d2011-07-07 08:21:25 +000094}
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.orgc381a842012-10-30 18:22:02 +0000100I420VideoFrame* 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.com470e71d2011-07-07 08:21:25 +0000121 }
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +0000122 }
123 return ptrRenderFrame;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000126int32_t VideoFramesQueue::ReturnFrame(I420VideoFrame* ptrOldFrame) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000127 // 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.orgc381a842012-10-30 18:22:02 +0000138 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139}
140
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000141int32_t VideoFramesQueue::SetRenderDelay(uint32_t renderDelay) {
mikhal@webrtc.orgc381a842012-10-30 18:22:02 +0000142 _renderDelayMs = renderDelay;
143 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000145} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000146#endif // WEBRTC_MODULE_UTILITY_VIDEO