/** * Open RTMP connection and verify that the stream can be played. * * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]... * where 'app' is first one or two directories in the path * (e.g. /ondemand/, /flash/live/, etc.) * and 'playpath' is a file name (the rest of the path, * may be prefixed with "mp4:") * * Additional RTMP library options may be appended as * space-separated key-value pairs. */ static int rtmp_open(URLContext *s, const char *uri, int flags) { LibRTMPContext *ctx = s->priv_data; RTMP *r = &ctx->rtmp; int rc = 0, level; char *filename = s->filename; switch (av_log_get_level()) { default: case AV_LOG_FATAL: level = RTMP_LOGCRIT; break; case AV_LOG_ERROR: level = RTMP_LOGERROR; break; case AV_LOG_WARNING: level = RTMP_LOGWARNING; break; case AV_LOG_INFO: level = RTMP_LOGINFO; break; case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break; case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break; } RTMP_LogSetLevel(level); RTMP_LogSetCallback(rtmp_log); if (ctx->app || ctx->playpath) { int len = strlen(s->filename) + 1; if (ctx->app) len += strlen(ctx->app) + sizeof(" app="); if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath="); if (!(filename = av_malloc(len))) return AVERROR(ENOMEM); av_strlcpy(filename, s->filename, len); if (ctx->app) { av_strlcat(filename, " app=", len); av_strlcat(filename, ctx->app, len); } if (ctx->playpath) { av_strlcat(filename, " playpath=", len); av_strlcat(filename, ctx->playpath, len); } } RTMP_Init(r); if (!RTMP_SetupURL(r, filename)) { rc = AVERROR_UNKNOWN; goto fail; } if (flags & AVIO_FLAG_WRITE) RTMP_EnableWrite(r); if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) { rc = AVERROR_UNKNOWN; goto fail; } s->is_streamed = 1; rc = 0; fail: if (filename != s->filename) av_freep(&filename); return rc; }
/** * Open RTMP connection and verify that the stream can be played. * * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]... * where 'app' is first one or two directories in the path * (e.g. /ondemand/, /flash/live/, etc.) * and 'playpath' is a file name (the rest of the path, * may be prefixed with "mp4:") * * Additional RTMP library options may be appended as * space-separated key-value pairs. */ static int rtmp_open(URLContext *s, const char *uri, int flags) { RTMP *r; int rc; r = av_mallocz(sizeof(RTMP)); if (!r) return AVERROR(ENOMEM); switch (av_log_get_level()) { default: case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break; case AV_LOG_ERROR: rc = RTMP_LOGERROR; break; case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break; case AV_LOG_INFO: rc = RTMP_LOGINFO; break; case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break; case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break; } RTMP_LogSetLevel(rc); RTMP_LogSetCallback(rtmp_log); RTMP_Init(r); if (!RTMP_SetupURL(r, s->filename)) { rc = -1; goto fail; } if (flags & AVIO_WRONLY) RTMP_EnableWrite(r); if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) { rc = -1; goto fail; } s->priv_data = r; s->is_streamed = 1; return 0; fail: av_free(r); return rc; }
static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) { struct rtmp_stream *stream = bzalloc(sizeof(struct rtmp_stream)); stream->output = output; pthread_mutex_init_value(&stream->packets_mutex); RTMP_Init(&stream->rtmp); RTMP_LogSetCallback(log_rtmp); RTMP_LogSetLevel(RTMP_LOGWARNING); if (pthread_mutex_init(&stream->packets_mutex, NULL) != 0) goto fail; if (os_event_init(&stream->stop_event, OS_EVENT_TYPE_MANUAL) != 0) goto fail; if (pthread_mutex_init(&stream->write_buf_mutex, NULL) != 0) { warn("Failed to initialize write buffer mutex"); goto fail; } if (os_event_init(&stream->buffer_space_available_event, OS_EVENT_TYPE_AUTO) != 0) { warn("Failed to initialize write buffer event"); goto fail; } if (os_event_init(&stream->buffer_has_data_event, OS_EVENT_TYPE_AUTO) != 0) { warn("Failed to initialize data buffer event"); goto fail; } if (os_event_init(&stream->socket_available_event, OS_EVENT_TYPE_AUTO) != 0) { warn("Failed to initialize socket buffer event"); goto fail; } if (os_event_init(&stream->send_thread_signaled_exit, OS_EVENT_TYPE_MANUAL) != 0) { warn("Failed to initialize socket exit event"); goto fail; } UNUSED_PARAMETER(settings); return stream; fail: rtmp_stream_destroy(stream); return NULL; }
static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) { struct rtmp_stream *stream = bzalloc(sizeof(struct rtmp_stream)); stream->output = output; pthread_mutex_init_value(&stream->packets_mutex); RTMP_Init(&stream->rtmp); RTMP_LogSetCallback(log_rtmp); RTMP_LogSetLevel(RTMP_LOGWARNING); if (pthread_mutex_init(&stream->packets_mutex, NULL) != 0) goto fail; if (os_event_init(&stream->stop_event, OS_EVENT_TYPE_MANUAL) != 0) goto fail; UNUSED_PARAMETER(settings); return stream; fail: rtmp_stream_destroy(stream); return NULL; }
/** * Open RTMP connection and verify that the stream can be played. * * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]... * where 'app' is first one or two directories in the path * (e.g. /ondemand/, /flash/live/, etc.) * and 'playpath' is a file name (the rest of the path, * may be prefixed with "mp4:") * * Additional RTMP library options may be appended as * space-separated key-value pairs. */ static int rtmp_open(URLContext *s, const char *uri, int flags) { LibRTMPContext *ctx = s->priv_data; RTMP *r = &ctx->rtmp; int rc = 0, level; char *filename = s->filename; int len = strlen(s->filename) + 1; switch (av_log_get_level()) { default: case AV_LOG_FATAL: level = RTMP_LOGCRIT; break; case AV_LOG_ERROR: level = RTMP_LOGERROR; break; case AV_LOG_WARNING: level = RTMP_LOGWARNING; break; case AV_LOG_INFO: level = RTMP_LOGINFO; break; case AV_LOG_VERBOSE: level = RTMP_LOGDEBUG; break; case AV_LOG_DEBUG: level = RTMP_LOGDEBUG2; break; } RTMP_LogSetLevel(level); RTMP_LogSetCallback(rtmp_log); if (ctx->app) len += strlen(ctx->app) + sizeof(" app="); if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl="); if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl="); if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver="); if (ctx->conn) { char *sep, *p = ctx->conn; int options = 0; while (p) { options++; p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); if (sep) p = sep + 1; else break; } len += options * sizeof(" conn="); len += strlen(ctx->conn); } if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath="); if (ctx->live) len += sizeof(" live=1"); if (ctx->subscribe) len += strlen(ctx->subscribe) + sizeof(" subscribe="); if (ctx->client_buffer_time) len += strlen(ctx->client_buffer_time) + sizeof(" buffer="); if (ctx->swfurl || ctx->swfverify) { len += sizeof(" swfUrl="); if (ctx->swfverify) len += strlen(ctx->swfverify) + sizeof(" swfVfy=1"); else len += strlen(ctx->swfurl); } if (!(ctx->temp_filename = filename = av_malloc(len))) return AVERROR(ENOMEM); av_strlcpy(filename, s->filename, len); if (ctx->app) { av_strlcat(filename, " app=", len); av_strlcat(filename, ctx->app, len); } if (ctx->tcurl) { av_strlcat(filename, " tcUrl=", len); av_strlcat(filename, ctx->tcurl, len); } if (ctx->pageurl) { av_strlcat(filename, " pageUrl=", len); av_strlcat(filename, ctx->pageurl, len); } if (ctx->swfurl) { av_strlcat(filename, " swfUrl=", len); av_strlcat(filename, ctx->swfurl, len); } if (ctx->flashver) { av_strlcat(filename, " flashVer=", len); av_strlcat(filename, ctx->flashver, len); } if (ctx->conn) { char *sep, *p = ctx->conn; while (p) { av_strlcat(filename, " conn=", len); p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); if (sep) *sep = '\0'; av_strlcat(filename, p, len); if (sep) p = sep + 1; else break; } } if (ctx->playpath) { av_strlcat(filename, " playpath=", len); av_strlcat(filename, ctx->playpath, len); } if (ctx->live) av_strlcat(filename, " live=1", len); if (ctx->subscribe) { av_strlcat(filename, " subscribe=", len); av_strlcat(filename, ctx->subscribe, len); } if (ctx->client_buffer_time) { av_strlcat(filename, " buffer=", len); av_strlcat(filename, ctx->client_buffer_time, len); } if (ctx->swfurl || ctx->swfverify) { av_strlcat(filename, " swfUrl=", len); if (ctx->swfverify) { av_strlcat(filename, ctx->swfverify, len); av_strlcat(filename, " swfVfy=1", len); } else { av_strlcat(filename, ctx->swfurl, len); } } RTMP_Init(r); if (!RTMP_SetupURL(r, filename)) { rc = AVERROR_UNKNOWN; goto fail; } if (flags & AVIO_FLAG_WRITE) RTMP_EnableWrite(r); if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) { rc = AVERROR_UNKNOWN; goto fail; } #if CONFIG_NETWORK if (ctx->buffer_size >= 0 && (flags & AVIO_FLAG_WRITE)) { int tmp = ctx->buffer_size; setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)); } #endif s->is_streamed = 1; return 0; fail: av_freep(&ctx->temp_filename); if (rc) RTMP_Close(r); return rc; }
DWORD WINAPI RTMPPublisher::CreateConnectionThread(RTMPPublisher *publisher) { //------------------------------------------------------ // set up URL bool bRetry = false; bool bSuccess = false; bool bCanRetry = false; String failReason; String strBindIP; int serviceID = AppConfig->GetInt (TEXT("Publish"), TEXT("Service")); String strURL = AppConfig->GetString(TEXT("Publish"), TEXT("URL")); String strPlayPath = AppConfig->GetString(TEXT("Publish"), TEXT("PlayPath")); strURL.KillSpaces(); strPlayPath.KillSpaces(); LPSTR lpAnsiURL = NULL, lpAnsiPlaypath = NULL; RTMP *rtmp = NULL; //-------------------------------- // unbelievably disgusting hack for elgato devices String strOldDirectory; UINT dirSize = GetCurrentDirectory(0, 0); strOldDirectory.SetLength(dirSize); GetCurrentDirectory(dirSize, strOldDirectory.Array()); OSSetCurrentDirectory(API->GetAppPath()); //-------------------------------- if(!strURL.IsValid()) { failReason = TEXT("No server specified to connect to"); goto end; } if(serviceID != 0) { XConfig serverData; if(!serverData.Open(TEXT("services.xconfig"))) { failReason = TEXT("Could not open services.xconfig"); goto end; } XElement *services = serverData.GetElement(TEXT("services")); if(!services) { failReason = TEXT("Could not find any services in services.xconfig"); goto end; } XElement *service = NULL; DWORD numServices = services->NumElements(); for(UINT i=0; i<numServices; i++) { XElement *curService = services->GetElementByID(i); if(curService->GetInt(TEXT("id")) == serviceID) { service = curService; break; } } if(!service) { failReason = TEXT("Could not find the service specified in services.xconfig"); goto end; } XElement *servers = service->GetElement(TEXT("servers")); if(!servers) { failReason = TEXT("Could not find any servers for the service specified in services.xconfig"); goto end; } XDataItem *item = servers->GetDataItem(strURL); if(!item) item = servers->GetDataItemByID(0); strURL = item->GetData(); Log(TEXT("Using RTMP service: %s"), service->GetName()); Log(TEXT(" Server selection: %s"), strURL.Array()); } //------------------------------------------------------ // now back to the elgato directory if it needs the directory changed still to function *sigh* OSSetCurrentDirectory(strOldDirectory); //------------------------------------------------------ rtmp = RTMP_Alloc(); RTMP_Init(rtmp); RTMP_LogSetCallback(librtmpErrorCallback); //RTMP_LogSetLevel(RTMP_LOGERROR); lpAnsiURL = strURL.CreateUTF8String(); lpAnsiPlaypath = strPlayPath.CreateUTF8String(); if(!RTMP_SetupURL2(rtmp, lpAnsiURL, lpAnsiPlaypath)) { failReason = Str("Connection.CouldNotParseURL"); goto end; } char *rtmpUser = AppConfig->GetString(TEXT("Publish"), TEXT("Username")).CreateUTF8String(); char *rtmpPass = AppConfig->GetString(TEXT("Publish"), TEXT("Password")).CreateUTF8String(); if (rtmpUser) { rtmp->Link.pubUser.av_val = rtmpUser; rtmp->Link.pubUser.av_len = (int)strlen(rtmpUser); } if (rtmpPass) { rtmp->Link.pubPasswd.av_val = rtmpPass; rtmp->Link.pubPasswd.av_len = (int)strlen(rtmpPass); } RTMP_EnableWrite(rtmp); //set it to publish rtmp->Link.swfUrl.av_len = rtmp->Link.tcUrl.av_len; rtmp->Link.swfUrl.av_val = rtmp->Link.tcUrl.av_val; /*rtmp->Link.pageUrl.av_len = rtmp->Link.tcUrl.av_len; rtmp->Link.pageUrl.av_val = rtmp->Link.tcUrl.av_val;*/ rtmp->Link.flashVer.av_val = "FMLE/3.0 (compatible; FMSc/1.0)"; rtmp->Link.flashVer.av_len = (int)strlen(rtmp->Link.flashVer.av_val); //----------------------------------------- UINT tcpBufferSize = AppConfig->GetInt(TEXT("Publish"), TEXT("TCPBufferSize"), 64*1024); if(tcpBufferSize < 8192) tcpBufferSize = 8192; else if(tcpBufferSize > 1024*1024) tcpBufferSize = 1024*1024; rtmp->m_outChunkSize = 4096;//RTMP_DEFAULT_CHUNKSIZE;// rtmp->m_bSendChunkSizeInfo = TRUE; rtmp->m_bUseNagle = TRUE; strBindIP = AppConfig->GetString(TEXT("Publish"), TEXT("BindToIP"), TEXT("Default")); if (scmp(strBindIP, TEXT("Default"))) { rtmp->m_bindIP.addr.sin_family = AF_INET; rtmp->m_bindIP.addrLen = sizeof(rtmp->m_bindIP.addr); if (WSAStringToAddress(strBindIP.Array(), AF_INET, NULL, (LPSOCKADDR)&rtmp->m_bindIP.addr, &rtmp->m_bindIP.addrLen) == SOCKET_ERROR) { // no localization since this should rarely/never happen failReason = TEXT("WSAStringToAddress: Could not parse address"); goto end; } } LogInterfaceType(rtmp); //----------------------------------------- if(!RTMP_Connect(rtmp, NULL)) { failReason = Str("Connection.CouldNotConnect"); failReason << TEXT("\r\n\r\n") << RTMPPublisher::GetRTMPErrors(); bCanRetry = true; goto end; } if(!RTMP_ConnectStream(rtmp, 0)) { failReason = Str("Connection.InvalidStream"); failReason << TEXT("\r\n\r\n") << RTMPPublisher::GetRTMPErrors(); bCanRetry = true; goto end; } //----------------------------------------- OSDebugOut(TEXT("Connected: %u\r\n"), OSGetTime()); publisher->RequestKeyframe(1000); //----------------------------------------- bSuccess = true; end: if (lpAnsiURL) Free(lpAnsiURL); if (lpAnsiPlaypath) Free(lpAnsiPlaypath); if(!bSuccess) { if(rtmp) { RTMP_Close(rtmp); RTMP_Free(rtmp); } if(failReason.IsValid()) App->SetStreamReport(failReason); if(!publisher->bStopping) PostMessage(hwndMain, OBS_REQUESTSTOP, bCanRetry ? 0 : 1, 0); Log(TEXT("Connection to %s failed: %s"), strURL.Array(), failReason.Array()); publisher->bStopping = true; } else { publisher->Init(rtmp, tcpBufferSize); publisher->bConnected = true; publisher->bConnecting = false; } return 0; }
static int rtmp_init(void) { RTMP_LogSetCallback(rtmp_log); return 0; }
int rtmp_open(LibRTMPContext *ctx , int flags) { RTMP *r = &ctx->rtmp; int rc = 0; char *filename = NULL; if(ctx->filename==NULL) return -1; int len = strlen(ctx->filename) + 1; RTMP_LogSetLevel(RTMP_LOGERROR); RTMP_LogSetCallback(rtmp_log); if (ctx->app) len += strlen(ctx->app) + sizeof(" app="); if (ctx->tcurl) len += strlen(ctx->tcurl) + sizeof(" tcUrl="); if (ctx->pageurl) len += strlen(ctx->pageurl) + sizeof(" pageUrl="); if (ctx->flashver) len += strlen(ctx->flashver) + sizeof(" flashver="); if (ctx->conn) { char *sep, *p = ctx->conn; int options = 0; while (p) { options++; p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); if (sep) p = sep + 1; else break; } len += options * sizeof(" conn="); len += strlen(ctx->conn); } if (ctx->playpath) len += strlen(ctx->playpath) + sizeof(" playpath="); if (ctx->live) len += sizeof(" live=1"); if (ctx->subscribe) len += strlen(ctx->subscribe) + sizeof(" subscribe="); if (ctx->client_buffer_time) len += strlen(ctx->client_buffer_time) + sizeof(" buffer="); if (ctx->swfurl || ctx->swfverify) { len += sizeof(" swfUrl="); if (ctx->swfverify) len += strlen(ctx->swfverify) + sizeof(" swfVfy=1"); else len += strlen(ctx->swfurl); } if (!(ctx->temp_filename = filename = malloc(len))) return ERROR_MEM; strlcpy(filename, ctx->filename, len); if (ctx->app) { strlcat(filename, " app=", len); strlcat(filename, ctx->app, len); } if (ctx->tcurl) { strlcat(filename, " tcUrl=", len); strlcat(filename, ctx->tcurl, len); } if (ctx->pageurl) { strlcat(filename, " pageUrl=", len); strlcat(filename, ctx->pageurl, len); } if (ctx->swfurl) { strlcat(filename, " swfUrl=", len); strlcat(filename, ctx->swfurl, len); } if (ctx->flashver) { strlcat(filename, " flashVer=", len); strlcat(filename, ctx->flashver, len); } if (ctx->conn) { char *sep, *p = ctx->conn; while (p) { strlcat(filename, " conn=", len); p += strspn(p, " "); if (!*p) break; sep = strchr(p, ' '); if (sep) *sep = '\0'; strlcat(filename, p, len); if (sep) p = sep + 1; } } if (ctx->playpath) { strlcat(filename, " playpath=", len); strlcat(filename, ctx->playpath, len); } if (ctx->live) strlcat(filename, " live=1", len); if (ctx->subscribe) { strlcat(filename, " subscribe=", len); strlcat(filename, ctx->subscribe, len); } if (ctx->client_buffer_time) { strlcat(filename, " buffer=", len); strlcat(filename, ctx->client_buffer_time, len); } if (ctx->swfurl || ctx->swfverify) { strlcat(filename, " swfUrl=", len); if (ctx->swfverify) { strlcat(filename, ctx->swfverify, len); strlcat(filename, " swfVfy=1", len); } else { strlcat(filename, ctx->swfurl, len); } } RTMP_Init(r); if (!RTMP_SetupURL(r, filename)) { rc = ERROR_SETUP; goto fail; } // if (flags & WRITE_FLAGS) RTMP_EnableWrite(r); if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) { rc = ERROR_CONNECT; goto fail; } /* if (ctx->buffer_size >= 0 && (flags & WRITE_FLAGS)) { int tmp = ctx->buffer_size; setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)); } */ return 0; fail: //free(ctx->temp_filename); if (rc) RTMP_Close(r); return rc; }