我有一个使用 GCDAsyncSocket 作为网络接口库的 Cocoa 直播视频程序。播放视频帧时有明显的卡顿。例如,视频上的时间几乎是正常时间的两倍。通过性能检查,发现GCDAsyncSocket的readDataToLength方法调用过于稀疏。以下是带有时间戳的日志:
2012-05-16 11:18:27.054 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.256 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.285 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.452 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.475 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.697 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.726 DVRLibDemo[1181:903] readDataToLength
2012-05-16 11:18:27.856 DVRLibDemo[1181:903] readDataToLength
我在创建套接字时设置了一个单独的委托调度队列,但这并没有多大帮助。该消息有一个固定的标头,我首先读取标头然后读取有效负载。
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
DVRConnection *conn = [self getConnectionWithSocket:sock];
//DebugLogInfo(@"Receive raw data: %@ for connection: %d", data, conn.tag);
if (conn != nil && conn.connected)
{
if (tag == TAG_XM_MESSAGE_HEADER)
{
uint32_t bodyLen = [_parser parseMessageHeader:data];
[sock readDataToLength:bodyLen withTimeout:-1 tag:TAG_XM_MESSAGE_PAYLOAD];
}
else if (tag == TAG_XM_MESSAGE_PAYLOAD)
{
[_parser parseMessage:data forConnection:conn device:self];
[sock readDataToLength:XM_MESSAGE_HEADER_LENGTH withTimeout:-1 tag:TAG_XM_MESSAGE_HEADER];
}
}
}
有谁知道提高从套接字读取数据的速度?谢谢!