예제 #1
0
BaseProtocol *InboundHTTP4RTMP::Bind(string sid) {
	BaseProtocol *pResult = NULL;
	if (_pNearProtocol == NULL) {
		//14. This might be a new connection. Do we have that sid generated?
		if (!MAP_HAS1(_generatedSids, sid)) {
			FATAL("Invalid sid: %s", STR(sid));
			return false;
		}

		//15. See if we have to generate a new connection or we just pick up
		//a disconnected one
		if (MAP_HAS1(_protocolsBySid, sid)) {
			pResult = ProtocolManager::GetProtocol(_protocolsBySid[sid]);
		} else {
			pResult = new InboundRTMPProtocol();
			pResult->Initialize(GetCustomParameters());
			pResult->SetApplication(GetApplication());
			_protocolsBySid[sid] = pResult->GetId();
			SetNearProtocol(pResult);
			pResult->SetFarProtocol(this);
		}
	} else {
		pResult = _pNearProtocol;
	}

	return pResult;
}
예제 #2
0
파일: api.cpp 프로젝트: 2php/crtmpserver-1
void EnvRun(string ip, uint16_t port, int logLevel)
#endif /* ANDROID */
{
	//1. Initialize the logger
	logLevel = logLevel < 0 ? 0 : logLevel;
	logLevel = logLevel >= _FINEST_ ? _FINEST_ : logLevel;
	Logger::Init();
	BaseLogLocation *pLogLocation = NULL;
#ifdef ANDROID
	pLogLocation = new LogCatLogLocation();
	pLogLocation->SetLevel(logLevel);
	Logger::AddLogLocation(pLogLocation);
#else
	pLogLocation = new ConsoleLogLocation(true);
	pLogLocation->SetLevel(logLevel);
	Logger::AddLogLocation(pLogLocation);
#endif /* ANDROID */

#ifdef BUILD_SIGNATURE
	//1.1 Create the settings string
	FATAL("Build signature:\n%s", BUILD_SIGNATURE);
#endif /* BUILD_SIGNATURE */

	//2. Create the default protocol factory
	DefaultProtocolFactory *pFactory = new DefaultProtocolFactory();
	if (!ProtocolFactoryManager::RegisterProtocolFactory(pFactory)) {
		ASSERT("Unable to register default protocols factory");
	}

	//3. Initialize the I/O
	IOHandlerManager::Initialize();

	//4. Create the AppleStreamingClientApplication application
	Variant configuration;
	configuration[CONF_APPLICATION_NAME] = "applestreaminclient";
	configuration[CONF_APPLICATION_ALIASES].PushToArray("asc");
	configuration[CONF_APPLICATION_DEFAULT] = (bool)true;
	configuration[CONF_APPLICATION_VALIDATEHANDSHAKE] = (bool)true;
	configuration[CONF_APPLICATION_KEYFRAMESEEK] = (bool)true;
	configuration[CONF_APPLICATION_CLIENTSIDEBUFFER] = (int32_t) 15;
	configuration[CONF_APPLICATION_SEEKGRANULARITY] = 1.00;
	configuration[CONF_APPLICATION_MEDIAFOLDER] = "./";
	configuration[CONF_APPLICATION_GENERATE_META_FILES] = (bool)false;
	configuration["rtspHost"] = format("rtsp://127.0.0.1:%hu/", port);
	AppleStreamingClientApplication *pApp = new AppleStreamingClientApplication(
			configuration);
#ifdef ANDROID
	pApp->SetJavaCallBackInterface(ci);
#endif /* ANDROID */
	if (!pApp->Initialize()) {
		ASSERT("Unable to initialize the application");
	}

	//6. Register it to the app manager
	if (!ClientApplicationManager::RegisterApplication(pApp)) {
		ASSERT("Unable to register application");
	}

	//7. Create the RTSP acceptor
	Variant acceptorConfig;
	acceptorConfig[CONF_IP] = ip;
	acceptorConfig[CONF_PORT] = (uint16_t) port;
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTSP;
	vector<uint64_t> chain;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	TCPAcceptor *pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP],
			(uint16_t) acceptorConfig[CONF_PORT], acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

	//8. Create the bin variant acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 1);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_BIN_VARIANT;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

	//9. Create the xml variant acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 2);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_XML_VARIANT;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}

#ifdef HAS_PROTOCOL_RTMP
	//9. Create the RTMP acceptor
	acceptorConfig[CONF_PORT] = (uint16_t) (1935);
	acceptorConfig[CONF_PROTOCOL] = CONF_PROTOCOL_INBOUND_RTMP;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	pAcceptor = new TCPAcceptor(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT],
			acceptorConfig, chain);
	if (!pAcceptor->StartAccept(pApp)) {
		ASSERT("Unable to fire up acceptor");
	}
#endif /* HAS_PROTOCOL_RTMP */

	//10. Create the timer UDP protocol
#ifdef HAS_MS_TIMER
	acceptorConfig[CONF_PORT] = (uint16_t) (port + 3);
	acceptorConfig[CONF_PROTOCOL] = "fineTimer";
	acceptorConfig["FineTimerPeriod"] = 0.2;
	chain = ProtocolFactoryManager::ResolveProtocolChain(acceptorConfig[CONF_PROTOCOL]);
	if (chain.size() == 0) {
		ASSERT("Invalid protocol chain: %s", STR(acceptorConfig[CONF_PROTOCOL]));
	}
	UDPCarrier *pUDPCarrier = UDPCarrier::Create(acceptorConfig[CONF_IP], (uint16_t) acceptorConfig[CONF_PORT]);
	if (pUDPCarrier == NULL) {
		ASSERT("Unable to bind on udp://%s:%hu", STR(acceptorConfig[CONF_IP]), (uint16_t) acceptorConfig[CONF_PORT]);
	}
	BaseProtocol *pTimer = ProtocolFactoryManager::CreateProtocolChain(chain, acceptorConfig);
	pTimer->GetFarEndpoint()->SetIOHandler(pUDPCarrier);
	pUDPCarrier->SetProtocol(pTimer->GetFarEndpoint());
	pApp->SetFineTimerId(pTimer->GetId());
#endif /* HAS_MS_TIMER */

	inet_aton("127.0.0.1", &gAddress.sin_addr);
	memset(&gAddress, 0, sizeof (gAddress));
	gAddress.sin_family = AF_INET;
	gAddress.sin_port = EHTONS(port + 1);

	//10. Run
	while (IOHandlerManager::Pulse()) {
		IOHandlerManager::DeleteDeadHandlers();
		ProtocolManager::CleanupDeadProtocols();
	}

	//11. Shutting down protocols manager;
	ProtocolManager::Shutdown();
	ProtocolManager::CleanupDeadProtocols();

	//12. Shutting down I/O handlers manager
	IOHandlerManager::ShutdownIOHandlers();
	IOHandlerManager::DeleteDeadHandlers();
	IOHandlerManager::Shutdown();

	//13. Unregister and delete default protocol handler
	ProtocolFactoryManager::UnRegisterProtocolFactory(pFactory);
	delete pFactory;
	pFactory = NULL;

	//14. Shutting down applications
	ClientApplicationManager::Shutdown();

	//15. Shutting down the logger leaving you in the dark. Bye bye
	Logger::Free(true);
}