簡體   English   中英

如何從iphone相機獲取實時視頻流並將其發送到服務器?

[英]How to get real time video stream from iphone camera and send it to server?

我正在使用AVCaptureSession捕獲視頻並從iPhone攝像頭獲取實時幀但如何將其發送到具有多路復用框架和聲音的服務器以及如何使用ffmpeg來完成此任務,如果任何人有任何關於ffmpeg或任何示例的教程請在這里分享。

我這樣做的方法是實現一個AVCaptureSession,它有一個帶有回調的委托,該回調在每一幀上運行。 該回調通過網絡將每個幀發送到服務器,該服務器具有接收它的自定義設置。

這是流程:

http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/03_MediaCapture.html#//apple_ref/doc/uid/TP40010188-CH5-SW2

這里是一些代碼:

// make input device

NSError *deviceError;

AVCaptureDevice *cameraDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *inputDevice = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&deviceError];

// make output device

AVCaptureVideoDataOutput *outputDevice = [[AVCaptureVideoDataOutput alloc] init];

[outputDevice setSampleBufferDelegate:self queue:dispatch_get_main_queue()];

// initialize capture session

AVCaptureSession *captureSession = [[[AVCaptureSession alloc] init] autorelease];

[captureSession addInput:inputDevice];

[captureSession addOutput:outputDevice];

// make preview layer and add so that camera's view is displayed on screen

AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer    layerWithSession:captureSession];
previewLayer.frame = view.bounds;
[view.layer addSublayer:previewLayer];

// go!

[captureSession startRunning];

然后輸出設備的委托(這里,self)必須實現回調:

-(void) captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection

{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer( sampleBuffer );

CGSize imageSize = CVImageBufferGetEncodedSize( imageBuffer );

// also in the 'mediaSpecific' dict of the sampleBuffer

   NSLog( @"frame captured at %.fx%.f", imageSize.width, imageSize.height );

    }

發送原始幀或單個圖像對您來說永遠不會很好(因為數據量和幀數)。 您也無法通過電話合理地提供任何服務(WWAN網絡具有各種防火牆)。 您需要對視頻進行編碼,然后將其流式傳輸到服務器,最有可能采用標准流式傳輸格式(RTSP,RTMP)。 iPhone上有一個H.264編碼器芯片> = 3GS。 問題是它不是面向流的。 也就是說,它輸出最后解析視頻所需的元數據。 這為您提供了一些選擇。

1)獲取原始數據並使用FFmpeg在手機上進行編碼(將使用大量的CPU和電池)。

2)為H.264 / AAC輸出編寫自己的解析器(非常難)。

3)以塊的形式記錄和處理(將增加等於塊長度的延遲,並在啟動和停止會話時在每個塊之間減少大約1/4秒的視頻)。

看這里在這里

嘗試使用AV Foundation框架捕獲視頻。 使用HTTP流式傳輸將其上傳到您的服務器。

還要檢查下面的堆棧另一個堆棧溢出帖子

(以下帖子在此鏈接中找到)

你很可能已經知道....

 1) How to get compressed frames and audio from iPhone's camera?

你不能做這個。 AVFoundation API已經從各個角度防止了這種情況。 我甚至嘗試過命名管道和其他一些偷偷摸摸的unix foo。 沒有這樣的運氣。 你別無選擇,只能把它寫到文件中。 在您的鏈接帖子中,用戶建議設置回調以提供編碼的幀。 據我所知,這對H.264流是不可能的。 捕獲代表將提供以特定像素格式編碼的圖像。 正是Movie Writers和AVAssetWriter進行編碼。

 2) Encoding uncompressed frames with ffmpeg's API is fast enough for
 real-time streaming?

是的。 但是,您必須使用libx264才能進入GPL領域。 這與app商店並不完全兼容。

出於效率原因,我建議使用AVFoundation和AVAssetWriter。

有一個很長很短的故事。

這是一個簡短的問題:去看看https://github.com/OpenWatch/H264-RTSP-Server-iOS

這是一個起點。

你可以得到它,看看他如何提取框架。 這是一個小而簡單的項目。

然后你可以看看具有特定功能“encodedFrame”的kickflip,它被稱為背面和編碼幀從這一點到達你可以用它做你想要的,通過websocket發送。 有一堆非常難以讀取mpeg原子的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM