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 "webrtc/base/logging.h" | 11 #include "webrtc/base/logging.h" |
12 #include "webrtc/base/trace_event.h" | 12 #include "webrtc/base/trace_event.h" |
13 #include "webrtc/modules/video_coding/include/video_coding.h" | 13 #include "webrtc/modules/video_coding/include/video_coding.h" |
14 #include "webrtc/modules/video_coding/generic_decoder.h" | 14 #include "webrtc/modules/video_coding/generic_decoder.h" |
15 #include "webrtc/modules/video_coding/internal_defines.h" | 15 #include "webrtc/modules/video_coding/internal_defines.h" |
16 #include "webrtc/system_wrappers/include/clock.h" | 16 #include "webrtc/system_wrappers/include/clock.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming& timing, | 20 VCMDecodedFrameCallback::VCMDecodedFrameCallback(VCMTiming* timing, |
21 Clock* clock) | 21 Clock* clock) |
22 : _critSect(CriticalSectionWrapper::CreateCriticalSection()), | 22 : _critSect(CriticalSectionWrapper::CreateCriticalSection()), |
23 _clock(clock), | 23 _clock(clock), |
24 _receiveCallback(NULL), | 24 _receiveCallback(NULL), |
25 _timing(timing), | 25 _timing(timing), |
26 _timestampMap(kDecoderFrameMemoryLength), | 26 _timestampMap(kDecoderFrameMemoryLength), |
27 _lastReceivedPictureID(0) {} | 27 _lastReceivedPictureID(0) {} |
28 | 28 |
29 VCMDecodedFrameCallback::~VCMDecodedFrameCallback() | 29 VCMDecodedFrameCallback::~VCMDecodedFrameCallback() { |
30 { | 30 delete _critSect; |
31 delete _critSect; | |
32 } | 31 } |
33 | 32 |
34 void VCMDecodedFrameCallback::SetUserReceiveCallback( | 33 void VCMDecodedFrameCallback::SetUserReceiveCallback( |
35 VCMReceiveCallback* receiveCallback) | 34 VCMReceiveCallback* receiveCallback) { |
36 { | 35 CriticalSectionScoped cs(_critSect); |
37 CriticalSectionScoped cs(_critSect); | 36 _receiveCallback = receiveCallback; |
38 _receiveCallback = receiveCallback; | |
39 } | 37 } |
40 | 38 |
41 VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() | 39 VCMReceiveCallback* VCMDecodedFrameCallback::UserReceiveCallback() { |
42 { | 40 CriticalSectionScoped cs(_critSect); |
43 CriticalSectionScoped cs(_critSect); | 41 return _receiveCallback; |
44 return _receiveCallback; | |
45 } | 42 } |
46 | 43 |
47 int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { | 44 int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) { |
48 return Decoded(decodedImage, -1); | 45 return Decoded(decodedImage, -1); |
49 } | 46 } |
50 | 47 |
51 int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, | 48 int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage, |
52 int64_t decode_time_ms) { | 49 int64_t decode_time_ms) { |
53 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", | 50 TRACE_EVENT_INSTANT1("webrtc", "VCMDecodedFrameCallback::Decoded", |
54 "timestamp", decodedImage.timestamp()); | 51 "timestamp", decodedImage.timestamp()); |
55 // TODO(holmer): We should improve this so that we can handle multiple | 52 // TODO(holmer): We should improve this so that we can handle multiple |
56 // callbacks from one call to Decode(). | 53 // callbacks from one call to Decode(). |
57 VCMFrameInformation* frameInfo; | 54 VCMFrameInformation* frameInfo; |
58 VCMReceiveCallback* callback; | 55 VCMReceiveCallback* callback; |
59 { | 56 { |
60 CriticalSectionScoped cs(_critSect); | 57 CriticalSectionScoped cs(_critSect); |
61 frameInfo = _timestampMap.Pop(decodedImage.timestamp()); | 58 frameInfo = _timestampMap.Pop(decodedImage.timestamp()); |
62 callback = _receiveCallback; | 59 callback = _receiveCallback; |
63 } | 60 } |
64 | 61 |
65 if (frameInfo == NULL) { | 62 if (frameInfo == NULL) { |
66 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " | 63 LOG(LS_WARNING) << "Too many frames backed up in the decoder, dropping " |
67 "this one."; | 64 "this one."; |
68 return WEBRTC_VIDEO_CODEC_OK; | 65 return WEBRTC_VIDEO_CODEC_OK; |
69 } | 66 } |
70 | 67 |
71 const int64_t now_ms = _clock->TimeInMilliseconds(); | 68 const int64_t now_ms = _clock->TimeInMilliseconds(); |
72 if (decode_time_ms < 0) { | 69 if (decode_time_ms < 0) { |
73 decode_time_ms = | 70 decode_time_ms = |
74 static_cast<int32_t>(now_ms - frameInfo->decodeStartTimeMs); | 71 static_cast<int32_t>(now_ms - frameInfo->decodeStartTimeMs); |
75 } | 72 } |
76 _timing.StopDecodeTimer( | 73 _timing->StopDecodeTimer(decodedImage.timestamp(), decode_time_ms, now_ms, |
77 decodedImage.timestamp(), | 74 frameInfo->renderTimeMs); |
78 decode_time_ms, | |
79 now_ms, | |
80 frameInfo->renderTimeMs); | |
81 | 75 |
82 if (callback != NULL) | 76 if (callback != NULL) { |
83 { | 77 decodedImage.set_render_time_ms(frameInfo->renderTimeMs); |
84 decodedImage.set_render_time_ms(frameInfo->renderTimeMs); | 78 decodedImage.set_rotation(frameInfo->rotation); |
85 decodedImage.set_rotation(frameInfo->rotation); | 79 callback->FrameToRender(decodedImage); |
86 callback->FrameToRender(decodedImage); | 80 } |
87 } | 81 return WEBRTC_VIDEO_CODEC_OK; |
88 return WEBRTC_VIDEO_CODEC_OK; | |
89 } | 82 } |
90 | 83 |
91 int32_t | 84 int32_t VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( |
92 VCMDecodedFrameCallback::ReceivedDecodedReferenceFrame( | 85 const uint64_t pictureId) { |
93 const uint64_t pictureId) | 86 CriticalSectionScoped cs(_critSect); |
94 { | 87 if (_receiveCallback != NULL) { |
95 CriticalSectionScoped cs(_critSect); | 88 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); |
96 if (_receiveCallback != NULL) | 89 } |
97 { | 90 return -1; |
98 return _receiveCallback->ReceivedDecodedReferenceFrame(pictureId); | |
99 } | |
100 return -1; | |
101 } | 91 } |
102 | 92 |
103 int32_t | 93 int32_t VCMDecodedFrameCallback::ReceivedDecodedFrame( |
104 VCMDecodedFrameCallback::ReceivedDecodedFrame(const uint64_t pictureId) | 94 const uint64_t pictureId) { |
105 { | 95 _lastReceivedPictureID = pictureId; |
106 _lastReceivedPictureID = pictureId; | 96 return 0; |
107 return 0; | |
108 } | 97 } |
109 | 98 |
110 uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const | 99 uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const { |
111 { | 100 return _lastReceivedPictureID; |
112 return _lastReceivedPictureID; | |
113 } | 101 } |
114 | 102 |
115 void VCMDecodedFrameCallback::OnDecoderImplementationName( | 103 void VCMDecodedFrameCallback::OnDecoderImplementationName( |
116 const char* implementation_name) { | 104 const char* implementation_name) { |
117 CriticalSectionScoped cs(_critSect); | 105 CriticalSectionScoped cs(_critSect); |
118 if (_receiveCallback) | 106 if (_receiveCallback) |
119 _receiveCallback->OnDecoderImplementationName(implementation_name); | 107 _receiveCallback->OnDecoderImplementationName(implementation_name); |
120 } | 108 } |
121 | 109 |
122 void VCMDecodedFrameCallback::Map(uint32_t timestamp, | 110 void VCMDecodedFrameCallback::Map(uint32_t timestamp, |
123 VCMFrameInformation* frameInfo) { | 111 VCMFrameInformation* frameInfo) { |
124 CriticalSectionScoped cs(_critSect); | 112 CriticalSectionScoped cs(_critSect); |
125 _timestampMap.Add(timestamp, frameInfo); | 113 _timestampMap.Add(timestamp, frameInfo); |
126 } | 114 } |
127 | 115 |
128 int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) | 116 int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp) { |
129 { | 117 CriticalSectionScoped cs(_critSect); |
130 CriticalSectionScoped cs(_critSect); | 118 if (_timestampMap.Pop(timestamp) == NULL) { |
131 if (_timestampMap.Pop(timestamp) == NULL) | 119 return VCM_GENERAL_ERROR; |
132 { | 120 } |
133 return VCM_GENERAL_ERROR; | 121 return VCM_OK; |
134 } | |
135 return VCM_OK; | |
136 } | 122 } |
137 | 123 |
138 VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) | 124 VCMGenericDecoder::VCMGenericDecoder(VideoDecoder* decoder, bool isExternal) |
139 : _callback(NULL), | 125 : _callback(NULL), |
140 _frameInfos(), | 126 _frameInfos(), |
141 _nextFrameInfoIdx(0), | 127 _nextFrameInfoIdx(0), |
142 _decoder(decoder), | 128 _decoder(decoder), |
143 _codecType(kVideoCodecUnknown), | 129 _codecType(kVideoCodecUnknown), |
144 _isExternal(isExternal), | 130 _isExternal(isExternal), |
145 _keyFrameDecoded(false) {} | 131 _keyFrameDecoded(false) {} |
146 | 132 |
147 VCMGenericDecoder::~VCMGenericDecoder() {} | 133 VCMGenericDecoder::~VCMGenericDecoder() {} |
148 | 134 |
149 int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, | 135 int32_t VCMGenericDecoder::InitDecode(const VideoCodec* settings, |
150 int32_t numberOfCores) | 136 int32_t numberOfCores) { |
151 { | 137 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); |
152 TRACE_EVENT0("webrtc", "VCMGenericDecoder::InitDecode"); | 138 _codecType = settings->codecType; |
153 _codecType = settings->codecType; | |
154 | 139 |
155 return _decoder->InitDecode(settings, numberOfCores); | 140 return _decoder->InitDecode(settings, numberOfCores); |
156 } | 141 } |
157 | 142 |
158 int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { | 143 int32_t VCMGenericDecoder::Decode(const VCMEncodedFrame& frame, int64_t nowMs) { |
159 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", | 144 TRACE_EVENT1("webrtc", "VCMGenericDecoder::Decode", "timestamp", |
160 frame.EncodedImage()._timeStamp); | 145 frame.EncodedImage()._timeStamp); |
161 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; | 146 _frameInfos[_nextFrameInfoIdx].decodeStartTimeMs = nowMs; |
162 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); | 147 _frameInfos[_nextFrameInfoIdx].renderTimeMs = frame.RenderTimeMs(); |
163 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); | 148 _frameInfos[_nextFrameInfoIdx].rotation = frame.rotation(); |
164 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); | 149 _callback->Map(frame.TimeStamp(), &_frameInfos[_nextFrameInfoIdx]); |
165 | 150 |
166 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; | 151 _nextFrameInfoIdx = (_nextFrameInfoIdx + 1) % kDecoderFrameMemoryLength; |
167 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), | 152 int32_t ret = _decoder->Decode(frame.EncodedImage(), frame.MissingFrame(), |
168 frame.FragmentationHeader(), | 153 frame.FragmentationHeader(), |
169 frame.CodecSpecific(), frame.RenderTimeMs()); | 154 frame.CodecSpecific(), frame.RenderTimeMs()); |
170 | 155 |
171 _callback->OnDecoderImplementationName(_decoder->ImplementationName()); | 156 _callback->OnDecoderImplementationName(_decoder->ImplementationName()); |
172 if (ret < WEBRTC_VIDEO_CODEC_OK) | 157 if (ret < WEBRTC_VIDEO_CODEC_OK) { |
173 { | |
174 LOG(LS_WARNING) << "Failed to decode frame with timestamp " | 158 LOG(LS_WARNING) << "Failed to decode frame with timestamp " |
175 << frame.TimeStamp() << ", error code: " << ret; | 159 << frame.TimeStamp() << ", error code: " << ret; |
176 _callback->Pop(frame.TimeStamp()); | 160 _callback->Pop(frame.TimeStamp()); |
177 return ret; | 161 return ret; |
178 } | 162 } else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || |
179 else if (ret == WEBRTC_VIDEO_CODEC_NO_OUTPUT || | 163 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) { |
180 ret == WEBRTC_VIDEO_CODEC_REQUEST_SLI) | |
181 { | |
182 // No output | 164 // No output |
183 _callback->Pop(frame.TimeStamp()); | 165 _callback->Pop(frame.TimeStamp()); |
184 } | 166 } |
185 return ret; | 167 return ret; |
186 } | 168 } |
187 | 169 |
188 int32_t VCMGenericDecoder::Release() { | 170 int32_t VCMGenericDecoder::Release() { |
189 return _decoder->Release(); | 171 return _decoder->Release(); |
190 } | 172 } |
191 | 173 |
192 int32_t VCMGenericDecoder::Reset() { | 174 int32_t VCMGenericDecoder::Reset() { |
193 return _decoder->Reset(); | 175 return _decoder->Reset(); |
194 } | 176 } |
195 | 177 |
196 int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( | 178 int32_t VCMGenericDecoder::RegisterDecodeCompleteCallback( |
197 VCMDecodedFrameCallback* callback) { | 179 VCMDecodedFrameCallback* callback) { |
198 _callback = callback; | 180 _callback = callback; |
199 return _decoder->RegisterDecodeCompleteCallback(callback); | 181 return _decoder->RegisterDecodeCompleteCallback(callback); |
200 } | 182 } |
201 | 183 |
202 bool VCMGenericDecoder::External() const { | 184 bool VCMGenericDecoder::External() const { |
203 return _isExternal; | 185 return _isExternal; |
204 } | 186 } |
205 | 187 |
206 bool VCMGenericDecoder::PrefersLateDecoding() const { | 188 bool VCMGenericDecoder::PrefersLateDecoding() const { |
207 return _decoder->PrefersLateDecoding(); | 189 return _decoder->PrefersLateDecoding(); |
208 } | 190 } |
209 | 191 |
210 } // namespace | 192 } // namespace webrtc |
OLD | NEW |