⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frame.c

📁 g729 coding ipaddressing
💻 C
📖 第 1 页 / 共 5 页
字号:
		    	wfi.wBitsPerSample = bitsPerSample;
		 		woo = waveInOpen(&hWaveIn, waAudioInDevice, (LPWAVEFORMATEX) &wfi,
		 				0L, 0L, WAVE_FORMAT_QUERY);
	 		}
	 		
	 		/* If we've failed to initialise in either 16 or 8 bit mode
	 		   at 8000 samples per second, it's possible the sound card
	 		   doesn't support any sampling mode below the Windows standard
	 		   of 11025 samples per second.  Have another go-round and see
	 		   if 11025 works. */
	 		
	 		if (woo == WAVERR_BADFORMAT && samplesPerSecond == 8000) {
	 			samplesPerSecond = 11025;
	 			bitsPerSample = 16;
	 			sampleAlignment = bitsPerSample / 8;
	 			bytesPerSecond = samplesPerSecond * sampleAlignment;  
	 		} else {
	 			break;
	 		}
	 	} 
		if (woo != 0) {
        	MessageBox(hwnd, rstring(IDS_T_WAVE_RECORD_FORMAT_ERR),
		   		NULL, MB_OK | MB_ICONEXCLAMATION);
			return FALSE;
    	}
    	for (i = 0; i < 2; i++) {
		    if ((woo = waveInOpen(&hWaveIn, waAudioInDevice,
				  (LPWAVEFORMATEX) &wfi, (DWORD) (UINT) hwndMDIFrame, 0L, (DWORD) CALLBACK_WINDOW)) != 0) {
		    	char et[MAXERRORLENGTH];
			    
	    	
		    	/* The next line looks wrong, doesn't it?  But I've seen drivers
		    	   for half-duplex sound boards that return NOTSUPPORTED instead
		    	   of ALLOCATED when you try to open input and output at the
		    	   same time. */
		    
			    if (i == 0 && outputActive && (woo == MMSYSERR_ALLOCATED || 
			    		woo == MMSYSERR_NOTSUPPORTED)) {
			    
			    	/* Okay, the preponderance of evidence points at our
			    	   machine being burdened with a half-duplex audio
			    	   device--one where the fact that we're playing audio
			    	   prevents from from simultaneously receiving it.
			    	   This is bad.  Let's proceed as we'd have done on
			    	   the 80 metre band in 1935--mute the receiver and
			    	   blast on our carrier regardless. */
			    	
			    	V waveOutReset(hWaveOut);
			    	halfDuplex = TRUE;			// Indicate wave output swiped
					if (outputPending) {
						halfDuplexTransition = TRUE;
	    				propUpdateAudio();
						return TRUE;
					} else {
						V waveOutClose(hWaveOut);
						outputActive = FALSE;
	    				propUpdateAudio();
					}
			    	continue;   
			    }	
		    	waveInGetErrorText(woo, et, sizeof et);
	        	MessageBox(hwnd, et,
			   		rstring(IDS_T_ERR_OPEN_WAVE_INPUT), MB_OK | MB_ICONEXCLAMATION);
				return FALSE;
		    }
		    break;
	    }
	    
		/*	Since the user is allowed to change the sample size
			and compression modes on the fly, but the audio input
			buffers are used for the entire run of the program, we
			allocate them for the worst-case scenario: the most extreme
			compression mode plus 2X compression.  We've already
			established whether the hardware can run at 8000 samples
			per second, so only allocate the larger buffer needed for
			11025 sample per second hardware if it's actually required.
			We determine the worst-case size based on BUFL, which specifies
			the largest sound buffer size in samples, which alredy takes into
			account maximum buffer size and compression.  Note tha BUFL
			already takes into account the possibility that samples may
			be two bytes, as is necessary when dealing with CODECs which
			require 16 bit PCM samples.  */
			
	    inputSoundBufferSize = (samplesPerSecond == EXCHANGE_SAMPLE_RATE) ? BUFL :
									(((BUFL + (EXCHANGE_SAMPLE_RATE - 1)) * 11025) /
											EXCHANGE_SAMPLE_RATE);
	    
	    /*	Now allocate and prepare the sound input buffers.  Don't
	    	you just love the code vomit Windows' inability to free
	    	resources when a program terminates creates?  */
	    
	    currentInputLength = inputBufferLength();
	    currentInputSamples = inputSampleCount();
	    Assert(currentInputLength <= inputSoundBufferSize);
	    			
	    for (i = 0; i < nWaveHeaders; i++) {
	    	inWaveHeader[i] = NULL;
	    }
	    for (i = 0; i < nWaveHeaders; i++) {
			inWaveHeader[i] = (LPWAVEHDR) GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE,
												sizeof(WAVEHDR));
			if (inWaveHeader[i] == NULL) {
				int j;
				
				for (j = i - 1; j >= 0; j++) {
					waveInUnprepareHeader(hWaveIn, inWaveHeader[j], sizeof(WAVEHDR));
					GlobalFreePtr(inWaveHeader[j]->lpData);
					GlobalFreePtr(inWaveHeader[j]);
					inWaveHeader[j] = NULL;
				}
	        	MessageBox(hwnd, rstring(IDS_T_INPUT_HEADER_ERR),
			   		NULL, MB_OK | MB_ICONEXCLAMATION);
			   	waveInClose(hWaveIn);
				return FALSE;
			}
			
			/*	Allocate the wave input buffer, reserving space at the end for
				the int where we stuff the number of microseconds it will take
				to play this buffer at the nominal output rate.  */
				
			inWaveHeader[i]->lpData = (LPSTR) GlobalAllocPtr(GMEM_MOVEABLE | GMEM_SHARE,
				(DWORD) (inputSoundBufferSize + sizeof(int)));

			if (inWaveHeader[i]->lpData == NULL) {
				int j;
				
				GlobalFreePtr(inWaveHeader[i]);
				inWaveHeader[i] = 0;
				for (j = i - 1; j >= 0; j++) {
					waveInUnprepareHeader(hWaveIn, inWaveHeader[j], sizeof(WAVEHDR));
					GlobalFreePtr(inWaveHeader[j]->lpData);
					GlobalFreePtr(inWaveHeader[j]);
					inWaveHeader[j] = NULL;
				}

	        	MessageBox(hwnd, rstring(IDS_T_INPUT_BUFFER_ERR),
			   		NULL, MB_OK | MB_ICONEXCLAMATION);
			   	waveInClose(hWaveIn);
				return FALSE;
			}
			
			inWaveHeader[i]->dwBufferLength = currentInputLength;
			inWaveHeader[i]->dwFlags = 0;
			waveInPrepareHeader(hWaveIn, inWaveHeader[i], sizeof(WAVEHDR));
	    }
	    waveHeadersAllocated = nWaveHeaders; 
	    
		for (i = 0; i < nWaveHeaders; i++) {
			waveInAddBuffer(hWaveIn, inWaveHeader[i], sizeof(WAVEHDR)); 
		}
		inputTerm = FALSE;
	    waveInStart(hWaveIn);
	    inputActive = TRUE;
	    aboutInSamples = samplesPerSecond;
	    aboutInBits = bitsPerSample;
	    propUpdateAudio();
    }
    return TRUE;
}
                                      
/*  TERMINATEWAVEINPUT  --  Shut down wave input and release all
							resources associated with it.  */
							
void terminateWaveInput(void)
{
	if (inputActive) {
		int i;
		
		inputTerm = TRUE;
		waveInReset(hWaveIn);
#ifdef OLDWAY		
		for (i = 0; i < nWaveHeaders; i++) {
			if (inWaveHeader[i] != NULL) {
				waveInUnprepareHeader(hWaveIn, inWaveHeader[i], sizeof(WAVEHDR));
				if (inWaveHeader[i]->lpData != NULL) {
					GlobalFreePtr(inWaveHeader[i]->lpData);
				}
				GlobalFreePtr(inWaveHeader[i]);
				inWaveHeader[i] = NULL;
			}
		}
#else		
		for (i = 0; i < nWaveHeaders; i++) {
			inWaveHeader[i] = NULL;
		}
#endif		
//		waveInClose(hWaveIn);
//		hWaveIn = NULL;
//		inputActive = FALSE;
//	    propUpdateAudio();
#ifdef TRYTHIS
		while (inputActive) {
			MSG msg;
			
			while (GetMessage(&msg, NULL, 0, 0)) {
				extern void MessageLoop(MSG *pmsg);
				
				MessageLoop(&msg);
		  	}
		}
#endif		
	}
}

/*	REMEMBERNEWCONNECTION  --  Add a connection file name to the list
							   of remembered connections.  If it's
							   already in the list, promote it to the
							   first item.  */
							   
static void rememberNewConnection(LPSTR connectionFile)
{
	int i;
	LPSTR hostSave;
	    	
    /*	See if the connection file already appears in the list
    	of recent connections.  If so, delete it from the list.  */
		    
    hostSave = GlobalAllocPtr(GPTR, strlen(connectionFile) + 1);
	strcpy(hostSave, connectionFile);
			
	/*	Careful!  Be sure not to reference connectionFile beyond
		this point, since if we're re-opening a remembered
		connection the following loop may (in fact almost certainly
		will) cause it to be deallocated. */
				 
    if (hostSave != NULL) {
		for (i = 0; i < rememberedConnections; i++) {
			if (_stricmp(hostSave, rememberedConnection[i]) == 0) {
	        	int j;
					
				GlobalFreePtr(rememberedConnection[i]);
				for (j = i + 1; j < rememberedConnections; j++) {
					rememberedConnection[j - 1] = rememberedConnection[j];	
				}
				rememberedConnections--;
			}
		}
				
		//	Push the list of remembered connections and add this one
				
		if (rememberedConnections == REMEMBER_CONNECTIONS) {
			GlobalFreePtr(rememberedConnection[REMEMBER_CONNECTIONS - 1]);
			rememberedConnections--;
		}
		for (i = REMEMBER_CONNECTIONS - 1; i >= 1; i--) {
			rememberedConnection[i] = rememberedConnection[i - 1];		
		}
		rememberedConnection[0] = hostSave;
		rememberedConnections++;
	}	    		
}							   
							                                      
/*  NEWCONNECTION  --  Initiate a connection to a given named
					   host.  If there's already a connection
					   to this host active, activate its window.  */
							
VOID newConnection(HWND hwnd, LPSTR connectionFile, LPSTR knownHost)
{
    CHAR szHostName[MAX_HOST];
    SOCKADDR_IN sockHost;
#define addrHost sockHost.sin_addr
    LPCLIENT_DATA pClientData = NULL;
    HWND hwndClient;
    unsigned short port;
    char *cg;

    //  Prompt the user for a new host

	if (connectionFile == NULL) {
		if (knownHost == NULL) {
		    if (!newHostDialogue(hwndMDIFrame, szHostName, &addrHost, &port)) {
		        return;
		    }
		} else {
	    	char *cp;
	    	long iport;
		    char cb[MAX_HOST];
			
			strcpy(cb, knownHost);
		    if ((cp = strchr(cb, '/')) != NULL ||
		    	(cp = strchr(cb, ':')) != NULL) {
		    	
		    	iport = atol(cp + 1);
		    	if (iport <= 0) {
			        MsgBox(hwnd, MB_ICONSTOP | MB_OK, Format(72), *cp); 
			        return;
		    	}
		    	port = (unsigned short) iport;
		    	*cp = 0;
		    } else {
		    	port = NETFONE_COMMAND_PORT;
		    }
		    
			addrHost.s_addr = inet_addr(cb);
			if (addrHost.s_addr != INADDR_NONE) {
				wsprintf(szHostName, Format(61), (LPSTR) cb);
			} else {
			
				//	If it didn't parse as an IP address, try it as a domain name
			
				LPHOSTENT h = gethostbyname(cb);
				
				if (h == NULL) {
		            int serr = WSAGetLastError();
							            
			        MsgBox(hwnd, MB_ICONSTOP | MB_OK, Format(41),
			                cb, serr, SockerrToString(serr));
			        return;
				} else {
					struct in_addr newaddr;
					
					newaddr = *((LPIN_ADDR) h->h_addr);
					memcpy(&addrHost.s_addr, (h->h_addr), sizeof addrHost.s_addr);
					strcpy(szHostName, cb);
				}
			}
		}
	} else {
		char inetAddr[64];
		
		/*	If the connection file contains a host name and IP address
			(which will be the case for any file we write), use them as
			given.  If only a host name is present, try to find the IP
			address with gethostbyname().  This is primarily intended for
			programs which wish to launch Speak Freely to contact a host
			with known name but unknown IP address.  I'm sure the next turn
			of the screw will have us doing it the other way around as
			well.  */
		
		cg = rstring(IDS_PF_HOST);
		if (GetPrivateProfileString(cg, rstring(IDS_PF_HOST_NAME), "", szHostName,
					sizeof szHostName, connectionFile) == 0) {
	        	MessageBox(hwnd, rstring(IDS_T_CONN_PROFILE_INVALID), connectionFile,
			   		MB_OK | MB_ICONEXCLAMATION);
			   	return;
		} else {
			if (GetPrivateProfileString(cg, rstring(IDS_PF_NETADDR), "", inetAddr,
					sizeof inetAddr, connectionFile) == 0 ||
				(addrHost.s_addr = inet_addr(inetAddr)) == INADDR_NONE) {
				LPHOSTENT h = gethostbyname(szHostName);
				
				if (h == NULL) {
		            int serr = WSAGetLastError();
							            
			        MsgBox(hwnd, MB_ICONSTOP | MB_OK, Format(41),
			                (LPSTR) szHostName,
			                serr, SockerrToString(serr));
			        return;
				} else {
					struct in_addr newaddr;
					
					newaddr = *((LPIN_ADDR) h->h_addr);
					memcpy(&addrHost.s_addr, (h->h_addr),
						sizeof addrHost.s_addr);
				}
			}
			port = GetPrivateProfileInt(cg, rstring(IDS_PF_PORT_NUMBER),
					NETFONE_COMMAND_PORT, connectionFile);
		} 
	}
    
    /*	See if there's already a client window communicating with
    	this host.  Use equality of IP number and port as the criterion to
    	prevent spoofing due to aliases. */
    
    sockHost.sin_port = htons(port);	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -