www.pudn.com > 使用IP网络聊天的VOIP源码.zip > dialog.cpp


/* 
   Talker - A small program which utilizes the Layer-3 codec (ACM) in windows for voice-over-IP 
   Copyright (C) 1999 Dino Klein 
 
   This program is free software; you can redistribute it and/or modify it under the terms of 
   the GNU General Public License as published by the Free Software Foundation; either 
   version 2 of the License, or (at your option) any later version.  
 
   This program is distributed in the hope that it will be useful, but WITHOUT ANY 
   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
   more details.  
 
   You should have received a copy of the GNU General Public License along with this 
   program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
   MA 02139, USA. 
 
   email: dinoklein@hotmail.com 
*/ 
 
 
#define  WIN32_LEAN_AND_MEAN 
 
#include  
#include  
#include  
#include  
#include  
#include "resource.h" 
#include "defines.h" 
#include  
 
 
void ErrB(char*, int=0, HWND=0); 
bool set_capture_events (int); 
 
 
extern LPDIRECTSOUNDCAPTUREBUFFER  lpdscb; 
extern LPDIRECTSOUNDCAPTURE lpdsc; 
extern LPDIRECTSOUNDNOTIFY lpdsnc; 
 
 
bool init_wave_out_ds (HWND hwnd); 
bool init_wave_in_ds (HWND hwnd); 
void close_wave_out_ds (); 
void close_wave_in_ds (); 
 
 
 
extern bool           dbl; 
extern HANDLE         heap, hData; 
extern CRITICAL_SECTION cs; 
extern HWAVEIN        hIn; 
extern char           my_addr_txt [16], dst_addr_txt [16]; 
extern unsigned long  my_addr, dst_addr; 
extern char           listen_mode; 
extern HWND           talker_dlg; 
extern HANDLE         Thread [3]; 
extern int            band; 
extern bool  stereo, use_primary; 
 
extern HANDLE       hRec[10]; 
extern sound_sec    sqq[MAX_QUEUE]; 
extern int          index_put, index_take; 
extern int          queue_size, segments, lock_size; 
 
void list_interfaces (HWND hwnd) 
{ 
   LPHOSTENT  h; 
   char       name [128]; 
   long       r; 
   char       *add, **list; 
   int        index; 
   WSADATA    wd; 
 
 
   r = WSAStartup (0x0002, &wd); 
   if (!r) 
   { 
      r = gethostname (name, 128); 
      if (!r) 
      { 
         h = gethostbyname (name); 
         if (h) 
         { 
            if (h -> h_addrtype == AF_INET) 
            { 
               list = h->h_addr_list; 
               add = *list; 
               my_addr = *((DWORD*)add); 
               sprintf(name, "%u.%u.%u.%u", (unsigned)(unsigned char)add[0], (unsigned)(unsigned char)add[1], (unsigned)(unsigned char)add[2], (unsigned)(unsigned char)add[3]); 
               strcpy(my_addr_txt, name); 
               while (*list) 
               { 
                  add = *list; 
                  sprintf(name, "%u.%u.%u.%u", (unsigned)(unsigned char)add[0], (unsigned)(unsigned char)add[1], (unsigned)(unsigned char)add[2], (unsigned)(unsigned char)add[3]); 
                  index = SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_ADDSTRING, 0, (LPARAM)name); 
                  SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_SETITEMDATA, index, (LPARAM)*((DWORD*)add)); 
                  list++; 
               } 
               SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_SETCURSEL, 0, 0); 
               if (SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_GETITEMDATA, 0, 0) != 0x0100007F) 
               { 
                  strcpy(name, "127.0.0.1"); 
                  index = SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_ADDSTRING, 0, (LPARAM)name); 
                  SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_SETITEMDATA, index, (LPARAM)0x0100007F); 
               } 
 
            } 
         } 
      } 
   } 
   WSACleanup(); 
} 
 
 
BOOL CALLBACK talk_dialog (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 
{ 
   switch (msg) 
   { 
      case WM_INITDIALOG: 
         if (init_wave_out_ds(hwnd)==false) 
         { 
            MessageBox(hwnd, "failed to init wave out", 0, 0); 
            EndDialog(hwnd, 0); 
            lpdsc = 0; 
            lpdscb = 0; 
            lpdsnc = 0; 
            return 0; 
         } 
         if (init_wave_in_ds(hwnd)==false) 
         { 
            MessageBox(hwnd, "failed to init wave in", 0, 0); 
            EndDialog(hwnd,0); 
            close_wave_out_ds(); 
            return 0; 
         } 
 
         if (band!=0) EnableWindow(GetDlgItem(hwnd, IDC_DOUBLE), false); 
         SetDlgItemText (hwnd, IDC_STATIC2, my_addr_txt); 
         talker_dlg = hwnd; 
         if (listen_mode) SetDlgItemText (hwnd, IDC_STATIC4, "Awaiting Contact"); 
         else 
         { 
            SetDlgItemText (hwnd, IDC_STATIC4, dst_addr_txt); 
            lpdscb->Start(DSCBSTART_LOOPING); 
         } 
         SendDlgItemMessage(hwnd, IDC_REC4, BM_SETCHECK, BST_CHECKED, 0); 
         ResumeThread (Thread[1]); 
         ResumeThread (Thread[2]); 
         return (1); 
 
 
      case WM_COMMAND: 
         if (LOWORD(wparam)==IDB_CLOSE && HIWORD(wparam)==BN_CLICKED) 
         { 
            lpdscb->Stop(); 
            EndDialog (hwnd, 0); 
         } 
 
         if (LOWORD(wparam)==IDC_DOUBLE && HIWORD(wparam)==BN_CLICKED) 
         { 
            if (SendDlgItemMessage(hwnd, IDC_DOUBLE, BM_GETCHECK, 0, 0)==BST_CHECKED) dbl = true; 
            else dbl = false; 
         } 
 
         if (LOWORD(wparam)==IDB_FLUSH && HIWORD(wparam)==BN_CLICKED) 
         { 
            EnterCriticalSection(&cs); 
            int  xx = 0; 
 
            while (WaitForSingleObject(hData, 0)!=WAIT_TIMEOUT) xx++; 
            queue_size -= xx; 
            while (xx>0) 
            { 
               if (--index_put<0) index_put = MAX_QUEUE-1; 
               xx--; 
            } 
            LeaveCriticalSection(&cs); 
 
         } 
 
         // will have to reset the radio box to the previous one in case of failure 
         if (LOWORD(wparam)==IDC_REC10 && HIWORD(wparam)==BN_CLICKED && segments!=10) 
         { 
            lpdscb->Stop(); 
            if (set_capture_events(10)) 
            { 
               segments = 10; 
               lock_size = (stereo)?4408:2204; 
               for (int i=0; i<10; i++) ResetEvent(hRec[i]); 
            } 
            else MessageBox(hwnd, "failed on cap eve", 0, 0); 
            lpdscb->Start(DSCBSTART_LOOPING); 
         } 
         if (LOWORD(wparam)==IDC_REC4 && HIWORD(wparam)==BN_CLICKED && segments!=4) 
         { 
            lpdscb->Stop(); 
            if (set_capture_events(4)) 
            { 
               segments = 4; 
               lock_size = (stereo)?11024:5512; 
               for (int i=0; i<10; i++) ResetEvent(hRec[i]); 
            } 
            lpdscb->Start(DSCBSTART_LOOPING); 
         } 
         if (LOWORD(wparam)==IDC_REC3 && HIWORD(wparam)==BN_CLICKED && segments!=3) 
         { 
            lpdscb->Stop(); 
            if (set_capture_events(3)) 
            { 
               segments = 3; 
               lock_size = (stereo)?14700:7350; 
               for (int i=0; i<10; i++) ResetEvent(hRec[i]); 
            } 
            lpdscb->Start(DSCBSTART_LOOPING); 
         } 
         if (LOWORD(wparam)==IDC_REC2 && HIWORD(wparam)==BN_CLICKED && segments!=2) 
         { 
            lpdscb->Stop(); 
            if (set_capture_events(2)) 
            { 
               segments = 2; 
               lock_size = (stereo)?22048:11024; 
               for (int i=0; i<10; i++) ResetEvent(hRec[i]); 
            } 
            lpdscb->Start(DSCBSTART_LOOPING); 
         } 
         break; 
 
      case WM_CLOSE: 
         if (lpdscb) lpdscb->Stop(); 
         EndDialog(hwnd, 0); 
         return 1; 
 
 
      case RT_CONNECTED: 
         SetDlgItemText (hwnd, IDC_STATIC4, dst_addr_txt); 
         return (1); 
 
 
      case ST_SIZE: 
         char bf [32]; 
 
         sprintf (bf, "%d Milliseconds", (int) wparam); 
         SetDlgItemText (hwnd, IDC_STATIC5, bf); 
         return (1); 
 
      case ST_SEQ: 
         char bfg [32]; 
 
         sprintf (bfg, "%d - %d/%d", (int)wparam, ((int)lparam)&0xFFFF, ((int)lparam)>>16); 
         SetDlgItemText (hwnd, IDC_STATIC6, bfg); 
         return (1); 
 
      case ST_ERR: 
         char bfh[32]; 
         sprintf(bfh, "%d / %d", (int) wparam, (int)lparam); 
         SetDlgItemText(hwnd, IDS_ERROR, bfh); 
         return 1; 
 
      case ST_QUE: 
         char bfa[32]; 
         sprintf(bfa, "%d", (int) wparam); 
         SetDlgItemText(hwnd, IDS_QUEUE, bfa); 
         return 1; 
 
   } 
 
   return (0); 
} 
 
 
 
 
char check_address (HWND hwnd) 
{ 
   if (GetDlgItemText (hwnd, IDC_EDIT, dst_addr_txt, 16)) 
   { 
      dst_addr = inet_addr (dst_addr_txt); 
      if (dst_addr != INADDR_NONE) return (1); 
   } 
   return (0); 
} 
 
 
 
BOOL CALLBACK addr_dialog (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) 
{ 
   switch (msg) 
   { 
      case WM_INITDIALOG: 
         SendDlgItemMessage (hwnd, IDC_EDIT, EM_SETLIMITTEXT, 15, 0); 
         SendDlgItemMessage(hwnd, IDC_RADIO1, BM_SETCHECK, BST_CHECKED, 0); 
         list_interfaces(hwnd); 
         stereo = use_primary = false; 
         return (1); 
 
          
      case WM_COMMAND: 
         switch (LOWORD (wparam)) 
         { 
            case IDB_OK: 
               if (HIWORD (wparam) == BN_CLICKED) 
               { 
                  if (check_address (hwnd)) 
                  { 
                     EndDialog (hwnd, 1); 
                     listen_mode = 0; 
                  } 
                  else ErrB("the address you type is invalid", 0, hwnd); 
                  return (1); 
               } 
               break; 
 
            case IDB_CANCEL: 
               if (HIWORD (wparam) == BN_CLICKED) 
               { 
                  EndDialog (hwnd, 0); 
                  return (1); 
               } 
               break; 
 
            case IDB_LISTEN: 
               if (HIWORD (wparam) == BN_CLICKED) 
               { 
                  listen_mode = 1; 
                  EndDialog (hwnd, 1); 
                  return (1); 
               } 
               break; 
 
            case IDC_STEREO: 
               if (HIWORD(wparam) == BN_CLICKED) 
               { 
                  if (SendDlgItemMessage(hwnd, IDC_STEREO, BM_GETCHECK, 0, 0)==BST_CHECKED) 
                  { 
                     stereo = true; 
                     band = 2; 
                     SendDlgItemMessage(hwnd, IDC_RADIO3, BM_SETCHECK, BST_CHECKED, 0); 
                     SendDlgItemMessage(hwnd, IDC_RADIO1, BM_SETCHECK, BST_UNCHECKED, 0); 
                     SendDlgItemMessage(hwnd, IDC_RADIO2, BM_SETCHECK, BST_UNCHECKED, 0); 
                     EnableWindow(GetDlgItem(hwnd, IDC_RADIO1), 0); 
                     EnableWindow(GetDlgItem(hwnd, IDC_RADIO2), 0); 
                  } 
                  else 
                  {       
                     stereo = false; 
                     EnableWindow(GetDlgItem(hwnd, IDC_RADIO1), 1); 
                     EnableWindow(GetDlgItem(hwnd, IDC_RADIO2), 1); 
                  } 
               } 
               break; 
 
 
            case IDC_PRIMARY: 
               if (HIWORD(wparam) == BN_CLICKED) 
               { 
                  if (SendDlgItemMessage(hwnd, IDC_PRIMARY, BM_GETCHECK, 0, 0)==BST_CHECKED) use_primary = true; 
                  else use_primary = false; 
               } 
               break; 
 
 
            case IDC_RADIO1: 
               if (HIWORD(wparam) == BN_CLICKED) band = 0; 
               break; 
 
            case IDC_RADIO2: 
               if (HIWORD(wparam) == BN_CLICKED) band = 1; 
               break; 
 
            case IDC_RADIO3: 
               if (HIWORD(wparam) == BN_CLICKED) band = 2; 
               break; 
 
            case IDC_INTERFACE: 
               if (HIWORD(wparam)==LBN_SELCHANGE) 
               { 
                  CHAR  *hlp; 
 
 
                  my_addr = SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_GETITEMDATA, SendDlgItemMessage(hwnd, IDC_INTERFACE, LB_GETCURSEL, 0, 0), 0); 
                  hlp = (char*)&my_addr; 
                  sprintf(my_addr_txt, "%u.%u.%u.%u", (unsigned)(unsigned char)hlp[0], (unsigned)(unsigned char)hlp[1], (unsigned)(unsigned char)hlp[2], (unsigned)(unsigned char)hlp[3]); 
               } 
               break; 
 
         } 
         break; 
 
      case WM_CLOSE: 
         EndDialog(hwnd, 0); 
         return 1; 
         break; 
   }    
 
   return (0); 
}