| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 | 11 |
| 12 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h" | 12 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_decoder.h" |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 | 15 |
| 16 #if defined(WEBRTC_IOS) | 16 #if defined(WEBRTC_IOS) |
| 17 #include "RTCUIApplication.h" | 17 #include "RTCUIApplication.h" |
| 18 #endif | 18 #endif |
| 19 #include "libyuv/convert.h" | 19 #include "libyuv/convert.h" |
| 20 #include "webrtc/api/video/video_frame.h" | 20 #include "webrtc/api/video/video_frame.h" |
| 21 #include "webrtc/base/checks.h" | 21 #include "webrtc/base/checks.h" |
| 22 #include "webrtc/base/logging.h" | 22 #include "webrtc/base/logging.h" |
| 23 #include "webrtc/common_video/include/corevideo_frame_buffer.h" | 23 #include "webrtc/common_video/include/corevideo_frame_buffer.h" |
| 24 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h" | 24 #include "webrtc/sdk/objc/Framework/Classes/h264_video_toolbox_nalu.h" |
| 25 #include "webrtc/video_frame.h" | 25 #include "webrtc/video_frame.h" |
| 26 | 26 |
| 27 namespace internal { | 27 namespace webrtc { |
| 28 namespace { |
| 28 | 29 |
| 29 static const int64_t kMsPerSec = 1000; | 30 static const int64_t kMsPerSec = 1000; |
| 30 | 31 |
| 31 // Convenience function for creating a dictionary. | 32 // Convenience function for creating a dictionary. |
| 32 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, | 33 inline CFDictionaryRef CreateCFDictionary(CFTypeRef* keys, |
| 33 CFTypeRef* values, | 34 CFTypeRef* values, |
| 34 size_t size) { | 35 size_t size) { |
| 35 return CFDictionaryCreate(nullptr, keys, values, size, | 36 return CFDictionaryCreate(nullptr, keys, values, size, |
| 36 &kCFTypeDictionaryKeyCallBacks, | 37 &kCFTypeDictionaryKeyCallBacks, |
| 37 &kCFTypeDictionaryValueCallBacks); | 38 &kCFTypeDictionaryValueCallBacks); |
| 38 } | 39 } |
| 39 | 40 |
| 40 // Struct that we pass to the decoder per frame to decode. We receive it again | 41 // Struct that we pass to the decoder per frame to decode. We receive it again |
| 41 // in the decoder callback. | 42 // in the decoder callback. |
| 42 struct FrameDecodeParams { | 43 struct FrameDecodeParams { |
| 43 FrameDecodeParams(webrtc::DecodedImageCallback* cb, int64_t ts) | 44 FrameDecodeParams(DecodedImageCallback* cb, int64_t ts) |
| 44 : callback(cb), timestamp(ts) {} | 45 : callback(cb), timestamp(ts) {} |
| 45 webrtc::DecodedImageCallback* callback; | 46 DecodedImageCallback* callback; |
| 46 int64_t timestamp; | 47 int64_t timestamp; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 // This is the callback function that VideoToolbox calls when decode is | 50 // This is the callback function that VideoToolbox calls when decode is |
| 50 // complete. | 51 // complete. |
| 51 void VTDecompressionOutputCallback(void* decoder, | 52 void VTDecompressionOutputCallback(void* decoder, |
| 52 void* params, | 53 void* params, |
| 53 OSStatus status, | 54 OSStatus status, |
| 54 VTDecodeInfoFlags info_flags, | 55 VTDecodeInfoFlags info_flags, |
| 55 CVImageBufferRef image_buffer, | 56 CVImageBufferRef image_buffer, |
| 56 CMTime timestamp, | 57 CMTime timestamp, |
| 57 CMTime duration) { | 58 CMTime duration) { |
| 58 std::unique_ptr<FrameDecodeParams> decode_params( | 59 std::unique_ptr<FrameDecodeParams> decode_params( |
| 59 reinterpret_cast<FrameDecodeParams*>(params)); | 60 reinterpret_cast<FrameDecodeParams*>(params)); |
| 60 if (status != noErr) { | 61 if (status != noErr) { |
| 61 LOG(LS_ERROR) << "Failed to decode frame. Status: " << status; | 62 LOG(LS_ERROR) << "Failed to decode frame. Status: " << status; |
| 62 return; | 63 return; |
| 63 } | 64 } |
| 64 // TODO(tkchin): Handle CVO properly. | 65 // TODO(tkchin): Handle CVO properly. |
| 65 rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = | 66 rtc::scoped_refptr<VideoFrameBuffer> buffer = |
| 66 new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(image_buffer); | 67 new rtc::RefCountedObject<CoreVideoFrameBuffer>(image_buffer); |
| 67 webrtc::VideoFrame decoded_frame(buffer, decode_params->timestamp, | 68 VideoFrame decoded_frame(buffer, decode_params->timestamp, |
| 68 CMTimeGetSeconds(timestamp) * kMsPerSec, | 69 CMTimeGetSeconds(timestamp) * kMsPerSec, |
| 69 webrtc::kVideoRotation_0); | 70 kVideoRotation_0); |
| 70 decode_params->callback->Decoded(decoded_frame); | 71 decode_params->callback->Decoded(decoded_frame); |
| 71 } | 72 } |
| 72 | 73 |
| 73 } // namespace internal | 74 } // namespace |
| 74 | |
| 75 namespace webrtc { | |
| 76 | 75 |
| 77 H264VideoToolboxDecoder::H264VideoToolboxDecoder() | 76 H264VideoToolboxDecoder::H264VideoToolboxDecoder() |
| 78 : callback_(nullptr), | 77 : callback_(nullptr), |
| 79 video_format_(nullptr), | 78 video_format_(nullptr), |
| 80 decompression_session_(nullptr) {} | 79 decompression_session_(nullptr) {} |
| 81 | 80 |
| 82 H264VideoToolboxDecoder::~H264VideoToolboxDecoder() { | 81 H264VideoToolboxDecoder::~H264VideoToolboxDecoder() { |
| 83 DestroyDecompressionSession(); | 82 DestroyDecompressionSession(); |
| 84 SetVideoFormat(nullptr); | 83 SetVideoFormat(nullptr); |
| 85 } | 84 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 } | 132 } |
| 134 CMSampleBufferRef sample_buffer = nullptr; | 133 CMSampleBufferRef sample_buffer = nullptr; |
| 135 if (!H264AnnexBBufferToCMSampleBuffer(input_image._buffer, | 134 if (!H264AnnexBBufferToCMSampleBuffer(input_image._buffer, |
| 136 input_image._length, video_format_, | 135 input_image._length, video_format_, |
| 137 &sample_buffer)) { | 136 &sample_buffer)) { |
| 138 return WEBRTC_VIDEO_CODEC_ERROR; | 137 return WEBRTC_VIDEO_CODEC_ERROR; |
| 139 } | 138 } |
| 140 RTC_DCHECK(sample_buffer); | 139 RTC_DCHECK(sample_buffer); |
| 141 VTDecodeFrameFlags decode_flags = | 140 VTDecodeFrameFlags decode_flags = |
| 142 kVTDecodeFrame_EnableAsynchronousDecompression; | 141 kVTDecodeFrame_EnableAsynchronousDecompression; |
| 143 std::unique_ptr<internal::FrameDecodeParams> frame_decode_params; | 142 std::unique_ptr<FrameDecodeParams> frame_decode_params; |
| 144 frame_decode_params.reset( | 143 frame_decode_params.reset( |
| 145 new internal::FrameDecodeParams(callback_, input_image._timeStamp)); | 144 new FrameDecodeParams(callback_, input_image._timeStamp)); |
| 146 OSStatus status = VTDecompressionSessionDecodeFrame( | 145 OSStatus status = VTDecompressionSessionDecodeFrame( |
| 147 decompression_session_, sample_buffer, decode_flags, | 146 decompression_session_, sample_buffer, decode_flags, |
| 148 frame_decode_params.release(), nullptr); | 147 frame_decode_params.release(), nullptr); |
| 149 #if defined(WEBRTC_IOS) | 148 #if defined(WEBRTC_IOS) |
| 150 // Re-initialize the decoder if we have an invalid session while the app is | 149 // Re-initialize the decoder if we have an invalid session while the app is |
| 151 // active and retry the decode request. | 150 // active and retry the decode request. |
| 152 if (status == kVTInvalidSessionErr && | 151 if (status == kVTInvalidSessionErr && |
| 153 ResetDecompressionSession() == WEBRTC_VIDEO_CODEC_OK) { | 152 ResetDecompressionSession() == WEBRTC_VIDEO_CODEC_OK) { |
| 154 frame_decode_params.reset( | 153 frame_decode_params.reset( |
| 155 new internal::FrameDecodeParams(callback_, input_image._timeStamp)); | 154 new FrameDecodeParams(callback_, input_image._timeStamp)); |
| 156 status = VTDecompressionSessionDecodeFrame( | 155 status = VTDecompressionSessionDecodeFrame( |
| 157 decompression_session_, sample_buffer, decode_flags, | 156 decompression_session_, sample_buffer, decode_flags, |
| 158 frame_decode_params.release(), nullptr); | 157 frame_decode_params.release(), nullptr); |
| 159 } | 158 } |
| 160 #endif | 159 #endif |
| 161 CFRelease(sample_buffer); | 160 CFRelease(sample_buffer); |
| 162 if (status != noErr) { | 161 if (status != noErr) { |
| 163 LOG(LS_ERROR) << "Failed to decode frame with code: " << status; | 162 LOG(LS_ERROR) << "Failed to decode frame with code: " << status; |
| 164 return WEBRTC_VIDEO_CODEC_ERROR; | 163 return WEBRTC_VIDEO_CODEC_ERROR; |
| 165 } | 164 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 static size_t const attributes_size = 3; | 200 static size_t const attributes_size = 3; |
| 202 CFTypeRef keys[attributes_size] = { | 201 CFTypeRef keys[attributes_size] = { |
| 203 #if defined(WEBRTC_IOS) | 202 #if defined(WEBRTC_IOS) |
| 204 kCVPixelBufferOpenGLESCompatibilityKey, | 203 kCVPixelBufferOpenGLESCompatibilityKey, |
| 205 #elif defined(WEBRTC_MAC) | 204 #elif defined(WEBRTC_MAC) |
| 206 kCVPixelBufferOpenGLCompatibilityKey, | 205 kCVPixelBufferOpenGLCompatibilityKey, |
| 207 #endif | 206 #endif |
| 208 kCVPixelBufferIOSurfacePropertiesKey, | 207 kCVPixelBufferIOSurfacePropertiesKey, |
| 209 kCVPixelBufferPixelFormatTypeKey | 208 kCVPixelBufferPixelFormatTypeKey |
| 210 }; | 209 }; |
| 211 CFDictionaryRef io_surface_value = | 210 CFDictionaryRef io_surface_value = CreateCFDictionary(nullptr, nullptr, 0); |
| 212 internal::CreateCFDictionary(nullptr, nullptr, 0); | |
| 213 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; | 211 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange; |
| 214 CFNumberRef pixel_format = | 212 CFNumberRef pixel_format = |
| 215 CFNumberCreate(nullptr, kCFNumberLongType, &nv12type); | 213 CFNumberCreate(nullptr, kCFNumberLongType, &nv12type); |
| 216 CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value, | 214 CFTypeRef values[attributes_size] = {kCFBooleanTrue, io_surface_value, |
| 217 pixel_format}; | 215 pixel_format}; |
| 218 CFDictionaryRef attributes = | 216 CFDictionaryRef attributes = |
| 219 internal::CreateCFDictionary(keys, values, attributes_size); | 217 CreateCFDictionary(keys, values, attributes_size); |
| 220 if (io_surface_value) { | 218 if (io_surface_value) { |
| 221 CFRelease(io_surface_value); | 219 CFRelease(io_surface_value); |
| 222 io_surface_value = nullptr; | 220 io_surface_value = nullptr; |
| 223 } | 221 } |
| 224 if (pixel_format) { | 222 if (pixel_format) { |
| 225 CFRelease(pixel_format); | 223 CFRelease(pixel_format); |
| 226 pixel_format = nullptr; | 224 pixel_format = nullptr; |
| 227 } | 225 } |
| 228 VTDecompressionOutputCallbackRecord record = { | 226 VTDecompressionOutputCallbackRecord record = { |
| 229 internal::VTDecompressionOutputCallback, this, | 227 VTDecompressionOutputCallback, this, |
| 230 }; | 228 }; |
| 231 OSStatus status = | 229 OSStatus status = |
| 232 VTDecompressionSessionCreate(nullptr, video_format_, nullptr, attributes, | 230 VTDecompressionSessionCreate(nullptr, video_format_, nullptr, attributes, |
| 233 &record, &decompression_session_); | 231 &record, &decompression_session_); |
| 234 CFRelease(attributes); | 232 CFRelease(attributes); |
| 235 if (status != noErr) { | 233 if (status != noErr) { |
| 236 DestroyDecompressionSession(); | 234 DestroyDecompressionSession(); |
| 237 return WEBRTC_VIDEO_CODEC_ERROR; | 235 return WEBRTC_VIDEO_CODEC_ERROR; |
| 238 } | 236 } |
| 239 ConfigureDecompressionSession(); | 237 ConfigureDecompressionSession(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 269 if (video_format_) { | 267 if (video_format_) { |
| 270 CFRetain(video_format_); | 268 CFRetain(video_format_); |
| 271 } | 269 } |
| 272 } | 270 } |
| 273 | 271 |
| 274 const char* H264VideoToolboxDecoder::ImplementationName() const { | 272 const char* H264VideoToolboxDecoder::ImplementationName() const { |
| 275 return "VideoToolbox"; | 273 return "VideoToolbox"; |
| 276 } | 274 } |
| 277 | 275 |
| 278 } // namespace webrtc | 276 } // namespace webrtc |
| OLD | NEW |