CURLOPT_IN...VEFUNCTION(3)

πŸŽ€ NAME πŸŽ€

CURLOPT_INTERLEAVEFUNCTION β€” okay so this is the callback for RTSP interleaved data, and yes bestie the page header is literally CURLOPT_IN...VEFUNCTION(3) because it got truncated, that's just how the Library Functions Manual rolls, iykyk πŸ’…


✨ SYNOPSIS ✨

#include <curl/curl.h>

size_t interleave_callback(void *ptr, size_t size, size_t nmemb,
                           void *userdata);

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_INTERLEAVEFUNCTION,
                          interleave_callback);

πŸ¦‹ DESCRIPTION πŸ¦‹

So you pass in a pointer to your callback function, and it has to match the prototype up there. No improvising, okay?

This callback gets called by libcurl the literal second it has received interleaved RTP data. It gets called for each $ block, so each call contains exactly one upper-layer protocol unit (like, one RTP packet, for example). curl writes the interleaved header as well as the included data on every single call. The first byte is always an ASCII dollar sign πŸ’° β€” and that dollar sign is followed by a one byte channel identifier, and then a 2 byte integer length in network byte order. If you wanna know more about how RTP interleaving behaves, go read RFC 2326 Section 10.12. And if this is unset or set to NULL, curl just uses the default write function, no biggie.

Bestie, interleaved RTP is lowkey a lot for the client application. Since the stream data is sharing the RTSP control connection, servicing the RTP in a timely fashion is, like, critical. If the RTP data is not handled quickly, subsequent response processing may become unreasonably delayed and the connection may close 😭. The application may use CURL_RTSPREQ_RECEIVE to service RTP data when no requests are desired. And if the application makes a request (e.g. CURL_RTSPREQ_PAUSE) then the response handler processes any pending RTP data before marking the request as finished.

Also, CURLOPT_INTERLEAVEDATA(3) is what gets passed in the userdata argument in the callback. That's the vibe. 🫢

Your callback should return the number of bytes actually taken care of. If that amount differs from the amount passed to your callback function, it signals an error condition to the library β€” the transfer aborts and the libcurl function used returns CURLE_WRITE_ERROR. πŸ’€

You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0) ✨


🌸 DEFAULT 🌸

NULL β€” the interleave data is then passed to the regular write function: CURLOPT_WRITEFUNCTION(3).


πŸ‘‘ PROTOCOLS πŸ‘‘

This functionality affects rtsp only. That's it. That's the whole protocol list. πŸ’…


🌈 EXAMPLE 🌈

struct local {
  void *custom;
};

static size_t rtp_write(void *ptr, size_t size, size_t nmemb, void *userp)
{
  struct local *l = userp;
  printf("our ptr: %p\n", l->custom);
  /* take care of the packet in 'ptr', then return... */
  return size * nmemb;
}

int main(void)
{
  struct local rtp_data;
  CURL *curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_INTERLEAVEFUNCTION, rtp_write);
    curl_easy_setopt(curl, CURLOPT_INTERLEAVEDATA, &rtp_data);
  }
}

⭐ AVAILABILITY ⭐

Added in curl 7.20.0 β€” vintage, but make it cute πŸŽ€


πŸ’œ RETURN VALUE πŸ’œ

curl_easy_setopt(3) returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK πŸ₯³, and non-zero means an error occurred, so go see libcurl-errors(3).


πŸ“ SEE ALSO πŸ“


and that's the whole page bestie, xoxo πŸ’–

This one's from the libcurl fam, dated 2026-10-12 β€” see you in the next manpage, mwah πŸ§šβ€β™€οΈβœ¨

πŸ“¦ where this page comes from