说一说IOS使用GCDSocketMenger+硬解码遇到的坑

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{

//_readagainTime = [self getTimeNow];

//[h264Decoder decodeNalu:(uint8_t *)[data bytes] withSize:(uint32_t)data.length];

int nRef = 0;

uint8_t *puf = nil;

if(tag == Data){

int type = [self typeOfNalu:data];

if (type == 7 || type == 8 || type == 6 || type == 5) { //SPS PPS SEI IDR

if (type == 7 || type == 8) {

if (h264Decoder) {

nRef = (int)data.length;

puf = (uint8_t *)malloc(data.length);

memcpy(puf,[data bytes],data.length);

const char bytes[] = "\x00\x00\x00\x01";

size_t length = (sizeof bytes) - 1;

NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];

NSMutableData *h264Data = [[NSMutableData alloc] init];

[h264Data appendData:ByteHeader];

[h264Data appendData:[NSData dataWithBytes:puf length:nRef]];

h264Decoder.nalu_type = type;

[h264Decoder decodeNalu:(uint8_t *)[h264Data bytes] withSize:(uint32_t)h264Data.length];

free(puf);

puf = nil;

}

}else{

[keyFrame appendData:lastStartCode];

[keyFrame appendBytes:[data bytes] length:[data length] - [self startCodeLenth:data]];

}

}

if (type == 5 || type == 1  || type == 6) {//IDR P frame

if (type == 5 || type == 6) {

h264Decoder.nalu_type = type;

[h264Decoder decodeNalu:(uint8_t *)[keyFrame bytes] withSize:(uint32_t)keyFrame.length];

keyFrame = [[NSMutableData alloc] init];//reset keyframe

}else{

NSMutableData *nalu = [[NSMutableData alloc]initWithData:lastStartCode];

[nalu appendBytes:[data bytes] length:[data length]-[self startCodeLenth:data]];

char *chBuf = (char*)[nalu bytes];

// NSLog(@"chbuf ======== %x %x %x %x %x %x %x",chBuf[0],chBuf[1],chBuf[2],chBuf[3],chBuf[4],chBuf[5],chBuf[6]);

if (chBuf[0] == 0x00 && chBuf[1]==0x00 && chBuf[2]==0x01) {

nRef = (int)nalu.length-3;

puf = (uint8_t *)malloc(nalu.length-3);

memcpy(puf,[nalu bytes]+3,nalu.length-3);

const char bytes[] = "\x00\x00\x00\x01";

size_t length = (sizeof bytes) - 1;

NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];

NSMutableData *h264Data = [[NSMutableData alloc] init];

[h264Data appendData:ByteHeader];

[h264Data appendData:[NSData dataWithBytes:puf length:nRef]];

h264Decoder.nalu_type = type;

[h264Decoder decodeNalu:(uint8_t *)[h264Data bytes] withSize:(uint32_t)h264Data.length];

}else if (chBuf[0] == 0x00 && chBuf[1]==0x00 && chBuf[2]==0x00 && chBuf[3]==0x01) {

nRef = (int)nalu.length-4;

puf = (uint8_t *)malloc(nalu.length-4);

memcpy(puf,[nalu bytes]+4,nalu.length-4);

const char bytes[] = "\x00\x00\x00\x01";

size_t length = (sizeof bytes) - 1;

NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];

NSMutableData *h264Data = [[NSMutableData alloc] init];

[h264Data appendData:ByteHeader];

[h264Data appendData:[NSData dataWithBytes:puf length:nRef]];

h264Decoder.nalu_type = type;

[h264Decoder decodeNalu:(uint8_t *)[h264Data bytes] withSize:(uint32_t)h264Data.length];

} else{

const char bytes[] = "\x00\x00\x00\x01";

size_t length = (sizeof bytes) - 1;

NSData *ByteHeader = [NSData dataWithBytes:bytes length:length];

NSMutableData *h264Data = [[NSMutableData alloc] init];

[h264Data appendData:ByteHeader];

[h264Data appendData:nalu];

h264Decoder.nalu_type = type;

[h264Decoder decodeNalu:(uint8_t *)[h264Data bytes] withSize:(uint32_t)h264Data.length];

}

//NSLog(@"chBuf %x %x %x %x %x %x %x %x %x %x",chBuf[0],chBuf[1],chBuf[2],chBuf[3],chBuf[4],chBuf[5],chBuf[6],chBuf[7],chBuf[8],chBuf[9]);

}

}else{

//            isGetData = NO;

[socket readDataToData:startcodeData withTimeout:-1 tag:Data];

}

}

if (data.length > [self startCodeLenth:data]) {

[self saveStartCode:data];

}

}

- (int)typeOfNalu:(NSData *)data{

char first = *(char *)[data bytes];

return first & 0x1f;

}

- (int)startCodeLenth:(NSData *)data

{

char temp = *((char *)[data bytes] + [data length] - 4);

return temp == 0x00 ? 4 : 3;

}

- (void)saveStartCode:(NSData *)data

{

int startCodeLen = [self startCodeLenth:data];

NSRange startCodeRange = {[data length] - startCodeLen, startCodeLen};

lastStartCode = [data subdataWithRange:startCodeRange];

}

最主要的是通过socket得到裸码流以后进行转换,在这里卡了好久

你可能感兴趣的:(说一说IOS使用GCDSocketMenger+硬解码遇到的坑)