Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more: , +
made two code the code no 1 works fine when i put the code no1 in an application it breaks,can u tell me the error.I have mentioned in code no 2 where it breaks at "return 0".
Here are two code:-
C++
ServerMediaSession* sms[2];

UsageEnvironment* env;

Boolean reuseFirstSource = False;

Boolean iFramesOnly = False;

TaskScheduler* scheduler;
UserAuthenticationDatabase* authDB;
char const* streamName[2];
char const* inputAddressStr[2];
int inputPortNum[2];
int i = 0,j = 0;

DWORD WINAPI START_STREAM();

char const* descriptionString[2] ={"Session streamed by \"testOnDemandRTSPServer1\"","Session streamed by \"testOnDemandRTSPServer2\""};
RTSPServer* rtspServer;

char* url[2];
HANDLE Th1;
DWORD Tid1=NULL; 
int main()
{
	streamName[0] = "test";		
	inputAddressStr[0] = "239.255.42.42";	
	inputPortNum[0] = 4444;		

	streamName[1] = "stream2";		
	inputAddressStr[1] = "237.255.42.42";	
	inputPortNum[1] = 5043;	
        scheduler = BasicTaskScheduler::createNew();
	env = BasicUsageEnvironment::createNew(*scheduler);
	UserAuthenticationDatabase* authDB = NULL;
rtspServer = RTSPServer::createNew(*env,554, authDB);

	while(1)
	{
		printf("\nEnter any key to STRAT_STREAMER\n");
		getch();
		Th1=CreateThread(NULL,0(LPTHREAD_START_ROUTINE)START_STREAM,NULL,0,&Tid1);
		if(Th1 == NULL)
			*env<< "\nTh1 failed..\n";

	}
	return 0;
}
DWORD WINAPI START_STREAM()
{
	Boolean const inputStreamIsRawUDP = False; 

	printf("\nIn Start Stream\n");
	sms[i] = ServerMediaSession::createNew(*env, streamName[i], streamName[i],descriptionString[i]);
	sms[i]->addSubsession(MPEG2TransportUDPServerMediaSubsession::createNew(*env,inputAddressStr[i],inputPortNum[i],inputStreamIsRawUDP));
	rtspServer->addServerMediaSession(sms[i]);

	url[i] = rtspServer->rtspURL(sms[i]);
	printf("\n\"%s \" stream, from a UDP Transport Stream input source\n\t(",streamName[i]);
	//*env[i] << "\n\"" << streamName[i] << "\" stream, from a UDP Transport Stream input source \n\t(";
	if (inputAddressStr[i] != NULL)
	{
		printf("\nIP multicast address %s,",inputAddressStr[i]);
		//*env[i] << "IP multicast address " << inputAddressStr[i] << ",";
	} 
	else 
	{
		printf("unicast;");
		//*env[i] << "unicast;";
	}
	printf("port %d \n",inputPortNum[i]);
	//*env[i] << " port " << inputPortNum[i] << ")\n";
	printf("Play this stream using the URL \" %s \"",url[i]);
	//*env[i] << "Play this stream using the URL \"" << url[i] << "\"\n";
	delete[] url[i];
	if(j == 0)
	{
	if (rtspServer->setUpTunnelingOverHTTP(http_port[i]) || rtspServer->setUpTunnelingOverHTTP(http_port[i]) || rtspServer->setUpTunnelingOverHTTP(http_port[i]))
		{
			printf("\n\n\n(We use port %d for optional RTSP-over-HTTP tunneling.)\n",rtspServer->httpServerPortNum());
			
		} 
		else
		{
			printf("\n(RTSP-over-HTTP tunneling is not available.)");
		}
		j++;
	}
	else
	printf("\n\n\n(We use port %d for optional RTSP-over HTTPtunneling.)\n",rtspServer->httpServerPortNum());
	
	i++;
	if(i == 1)
	env->taskScheduler().doEventLoop(); // does not return

	return 0;
}


Here is mine code no 2:-
C++
ServerMediaSession* sms[50];

UsageEnvironment* env;
Boolean reuseFirstSource = False;
Boolean iFramesOnly = False;
TaskScheduler* scheduler;

UserAuthenticationDatabase* authDB;
char const* descriptionString ="Session streamed by \"testOnDemandRTSPServer1\"";
RTSPServer* rtspServer;
char* url[50]
static int k=0,j=0,z=0,w=0;
char const* descriptionString ="Session streamed by \"testOnDemandRTSPServer1\"";
RTSPServer* rtspServer;
char* url[50];
char const* streamName[2];
char const* inputAddressStr[2];
int inputPortNum[2];


int main()
{

StartRtspServer();
	b.OnRun();

}
BOOL CFIServerDlg::OnRun()
{
	OnStartFIServer();
return TRUE;  // ret
}

int StartRtspServer()
{
	char buffer[2500];
	wLog->WriteDebugLog("Entering in StartRtspServer().");
	
	// Begin by setting up our usage environment:
	scheduler = BasicTaskScheduler::createNew();
	env = BasicUsageEnvironment::createNew(*scheduler);

	UserAuthenticationDatabase* authDB = NULL;
portNumBits rtspServerPortNum = 554;

	rtspServer = RTSPServer::createNew(*env,rtspServerPortNum, authDB);
if (rtspServer == NULL)
	{
		sprintf(buffer,"Failed to create RTSP server with Port:%d %s",rtspServerPortNum,env->getResultMsg());
		wLog->WriteErrorLog(buffer);
		*env << "Failed to create RTSP server: " <<env->getResultMsg()<<"\n";
			return 1;
		
	}
	else
	{
		wLog->WriteInfoLog("Created RTSP server..");
		*env << "Created RTSP server.."<<"\n.";
			return 0;
	}
void CFIServerDlg::OnStartFIServer() 
{
	wLog->WriteDebugLog("Entering in OnStartFIServer()");


	int success = m_FINetCom->StartFIServer();
	if(success == 0)
	{		
		
		m_FINetCom->StartFICom();
		
	}
}
DWORD CFINetCom::StartFIServer()
{
//AL VARIABLES ARE DEFINED NO ISSUE REGARDING VARIABLES
m_ThreadHandle[2] = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)MessageHandleThread, this, 0, &ThreadId3);

	if(m_ThreadHandle[2] == NULL)
	{
		wLog->WriteErrorLog("StartFIServer() Error while creating MessageHandleThread thread.");
		cout<<"Debug: StartFIServer() Error while creating MessageHandleThread thread.\n";

	}
}
*************************************************************************
*
* NAME        : MessageHandleThread()
*
* DESCRIPTION : The message handle thread for handling various messages comming from 
client and streamer.
*
* PARAMETERS  : CFINetCom*
*
* RETURN      : DWORD 
*
**************************************************************************/

DWORD WINAPI CFINetCom::MessageHandleThread(CFINetCom* MyServ)
{
	case START_STREAM:
   {
      HANDLE th1;
      DWORD tid=NULL;
      th1 = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)StartStreamingThread,&ip_port, 0, &tid);
	if(th1 == NULL)
	{
	wLog->WriteDebugLog("StartFIServer() Error while creating MessageReplyThread .");
									cout<<"Debug: StartFIServer() Error while creating MessageReplyThread .\n";
	}
break;
}

DWORD WINAPI StartStreamingThread(Parameters ip_port)
{
	streamName[0] = "test";		
	inputAddressStr[0] = "239.255.42.42";	
	inputPortNum[0] = 4444;		

	streamName[1] = "stream2";		
	inputAddressStr[1] = "237.255.42.42";	
	inputPortNum[1] = 5043;
Boolean const inputStreamIsRawUDP = False; 
sms[w] = ServerMediaSession::createNew(*env, streamName[w], streamName[w],descriptionString);
	sms[w]->addSubsession(MPEG2TransportUDPServerMediaSubsession::createNew(*env, inputAddressStr[w], inputPortNum[w], inputStreamIsRawUDP));
	rtspServer->addServerMediaSession(sms[w]);

	url[w] = rtspServer->rtspURL(sms[w]);
if (inputAddressStr[w] != NULL)
	{
		
		sprintf(buffer,"IP multicast address %s",inputAddressStr[w]);
		wLog->WriteDebugLog(buffer);
		*env << "IP multicast address " << inputAddressStr[w] << ",";
	}
	else
	{
		//printf("\nunicast;\n");
		wLog->WriteDebugLog("unicast;");
		*env << "unicast;";
	}
	delete[] url[w];
if(j==0)
	{
		if (rtspServer->setUpTunnelingOverHTTP(TunnelingPort) || rtspServer->setUpTunnelingOverHTTP(TunnelingPort) || rtspServer->setUpTunnelingOverHTTP(TunnelingPort))
		{
			
			sprintf(buffer,"(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
			wLog->WriteDebugLog(buffer);
			*env << "\n(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";
		} 
		else 
		{
			
			wLog->WriteDebugLog("(RTSP-over-HTTP tunneling is not available.)");
			*env << "\n(RTSP-over-HTTP tunneling is not available.)\n";
		}
		j++;
	}
	else
	{
		printf("(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
		sprintf(buffer,"(We use port %d for optional RTSP-over-HTTP tunneling.)", rtspServer->httpServerPortNum());
		wLog->WriteDebugLog(buffer);
	}

	
	
	w++;
	wLog->WriteDebugLog("Leaving StartStreamingThread");
	if(w == 1)
		env->taskScheduler().doEventLoop(); // does not return

	return 0; // only to prevent compiler warning,IT BREAKS HERE

}	
Posted
Comments
barneyman 25-Jul-12 8:16am    
"it breaks" doesn't really help .. it doesn't compile, it crashes ... what?
Tarun22 25-Jul-12 8:38am    
it crashed at return 0 the 2nd time thread is created but when the second time thread is created in code no 1 it runs successfully
Jochen Arndt 25-Jul-12 8:35am    
As already noted by Barneyman, a more specific error description would help us to help you.

You have modified the code in your 2nd version:
Global array size sms[] is changed, global variable 'i' replaced by 'w', and you replaced printf() output by buffered logging using the variable 'buffer' that seems to does not exist in the scope where used (I didn't find it by my quick view).
Tarun22 25-Jul-12 8:40am    
it crashed at return 0 the 2nd time thread is created but when the second time thread is created in code no 1 it runs successfully,please leave that buffer line and does it matter if i replace the name of same variable sir.
Jochen Arndt 25-Jul-12 8:53am    
Crash: Still not enough information. Any error message or exception? Did you use a Debug build and execute it from within debugger.

When execution breaks, the error itself is often not at the break position. Many of these errors are memory violations. So you should check if you access an array with an invalid index. We can't do that from your code because some array sizes (m_ThreadHandles) and assignments of index vars ('w') are not shown.

Regarding the buffer var: The 2nd code as posted would not compile.
Renaming vars: It may does not matter, but is worth checking.

1 solution

There are multiple problems with your code ....

1 ) There is no code which will wait for your spawned threads in main function.

main function represents main thread of program. If it exits , all threads will be terminated automatically.
In your Code 1 ,you have managed it by keeping main thread busy in while(1).
But for code 2 there is no such provision. Your main thread will not wait for your spawned threads to finish.

2 ) Your are not returning anything from StartFIServer() function

Hence success in following call would be a garbage value
C++
int success = m_FINetCom->StartFIServer();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900