| 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 |
| 76 | }else{ |
| 77 | LOGE("HEVC not available on this OS"); |
| 78 | } |
| 79 | } |
| 80 | [renderer _setSizeWidth:(uint16_t)width height:(uint16_t)height]; |
| 81 | needReset=true; |
| 82 | } |
| 83 |
| 84 | void SampleBufferDisplayLayerRenderer::DecodeAndDisplay(Buffer frame, uint32_t pts){ |
| 85 |
| 86 | std::vector<uint32_t> nalStartOffsets; |
| 87 | uint8_t* _data=*frame; |
| 88 | for(uint32_t offset=0;offset<frame.Length()-4;offset++){ |
| 89 | if(_data[offset]==0 && _data[offset+1]==0 && ((_data[offset+2]==0 && _data[offset+3]==1) || _data[offset+2]==1)){ |
| 90 | nalStartOffsets.push_back(offset+(_data[offset+2]==1 ? 3 : 4)); |
| 91 | offset+=4; |
| 92 | } |
| 93 | } |
| 94 | BufferOutputStream out(frame.Length()); |
| 95 | for(uint32_t i=0;i<nalStartOffsets.size();i++){ |
| 96 | uint32_t length=(i==nalStartOffsets.size()-1 ? (uint32_t)frame.Length() : nalStartOffsets[i+1])-nalStartOffsets[i]; |
| 97 | uint8_t lenBytes[]={(uint8_t)(length >> 24), (uint8_t)(length >> 16), (uint8_t)(length >> 8), (uint8_t)length}; |
| 98 | out.WriteBytes(lenBytes, 4); |
| 99 | out.WriteBytes(frame, nalStartOffsets[i], length); |
| 100 | } |
| 101 |
| 102 | CMBlockBufferRef blockBuffer; |
| 103 |
| 104 | OSStatus status=CMBlockBufferCreateWithMemoryBlock(kCFAllocatorDefault, out.GetBuffer(), out.GetLength(), kCFAllocatorNull, NULL, 0, out.GetLength(), 0, &blockBuffer); |
| 105 | if(status!=noErr){ |
| 106 | LOGE("CMBlockBufferCreateWithMemoryBlock failed: %d", status); |
| 107 | return; |
| 108 | } |
| 109 | CMSampleBufferRef sampleBuffer; |
| 110 | status=CMSampleBufferCreate(kCFAllocatorDefault, blockBuffer, true, NULL, NULL, formatDesc, 1, 0, NULL, 0, NULL, &sampleBuffer); |
| 111 | if(status!=noErr){ |
| 112 | LOGE("CMSampleBufferCreate failed: %d", status); |
| 113 | return; |
| 114 | } |
| 115 |
| 116 | CFRelease(blockBuffer); |
| 117 | CFArrayRef attachments=CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true); |
| 118 | CFMutableDictionaryRef dict=(CFMutableDictionaryRef)CFArrayGetValueAtIndex(attachments, 0); |
| 119 | CFDictionarySetValue(dict, kCMSampleAttachmentKey_DisplayImmediately, kCFBooleanTrue); |
| 120 |
| 121 | [renderer _enqueueBuffer:sampleBuffer reset:needReset]; |
| 122 | needReset=false; |
| 123 | CFRelease(sampleBuffer); |
| 124 | } |
| 125 |
| 126 | void SampleBufferDisplayLayerRenderer::SetStreamEnabled(bool enabled){ |
| 127 | if(enabled!=streamEnabled){ |
| 128 | streamEnabled=enabled; |
| 129 | if(enabled){ |
| 130 | [renderer _setResumed]; |
| 131 | }else{ |
| 132 | [renderer _setStopped]; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |
| 137 | int SampleBufferDisplayLayerRenderer::GetMaximumResolution(){ |
| 138 | #if TARGET_OS_IPHONE |
| 139 | CGRect screenSize=[UIScreen mainScreen].nativeBounds; |
| 140 | CGFloat minSize=std::min(screenSize.size.width, screenSize.size.height); |
| 141 | if(minSize>720.f){ |
| 142 | return INIT_VIDEO_RES_1080; |
| 143 | }else if(minSize>480.f){ |
| 144 | return INIT_VIDEO_RES_720; |
| 145 | }else{ |
| 146 | return INIT_VIDEO_RES_480; |
| 147 | } |
| 148 | #else // OS X |
| 149 | // TODO support OS X |
| 150 | #endif |
| 151 | return INIT_VIDEO_RES_1080; |
| 152 | } |
| 153 |
| 154 | void SampleBufferDisplayLayerRenderer::SetRotation(uint16_t rotation){ |
| 155 | [renderer _setRotation:rotation]; |
| 156 | } |
| 157 |
| 158 | void SampleBufferDisplayLayerRenderer::SetStreamPaused(bool paused){ |
| 159 | if(paused){ |
| 160 | [renderer _setPaused]; |
| 161 | }else if(streamEnabled){ |
| 162 | [renderer _setResumed]; |
| 163 | } |
| 164 | } |
| 165 |
| 166 | std::vector<uint32_t> SampleBufferDisplayLayerRenderer::GetAvailableDecoders(){ |
| 167 | std::vector<uint32_t> res; |
| 168 | res.push_back(CODEC_AVC); |
| 169 | if(@available(iOS 11.0, *)){ |
| 170 | if(VTIsHardwareDecodeSupported(kCMVideoCodecType_HEVC)){ |
| 171 | res.push_back(CODEC_HEVC); |
| 172 | } |
| 173 | } |
| 174 | return res; |
| 175 | } |
| 176 |