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