예제 #1
0
int CMediaNet::MediaNet_Thread( void * pThisVoid )
{
	CMediaNet *pThis = ( CMediaNet* )pThisVoid;

	do 
	{
		// 开始初始化.
		pThis->SetRtspStatus( RTSPStatus_Init );

		// Begin by setting up our usage environment:
		TaskScheduler* scheduler = BasicTaskScheduler::createNew();
		env = BasicUsageEnvironment::createNew(*scheduler);

		progName = "M_CU";

		string strUrl = pThis->m_strRTSPUrlA;

		gettimeofday(&startTime, NULL);

		unsigned short desiredPortNum = 0;

		// unfortunately we can't use getopt() here, as Windoze doesn't have it

		// Create our client object:
		ourClient = createClient(*env, verbosityLevel, progName);
		if (ourClient == NULL) 
		{
			*env << "Failed to create " << clientProtocolName
				<< " client: " << env->getResultMsg() << "\n";

			pThis->SetRtspStatus( RTSPStatus_Error_Connect_Srv );
			break;
		}

		// 开始获取Opition.
		pThis->SetRtspStatus( RTSPStatus_Opitiion );
		// Begin by sending an "OPTIONS" command:
		char* optionsResponse
			= getOptionsResponse(ourClient, pThis->m_strRTSPUrlA.c_str(), username, password);

		if (optionsResponse == NULL) 
		{
			*env << clientProtocolName << " \"OPTIONS\" request failed: "
				<< env->getResultMsg() << "\n";

			pThis->SetRtspStatus( RTSPStatus_Error_Connect_Srv );
			break;
		} 
		else 
		{
			*env << clientProtocolName << " \"OPTIONS\" request returned: "
				<< optionsResponse << "\n";
		}
		if( optionsResponse )
		{
			delete[] optionsResponse;
		}
			

		// 开始获取Description.
		// Open the URL, to get a SDP description:
		pThis->SetRtspStatus( RTSPStatus_Description );
		char* sdpDescription
			= getSDPDescriptionFromURL(ourClient, pThis->m_strRTSPUrlA.c_str(), username, password,
			proxyServerName, proxyServerPortNum,
			desiredPortNum);
		if (sdpDescription == NULL) 
		{
			*env << "Failed to get a SDP description from URL \"" << pThis->m_strRTSPUrlA.c_str()
				<< "\": " << env->getResultMsg() << "\n";
			pThis->SetRtspStatus( RTSPStatus_Error_Connect_Srv );
			break;
		}

		*env << "Opened URL \"" << pThis->m_strRTSPUrlA.c_str()
			<< "\", returning a SDP description:\n" << sdpDescription << "\n";

		// Create a media session object from this SDP description:
		session = MediaSession::createNew(*env, sdpDescription);
		delete[] sdpDescription;
		if (session == NULL) 
		{
			*env << "Failed to create a MediaSession object from the SDP description: " << env->getResultMsg() << "\n";
			pThis->SetRtspStatus( RTSPStatus_Error_Connect_Srv );
			break;
		} 
		else if (!session->hasSubsessions()) 
		{
			*env << "This session has no media subsessions (i.e., \"m=\" lines)\n";
			pThis->SetRtspStatus( RTSPStatus_Error_Connect_Srv );
			break;
		}

		// Then, setup the "RTPSource"s for the session:
		MediaSubsessionIterator iter(*session);
		MediaSubsession *subsession;
		Boolean madeProgress = False;
		char const* singleMediumToTest = singleMedium;
		while ((subsession = iter.next()) != NULL) 
		{
			// If we've asked to receive only a single medium, then check this now:
			if (singleMediumToTest != NULL) 
			{
				if (strcmp(subsession->mediumName(), singleMediumToTest) != 0) 
				{
					*env << "Ignoring \"" << subsession->mediumName()
						<< "/" << subsession->codecName()
						<< "\" subsession, because we've asked to receive a single " << singleMedium
						<< " session only\n";
					continue;
				} 
				else 
				{
					// Receive this subsession only
					singleMediumToTest = "xxxxx";
					// this hack ensures that we get only 1 subsession of this type
				}
			}

			desiredPortNum = 0;
			if (desiredPortNum != 0) 
			{
				subsession->setClientPortNum(desiredPortNum);
				desiredPortNum += 2;
			}

			if (true) 
			{
				if (!subsession->initiate(simpleRTPoffsetArg)) 
				{
					*env << "Unable to create receiver for \"" << subsession->mediumName()
						<< "/" << subsession->codecName()
						<< "\" subsession: " << env->getResultMsg() << "\n";
				} 
				else 
				{
					*env << "Created receiver for \"" << subsession->mediumName()
						<< "/" << subsession->codecName()
						<< "\" subsession (client ports " << subsession->clientPortNum()
						<< "-" << subsession->clientPortNum()+1 << ")\n";
					madeProgress = True;

					if (subsession->rtpSource() != NULL) 
					{
						// Because we're saving the incoming data, rather than playing
						// it in real time, allow an especially large time threshold
						// (1 second) for reordering misordered incoming packets:
						unsigned const thresh = 1000000; // 1 second
						subsession->rtpSource()->setPacketReorderingThresholdTime(thresh);

						if (socketInputBufferSize > 0) 
						{
							// Set the RTP source's input buffer size as specified:
							int socketNum
								= subsession->rtpSource()->RTPgs()->socketNum();
							unsigned curBufferSize
								= getReceiveBufferSize(*env, socketNum);
							unsigned newBufferSize
								= setReceiveBufferTo(*env, socketNum, socketInputBufferSize);
							*env << "Changed socket receive buffer size for the \""
								<< subsession->mediumName()
								<< "/" << subsession->codecName()
								<< "\" subsession from "
								<< curBufferSize << " to "
								<< newBufferSize << " bytes\n";
						}
					}
				}
			} 
			else 
			{
				mcu::tlog << _T( "Use port: " ) << (int)subsession->clientPortNum() << endl;
				if (subsession->clientPortNum() == 0) 
				{
					*env << "No client port was specified for the \""
						<< subsession->mediumName()
						<< "/" << subsession->codecName()
						<< "\" subsession.  (Try adding the \"-p <portNum>\" option.)\n";
				} 
				else 
				{
					madeProgress = True;
				}
			}
		}
		if (!madeProgress) 
			break;

		// Perform additional 'setup' on each subsession, before playing them:
		pThis->SetRtspStatus( RTSPStatus_Setup );
		unsigned nResponseCode = NULL;
		BOOL bSetupSuccess = setupStreams( &nResponseCode );
		if ( !bSetupSuccess )
		{
			// setup失败!
			if ( RTSPResp_Error_Server_Full == nResponseCode )
			{
				pThis->SetRtspStatus( RTSPStatus_Error_Server_Full );
			}
			else
			{
				pThis->SetRtspStatus( RTSPStatus_Idle );
			}
			break;
		}
		// Create output files:
		
		if ( true  ) 
		{
				// Create and start "FileSink"s for each subsession: 
				madeProgress = False;
				iter.reset();
				while ((subsession = iter.next()) != NULL) 
				{
					if (subsession->readSource() == NULL) continue; // was not initiated

					MediaSink *pDecodeSink = 0;
					if (strcmp(subsession->mediumName(), "video") == 0 )
					{
						int nBandWidth = subsession->GetBandWidth();

						if ( strcmp(subsession->codecName(), "MP4V-ES") == 0 )
						{
							CMpeg4StreamDecodeSink *pMsds = CMpeg4StreamDecodeSink::CreateNew( *env, 20000, nBandWidth );
							 pDecodeSink = pMsds;
							
						}
						else if ( strcmp( subsession->codecName(), "H264" ) == 0 )
						{
							 CH264StreamDecodeSink *pHsds = CH264StreamDecodeSink::CreateNew( *env, 20000, nBandWidth );
							 pDecodeSink = pHsds;
						}
						else
						{
							continue;
						}
					}				

					subsession->sink = pDecodeSink;
					if (subsession->sink == NULL) 
					{
						*env << "Failed to create CH264StreamDecodeSink \""  << "\n";
					} 


					subsession->sink->startPlaying(*(subsession->readSource()),
						subsessionAfterPlaying,
						subsession);

					// Also set a handler to be called if a RTCP "BYE" arrives
					// for this subsession:
					if (subsession->rtcpInstance() != NULL) 
					{
						subsession->rtcpInstance()->setByeHandler(subsessionByeHandler,
							subsession);
					}

					// 发送NAT探测包。
					unsigned char temp[112] = {0};
					temp[0] = 0x80;
					subsession->rtpSource()->RTPgs()->output( *env, 0,temp, 112 );

					madeProgress = True;
				}
			}


		// Finally, start playing each subsession, to start the data flow:
		pThis->SetRtspStatus( RTSPStatus_Play );
		startPlayingStreams();


		pThis->SetRtspStatus( RTSPStatus_Running );
		// 传入结束标志指针。 
		env->taskScheduler().doEventLoop( &pThis->m_runFlag ); 

		pThis->SetRtspStatus( RTSPStatus_Idle );

	} while(0);	

	return 0;
}