cmake/Utilities/cmcurl/lib/curl_rtmp.c

337 lines
12 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2021-09-14 00:13:48 +02:00
* Copyright (C) 2012 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
* Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "curl_setup.h"
#ifdef USE_LIBRTMP
2017-07-20 19:35:53 +02:00
#include "curl_rtmp.h"
2015-04-27 22:25:09 +02:00
#include "urldata.h"
#include "nonblock.h" /* for curlx_nonblock */
#include "progress.h" /* for Curl_pgrsSetUploadSize */
#include "transfer.h"
#include "warnless.h"
#include <curl/curl.h>
#include <librtmp/rtmp.h>
#include "curl_memory.h"
/* The last #include file should be: */
#include "memdebug.h"
2018-11-29 20:27:00 +01:00
#if defined(WIN32) && !defined(USE_LWIPSOCK)
2015-04-27 22:25:09 +02:00
#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
#define SET_RCVTIMEO(tv,s) int tv = s*1000
2018-11-29 20:27:00 +01:00
#elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
#define SET_RCVTIMEO(tv,s) int tv = s*1000
2015-04-27 22:25:09 +02:00
#else
#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
#endif
#define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_setup_connection(struct Curl_easy *data,
struct connectdata *conn);
static CURLcode rtmp_do(struct Curl_easy *data, bool *done);
static CURLcode rtmp_done(struct Curl_easy *data, CURLcode, bool premature);
static CURLcode rtmp_connect(struct Curl_easy *data, bool *done);
static CURLcode rtmp_disconnect(struct Curl_easy *data,
struct connectdata *conn, bool dead);
2015-04-27 22:25:09 +02:00
static Curl_recv rtmp_recv;
static Curl_send rtmp_send;
/*
2016-09-11 17:22:32 +02:00
* RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
2015-04-27 22:25:09 +02:00
*/
const struct Curl_handler Curl_handler_rtmp = {
"RTMP", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMP, /* defport */
CURLPROTO_RTMP, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMP, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
const struct Curl_handler Curl_handler_rtmpt = {
"RTMPT", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMPT, /* defport */
CURLPROTO_RTMPT, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMPT, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
const struct Curl_handler Curl_handler_rtmpe = {
"RTMPE", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMP, /* defport */
CURLPROTO_RTMPE, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMPE, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
const struct Curl_handler Curl_handler_rtmpte = {
"RTMPTE", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMPT, /* defport */
CURLPROTO_RTMPTE, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMPTE, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
const struct Curl_handler Curl_handler_rtmps = {
"RTMPS", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMPS, /* defport */
CURLPROTO_RTMPS, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMP, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
const struct Curl_handler Curl_handler_rtmpts = {
"RTMPTS", /* scheme */
2015-11-17 17:22:37 +01:00
rtmp_setup_connection, /* setup_connection */
2015-04-27 22:25:09 +02:00
rtmp_do, /* do_it */
rtmp_done, /* done */
ZERO_NULL, /* do_more */
rtmp_connect, /* connect_it */
ZERO_NULL, /* connecting */
ZERO_NULL, /* doing */
ZERO_NULL, /* proto_getsock */
ZERO_NULL, /* doing_getsock */
ZERO_NULL, /* domore_getsock */
ZERO_NULL, /* perform_getsock */
rtmp_disconnect, /* disconnect */
ZERO_NULL, /* readwrite */
2018-01-26 17:06:56 +01:00
ZERO_NULL, /* connection_check */
2021-09-14 00:13:48 +02:00
ZERO_NULL, /* attach connection */
2015-04-27 22:25:09 +02:00
PORT_RTMPS, /* defport */
CURLPROTO_RTMPTS, /* protocol */
2021-09-14 00:13:48 +02:00
CURLPROTO_RTMPT, /* family */
2015-04-27 22:25:09 +02:00
PROTOPT_NONE /* flags*/
};
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_setup_connection(struct Curl_easy *data,
struct connectdata *conn)
2015-04-27 22:25:09 +02:00
{
RTMP *r = RTMP_Alloc();
if(!r)
return CURLE_OUT_OF_MEMORY;
RTMP_Init(r);
RTMP_SetBufferMS(r, DEF_BUFTIME);
2021-09-14 00:13:48 +02:00
if(!RTMP_SetupURL(r, data->state.url)) {
2015-04-27 22:25:09 +02:00
RTMP_Free(r);
return CURLE_URL_MALFORMAT;
}
2020-08-30 11:54:41 +02:00
conn->proto.rtmp = r;
2015-04-27 22:25:09 +02:00
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_connect(struct Curl_easy *data, bool *done)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2020-08-30 11:54:41 +02:00
RTMP *r = conn->proto.rtmp;
2015-11-17 17:22:37 +01:00
SET_RCVTIMEO(tv, 10);
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
2015-04-27 22:25:09 +02:00
/* We have to know if it's a write before we send the
* connect request packet
*/
2021-09-14 00:13:48 +02:00
if(data->set.upload)
2015-04-27 22:25:09 +02:00
r->Link.protocol |= RTMP_FEATURE_WRITE;
/* For plain streams, use the buffer toggle trick to keep data flowing */
if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
!(r->Link.protocol & RTMP_FEATURE_HTTP))
r->Link.lFlags |= RTMP_LF_BUFX;
2015-11-17 17:22:37 +01:00
(void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
2015-04-27 22:25:09 +02:00
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
(char *)&tv, sizeof(tv));
if(!RTMP_Connect1(r, NULL))
return CURLE_FAILED_INIT;
/* Clients must send a periodic BytesReceived report to the server */
r->m_bSendCounter = true;
*done = TRUE;
conn->recv[FIRSTSOCKET] = rtmp_recv;
conn->send[FIRSTSOCKET] = rtmp_send;
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_do(struct Curl_easy *data, bool *done)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2020-08-30 11:54:41 +02:00
RTMP *r = conn->proto.rtmp;
2015-04-27 22:25:09 +02:00
if(!RTMP_ConnectStream(r, 0))
return CURLE_FAILED_INIT;
2021-09-14 00:13:48 +02:00
if(data->set.upload) {
2019-11-11 23:01:05 +01:00
Curl_pgrsSetUploadSize(data, data->state.infilesize);
Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
2015-04-27 22:25:09 +02:00
}
else
2019-11-11 23:01:05 +01:00
Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
2015-04-27 22:25:09 +02:00
*done = TRUE;
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_done(struct Curl_easy *data, CURLcode status,
2015-04-27 22:25:09 +02:00
bool premature)
{
2021-09-14 00:13:48 +02:00
(void)data; /* unused */
2015-04-27 22:25:09 +02:00
(void)status; /* unused */
(void)premature; /* unused */
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static CURLcode rtmp_disconnect(struct Curl_easy *data,
struct connectdata *conn,
2015-04-27 22:25:09 +02:00
bool dead_connection)
{
2020-08-30 11:54:41 +02:00
RTMP *r = conn->proto.rtmp;
2021-09-14 00:13:48 +02:00
(void)data;
2015-04-27 22:25:09 +02:00
(void)dead_connection;
if(r) {
2020-08-30 11:54:41 +02:00
conn->proto.rtmp = NULL;
2015-04-27 22:25:09 +02:00
RTMP_Close(r);
RTMP_Free(r);
}
return CURLE_OK;
}
2021-09-14 00:13:48 +02:00
static ssize_t rtmp_recv(struct Curl_easy *data, int sockindex, char *buf,
2015-04-27 22:25:09 +02:00
size_t len, CURLcode *err)
{
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2020-08-30 11:54:41 +02:00
RTMP *r = conn->proto.rtmp;
2015-04-27 22:25:09 +02:00
ssize_t nread;
(void)sockindex; /* unused */
nread = RTMP_Read(r, buf, curlx_uztosi(len));
if(nread < 0) {
if(r->m_read.status == RTMP_READ_COMPLETE ||
2021-09-14 00:13:48 +02:00
r->m_read.status == RTMP_READ_EOF) {
data->req.size = data->req.bytecount;
2015-04-27 22:25:09 +02:00
nread = 0;
}
else
*err = CURLE_RECV_ERROR;
}
return nread;
}
2021-09-14 00:13:48 +02:00
static ssize_t rtmp_send(struct Curl_easy *data, int sockindex,
2015-04-27 22:25:09 +02:00
const void *buf, size_t len, CURLcode *err)
{
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2020-08-30 11:54:41 +02:00
RTMP *r = conn->proto.rtmp;
2015-04-27 22:25:09 +02:00
ssize_t num;
(void)sockindex; /* unused */
num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
if(num < 0)
*err = CURLE_SEND_ERROR;
return num;
}
#endif /* USE_LIBRTMP */