Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:-
 
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:-
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 25-Jul-12 2:12am
Tarun22557
Comments
barneyman at 25-Jul-12 8:16am
   
"it breaks" doesn't really help .. it doesn't compile, it crashes ... what?
Tarun22 at 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 at 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 at 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 at 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.
Tarun22 at 25-Jul-12 9:40am
   
here is thread handles
HANDLE m_ThreadHandle[4];
var ("w") is defined globally
 
i get an error like :-
Unhandled exception at 0x782df531(mfc100d.dll) in myapplication.exe:0xC0000005:Access violation in reading location 0xcccccccc.
Jochen Arndt at 25-Jul-12 9:56am
   
So it is, as expected, a memory access violation. Is the value of 'w' greater than 1 when the error occurs? If so, the error is accessing 'inputAddressStr' which still has an array size of 2. If not, check all your indexes and pointers (like 'rtspServer').
 
Just saw that 'rtspServer' is defined twice. You should also always initialise your variables. Detecting errors with NULL initialised pointers is much easier.
Tarun22 at 25-Jul-12 10:05am
   
sir where the 'rtspServer' is defined twice
please highlight me
Jochen Arndt at 25-Jul-12 10:08am
   
In your posted code no. 2, lines 10 and 14.
Tarun22 at 25-Jul-12 10:29am
   
i made the changes for "rtspserver" and server the value of "w" is 1 when the error occurs as u can see i incremented the vale of "w" in the end
Jochen Arndt at 25-Jul-12 11:26am
   
Because your code is inside a thread, the error source can be anywhere, even in code parts not shown here. That's why I'm writing comments to help you to identify the source yourself.
 
To ensure that variables are valid, you may check them using ASSERTs:
ASSERT(w >= 0 && w < 2);
ASSERT(rtspServer); // must be initialised with NULL
ASSERT(env); // must be initialised with NULL
and so on for all vars used by the function and assigned during execution (e.g. sms[w]).
If the error still occurs and you have checked all variables, the error is probably somewhere else.
 
Another problem are data accessed by multiple threads. You must use locking when accessing these. With basic types like int, it may be sufficient to declare them as volatile when not changed by multiple threads.
Tarun22 at 26-Jul-12 3:33am
   
sir if u have time can u see the code online through team viewer software
Jochen Arndt at 26-Jul-12 3:44am
   
I'm sorry, but I have no time. I'm at work. The amount of code you posted here is already at the limit for most trying to help here.
 
Please follow my suggestions to locate the error yourself (check all possible locations not only by viewing but also using ASSERTs). It will also help you in the future. This will not be the last time you get access violations. I still get some during development. And I'm programming since 30 years.
Tarun22 at 26-Jul-12 3:47am
   
sir i will follow your suggestion sir do u have ever worked with live media libraries or any experience with android ndk development?
Jochen Arndt at 26-Jul-12 4:25am
   
Neither. But you may post specific questions here and others may be able to help you.
Tarun22 at 25-Jul-12 10:55am
   
@PrafullaVedante sir i appreciate your observation actually the code i have pasted is a cut paste i cannot put the whole code as not so much space is provided to put the application code complete,there is "while(1)" in main and in StartFIServer i am returning "return SERVERERR_SUCCESS;"

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
int success = m_FINetCom->StartFIServer();
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 271
1 OriginalGriff 253
2 nv3 50
3 VJ Reddy 45
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 25 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100