【问题标题】:streaming images over bonjour between two iOS device在两个 iOS 设备之间通过 bonjour 流式传输图像
【发布时间】:2011-11-08 21:32:10
【问题描述】:

我的目标是通过 bonjour 将 AVCpatureInput 捕获的图像从一台 iOS 设备流式传输到另一台设备。

这是我目前的方法:

1) 从视频输入中捕获帧

- (void)captureOutput:(AVCaptureOutput *)captureOutput 
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
   fromConnection:(AVCaptureConnection *)connection 
{ 
    /*code to convert sampleBuffer into UIImage */
    NSData * imageData = UIImageJPEGRepresentation(image,1.0);
    [connection sendImage:image];
}

2) 通过 TCP 连接发送(来自http://mobileorchard.com/tutorial-networking-and-bonjour-on-iphone/

// Send raw image over network
- (void)sendRawImagePacket:(UIImage *)image {
// Encode packet
NSData * imageData = UIImageJPEGRepresentation(image, 1.0);

NSData * rawPacket = [NSKeyedArchiver archivedDataWithRootObject:imageData];

// Write header: length of raw packet
int packetLength = [rawPacket length];

[outgoingDataBuffer appendBytes:&packetLength length:sizeof(int)];

[outgoingDataBuffer appendData:rawPacket];

// Try to write to stream
[self writeOutgoingBufferToStream];
}

3) 读取数据并将其转换回图像以显示在接收设备的 UIImageView 上

// Read as many bytes from the stream as possible and try to extract meaningful packets
- (void)readFromStreamIntoIncomingBuffer {
// Temporary buffer to read data into
UInt8 buf[1024];

// Try reading while there is data
while( CFReadStreamHasBytesAvailable(readStream) ) {  
   CFIndex len = CFReadStreamRead(readStream, buf, sizeof(buf));
if ( len <= 0 ) {
  // Either stream was closed or error occurred. Close everything up and treat this as "connection terminated"
  [self close];
  [delegate connectionTerminated:self];
  return;
 }

  [incomingDataBuffer appendBytes:buf length:len];
}

// Try to extract packets from the buffer.
//
// Protocol: header + body
//  header: an integer that indicates length of the body
//  body: bytes that represent encoded NSDictionary

// We might have more than one message in the buffer - that's why we'll be reading it inside the while loop
 while( YES ) {
// Did we read the header yet?
if ( packetBodySize == -1 ) {
  // Do we have enough bytes in the buffer to read the header?
  if ( [incomingDataBuffer length] >= sizeof(int) ) {
    // extract length
    memcpy(&packetBodySize, [incomingDataBuffer bytes], sizeof(int));

    // remove that chunk from buffer
    NSRange rangeToDelete = {0, sizeof(int)};
    [incomingDataBuffer replaceBytesInRange:rangeToDelete withBytes:NULL length:0];
  }
  else {
    // We don't have enough yet. Will wait for more data.

      break;

  }
}

// We should now have the header. Time to extract the body.
if ( [incomingDataBuffer length] >= packetBodySize ) {
  // We now have enough data to extract a meaningful packet.
  NSData* raw = [NSData dataWithBytes:[incomingDataBuffer bytes] length:packetBodySize];

  // Tell our delegate about it

            NSData * imageData = [NSKeyedUnarchiver unarchiveObjectWithData:raw];

            UIImage * image = [UIImage imageWithData:imageData];
            [delegate receivedNetworkRawImage:image viaConnection:self];


  // Remove that chunk from buffer
  NSRange rangeToDelete = {0, packetBodySize};
  [incomingDataBuffer replaceBytesInRange:rangeToDelete withBytes:NULL length:0];

  // We have processed the packet. Resetting the state.
  packetBodySize = -1;
}
else {
  // Not enough data yet. Will wait.
  break;
 }
}
}

但是,当连接不稳定时,UIImage 会抛出无法呈现 JPEG 的错误。

我应该如何通过 wifi 传递图像?

如果某些帧跳过也没关系,我需要一种方法来告诉 UIImage 跳过那“批”坏数据。

谢谢!

【问题讨论】:

  • 您好,出于好奇,您是否设法从一台设备到另一台设备进行了足够好的实时视频流?

标签: ios avfoundation bonjour nskeyedarchiver


【解决方案1】:

UIImage 不符合 NSCoding --> NSKeyedArchiver 失败。

您必须使用 UIImagePNGRepresentation() 来获取图像的数据。或者使用 UIImageJPEGRepresentation() 压缩数据。

【讨论】:

  • 是的,我在发送数据时有这个:NSData * imageData = UIImageJPEGRepresentation(image, 1.0)' 我的问题是接收数据。我错过了什么吗?
  • 哦,我现在明白了。我会完全删除 NSKeyedArchiver 的东西。你已经有了 NSData。
  • 您应该首先使用简单的 NSString 对象验证您的网络代码。这行得通吗?
  • 是的,我已经删除了 NSKeyedArchiver。因此,当 UIImage 到达另一端时,丢失位的问题会被传递到渲染 UIImage 中。网络代码适用于字符串、数组、图像和字典。但是,流式传输是一个问题,因为有时会丢失帧。
  • 我通过网络发送图像没有问题,我不知道错误在哪里。您可以测试它是否适用于 Base64 编码的图像。
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 2014-03-19
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
相关资源
最近更新 更多