2010-05-12 17:07:20 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-03-03 06:36:18 -05:00
|
|
|
* Copyright (C) 2012 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-05-12 17:07:20 -04: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
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2010-05-12 17:07:20 -04: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2010-05-12 17:07:20 -04:00
|
|
|
|
|
|
|
#ifdef USE_LIBRTMP
|
|
|
|
|
2017-04-29 13:17:51 -04:00
|
|
|
#include "curl_rtmp.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "nonblock.h" /* for curlx_nonblock */
|
|
|
|
#include "progress.h" /* for Curl_pgrsSetUploadSize */
|
|
|
|
#include "transfer.h"
|
|
|
|
#include "warnless.h"
|
2010-05-12 17:07:20 -04:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <librtmp/rtmp.h>
|
2010-06-02 08:13:02 -04:00
|
|
|
#include "curl_memory.h"
|
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2010-06-02 08:13:02 -04:00
|
|
|
|
rtmp: fix for compiling with lwIP
Compiling on _WIN32 and with USE_LWIPSOCK, causes this error:
curl_rtmp.c(223,3): error: use of undeclared identifier 'setsockopt'
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
^
curl_rtmp.c(41,32): note: expanded from macro 'setsockopt'
#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
^
Closes #3155
2018-10-22 04:33:44 -04:00
|
|
|
#if defined(WIN32) && !defined(USE_LWIPSOCK)
|
2010-05-12 17:07:20 -04: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
|
rtmp: fix for compiling with lwIP
Compiling on _WIN32 and with USE_LWIPSOCK, causes this error:
curl_rtmp.c(223,3): error: use of undeclared identifier 'setsockopt'
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
^
curl_rtmp.c(41,32): note: expanded from macro 'setsockopt'
#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
^
Closes #3155
2018-10-22 04:33:44 -04:00
|
|
|
#elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
|
|
|
|
#define SET_RCVTIMEO(tv,s) int tv = s*1000
|
2010-05-12 17:07:20 -04:00
|
|
|
#else
|
|
|
|
#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
|
|
|
|
|
2014-12-24 12:12:13 -05:00
|
|
|
static CURLcode rtmp_setup_connection(struct connectdata *conn);
|
2010-05-12 17:07:20 -04:00
|
|
|
static CURLcode rtmp_do(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode rtmp_done(struct connectdata *conn, CURLcode, bool premature);
|
|
|
|
static CURLcode rtmp_connect(struct connectdata *conn, bool *done);
|
2011-04-20 09:17:42 -04:00
|
|
|
static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
|
2010-05-12 17:07:20 -04:00
|
|
|
|
|
|
|
static Curl_recv rtmp_recv;
|
|
|
|
static Curl_send rtmp_send;
|
|
|
|
|
|
|
|
/*
|
2016-02-02 23:09:25 -05:00
|
|
|
* RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
|
2010-05-12 17:07:20 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_rtmp = {
|
|
|
|
"RTMP", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMP, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMP, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_rtmpt = {
|
|
|
|
"RTMPT", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMPT, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMPT, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_rtmpe = {
|
|
|
|
"RTMPE", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMP, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMPE, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_rtmpte = {
|
|
|
|
"RTMPTE", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMPT, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMPTE, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_rtmps = {
|
|
|
|
"RTMPS", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMPS, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMPS, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
2011-05-05 10:27:03 -04:00
|
|
|
|
2010-05-12 17:07:20 -04:00
|
|
|
const struct Curl_handler Curl_handler_rtmpts = {
|
|
|
|
"RTMPTS", /* scheme */
|
2014-12-24 12:12:13 -05:00
|
|
|
rtmp_setup_connection, /* setup_connection */
|
2010-05-12 17:07:20 -04: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 */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-12 17:07:20 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
rtmp_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 07:09:56 -04:00
|
|
|
ZERO_NULL, /* connection_check */
|
2010-05-12 17:07:20 -04:00
|
|
|
PORT_RTMPS, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_RTMPTS, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags*/
|
2010-05-12 17:07:20 -04:00
|
|
|
};
|
|
|
|
|
2014-12-24 12:12:13 -05:00
|
|
|
static CURLcode rtmp_setup_connection(struct connectdata *conn)
|
2010-05-12 17:07:20 -04:00
|
|
|
{
|
|
|
|
RTMP *r = RTMP_Alloc();
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!r)
|
2010-05-12 17:07:20 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
RTMP_Init(r);
|
|
|
|
RTMP_SetBufferMS(r, DEF_BUFTIME);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!RTMP_SetupURL(r, conn->data->change.url)) {
|
2010-05-12 17:07:20 -04:00
|
|
|
RTMP_Free(r);
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
|
|
|
conn->proto.generic = r;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
RTMP *r = conn->proto.generic;
|
2015-03-17 08:41:49 -04:00
|
|
|
SET_RCVTIMEO(tv, 10);
|
2010-05-12 17:07:20 -04:00
|
|
|
|
2017-07-07 05:52:48 -04:00
|
|
|
r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
|
2010-05-12 17:07:20 -04:00
|
|
|
|
|
|
|
/* We have to know if it's a write before we send the
|
|
|
|
* connect request packet
|
|
|
|
*/
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->data->set.upload)
|
2010-05-12 17:07:20 -04:00
|
|
|
r->Link.protocol |= RTMP_FEATURE_WRITE;
|
|
|
|
|
|
|
|
/* For plain streams, use the buffer toggle trick to keep data flowing */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
|
|
|
|
!(r->Link.protocol & RTMP_FEATURE_HTTP))
|
2010-05-12 17:07:20 -04:00
|
|
|
r->Link.lFlags |= RTMP_LF_BUFX;
|
|
|
|
|
2014-10-04 09:14:27 -04:00
|
|
|
(void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
|
2011-04-20 09:17:42 -04:00
|
|
|
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(char *)&tv, sizeof(tv));
|
2010-05-12 17:07:20 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!RTMP_Connect1(r, NULL))
|
2010-05-12 17:07:20 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode rtmp_do(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
RTMP *r = conn->proto.generic;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!RTMP_ConnectStream(r, 0))
|
2010-05-12 17:07:20 -04:00
|
|
|
return CURLE_FAILED_INIT;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->data->set.upload) {
|
2014-04-15 07:49:18 -04:00
|
|
|
Curl_pgrsSetUploadSize(conn->data, conn->data->state.infilesize);
|
2010-05-12 17:07:20 -04:00
|
|
|
Curl_setup_transfer(conn, -1, -1, FALSE, NULL, FIRSTSOCKET, NULL);
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else
|
2010-05-12 17:07:20 -04:00
|
|
|
Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
|
|
|
|
bool premature)
|
|
|
|
{
|
2010-05-25 09:43:41 -04:00
|
|
|
(void)conn; /* unused */
|
|
|
|
(void)status; /* unused */
|
|
|
|
(void)premature; /* unused */
|
|
|
|
|
2010-05-12 17:07:20 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-06 09:33:38 -05:00
|
|
|
static CURLcode rtmp_disconnect(struct connectdata *conn,
|
|
|
|
bool dead_connection)
|
2010-05-12 17:07:20 -04:00
|
|
|
{
|
|
|
|
RTMP *r = conn->proto.generic;
|
2010-12-06 09:33:38 -05:00
|
|
|
(void)dead_connection;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(r) {
|
2010-05-12 17:07:20 -04:00
|
|
|
conn->proto.generic = NULL;
|
|
|
|
RTMP_Close(r);
|
|
|
|
RTMP_Free(r);
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
|
|
|
|
size_t len, CURLcode *err)
|
|
|
|
{
|
|
|
|
RTMP *r = conn->proto.generic;
|
|
|
|
ssize_t nread;
|
|
|
|
|
2010-05-25 09:43:41 -04:00
|
|
|
(void)sockindex; /* unused */
|
|
|
|
|
2012-09-28 17:57:04 -04:00
|
|
|
nread = RTMP_Read(r, buf, curlx_uztosi(len));
|
2011-04-20 09:17:42 -04:00
|
|
|
if(nread < 0) {
|
|
|
|
if(r->m_read.status == RTMP_READ_COMPLETE ||
|
2010-05-12 17:07:20 -04:00
|
|
|
r->m_read.status == RTMP_READ_EOF) {
|
|
|
|
conn->data->req.size = conn->data->req.bytecount;
|
|
|
|
nread = 0;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else
|
2010-05-12 17:07:20 -04:00
|
|
|
*err = CURLE_RECV_ERROR;
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
|
|
|
|
const void *buf, size_t len, CURLcode *err)
|
|
|
|
{
|
|
|
|
RTMP *r = conn->proto.generic;
|
|
|
|
ssize_t num;
|
|
|
|
|
2010-05-25 09:43:41 -04:00
|
|
|
(void)sockindex; /* unused */
|
|
|
|
|
2012-09-28 17:57:04 -04:00
|
|
|
num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
|
2011-04-20 09:17:42 -04:00
|
|
|
if(num < 0)
|
2010-05-12 17:07:20 -04:00
|
|
|
*err = CURLE_SEND_ERROR;
|
2011-04-20 09:17:42 -04:00
|
|
|
|
2010-05-12 17:07:20 -04:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
#endif /* USE_LIBRTMP */
|