swisspol / GCDWebServer

The #1 HTTP server for iOS, macOS & tvOS (also includes web based uploader & WebDAV server)
Other
6.51k stars 1.25k forks source link

GCDWebServerStreamedResponse and AVPlayer returning error. #548

Closed goines closed 3 years ago

goines commented 3 years ago

I have an HLS streaming app and I'm using GCDWebServer as a reverse proxy for authentication, and extra data (playlists and iframe) manipulation. I noticed that the NSURLSessionDataTask that downloads our mp4 segments is slowing down our playback on slower connections as some of our segments might be 5MB and it downloads the whole thing before returning. I've tried to implement a solution using GCDWebServerStreamedResponse and I'm able to pass in the chunked data from our server (in ~40k chunks), but before the download is complete for the AVAsset segment, AVPlayer has already failed the item:

CoreMediaErrorDomain error -12939 - byte range and no content length - error code is 200

The error code matches the GCDWebServerStreamedResponse's status code, as I've tried with 206 (matching our servers reply) without any changes.

I've seen examples of using GCDWebServerStreamedResponse from a file, but is there any magic to having it go back into an AVPlayer for streaming?

goines commented 3 years ago

I realized what the issue is; the GCDWebServerStreamedResponse object really needs to have all of its properties set before the data is flowing. So, when you're feeding that data from a separate NSURLSessionDataTask you need to kick off the data task, wait for the response/header to come form that and then create the GCDWebServerStreamedResponse object with the values from the data task.

If you do it that way you'll have everything you need (as long as the real server is returning expectedContentLength) to created the streamed response, and then queue up any body reader completion blocks until data is ready from the data task.

Thanks!