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