espressif / esp-adf

Espressif Audio Development Framework
Other
1.53k stars 672 forks source link

Correct support for HTTP range requests in http_stream.c module (AUD-4549) #989

Closed ador3lora closed 1 year ago

ador3lora commented 1 year ago

Environment

Problem

In our project, we use HTTP range requests for cases when the connection breaks during track playback in order to resume playback from the place of the break when the connection is restored. During the tests, it was found that in the case when the server does not issue all the data at once, but several data ranges sequentially, the http_stream.c module interprets 0 received from the server as the end of the entire track, and not as the end of only the data range.

Code inside the _http_read function of the http_stream.c module


    if (rlen <= 0) {
        http->_errno = esp_http_client_get_errno(http->client);
        ESP_LOGW(TAG, "No more data,errno:%d, total_bytes:%llu, rlen = %d", http->_errno, info.byte_pos, rlen);
        if (http->_errno != 0) {  // Error occuered, reset connection
            ESP_LOGW(TAG, "Got %d errno(%s)", http->_errno, strerror(http->_errno));
            return http->_errno;
        }
        if (http->auto_connect_next_track) {
            if (dispatch_hook(self, HTTP_STREAM_FINISH_PLAYLIST, NULL, 0) != ESP_OK) {
                ESP_LOGE(TAG, "Failed to process user callback");
                return ESP_FAIL;
            }
        } else {
            if (dispatch_hook(self, HTTP_STREAM_FINISH_TRACK, NULL, 0) != ESP_OK) {
                ESP_LOGE(TAG, "Failed to process user callback");
                return ESP_FAIL;
            }
        }
        return ESP_OK;
    } else {
        audio_element_update_byte_pos(self, rlen);
    }

Actually, there is no check whether only the range or all the data has been read. After we read one range of data, there was a transition to the next track.

Solution

We added a check to see if the server returns the header Content-Range and if the request is a range request, then after receiving zero, an attempt is made to get the next range. If the server returns zero again, then the current track data is considered read.

I am attaching a patch for the http_stream.c module to this fix request.

Thanks for your attention. :)

esp-adf_v2.4.1_http_stream.c_correct_support_for_http_range_requests.zip

jason-mao commented 1 year ago

@ador3lora Thank your for your solution, let me check .