簡體   English   中英

如何使用 Proxygen 和 Folly 發送 HTTP 分塊響應以模擬視頻流?

[英]How to send HTTP chunked response to emulate a video stream using Proxygen and Folly?

我正在編寫一個基於 Facebooks Proxygen 的 HTTP 視頻流服務器。 沒有尋求計划。 使用proxygen::ResponseBuilder我能夠將 webm 編碼的視頻塊作為 HTTP 響應發送,即分塊傳輸編碼正在工作。 我的問題是,Proxygen 甚至在發送響應標頭之前等待proxygen::ResponseBuilder::sendWithEOM() 我希望它在每次調用proxygen::ResponseBuilder::send()后盡快實際發送數據。

我嘗試使用evb->runInLoop()evb->runInEventBaseThread()從 EventBaseThread 執行的 lambda 中運行 ResponseBuilder 調用

using namespace folly;
using namespace proxygen;

std::thread t([&](){
    EventBase* evb = EventBaseManager::get()->getExistingEventBase();    
    // send headers ...
    while ( chunks avail. ) {
        //...
        evb->runInLoop([&](){
            ResponseBuilder(downstream_)
                     .body(std::move(chunk))
                     .send();
        });
        //... 
    }
    // sendWithEOM ...
});
t.detach();

這段代碼是從我的RequestHandleronRequest()方法調用的。 我試圖調用ResponseBuilder::send()而不將其包裝到evb->runInLoop() ,但是帶有 Folly v0.42.0 的 Proxygen v0.25.0 禁止使用斷言從另一個線程調用ResponseBuilder::send() 我從這里刪除了這個斷言: https : //github.com/facebook/folly/blob/v0.42.0/folly/io/async/EventBase.cpp#L491

現在模擬流正在工作,但如果有並行請求,它就會崩潰。 我想它不應該像這樣使用,這就是斷言的用途。 但也許有人知道如何為我的用例正確使用 Proxygen 基礎設施?

那有同樣的問題。 我讓它與這樣的事情一起工作。

folly::EventBase* eventBase = folly::EventBaseManager::get()->getExistingEventBase();
thread t([&, eventBase]() {
    while( chunks exist ) {
        auto chunk = getChunk();    
        eventBase->runInEventBaseThread([&, chunk=chunk]() mutable {
            ResponseBuilder(downstream_).body(move(chunk)).send();
        });
    }
});
// sendWithEOM ...
t.detach();
using namespace folly;
using namespace proxygen;

//Get Event Base in worker thread and pass it to new thread. If you call this inside new thread then you won't get worker thread's event base.
EventBase* evb = EventBaseManager::get()->getExistingEventBase();   
std::thread t([&, evb](){

// send headers ...
while ( chunks avail. ) {
    //...
    evb->runInLoop([&](){
        ResponseBuilder(downstream_)
                 .body(std::move(chunk))
                 .send();
    });
    //... 
}
// sendWithEOM ...
});
t.detach();

所以不需要在 EventBase 源中注釋斷言。

暫無
暫無

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

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