| 7 | #include <TargetConditionals.h> |
| 8 | #if TARGET_OS_IPHONE |
| 9 | #include <UIKit/UIKit.h> |
| 10 | #endif |
| 11 | #include "SampleBufferDisplayLayerRenderer.h" |
| 12 | #include "../../PrivateDefines.h" |
| 13 | #include "../../logging.h" |
| 14 | #include "TGVVideoRenderer.h" |
| 15 |
| 16 | using namespace tgvoip; |
| 17 | using namespace tgvoip::video; |
| 18 |
| 19 | SampleBufferDisplayLayerRenderer::SampleBufferDisplayLayerRenderer(TGVVideoRenderer* renderer) : renderer(renderer){ |
| 20 |
| 21 | } |
| 22 |
| 23 | SampleBufferDisplayLayerRenderer::~SampleBufferDisplayLayerRenderer(){ |
| 24 |
| 25 | } |
| 26 |
| 27 | void SampleBufferDisplayLayerRenderer::Reset(uint32_t codec, unsigned int width, unsigned int height, std::vector<Buffer>& csd){ |
| 28 | LOGI("video renderer reset: %d x %d", width, height); |
| 29 | if(formatDesc){ |
| 30 | CFRelease(formatDesc); |
| 31 | } |
| 32 | if(codec==CODEC_AVC){ |
| 33 | if(csd.size()!=2){ |
| 34 | LOGE("H264 requires exactly 2 CSD buffers"); |
| 35 | return; |
| 36 | } |
| 37 | const uint8_t* params[]={*csd[0]+4, *csd[1]+4}; |
| 38 | size_t paramSizes[]={csd[0].Length()-4, csd[1].Length()-4}; |
| 39 | OSStatus status=CMVideoFormatDescriptionCreateFromH264ParameterSets(NULL, 2, params, paramSizes, 4, &formatDesc); |
| 40 | if(status!=noErr){ |
| 41 | LOGE("CMVideoFormatDescriptionCreateFromH264ParameterSets failed: %d", status); |
| 42 | return; |
| 43 | } |
| 44 | CGRect rect=CMVideoFormatDescriptionGetCleanAperture(formatDesc, true); |
| 45 | LOGI("size from formatDesc: %f x %f", rect.size.width, rect.size.height); |
| 46 | }else if(codec==CODEC_HEVC){ |
| 47 | if(@available(iOS 11.0, *)){ |
| 48 | if(csd.size()!=1){ |
| 49 | LOGE("HEVC requires exactly 1 CSD buffer"); |
| 50 | return; |
| 51 | } |
| 52 | int offsets[]={0, 0, 0}; |
| 53 | Buffer& buf=csd[0]; |
| 54 | int currentNAL=0; |
| 55 | for(int i=0;i<buf.Length()-4;i++){ |
| 56 | if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==0 && buf[i+3]==1){ |
| 57 | offsets[currentNAL]=i+4; |
| 58 | currentNAL++; |
| 59 | } |
| 60 | } |
| 61 | LOGV("CSD NAL offsets: %d %d %d", offsets[0], offsets[1], offsets[2]); |
| 62 | if(offsets[0]==0 || offsets[1]==0 || offsets[2]==0){ |
| 63 | LOGE("Error splitting CSD buffer"); |
| 64 | return; |
| 65 | } |
| 66 | const uint8_t* params[]={*buf+offsets[0], *buf+offsets[1], *buf+offsets[2]}; |
| 67 | size_t paramSizes[]={(size_t)((offsets[1]-4)-offsets[0]), (size_t)((offsets[2]-4)-offsets[1]), (size_t)(buf.Length()-offsets[2])}; |
| 68 | OSStatus status=CMVideoFormatDescriptionCreateFromHEVCParameterSets(NULL, 3, params, paramSizes, 4, NULL, &formatDesc); |
| 69 | if(status!=noErr){ |
| 70 | LOGE("CMVideoFormatDescriptionCreateFromHEVCParameterSets failed: %d", status); |
| 71 | return; |
| 72 | } |
| 73 | CGRect rect=CMVideoFormatDescriptionGetCleanAperture(formatDesc, true); |
| 74 | LOGI("size from formatDesc: %f x %f", rect.size.width, rect.size.height); |
| 75 | }else{ |
| 76 | LOGE("HEVC not available on this OS"); |
| 77 | } |
| 78 | } |
| 79 | needReset=true; |
| 80 | } |
| 81 |
| 82 | void SampleBufferDisplayLayerRenderer::DecodeAndDisplay(Buffer frame, uint32_t pts){ |
| 83 | CMBlockBufferRef blockBuffer; |
| 84 |
| 85 | OSStatus status=CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, *frame, frame.Length(), kCFAllocatorNull, NULL, 0, frame.Length(), 0, &blockBuffer); |
| 86 | if(status!=noErr){ |
| 87 | LOGE("CMBlockBufferCreateWithMemoryBlock failed: %d", status); |
| 88 | return; |
| 89 | } |
| 90 | uint32_t _len=(uint32_t)(frame.Length()-4); |
| 91 | uint8_t lenBytes[]={(uint8_t)(_len >> 24), (uint8_t)(_len >> 16), (uint8_t)(_len >> 8), (uint8_t)_len}; |
| 92 | status=CMBlockBufferReplaceDataBytes(lenBytes, blockBuffer, 0, 4); |
| 93 | if(status!=noErr){ |
| 94 | LOGE("CMBlockBufferReplaceDataBytes failed: %d", status); |
| 95 | return; |
| 96 | } |
| 97 | CMSampleBufferRef sampleBuffer; |
| 98 | status=CMSampleBufferCreate(kCFAllocatorDefault, blockBuffer, true, NULL, NULL, formatDesc, 1, 0, NULL, 0, NULL, &sampleBuffer); |
| 99 | if(status!=noErr){ |
| 100 | LOGE("CMSampleBufferCreate failed: %d", status); |
| 101 | return; |
| 102 | } |
| 103 |
| 104 | CFRelease(blockBuffer); |
| 105 | CFArrayRef attachments=CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true); |
| 106 | CFMutableDictionaryRef dict=(CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0); |
| 107 | CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue); |
| 108 |
| 109 | [renderer _enqueueBuffer:sampleBuffer reset:needReset]; |
| 110 | needReset=false; |
| 111 | CFRelease(sampleBuffer); |
| 112 | } |
| 113 |
| 114 | void SampleBufferDisplayLayerRenderer::SetStreamEnabled(bool enabled){ |
| 115 |
| 116 | } |
| 117 |
| 118 | int SampleBufferDisplayLayerRenderer::GetMaximumResolution(){ |
| 119 | #if TARGET_OS_IPHONE |
| 120 | CGRect screenSize=[UIScreen mainScreen].nativeBounds; |
| 121 | CGFloat minSize=std::min(screenSize.size.width, screenSize.size.height); |
| 122 | if(minSize>720.f){ |
| 123 | return INIT_VIDEO_RES_1080; |
| 124 | }else if(minSize>480.f){ |
| 125 | return INIT_VIDEO_RES_720; |
| 126 | }else{ |
| 127 | return INIT_VIDEO_RES_480; |
| 128 | } |
| 129 | #else // OS X |
| 130 | // TODO support OS X |
| 131 | #endif |
| 132 | return INIT_VIDEO_RES_1080; |
| 133 | } |
| 134 |
| 135 | std::vector<uint32_t> SampleBufferDisplayLayerRenderer::GetAvailableDecoders(){ |
| 136 | std::vector<uint32_t> res; |
| 137 | res.push_back(CODEC_AVC); |
| 138 | if(@available(iOS 11.0, *)){ |
| 139 | if(VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC)){ |
| 140 | res.push_back(CODEC_HEVC); |
| 141 | } |
| 142 | } |
| 143 | return res; |
| 144 | } |
| 145 |