/* ------------------------------------------------------------------- * main() * Entry point into Iperf * * sets up signal handlers * initialize global locks and conditions * parses settings from environment and command line * starts up server or client thread * waits for all threads to complete * ------------------------------------------------------------------- */ int main( int argc, char **argv ) { #ifdef WIN32 // Start winsock WSADATA wsaData; int rc; #endif // Set SIGTERM and SIGINT to call our user interrupt function my_signal( SIGTERM, Sig_Interupt ); my_signal( SIGINT, Sig_Interupt ); #ifndef WIN32 // SIGALRM=14, _NSIG=3... my_signal( SIGALRM, Sig_Interupt ); #endif #ifndef WIN32 // Ignore broken pipes signal(SIGPIPE,SIG_IGN); #endif #ifdef WIN32 // Start winsock rc = WSAStartup( 0x202, &wsaData ); WARN_errno( rc == SOCKET_ERROR, "WSAStartup" ); if (rc == SOCKET_ERROR) return 0; // Tell windows we want to handle our own signals SetConsoleCtrlHandler( sig_dispatcher, true ); #endif // Initialize global mutexes and conditions Condition_Initialize ( &ReportCond ); Condition_Initialize ( &ReportDoneCond ); Mutex_Initialize( &groupCond ); Mutex_Initialize( &clients_mutex ); // Initialize the thread subsystem thread_init( ); // Initialize the interrupt handling thread to 0 sThread = thread_zeroid(); // perform any cleanup when quitting Iperf atexit( cleanup ); // Allocate the "global" settings thread_Settings* ext_gSettings = new thread_Settings; // Initialize settings to defaults Settings_Initialize( ext_gSettings ); // read settings from environment variables Settings_ParseEnvironment( ext_gSettings ); // read settings from command-line parameters Settings_ParseCommandLine( argc, argv, ext_gSettings ); // Check for either having specified client or server if ( ext_gSettings->mThreadMode == kMode_Client || ext_gSettings->mThreadMode == kMode_Listener ) { #ifdef WIN32 // Start the server as a daemon // Daemon mode for non-windows in handled // in the listener_spawn function if ( isDaemon( ext_gSettings ) ) { CmdInstallService(argc, argv); return 0; } // Remove the Windows service if requested if ( isRemoveService( ext_gSettings ) ) { // remove the service if ( CmdRemoveService() ) { fprintf(stderr, "IPerf Service is removed.\n"); return 0; } } #endif // initialize client(s) if ( ext_gSettings->mThreadMode == kMode_Client ) { client_init( ext_gSettings ); } #ifdef HAVE_THREAD // start up the reporter and client(s) or listener { thread_Settings *into = NULL; // Create the settings structure for the reporter thread Settings_Copy( ext_gSettings, &into ); into->mThreadMode = kMode_Reporter; // Have the reporter launch the client or listener into->runNow = ext_gSettings; // Start all the threads that are ready to go thread_start( into ); } #else // No need to make a reporter thread because we don't have threads thread_start( ext_gSettings ); #endif } else { // neither server nor client mode was specified // print usage and exit #ifdef WIN32 // In Win32 we also attempt to start a previously defined service // Starting in 2.0 to restart a previously defined service // you must call iperf with "iperf -D" or using the environment variable SERVICE_TABLE_ENTRY dispatchTable[] = { { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main}, { NULL, NULL} }; // Only attempt to start the service if "-D" was specified if ( !isDaemon(ext_gSettings) || // starting the service by SCM, there is no arguments will be passed in. // the arguments will pass into Service_Main entry. !StartServiceCtrlDispatcher(dispatchTable) ) // If the service failed to start then print usage #endif fprintf( stderr, usage_short, argv[0], argv[0] ); return 0; } // wait for other (client, server) threads to complete thread_joinall(); // all done! return 0; } // end main
void debug_init() { #if IPERF_DEBUG Mutex_Initialize(&debugprint_mutex); debugprint_mutex_init = 1; #endif /* IPERF_DEBUG */ }
/** * @brief open muxer to write file * * @author dengzhou * @date 2013-04-07 * @param[in] * @return T_S32 * @retval if return 0 success, otherwise failed */ int mux_open(T_MUX_INPUT *mux_input) { T_MEDIALIB_MUX_OPEN_INPUT mux_open_input; T_MEDIALIB_MUX_OPEN_OUTPUT mux_open_output; T_MEDIALIB_MUX_INFO MuxInfo; T_CHR strFile[MAX_PATH]; memset(strFile, 0x00, sizeof(strFile)); fid = GetRecordFile(strFile); //Get the file handle and file name if(fid <= 0) { goto err; } index_fid = open("/mnt/index", O_RDWR | O_CREAT | O_TRUNC); //create avi file index if(index_fid <= 0 ) { goto err; } pfile1 = ak_rec_cb_load(fid, AK_FALSE, 6*1024*1024, 16 * 1024); pfile2 = ak_rec_cb_load(index_fid, AK_TRUE, 4*1024*1024, 16 * 1024); if(pfile1 == AK_NULL || pfile2 == AK_NULL) { goto err; } memset(&mux_open_input, 0, sizeof(T_MEDIALIB_MUX_OPEN_INPUT)); mux_open_input.m_MediaRecType = mux_input->m_MediaRecType;//MEDIALIB_REC_AVI_NORMAL; mux_open_input.m_hMediaDest = (T_S32)pfile1; mux_open_input.m_bCaptureAudio = mux_input->m_bCaptureAudio; mux_open_input.m_bNeedSYN = AK_TRUE; mux_open_input.m_bLocalMode = AK_TRUE; mux_open_input.m_bIdxInMem = AK_FALSE; mux_open_input.m_ulIndexMemSize = 0; mux_open_input.m_hIndexFile = (T_S32)pfile2; //for syn mux_open_input.m_ulVFifoSize = 200*1024; //video fifo size mux_open_input.m_ulAFifoSize = 100*1024; //audio fifo size mux_open_input.m_ulTimeScale = 1000; //time scale // set video open info mux_open_input.m_eVideoType = mux_input->m_eVideoType;//MEDIALIB_VIDEO_H264; mux_open_input.m_nWidth = mux_input->m_nWidth;//640; mux_open_input.m_nHeight = mux_input->m_nHeight;//480; mux_open_input.m_nFPS = parse.fps1; mux_open_input.m_nKeyframeInterval = parse.fps1-1; // set audio open info mux_open_input.m_eAudioType = mux_input->m_eAudioType;//MEDIALIB_AUDIO_PCM; mux_open_input.m_nSampleRate = mux_input->m_nSampleRate;//8000; mux_open_input.m_nChannels = 1; mux_open_input.m_wBitsPerSample = 16; switch (mux_input->m_eAudioType) { case MEDIALIB_AUDIO_PCM: { mux_open_input.m_ulSamplesPerPack = mux_input->m_nSampleRate*32/1000; mux_open_input.m_ulAudioBitrate = mux_input->m_nSampleRate*mux_open_input.m_wBitsPerSample *mux_open_input.m_ulSamplesPerPack; break; } case MEDIALIB_AUDIO_AAC: { mux_open_input.m_ulSamplesPerPack = 1024*mux_open_input.m_nChannels; mux_open_input.m_cbSize = 2; switch(mux_open_input.m_nSampleRate) { case 8000 : mux_open_input.m_ulAudioBitrate = 8000; break; case 11025 : mux_open_input.m_ulAudioBitrate = 11025; break; case 12000 : mux_open_input.m_ulAudioBitrate = 12000; break; case 16000: mux_open_input.m_ulAudioBitrate = 16000; break; case 22050: mux_open_input.m_ulAudioBitrate = 22050; break; case 24000: mux_open_input.m_ulAudioBitrate = 24000; break; case 32000: mux_open_input.m_ulAudioBitrate = 32000; break; case 44100: mux_open_input.m_ulAudioBitrate = 44100; break; case 48000: mux_open_input.m_ulAudioBitrate = 48000; break; default: mux_open_input.m_ulAudioBitrate = 48000; break; } break; } case MEDIALIB_AUDIO_ADPCM: { mux_open_input.m_wFormatTag = 0x11; mux_open_input.m_wBitsPerSample = 4; switch(mux_open_input.m_nSampleRate) { case 8000: case 11025: case 12000: case 16000: mux_open_input.m_nBlockAlign = 0x100; break; case 22050: case 24000: case 32000: mux_open_input.m_nBlockAlign = 0x200; break; case 44100: case 48000: case 64000: mux_open_input.m_nBlockAlign = 0x400; break; default: mux_open_input.m_nBlockAlign = 0x400; break; } mux_open_input.m_ulSamplesPerPack = (mux_open_input.m_nBlockAlign-4)*8/4+1; mux_open_input.m_ulAudioBitrate = mux_open_input.m_nAvgBytesPerSec = mux_open_input.m_nSampleRate * mux_open_input.m_nBlockAlign / mux_open_input.m_ulSamplesPerPack; mux_open_input.m_nBlockAlign *= mux_open_input.m_nChannels; mux_open_input.m_cbSize = 2; mux_open_input.m_pszData = (T_U8 *)&mux_open_input.m_ulSamplesPerPack; break; } default: goto err; } mux_open_input.m_CBFunc.m_FunPrintf= (MEDIALIB_CALLBACK_FUN_PRINTF)printf; mux_open_input.m_CBFunc.m_FunMalloc= (MEDIALIB_CALLBACK_FUN_MALLOC)malloc; mux_open_input.m_CBFunc.m_FunFree = (MEDIALIB_CALLBACK_FUN_FREE)free; mux_open_input.m_CBFunc.m_FunRead= (MEDIALIB_CALLBACK_FUN_READ)ak_rec_cb_fread; mux_open_input.m_CBFunc.m_FunSeek= (MEDIALIB_CALLBACK_FUN_SEEK)ak_rec_cb_fseek; mux_open_input.m_CBFunc.m_FunTell = (MEDIALIB_CALLBACK_FUN_TELL)ak_rec_cb_ftell; mux_open_input.m_CBFunc.m_FunWrite = (MEDIALIB_CALLBACK_FUN_WRITE)ak_rec_cb_fwrite; mux_open_input.m_CBFunc.m_FunFileHandleExist = ak_rec_cb_lnx_fhandle_exist; hMedia = MediaLib_Mux_Open(&mux_open_input, &mux_open_output); if (AK_NULL == hMedia) { goto err; } if (MediaLib_Mux_GetInfo(hMedia, &MuxInfo) == AK_FALSE) { goto err; } if (AK_FALSE == MediaLib_Mux_Start(hMedia)) { goto err; } Mutex_Initialize(&muxMutex); stop_record_flag = 0; return 0; err: if(fid > 0) { close(fid); } if(index_fid > 0) { close(index_fid); } remove(strFile); if(pfile1 != AK_NULL) { ak_rec_cb_unload(pfile1); } if(pfile2 != AK_NULL) { ak_rec_cb_unload(pfile2); } if(hMedia != AK_NULL) { MediaLib_Mux_Close(hMedia); } return -1; }
/* ------------------------------------------------------------------- * main() * Entry point into Iperf * * sets up signal handlers * initialize global locks and conditions * parses settings from environment and command line * starts up server or client thread * waits for all threads to complete * ------------------------------------------------------------------- */ #ifdef __cplusplus extern "C" #endif /* __cplusplus */ int IPERF_MAIN( int argc, char **argv ) { #ifdef NO_EXIT should_exit = 0; #endif /* NO_EXIT */ #ifdef IPERF_DEBUG debug_init(); #endif /* IPERF_DEBUG */ #ifndef NO_INTERRUPTS #ifdef WIN32 setsigalrmfunc(call_sigalrm); #endif /* WIN32 */ // Set SIGTERM and SIGINT to call our user interrupt function my_signal( SIGTERM, Sig_Interupt ); my_signal( SIGINT, Sig_Interupt ); my_signal( SIGALRM, Sig_Interupt ); #ifndef WIN32 // Ignore broken pipes signal(SIGPIPE,SIG_IGN); #else // Start winsock WORD wVersionRequested; WSADATA wsaData; // Using MAKEWORD macro, Winsock version request 2.2 wVersionRequested = MAKEWORD(2, 2); int rc = WSAStartup( wVersionRequested, &wsaData ); WARN_errno( rc == SOCKET_ERROR, ( "WSAStartup failed.\n" ) ); if (rc != 0) { fprintf(stderr, "The Winsock DLL was not found!\n"); return 1; } /* * Confirm that the WinSock DLL supports 2.2. Note that if the DLL supports * versions greater than 2.2 in addition to 2.2, it will still return 2.2 in * wVersion since that is the version we requested. */ if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 ) { /* Tell the user that we could not find a usable WinSock DLL. */ fprintf(stderr, "The DLL does not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion)); WSACleanup(); return 1; } // Tell windows we want to handle our own signals SetConsoleCtrlHandler( sig_dispatcher, true ); #endif /* WIN32 */ #endif /* NO_INTERRUPTS */ // Initialize global mutexes and conditions IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Initializing report condition.\n" ) ); Condition_Initialize ( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Initializing report done condition.\n" ) ); Condition_Initialize ( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Initializing group condition mutex.\n" ) ); Mutex_Initialize( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Initializing clients mutex.\n" ) ); Mutex_Initialize( &clients_mutex ); // Initialize the thread subsystem IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Initializing the thread subsystem.\n" ) ); thread_init( ); // Initialize the interrupt handling thread to 0 sThread = thread_zeroid(); #ifndef NO_EXIT // perform any cleanup when quitting Iperf atexit( cleanup ); #endif /* NO_EXIT */ // Allocate the "global" settings thread_Settings *ext_gSettings = (thread_Settings*) malloc( sizeof( thread_Settings ) ); FAIL( ext_gSettings == NULL, ( "Unable to allocate memory for thread_Settings ext_gSettings.\n" ), NULL ); IPERF_DEBUGF( MEMALLOC_DEBUG, IPERF_MEMALLOC_MSG( ext_gSettings, sizeof( thread_Settings ) ) ); // Initialize settings to defaults Settings_Initialize( ext_gSettings ); #ifndef NO_ENVIRONMENT // read settings from environment variables Settings_ParseEnvironment( ext_gSettings ); #endif /* NO_ENVIORNMENT */ // read settings from command-line parameters Settings_ParseCommandLine( argc, argv, ext_gSettings ); #ifdef NO_EXIT if (should_exit) { IPERF_DEBUGF( MEMFREE_DEBUG | IPERF_DBG_TRACE, IPERF_MEMFREE_MSG( ext_gSettings ) ); FREE_PTR( ext_gSettings ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report condition.\n" ) ); Condition_Destroy( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report done condition.\n" ) ); Condition_Destroy( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying group condition mutex.\n" ) ); Mutex_Destroy( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying clients mutex.\n" ) ); Mutex_Destroy( &clients_mutex ); return 0; } #endif /* NO_EXIT */ // Check for either having specified client or server if ( ext_gSettings->mThreadMode == kMode_Client || ext_gSettings->mThreadMode == kMode_Listener ) { #ifdef WIN32 #ifndef NO_DAEMON // Start the server as a daemon // Daemon mode for non-windows in handled // in the listener_spawn function if ( isDaemon( ext_gSettings ) ) { CmdInstallService(argc, argv); return 0; } #endif /* NO_DAEMON */ #ifndef NO_SERVICE // Remove the Windows service if requested if ( isRemoveService( ext_gSettings ) ) { // remove the service if ( CmdRemoveService() ) { fprintf(stderr, "IPerf Service is removed.\n"); return 0; } } #endif /* NO_SERVICE */ #endif /* WIN32 */ // initialize client(s) if ( ext_gSettings->mThreadMode == kMode_Client ) { IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | IPERF_DBG_TRACE, ( "Initializing client(s)...\n" ) ); client_init( ext_gSettings ); } #ifdef HAVE_THREAD // start up the reporter and client(s) or listener thread_Settings *into = NULL; // Create the settings structure for the reporter thread IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | REPORTER_DEBUG | IPERF_DBG_TRACE, ( "Creating the settings structure for the reporter thread.\n" ) ); Settings_Copy( ext_gSettings, &into ); into->mThreadMode = kMode_Reporter; // Have the reporter launch the client or listener IPERF_DEBUGF( CLIENT_DEBUG | LISTENER_DEBUG | IPERF_DBG_TRACE, ( "Setting the reporter to launch the client or listener before launching itself.\n" ) ); into->runNow = ext_gSettings; // Start all the threads that are ready to go IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Starting all the threads...\n" ) ); thread_start( into ); #else // No need to make a reporter thread because we don't have threads IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Starting iperf in a single thread...\n" ) ); thread_start( ext_gSettings ); #endif /* HAVE_THREAD */ } else { // neither server nor client mode was specified // print usage and exit #ifdef WIN32 // In Win32 we also attempt to start a previously defined service // Starting in 2.0 to restart a previously defined service // you must call iperf with "iperf -D" or using the environment variable SERVICE_TABLE_ENTRY dispatchTable[] = { { TEXT((char *) SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main}, { NULL, NULL} }; #ifndef NO_DAEMON // Only attempt to start the service if "-D" was specified if ( !isDaemon(ext_gSettings) || // starting the service by SCM, there is no arguments will be passed in. // the arguments will pass into Service_Main entry. !StartServiceCtrlDispatcher(dispatchTable) ) // If the service failed to start then print usage #endif /* NO_DAEMON */ #endif /* WIN32 */ fprintf( stderr, usage_short, argv[0], argv[0] ); return 0; } // wait for other (client, server) threads to complete // IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Waiting for other (client, server) threads to complete...\n" ) ); // thread_joinall(); #ifdef NO_EXIT /* We can't run the atexit function */ #ifdef WIN32 // Shutdown Winsock WSACleanup(); #endif /* WIN32 */ // clean up the list of clients Iperf_destroy ( &clients ); // shutdown the thread subsystem IPERF_DEBUGF( THREAD_DEBUG | IPERF_DBG_TRACE, ( "Deinitializing the thread subsystem.\n" ) ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report condition.\n" ) ); Condition_Destroy( &ReportCond ); IPERF_DEBUGF( CONDITION_DEBUG | IPERF_DBG_TRACE, ( "Destroying report done condition.\n" ) ); Condition_Destroy( &ReportDoneCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying group condition mutex.\n" ) ); Mutex_Destroy( &groupCond ); IPERF_DEBUGF( MUTEX_DEBUG | IPERF_DBG_TRACE, ( "Destroying clients mutex.\n" ) ); Mutex_Destroy( &clients_mutex ); #ifdef IPERF_DEBUG debug_init(); #endif /* IPERF_DEBUG */ #endif /* NO_EXIT */ // all done! IPERF_DEBUGF( IPERF_DBG_TRACE | IPERF_DBG_STATE, ( "Done!\n" ) ); return 0; } // end main