2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Network/multi_voice.cpp $
16 * Revision 1.7 2005/10/02 09:30:10 taylor
17 * sync up rest of big-endian network changes. it should at least be as good as what's in FS2_Open now, only better :)
19 * Revision 1.6 2004/09/20 01:31:44 theoddone33
22 * Revision 1.5 2004/06/11 01:46:06 tigital
23 * byte-swapping changes for bigendian systems
25 * Revision 1.4 2003/08/03 16:10:29 taylor
26 * cleanup; compile warning fixes
28 * Revision 1.3 2002/06/09 04:41:24 relnev
29 * added copyright header
31 * Revision 1.2 2002/05/07 03:16:47 theoddone33
32 * The Great Newline Fix
34 * Revision 1.1.1.1 2002/05/03 03:28:10 root
38 * 9 3/10/99 6:50p Dave
39 * Changed the way we buffer packets for all clients. Optimized turret
40 * fired packets. Did some weapon firing optimizations.
42 * 8 3/09/99 6:24p Dave
43 * More work on object update revamping. Identified several sources of
44 * unnecessary bandwidth.
46 * 7 1/08/99 4:42p Anoop
47 * Fixed memory overwrite problem.
49 * 6 11/19/98 4:19p Dave
50 * Put IPX sockets back in psnet. Consolidated all multiplayer config
53 * 5 11/19/98 8:03a Dave
54 * Full support for D3-style reliable sockets. Revamped packet lag/loss
55 * system, made it receiver side and at the lowest possible level.
57 * 4 11/17/98 11:12a Dave
58 * Removed player identification by address. Now assign explicit id #'s.
60 * 3 11/05/98 5:55p Dave
61 * Big pass at reducing #includes
63 * 2 10/07/98 10:53a Dave
66 * 1 10/07/98 10:50a Dave
68 * 43 6/16/98 4:22p Allender
69 * fix Net_player reference problem
71 * 42 6/13/98 9:32p Mike
72 * Kill last character in file which caused "Find in Files" to report the
73 * file as "not a text file."
75 * 41 6/13/98 6:01p Hoffoss
76 * Externalized all new (or forgot to be added) strings to all the code.
78 * 40 6/13/98 3:19p Hoffoss
79 * NOX()ed out a bunch of strings that shouldn't be translated.
81 * 39 6/12/98 2:49p Dave
84 * 38 5/27/98 4:14p Dave
85 * Spiffed up text display for voice status.
87 * 37 5/27/98 1:15p Dave
88 * Change pxo login failure popup text. Put in message display of player
89 * who is currently doing voice.
91 * 36 5/24/98 3:45a Dave
92 * Minor object update fixes. Justify channel information on PXO. Add a
93 * bunch of configuration stuff for the standalone.
95 * 35 5/24/98 12:12a Frank
96 * Make sure recording clients don't ever send/store/handle packets past
97 * the limit of the accum buffer.
99 * 34 5/22/98 9:35p Dave
100 * Put in channel based support for PXO. Put in "shutdown" button for
101 * standalone. UI tweaks for TvT
103 * 33 5/20/98 2:24a Dave
104 * Fixed server side voice muting. Tweaked multi debrief/endgame
105 * sequencing a bit. Much friendlier for stats tossing/accepting now.
107 * 32 4/25/98 2:02p Dave
108 * Put in multiplayer context help screens. Reworked ingame join ship
109 * select screen. Fixed places where network timestamps get hosed.
111 * 31 4/21/98 4:44p Dave
112 * Implement Vasudan ships in multiplayer. Added a debug function to bash
113 * player rank. Fixed a few rtvoice buffer overrun problems. Fixed ui
114 * problem in options screen.
116 * 30 4/17/98 5:27p Dave
117 * More work on the multi options screen. Fixed many minor ui todo bugs.
119 * 29 4/09/98 11:01p Dave
120 * Put in new multi host options screen. Tweaked multiplayer options a
123 * 28 4/08/98 10:11a Dave
124 * Fixed a voice packet indexing problem.
126 * 27 4/07/98 5:42p Dave
127 * Put in support for ui display of voice system status (recording,
128 * playing back, etc). Make sure main hall music is stopped before
129 * entering a multiplayer game via ingame join.
131 * 26 4/04/98 4:22p Dave
132 * First rev of UDP reliable sockets is done. Seems to work well if not
135 * 25 4/03/98 4:22p Allender
136 * Fixed a bug where voice recording was not cut off after MAX_TIME.
138 * 24 4/03/98 1:03a Dave
139 * First pass at unreliable guaranteed delivery packets.
141 * 23 3/30/98 6:27p Dave
142 * Put in a more official set of multiplayer options, including a system
143 * for distributing netplayer and settings.
145 * 22 3/27/98 9:46a Dave
146 * Made default quality of sound 10.
148 * 21 3/26/98 6:01p Dave
149 * Put in file checksumming routine in cfile. Made pilot pic xferring more
150 * robust. Cut header size of voice data packets in half. Put in
151 * restricted game host query system.
153 * 20 3/25/98 8:36p Dave
154 * Removed potential bug spots from voice code.
156 * 19 3/25/98 2:16p Dave
157 * Select random default image for newly created pilots. Fixed several
158 * multi-pause messaging bugs. Begin work on online help for multiplayer
161 * 18 3/24/98 5:00p Dave
162 * Fixed several ui bugs. Put in pre and post voice stream playback sound
163 * fx. Put in error specific popups for clients getting dropped from games
164 * through actions other than their own.
166 * 17 3/23/98 7:18p Dave
167 * Converted voice to use streaming of packets as they are recorded.
169 * 16 3/22/98 7:13p Lawrance
170 * Get streaming of recording voice working
172 * 15 3/20/98 1:01p Dave
173 * Made the token system for multi_voice more reliable. Fixed a bug where
174 * standalones and other server who can't play sound don't route
175 * multi_voice correctly.
177 * 14 3/19/98 5:05p Dave
178 * Put in support for targeted multiplayer text and voice messaging (all,
179 * friendly, hostile, individual).
181 * 13 3/18/98 5:52p Dave
182 * Put in netgame password popup. Numerous ui changes. Laid groundwork for
183 * streamed multi_voice data.
185 * 12 3/18/98 3:32p Dave
186 * Put in a hook for streamed rtvoice data from the rtvoice system.
188 * 11 3/18/98 9:43a Dave
189 * Removed an uninitialized data warning.
191 * 10 3/17/98 12:30a Dave
192 * Put in hud support for rtvoice. Several ui interface changes.
194 * 9 3/16/98 2:35p Dave
195 * Numerous bug fixes. Made the "cue sound" sound play before incoming
198 * 8 3/15/98 4:17p Dave
199 * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
200 * of network orientation matrices.
202 * 7 3/04/98 4:36p Dave
203 * Fixed a compiler warning.
205 * 6 2/26/98 6:01p Dave
206 * Made voice playback work correctly under NT.
208 * 5 2/26/98 5:01p Dave
209 * Fixed a multi_voice_reset() bug.
211 * 4 2/26/98 4:21p Dave
212 * More robust multiplayer voice.
214 * 3 2/25/98 8:58p Dave
215 * Got first full implementation of multi voice done.
217 * 2 2/24/98 11:56p Lawrance
218 * Change real-time voice code to provide the uncompressed size on decode.
220 * 1 2/24/98 10:12p Dave
221 * Initial pass at multiplayer voice streaming.
228 #include "freespace.h"
229 #include "gamesequence.h"
230 #include "multimsgs.h"
231 #include "multiutil.h"
232 #include "multi_voice.h"
233 #include "multi_pmsg.h"
237 #include "optionsmenumulti.h"
238 #include "stand_server.h"
240 #include "multi_fstracker.h"
243 // --------------------------------------------------------------------------------------------------
244 // MULTI VOICE DEFINES/VARS
247 // #define MULTI_VOICE_POST_DECOMPRESS // when we're _not_ using streaming
248 #define MULTI_VOICE_PRE_DECOMPRESS // when we _are_ using streaming
250 #define MULTI_VOICE_VERBOSE // keep this defined for verbose debug output
252 #define MULTI_VOICE_LOCAL_ECHO // keep this defined for local echo of recorded network voice
254 // flag indicating the status of the multi voice system
255 int Multi_voice_inited = 0;
256 int Multi_voice_can_record = 0;
257 int Multi_voice_can_play = 0;
258 int Multi_voice_send_mode = MULTI_MSG_NONE; // gotten from the multi_msg system when we start recording
260 // packet code defines
261 #define MV_CODE_GIVE_TOKEN 0 // received player side - he now has the token to speak
262 #define MV_CODE_DENY_TOKEN 1 // received player side - server has denied this request
263 #define MV_CODE_TAKE_TOKEN 2 // received player side - the server is forcibly taking his token
264 #define MV_CODE_RELEASE_TOKEN 3 // received server side - player is relinquishing token
265 #define MV_CODE_REQUEST_TOKEN 4 // received server side - player is requesting token
266 #define MV_CODE_PLAYER_PREFS 5 // received server side - player bitflags for who he'll receive from
267 #define MV_CODE_DATA 6 // sound data
268 #define MV_CODE_DATA_DUMMY 7 // in place of a packet which has been deemed too large, so that receivers don't time out early
270 // default quality of sound
271 #define MV_DEFAULT_QOS 10 // default quality of sound
272 int Multi_voice_qos; // default quality of sound
274 // sounds added to the front and end of a playing voice stream (set to -1 if none are wanted)
275 #define MULTI_VOICE_PRE_SOUND SND_CUE_VOICE
276 #define MULTI_VOICE_POST_SOUND SND_END_VOICE
277 int Multi_voice_pre_sound_size = 0;
281 // NOTE : the following 2 defines should be used for reference only. they represent the worst case situation,
282 // sending voice to a specific target under IPX. you should use multi_voice_max_chunk_size(...) when
283 // determining if a given chunk will fit into an individual freespace packet
284 // max size of a data packet header (note, this changes as the code itself changes - should probably never use this except for reference)
285 #define MULTI_VOICE_MAX_HEADER_SIZE 22
286 // size of an individual chunk (CHUNK == block of data stuck into a packet), in the worst case of header size (see above)
287 #define MULTI_VOICE_MAX_CHUNK_SIZE 488
289 // total max size of an incoming or an outgoing uncompressed buffer (note this is probably too big, but we won't worry about that for now)
290 #define MULTI_VOICE_MAX_BUFFER_SIZE ((1<<16)+(1<<14)) // 80k
292 // overall size of an total accum buffer for a stream
293 #define MULTI_VOICE_ACCUM_BUFFER_SIZE (1<<14) // 16k
295 // how many accum buffers need to be in a total accum buffer
296 // NOTE : we reference MULTI_VOICE_MAX_CHUNK_SIZE here because it is worst case. ie, we'll always have enough
297 // accum buffers in anything better than the worst case if we use MULTI_VOICE_MAX_CHUNK_SIZE
298 #define MULTI_VOICE_ACCUM_BUFFER_COUNT (MULTI_VOICE_ACCUM_BUFFER_SIZE / MULTI_VOICE_MAX_CHUNK_SIZE)
300 int Multi_voice_max_time; // current maximum recording time
301 char *Multi_voice_record_buffer = NULL; // buffer for recording back voice
302 char *Multi_voice_playback_buffer = NULL; // buffer for processing the accum buffer and playing the result
305 #ifdef MULTI_VOICE_POST_DECOMPRESS
306 char Multi_voice_unpack_buffer[MULTI_VOICE_MAX_BUFFER_SIZE];
309 // the max amount of tokens we want to be floating about (max sound streams)
310 #define MULTI_VOICE_MAX_STREAMS 1
312 // voice algorithm stuff
313 // it would probably be good to base the timeout time on some multiple of our average ping to the server
314 #define MV_ALG_TIMEOUT 500 // if start get new data for a window then a pause this long, play the window
315 int Multi_voice_stamps[MULTI_VOICE_MAX_STREAMS];
317 // NOTE : this should be > then MULTI_VOICE_MAX_TIME + the time for the data to come over a network connection!!
318 #define MULTI_VOICE_TOKEN_TIMEOUT 7000 // timeout - server will take the token back if he does not hear from the guy in this amount of time
320 #define MULTI_VOICE_TOKEN_RELEASE_WAIT (1.0f) // wait 1 second
322 // the token index of a voice stream is set to one of these values, or the index of the player who has the token
323 #define MULTI_VOICE_TOKEN_INDEX_FREE -1 // the token (and the stream are free)
324 #define MULTI_VOICE_TOKEN_INDEX_RELEASED 0xDEADBEAD // the token has been released but the stream is still active
326 typedef struct voice_stream {
327 int token_status; // status of the token (player index if a player has it) or one of the above defines
328 int token_stamp; // timestamp for the MULTI_VOICE_TOKEN_TIMEOUT
330 short stream_from; // id of the player the stream is coming from
332 ubyte *accum_buffer[MULTI_VOICE_ACCUM_BUFFER_COUNT]; // accum buffer
333 ubyte accum_buffer_flags[MULTI_VOICE_ACCUM_BUFFER_COUNT]; // flag indicating the existence of a given accum (sub)buffer
334 ushort accum_buffer_usize[MULTI_VOICE_ACCUM_BUFFER_COUNT]; // uncompressed size of the corresponding (sub)buffer
335 ushort accum_buffer_csize[MULTI_VOICE_ACCUM_BUFFER_COUNT]; // compressed size of the corresponding (sub)buffer
336 double accum_buffer_gain[MULTI_VOICE_ACCUM_BUFFER_COUNT]; // gain of the corresponding (sub)buffer
338 ubyte stream_id; // stream id #
339 fix stream_last_heard; // last time we heard from this stream
341 fix stream_start_time; // time the stream started playing
342 int stream_snd_handle; // sound playing instance handle
343 int stream_rtvoice_handle; // rtvoice buffer handle
345 voice_stream Multi_voice_stream[MULTI_VOICE_MAX_STREAMS]; // voice streams themselves
348 #define MULTI_VOICE_KEY SDLK_BACKQUOTE // key used for realtime voice
349 int Multi_voice_keydown = 0; // is the record key currently being pressed
350 int Multi_voice_recording = 0; // flag indicating if we're currently recording or not
351 int Multi_voice_token = 0; // if we currently have a token or not
352 int Multi_voice_recording_stamp = -1; // how long we've been recording
353 ubyte Multi_voice_stream_id = 0; // stream id for the stream we're currently sending
354 int Multi_voice_current_stream_index = 0; // packet index of the currently recodring stream
355 int Multi_voice_current_stream_sent = -1; // index of packet we've sent up to
358 ubyte Multi_voice_next_stream_id = 0; // kept on the server - given to the next valid token requester
359 int Multi_voice_player_prefs[MAX_PLAYERS]; // player bitflag preferences
361 // voice status data - used for determing the result of multi_voice_status
362 #define MULTI_VOICE_DENIED_TIME 1000 // how long to display the "denied" status
363 int Multi_voice_denied_stamp = -1; // timestamp for when we got denied a token
365 // local muting preferences
366 int Multi_voice_local_prefs = 0xffffffff;
369 // --------------------------------------------------------------------------------------------------
370 // MULTI VOICE FORWARD DECLARATIONS
373 // process voice details as the server
374 void multi_voice_server_process();
376 // process voice details as a player (may also be the server)
377 void multi_voice_player_process();
379 // determine if the voice key is down this frame
380 int multi_voice_keydown();
382 // find the voice stream index by token player index
383 int multi_voice_find_token(int player_index);
385 // <server> gives the token to a given player
386 void multi_voice_give_token(int stream_index,int player_index);
388 // <server> takes the token from a given stream entry
389 void multi_voice_take_token(int stream_index);
391 // <server> tells the client he's been denied on this request
392 void multi_voice_deny_token(int player_index);
394 // <player> releases the token back to the server
395 void multi_voice_release_token();
397 // <player> requests the token from the server
398 void multi_voice_request_token();
400 // <server> process a request for the token
401 void multi_voice_process_token_request(int player_index);
403 // free up any memory which may have been malloced
404 void multi_voice_free_all();
406 // <player> send the currently recorded sound
407 void multi_voice_player_send_stream();
409 // process incoming sound data, return bytes processed
410 int multi_voice_process_data(ubyte *data, int player_index,int msg_mode,net_player *target);
412 // <server> increment the current stream id#
413 void multi_voice_inc_stream_id();
415 // flush any old sound stream data because we've started to receive data for a new stream
416 void multi_voice_flush_old_stream(int stream_index);
418 // route sound data through the server to all appropriate players
419 void multi_voice_route_data(ubyte *data, int packet_size,int player_index,int mode,net_player *target);
421 // find the stream to apply incoming sound data to, freeing up old ones as necessary
422 int multi_voice_get_stream(int stream_id);
424 // NOTE : these 4 functions can be arbitrarily written to perform in any way necessary. This way the algorithm is
425 // completely seperate from the transport and token layers
426 // initialize the smart algorithm
427 void multi_voice_alg_init();
429 // process incoming sound data in whatever way necessary (this function should take care of playing data when necessary)
430 void multi_voice_alg_process_data(int player_index,int stream_index,ushort chunk_index,ushort chunk_size);
432 // process existing streams
433 void multi_voice_alg_process_streams();
435 // we are going to flush the current stream because we have started to receive data for a new one. do something first
436 void multi_voice_alg_flush_old_stream(int stream_index);
438 // is the given sound stream playing (compares uncompressed sound size with current playback position)
439 int multi_voice_stream_playing(int stream_index);
441 // tack on a post voice sound (pass -1 for none)
442 // return final buffer size
443 int multi_voice_mix(int post_sound,char *data,int cur_size,int max_size);
445 // send a dummy packet in the place of a too-large data packet
446 void multi_voice_send_dummy_packet();
448 // process a dummy data packet
449 int multi_voice_process_data_dummy(ubyte *data);
451 // max size of a sound chunk which we can fit into a packet
452 int multi_voice_max_chunk_size(int msg_mode);
454 // process a player preferences packet, return bytes processed
455 int multi_voice_process_player_prefs(ubyte *data,int player_index);
457 // process and play the current window of sound stream data we have. reset the window for the next incoming data as well
458 void multi_voice_alg_play_window(int stream_index);
460 // send all pending voice packets
461 void multi_voice_client_send_pending();
464 // --------------------------------------------------------------------------------------------------
465 // MULTI VOICE FUNCTIONS
468 // initialize the multiplayer voice system
469 void multi_voice_init()
471 int idx,s_idx,pre_size,pre_sound;
473 // if the voice system is already initialized, just reset some stuff
474 if(Multi_voice_inited){
479 // set the default quality of sound
480 Multi_voice_qos = MV_DEFAULT_QOS;
482 // if we're the standalone server, we can't record _or_ playback, but we can still route data and manage tokens
483 if(Game_mode & GM_STANDALONE_SERVER){
484 Multi_voice_can_record = 0;
485 Multi_voice_can_play = 0;
487 // initialize the realtime voice module
488 if(rtvoice_init_recording(Multi_voice_qos)){
489 nprintf(("Network","MULTI VOICE : Error initializing rtvoice - recording will not be possible\n"));
490 Multi_voice_can_record = 0;
492 Multi_voice_can_record = 1;
495 if(rtvoice_init_playback()){
496 nprintf(("Network","MULTI VOICE : Error initializing rtvoice - playback will not be possible\n"));
497 Multi_voice_can_play = 0;
499 Multi_voice_can_play = 1;
502 // _always_ set the quality of server
503 multi_voice_set_vars(MV_DEFAULT_QOS,MULTI_VOICE_MAX_TIME);
506 // initialize player-side data
507 Multi_voice_token = 0;
508 Multi_voice_keydown = 0;
509 Multi_voice_recording = 0;
510 Multi_voice_stream_id = 0;
511 Multi_voice_recording_stamp = -1;
512 Multi_voice_current_stream_index = 0;
513 Multi_voice_current_stream_sent = -1;
515 // initialize server-side data
516 memset(Multi_voice_player_prefs,0xff,sizeof(int)*MAX_PLAYERS);
517 Multi_voice_next_stream_id = 0;
519 Multi_voice_local_prefs = 0xffffffff;
521 // initialize the sound buffers
522 Multi_voice_record_buffer = NULL;
524 Multi_voice_playback_buffer = NULL;
525 Multi_voice_pre_sound_size = 0;
526 if(Multi_voice_can_play){
527 // attempt to allocate the buffer
528 Multi_voice_playback_buffer = (char*)malloc(MULTI_VOICE_MAX_BUFFER_SIZE);
529 if(Multi_voice_playback_buffer == NULL){
530 nprintf(("Network","MULTI VOICE : Error allocating playback buffer - playback will not be possible\n"));
531 Multi_voice_can_play = 0;
534 // attempt to copy in the "pre" voice sound
535 pre_sound = snd_load(&Snds[MULTI_VOICE_PRE_SOUND]);
536 Multi_voice_pre_sound_size = 0;
538 // get the pre-sound size
539 if((snd_size(pre_sound,&pre_size) != -1) && (pre_size < MULTI_VOICE_MAX_BUFFER_SIZE)){
540 if ( !snd_get_data(pre_sound,Multi_voice_playback_buffer) ) {
541 Multi_voice_pre_sound_size = pre_size;
547 // initialize the streams
548 memset(Multi_voice_stream,0,sizeof(voice_stream) * MULTI_VOICE_MAX_STREAMS);
549 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
550 Multi_voice_stream[idx].token_status = MULTI_VOICE_TOKEN_INDEX_FREE;
551 Multi_voice_stream[idx].token_stamp = -1;
552 Multi_voice_stream[idx].stream_snd_handle = -1;
554 // get a playback buffer handle
555 if(Multi_voice_can_play){
556 Multi_voice_stream[idx].stream_rtvoice_handle = -1;
557 Multi_voice_stream[idx].stream_rtvoice_handle = rtvoice_create_playback_buffer();
558 if(Multi_voice_stream[idx].stream_rtvoice_handle == -1){
559 nprintf(("Network","MULTI VOICE : Error getting rtvoice buffer handle - playback will not be possible!\n"));
560 multi_voice_free_all();
562 Multi_voice_can_play = 0;
565 // allocate the accum buffer
566 for(s_idx=0;s_idx<MULTI_VOICE_ACCUM_BUFFER_COUNT;s_idx++){
567 Multi_voice_stream[idx].accum_buffer[s_idx] = NULL;
568 Multi_voice_stream[idx].accum_buffer[s_idx] = (ubyte*)malloc(MULTI_VOICE_ACCUM_BUFFER_SIZE);
569 if(Multi_voice_stream[idx].accum_buffer[s_idx] == NULL){
570 nprintf(("Network","MULTI VOICE : Error allocating accum buffer - playback will not be possible\n"));
571 multi_voice_free_all();
573 Multi_voice_can_play = 0;
579 // initialize the default max time
580 Multi_voice_max_time = MULTI_VOICE_MAX_TIME;
582 // initialize voice status data
583 Multi_voice_denied_stamp = -1;
585 // initialize the smart algorithm
586 multi_voice_alg_init();
588 Multi_voice_inited = 1;
591 // shutdown the multiplayer voice system
592 void multi_voice_close()
596 // if the voice system isn't already initialized, don't do anything
597 if(!Multi_voice_inited){
602 multi_voice_free_all();
604 // release all the rtvoice buffers
605 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
606 if(Multi_voice_stream[idx].stream_rtvoice_handle != -1){
607 rtvoice_free_playback_buffer(Multi_voice_stream[idx].stream_rtvoice_handle);
608 Multi_voice_stream[idx].stream_rtvoice_handle = -1;
609 Multi_voice_stream[idx].stream_snd_handle = -1;
613 // close the realtime voice module
614 rtvoice_close_recording();
615 rtvoice_close_playback();
617 Multi_voice_inited = 0;
620 // reset between levels
621 void multi_voice_reset()
625 #ifdef MULTI_VOICE_VERBOSE
626 nprintf(("Network","MULTI VOICE : Resetting\n"));
629 SDL_assert(Multi_voice_inited);
631 // if we're the standalone server, we can't record _or_ playback, but we can still route data and manage tokens
632 if(Game_mode & GM_STANDALONE_SERVER){
633 Multi_voice_can_record = 0;
634 Multi_voice_can_play = 0;
637 // initialize player-side data
638 Multi_voice_token = 0;
639 Multi_voice_keydown = 0;
640 Multi_voice_recording = 0;
641 Multi_voice_stream_id = 0;
642 Multi_voice_recording_stamp = -1;
644 // initialize server-side data
645 memset(Multi_voice_player_prefs,0xff,sizeof(int)*MAX_PLAYERS);
646 Multi_voice_local_prefs = 0xffffffff;
647 Multi_voice_next_stream_id = 0;
649 // initialize the sound buffers
650 Multi_voice_record_buffer = NULL;
652 // initialize the streams
653 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
654 Multi_voice_stream[idx].token_status = MULTI_VOICE_TOKEN_INDEX_FREE;
655 Multi_voice_stream[idx].token_stamp = -1;
658 // initialize the smart algorithm
659 multi_voice_alg_init();
662 // process all voice details
663 void multi_voice_process()
667 // don't do anything if the voice module is not initialized
668 if((!Multi_voice_inited) || !(Net_player->flags & NETINFO_FLAG_CONNECTED)){
672 // send all pending voice packets
673 multi_voice_client_send_pending();
675 // find any playing sound streams which have finished and unmark them
676 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
677 if((Multi_voice_stream[idx].stream_snd_handle != -1) && !multi_voice_stream_playing(idx)){
678 Multi_voice_stream[idx].stream_snd_handle = -1;
682 // process seperately as player or server
683 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
684 multi_voice_server_process();
687 // all "players" do this, except the standalone who isn't a real player by definition
688 if(!(Game_mode & GM_STANDALONE_SERVER)){
689 multi_voice_player_process();
692 // everyont calls the general algorithm process function
693 multi_voice_alg_process_streams();
696 // voice settings debug console function
697 void multi_voice_dcf()
699 dc_get_arg(ARG_STRING);
701 // set the quality of sound
702 if (strcmp(Dc_arg, NOX("qos")) == 0) {
704 if(Dc_arg_type & ARG_INT){
705 if((Dc_arg_int >= 1) && (Dc_arg_int <= 10) && (Net_player->flags & NETINFO_FLAG_AM_MASTER)){
706 multi_voice_set_vars(Dc_arg_int,-1);
707 dc_printf("Quality of sound : %d\n",Dc_arg_int);
713 // the status of the voice system - use this to determine what bitmaps to display, etc see above MULTI_VOICE_STATUS_* defines
714 int multi_voice_status()
720 // if the "denied" timestamp is set, return that as the status
721 if(Multi_voice_denied_stamp != -1){
722 return MULTI_VOICE_STATUS_DENIED;
725 // if we're currently recording (has precedence over playing back a sound from somebody)
726 if(Multi_voice_recording){
727 return MULTI_VOICE_STATUS_RECORDING;
730 // find the stream which started playing the farthest back (if any)
733 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
734 // if we found a playing stream
735 if(Multi_voice_stream[idx].stream_snd_handle != -1){
736 if((earliest == -1) || (Multi_voice_stream[idx].stream_start_time < earliest_time)){
738 earliest_time = Multi_voice_stream[idx].stream_start_time;
742 // if we found a stream
744 return MULTI_VOICE_STATUS_PLAYING;
748 return MULTI_VOICE_STATUS_IDLE;
751 // update the qos if the current setting is different from the passed in value
752 void multi_voice_maybe_update_vars(int new_qos,int new_duration)
754 // if the current qos is different from the passed qos, set it
755 if((new_qos != Multi_voice_qos) || (new_duration != Multi_voice_max_time)){
756 multi_voice_set_vars(new_qos,new_duration);
761 // --------------------------------------------------------------------------------------------------
762 // MULTI VOICE FORWARD DECLARATIONS
765 // process voice details as the server
766 void multi_voice_server_process()
770 // process all the tokens for all the available streams
771 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
772 switch(Multi_voice_stream[idx].token_status){
773 // if the token is free, so is the stream - don't do anything
774 case MULTI_VOICE_TOKEN_INDEX_FREE:
777 // if the token has been released - check to see if the stream is "done" (ie, can be marked as FREE once again)
778 case MULTI_VOICE_TOKEN_INDEX_RELEASED:
779 // if the stream_last_heard var is -1, it means we never got sound from this guy so free the token up immediately
780 if(Multi_voice_stream[idx].stream_last_heard == -1){
781 Multi_voice_stream[idx].token_status = MULTI_VOICE_TOKEN_INDEX_FREE;
783 #ifdef MULTI_VOICE_VERBOSE
784 nprintf(("Network","MULTI VOICE : freeing released token (no packets)\n"));
787 // if a sufficiently long amount of time has elapsed since he released the token, free it up
790 t1 = f2fl(Multi_voice_stream[idx].stream_last_heard);
791 t2 = f2fl(timer_get_fixed_seconds());
792 if((t2 - t1) >= MULTI_VOICE_TOKEN_RELEASE_WAIT){
793 Multi_voice_stream[idx].token_status = MULTI_VOICE_TOKEN_INDEX_FREE;
795 #ifdef MULTI_VOICE_VERBOSE
796 nprintf(("Network","MULTI VOICE : freeing released token (time elapsed)\n"));
802 // if the token is still being held by a player
804 // if the token timestamp has elapsed, take the token back
805 if((Multi_voice_stream[idx].token_stamp != -1) && timestamp_elapsed(Multi_voice_stream[idx].token_stamp)){
806 SDL_assert(Multi_voice_stream[idx].token_status != MULTI_VOICE_TOKEN_INDEX_FREE);
807 multi_voice_take_token(idx);
813 // for each netplayer, if his token wait timestamp is running, see if it has popped yet
814 for(idx=0;idx<MAX_PLAYERS;idx++){
815 if(MULTI_CONNECTED(Net_players[idx]) && (Net_players[idx].s_info.voice_token_timestamp != -1) && timestamp_elapsed(Net_players[idx].s_info.voice_token_timestamp)){
816 // unset it so that he can have the token again
817 Net_players[idx].s_info.voice_token_timestamp = -1;
822 // process voice details as a player (may also be the server)
823 void multi_voice_player_process()
825 // if the voice key is down for the first time this frame, send a request for the token
826 if(!Multi_voice_keydown && multi_voice_keydown() && Multi_voice_can_record && !(Netgame.options.flags & MSO_FLAG_NO_VOICE)){
827 // mark the key as being down
828 Multi_voice_keydown = 1;
830 // send a request for a token
831 multi_voice_request_token();
833 #ifdef MULTI_VOICE_VERBOSE
834 nprintf(("Network","MULTI VOICE : Request\n"));
838 // if the key is still being pressed
839 if(Multi_voice_keydown && multi_voice_keydown() && Multi_voice_can_record){
840 // if we have the token
841 if(Multi_voice_token){
842 // if we're not already recording, start recording
843 if(!Multi_voice_recording){
844 #ifdef MULTI_VOICE_VERBOSE
845 nprintf(("Network","MULTI VOICE : RECORD %d\n",(int)Multi_voice_stream_id));
847 // flush the old stream
848 multi_voice_flush_old_stream(0);
850 // start the recording process with the appropriate callback function
851 if(rtvoice_start_recording(multi_voice_process_next_chunk)){
852 nprintf(("Network","MULTI VOICE : Error initializing recording!\n"));
856 // set myself to be recording
857 Multi_voice_recording = 1;
859 // set the time when I started recording
860 Multi_voice_recording_stamp = timestamp(Multi_voice_max_time);
862 // set the current packet/chunk index to 0
863 Multi_voice_current_stream_index = 0;
864 Multi_voice_current_stream_sent = 0;
866 // get the proper messaging mode
867 if(Game_mode & GM_IN_MISSION){
868 // in mission, paused
869 if(gameseq_get_state() == GS_STATE_MULTI_PAUSED){
870 Multi_voice_send_mode = MULTI_MSG_ALL;
872 // in mission, unpaused
874 Multi_voice_send_mode = multi_msg_mode();
877 Multi_voice_send_mode = MULTI_MSG_ALL;
881 // if we've recorded the max time allowed, send the data
882 if((Multi_voice_recording_stamp != -1) && timestamp_elapsed(Multi_voice_recording_stamp)){
883 #ifdef MULTI_VOICE_VERBOSE
884 nprintf(("Network","MULTI VOICE : timestamp popped"));
886 // mark me as no longer recording
887 Multi_voice_recording = 0;
888 Multi_voice_current_stream_sent = -1;
890 // stop the recording process
891 rtvoice_stop_recording();
893 #ifdef MULTI_VOICE_POST_DECOMPRESS
894 multi_voice_player_send_stream();
897 // play my sound locally as well
898 #ifdef MULTI_VOICE_LOCAL_ECHO
899 multi_voice_alg_play_window(0);
901 // release the token back to the server
902 multi_voice_release_token();
906 // if the key has been released
907 else if(Multi_voice_keydown && !multi_voice_keydown() && Multi_voice_can_record){
908 #ifdef MULTI_VOICE_VERBOSE
909 nprintf(("Network","MULTI VOICE : Release\n"));
912 // mark the kay as not being down
913 Multi_voice_keydown = 0;
915 // if we were recording, send the data
916 if(Multi_voice_recording){
917 // mark me as no longer recording
918 Multi_voice_recording = 0;
920 Multi_voice_current_stream_sent = -1;
922 // stop the recording process
923 rtvoice_stop_recording();
925 #ifdef MULTI_VOICE_POST_DECOMPRESS
926 multi_voice_player_send_stream();
929 // play my sound locally as well
930 #ifdef MULTI_VOICE_LOCAL_ECHO
931 multi_voice_alg_play_window(0);
934 // release the token back to the server
935 multi_voice_release_token();
939 // if the "denied" timestamp is set, but has elapsed or the user has let up on the key, set it to -1
940 if((Multi_voice_denied_stamp != -1) && (timestamp_elapsed(Multi_voice_denied_stamp) || !multi_voice_keydown())){
941 Multi_voice_denied_stamp = -1;
945 // determine if the voice key is down this frame
946 int multi_voice_keydown()
948 // if we're in the options screen, we should never allow the button to be pressed
949 if(gameseq_get_state() == GS_STATE_OPTIONS_MENU){
953 // if we're pre-game, we should just be checking the keyboard bitflags
954 if(!(Game_mode & GM_IN_MISSION)){
955 return (key_pressed(MULTI_VOICE_KEY) && !(key_pressed(SDLK_LSHIFT) || key_pressed(SDLK_RSHIFT))) ? 1 : 0;
958 // in-mission, paused - treat just like any other "chattable" screen.
959 if(gameseq_get_state() == GS_STATE_MULTI_PAUSED){
960 return (key_pressed(MULTI_VOICE_KEY) && !(key_pressed(SDLK_LSHIFT) || key_pressed(SDLK_RSHIFT))) ? 1 : 0;
963 // ingame, unpaused, rely on the multi-messaging system (ingame)
964 return multi_msg_voice_record();
967 // find the voice stream index by token player index
968 int multi_voice_find_token(int player_index)
972 // look through all the existing streams
973 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
974 if(Multi_voice_stream[idx].token_status == player_index){
983 // <server> gives the token to a given player
984 void multi_voice_give_token(int stream_index,int player_index)
989 // only the server should ever be here
990 SDL_assert(Net_player->flags & NETINFO_FLAG_AM_MASTER);
992 // set this player as having the token
993 Multi_voice_stream[stream_index].token_status = player_index;
995 // set the token timeout
996 Multi_voice_stream[stream_index].token_stamp = timestamp(MULTI_VOICE_TOKEN_TIMEOUT);
998 // set the stream id and increment the count
999 Multi_voice_stream[stream_index].stream_id = Multi_voice_next_stream_id;
1000 multi_voice_inc_stream_id();
1002 // set the last heard from time to -1 to indicate we've heard no sound from this guy
1003 Multi_voice_stream[stream_index].stream_last_heard = -1;
1005 #ifdef MULTI_VOICE_VERBOSE
1006 nprintf(("Network","MULTI VOICE : GIVE TOKEN %d\n",(int)Multi_voice_next_stream_id));
1009 // if we're giving to ourself, don't send any data
1010 if(Net_player == &Net_players[player_index]){
1011 Multi_voice_token = 1;
1013 Multi_voice_stream_id = Multi_voice_stream[stream_index].stream_id;
1015 // send the "give" packet to the guy
1016 BUILD_HEADER(VOICE_PACKET);
1017 code = MV_CODE_GIVE_TOKEN;
1020 // add the current stream id#
1021 ADD_DATA(Multi_voice_stream[stream_index].stream_id);
1024 multi_io_send_reliable(&Net_players[player_index], data, packet_size);
1028 // <server> takes the token from a given player
1029 void multi_voice_take_token(int stream_index)
1031 ubyte data[10],code;
1032 int packet_size = 0;
1034 // only the server should ever be here
1035 SDL_assert(Net_player->flags & NETINFO_FLAG_AM_MASTER);
1037 // if the index is -1, the token has probably been released to us "officially" already
1038 if((Multi_voice_stream[stream_index].token_status == (int)MULTI_VOICE_TOKEN_INDEX_FREE) || (Multi_voice_stream[stream_index].token_status == (int)MULTI_VOICE_TOKEN_INDEX_RELEASED)){
1039 Multi_voice_stream[stream_index].token_stamp = -1;
1043 // if i'm taking from myself, don't send any data
1044 if(Net_player == &Net_players[Multi_voice_stream[stream_index].token_status]){
1045 Multi_voice_token = 0;
1047 // timestamp this guy so that he can't get the token back immediately
1048 Net_player->s_info.voice_token_timestamp = timestamp(Netgame.options.voice_token_wait);
1050 // send the "take" packet to the guy
1051 BUILD_HEADER(VOICE_PACKET);
1052 code = MV_CODE_TAKE_TOKEN;
1056 multi_io_send_reliable(&Net_players[Multi_voice_stream[stream_index].token_status], data, packet_size);
1058 // timestamp this guy so that he can't get the token back immediately
1059 Net_players[Multi_voice_stream[stream_index].token_status].s_info.voice_token_timestamp = timestamp(Netgame.options.voice_token_wait);
1062 // take the token back from the dude
1063 Multi_voice_stream[stream_index].token_status = MULTI_VOICE_TOKEN_INDEX_RELEASED;
1064 Multi_voice_stream[stream_index].token_stamp = -1;
1067 // <server> tells the client he's been denied on this request
1068 void multi_voice_deny_token(int player_index)
1070 ubyte data[10],code;
1071 int packet_size = 0;
1073 // only the server should ever be here
1074 SDL_assert(Net_player->flags & NETINFO_FLAG_AM_MASTER);
1077 // if i'm denying myself, set the denied timestamp
1078 if(Net_player == &Net_players[player_index]){
1079 Multi_voice_denied_stamp = timestamp(MULTI_VOICE_DENIED_TIME);
1081 // send the "deny" packet to the guy
1082 BUILD_HEADER(VOICE_PACKET);
1083 code = MV_CODE_DENY_TOKEN;
1087 multi_io_send_reliable(&Net_players[player_index], data, packet_size);
1091 // <player> releases the token back to the server
1092 void multi_voice_release_token()
1094 ubyte data[10],code;
1095 int packet_size = 0;
1097 // I don't have the token anymore
1098 Multi_voice_token = 0;
1100 // if i'm the server, don't send any data
1101 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1102 // mark the token as being released
1103 int stream_index = multi_voice_find_token(MY_NET_PLAYER_NUM);
1104 Multi_voice_stream[stream_index].token_status = MULTI_VOICE_TOKEN_INDEX_RELEASED;
1106 // timestamp this guy so that he can't get the token back immediately
1107 Net_player->s_info.voice_token_timestamp = timestamp(Netgame.options.voice_token_wait);
1109 // send the "release" packet to the server
1110 BUILD_HEADER(VOICE_PACKET);
1111 code = MV_CODE_RELEASE_TOKEN;
1115 multi_io_send_reliable(Net_player, data, packet_size);
1119 // <player> requests the token from the server
1120 void multi_voice_request_token()
1122 ubyte data[10],code;
1123 int packet_size = 0;
1125 // if i'm the server, process the request right now
1126 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1127 multi_voice_process_token_request(MY_NET_PLAYER_NUM);
1129 // send the "request" packet to the server
1130 BUILD_HEADER(VOICE_PACKET);
1131 code = MV_CODE_REQUEST_TOKEN;
1135 multi_io_send_reliable(Net_player, data, packet_size);
1139 // <player> sends hit bitflag settings (who he'll receive sound from, etc)
1140 void multi_voice_set_prefs(int pref_flags)
1142 ubyte data[MAX_PACKET_SIZE],code;
1144 int packet_size = 0;
1146 // set the local flags
1147 Multi_voice_local_prefs = pref_flags;
1149 // if i'm the server, set the sound prefs right now
1150 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1151 Multi_voice_player_prefs[MY_NET_PLAYER_NUM] = pref_flags;
1153 // send the prefs to the server
1154 BUILD_HEADER(VOICE_PACKET);
1155 code = MV_CODE_PLAYER_PREFS;
1158 // add the address of all players being ignored
1159 for(idx=0;idx<MAX_PLAYERS;idx++){
1160 if(!(pref_flags & (1<<idx))){
1164 // add the player's id
1165 ADD_SHORT(Net_players[idx].player_id);
1168 // add final stop byte
1173 multi_io_send_reliable(Net_player, data, packet_size);
1177 // set the default voice quality and duration (if server passes -1, he just broadcasts the qos to all clients)
1178 void multi_voice_set_vars(int qos,int duration)
1180 int need_update = 0;
1182 // make sure its in the right range
1183 if((qos > 0) && (qos <= 10)){
1184 #ifdef MULTI_VOICE_VERBOSE
1185 nprintf(("Network","MULTI VOICE : SETTING QOS %d\n",qos));
1188 // set the default value
1189 Multi_voice_qos = qos;
1191 // set the value in the rtvoice module
1192 rtvoice_set_qos(Multi_voice_qos);
1194 // update the netgame settings
1195 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1196 Netgame.options.voice_qos = (ubyte)Multi_voice_qos;
1201 // set the maximum duration
1202 if((duration > 0) && (duration <= MULTI_VOICE_MAX_TIME)){
1203 #ifdef MULTI_VOICE_VERBOSE
1204 nprintf(("Network","MULTI VOICE : SETTING MAX RECORD TIME %d\n",duration));
1206 // set the default value
1207 Multi_voice_max_time = duration;
1209 // update the netgame settings
1210 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1211 Netgame.options.voice_record_time = duration;
1216 // send an options update if necessary
1217 if(need_update && !(Game_mode & GM_STANDALONE_SERVER)){
1218 multi_options_update_netgame();
1222 // <server> process a request for the token
1223 void multi_voice_process_token_request(int player_index)
1227 // if we're not doing voice on this server, return now
1228 if(Netgame.options.flags & MSO_FLAG_NO_VOICE){
1232 // if the player's token timestamp is not -1, can't give him the token
1233 if(Net_players[player_index].s_info.voice_token_timestamp != -1){
1234 #ifdef MULTI_VOICE_VERBOSE
1235 nprintf(("Network","MULTI VOICE : Not giving token because player %s's timestamp hasn't elapsed yet!\n",Net_players[player_index].player->callsign));
1236 nprintf(("Network","MULTI VOICE : token status %d\n",Multi_voice_stream[0].token_status));
1239 multi_voice_deny_token(player_index);
1243 // attempt to find a free token token
1244 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
1245 if(Multi_voice_stream[idx].token_status == MULTI_VOICE_TOKEN_INDEX_FREE){
1246 multi_voice_give_token(idx,player_index);
1252 // free up any memory which may have been malloced
1253 void multi_voice_free_all()
1257 // free up the playback buffer
1258 if(Multi_voice_playback_buffer != NULL){
1259 free(Multi_voice_playback_buffer);
1260 Multi_voice_playback_buffer = NULL;
1263 // free up the accum buffers
1264 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
1265 for(s_idx=0;s_idx<MULTI_VOICE_ACCUM_BUFFER_COUNT;s_idx++){
1266 if(Multi_voice_stream[idx].accum_buffer[s_idx] != NULL){
1267 free(Multi_voice_stream[idx].accum_buffer[s_idx]);
1268 Multi_voice_stream[idx].accum_buffer[s_idx] = NULL;
1274 // <player> send the currently recorded sound
1275 void multi_voice_player_send_stream()
1277 ubyte data[MAX_PACKET_SIZE],code,*rbuf,msg_mode,chunk_index;
1278 ushort chunk_size,uc_size;
1279 int packet_size = 0;
1280 int sound_size,size_sent,uncompressed_size,target_index,max_chunk_size;
1284 // we'd better not ever get here as we can't record voice
1285 SDL_assert(Multi_voice_can_record);
1288 rtvoice_get_data((unsigned char**)&Multi_voice_record_buffer,&sound_size,&uncompressed_size,&d_gain);
1289 gain = (float)d_gain;
1291 msg_mode = (ubyte)Multi_voice_send_mode;
1292 // get the specific target if we're in MSG_TARGET mode
1294 if(msg_mode == MULTI_MSG_TARGET){
1295 if(Player_ai->target_objnum != -1){
1296 target_index = multi_find_player_by_object(&Objects[Player_ai->target_objnum]);
1297 if(target_index == -1){
1305 // get the max chunk size
1306 max_chunk_size = multi_voice_max_chunk_size(Multi_voice_send_mode);
1308 // go through the data and send all of it
1309 code = MV_CODE_DATA;
1312 rbuf = (unsigned char*)Multi_voice_record_buffer;
1313 while(size_sent < sound_size){
1314 // build the header and add the opcode
1315 BUILD_HEADER(VOICE_PACKET);
1317 // add the packet code type
1320 // add the routing data and any necessary targeting information
1322 if(msg_mode == MULTI_MSG_TARGET){
1323 ADD_USHORT(Objects[Net_players[target_index].player->objnum].net_signature);
1327 ADD_SHORT(Net_player->player_id);
1329 // add the current stream id#
1330 ADD_DATA(Multi_voice_stream_id);
1332 SDL_assert(uncompressed_size < MULTI_VOICE_MAX_BUFFER_SIZE);
1333 uc_size = (ushort)uncompressed_size;
1334 ADD_USHORT(uc_size);
1336 // add the chunk index
1337 ADD_DATA(chunk_index);
1339 // determine how much we are going to send in this packet
1340 if((sound_size - size_sent) >= max_chunk_size){
1341 chunk_size = (ushort)max_chunk_size;
1343 chunk_size = (ushort)(sound_size - size_sent);
1345 ADD_USHORT(chunk_size);
1350 // add the chunk of data
1351 memcpy(data+packet_size, rbuf,chunk_size);
1352 packet_size += chunk_size;
1354 // send to the server or rebroadcast if I _am_ the server
1355 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1356 multi_voice_route_data(data,packet_size,MY_NET_PLAYER_NUM,(int)msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
1358 multi_io_send(Net_player, data, packet_size);
1361 // increment the chunk_index
1364 // increment bytes sent and the buffer
1365 size_sent += (int)chunk_size;
1370 // process incoming sound data, return bytes processed
1371 int multi_voice_process_data(ubyte *data, int player_index,int msg_mode,net_player *target)
1373 ubyte stream_id,chunk_index;
1374 ushort chunk_size,uc_size;
1380 // read in all packet data except for the sound chunk itself
1381 GET_SHORT(who_from);
1382 GET_DATA(stream_id);
1383 GET_USHORT(uc_size);
1384 GET_DATA(chunk_index);
1385 GET_USHORT(chunk_size);
1388 // if our netgame options are currently set for no voice, ignore the packet
1389 if((Netgame.options.flags & MSO_FLAG_NO_VOICE) || !Multi_options_g.std_voice){
1390 offset += chunk_size;
1394 // get a handle to a valid stream to be using, freeing old streams as necessary
1395 stream_index = multi_voice_get_stream((int)stream_id);
1397 // if this index is too high, flush the stream
1398 if(chunk_index >= MULTI_VOICE_ACCUM_BUFFER_COUNT){
1399 #ifdef MULTI_VOICE_VERBOSE
1400 nprintf(("Network","MULTI VOICE : flushing stream because packet index is too high!!\n"));
1404 multi_voice_flush_old_stream(stream_index);
1406 // return bytes processed
1407 offset += chunk_size;
1411 // if we found a stream to work with
1412 if(stream_index != -1){
1413 // set the id of where it came from
1414 Multi_voice_stream[stream_index].stream_from = who_from;
1416 // set the stream id#
1417 Multi_voice_stream[stream_index].stream_id = stream_id;
1420 Multi_voice_stream[stream_index].accum_buffer_gain[chunk_index] = (double)gain;
1422 // set the stream uncompressed size size
1423 Multi_voice_stream[stream_index].accum_buffer_usize[chunk_index] = uc_size;
1425 // set the token timestamp
1426 Multi_voice_stream[stream_index].token_stamp = timestamp(MULTI_VOICE_TOKEN_TIMEOUT);
1428 // set the last heard time
1429 Multi_voice_stream[stream_index].stream_last_heard = timer_get_fixed_seconds();
1431 // copy the data and setup any other accum buffer data necessary
1432 // ignore data if we can't play sounds
1433 if(Multi_voice_can_play){
1434 memcpy(Multi_voice_stream[stream_index].accum_buffer[chunk_index],data+offset,(int)chunk_size);
1437 Multi_voice_stream[stream_index].accum_buffer_flags[chunk_index] = 1;
1438 Multi_voice_stream[stream_index].accum_buffer_csize[chunk_index] = chunk_size;
1440 // pass the data into the smart voice algorithm
1441 if(player_index != -1){
1442 multi_voice_alg_process_data(player_index,stream_index,chunk_index,chunk_size);
1446 // increment the offset
1447 offset += (int)chunk_size;
1452 // <server> increment the current stream id#
1453 void multi_voice_inc_stream_id()
1455 SDL_assert(Net_player->flags & NETINFO_FLAG_AM_MASTER);
1457 if(Multi_voice_next_stream_id == 0xff){
1458 Multi_voice_next_stream_id = 0;
1460 Multi_voice_next_stream_id++;
1464 // flush any old sound stream data because we've started to receive data for a new stream
1465 void multi_voice_flush_old_stream(int stream_index)
1467 #ifdef MULTI_VOICE_VERBOSE
1468 nprintf(("Network","MULTI VOICE : old stream flush\n"));
1471 // call the smart algorithm for flushing streams
1472 multi_voice_alg_flush_old_stream(stream_index);
1474 // clear all the accum buffer flags
1475 memset(Multi_voice_stream[stream_index].accum_buffer_flags,0,MULTI_VOICE_ACCUM_BUFFER_COUNT);
1478 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1479 multi_voice_take_token(stream_index);
1482 Multi_voice_stream[stream_index].token_stamp = -1;
1483 Multi_voice_stream[stream_index].token_status = MULTI_VOICE_TOKEN_INDEX_FREE;
1485 // timestamp the player
1486 Net_player->s_info.voice_token_timestamp = timestamp(Netgame.options.voice_token_wait);
1489 // route sound data through the server to all appropriate players
1490 void multi_voice_route_data(ubyte *data, int packet_size,int player_index,int mode,net_player *target)
1494 // route the data to all other players
1497 for(idx=0;idx<MAX_PLAYERS;idx++){
1498 if(MULTI_CONNECTED( Net_players[idx] ) && // player is connected
1499 ( &Net_players[idx] != &Net_players[player_index] ) && // not the sending player
1500 ( Net_player != &Net_players[idx] ) && // not me
1501 ( Multi_voice_player_prefs[idx] & (1 << player_index) ) && // is accepting sound from this player
1502 !( Net_players[idx].p_info.options.flags & MLO_FLAG_NO_VOICE ) ){ // is accepting sound periods
1504 multi_io_send(&Net_players[idx], data, packet_size);
1509 case MULTI_MSG_FRIENDLY:
1510 for(idx=0;idx<MAX_PLAYERS;idx++){
1511 if(MULTI_CONNECTED( Net_players[idx] ) && // player is connected
1512 ( &Net_players[idx] != &Net_players[player_index] ) && // not the sending player
1513 ( Net_player != &Net_players[idx] ) && // not me
1514 ( Net_players[idx].p_info.team == Net_players[player_index].p_info.team ) &&// on the same team
1515 ( Multi_voice_player_prefs[idx] & (1 << player_index) ) && // is accepting sound from the sender
1516 !( Net_players[idx].p_info.options.flags & MLO_FLAG_NO_VOICE) ){ // is accepting sound periods
1518 multi_io_send(&Net_players[idx], data, packet_size);
1522 case MULTI_MSG_HOSTILE:
1523 for(idx=0;idx<MAX_PLAYERS;idx++){
1524 if(MULTI_CONNECTED( Net_players[idx] ) && // player is connected
1525 ( &Net_players[idx] != &Net_players[player_index] ) && // not the sending player
1526 ( Net_player != &Net_players[idx] ) && // not me
1527 ( Net_players[idx].p_info.team != Net_players[player_index].p_info.team ) &&// on the opposite team
1528 ( Multi_voice_player_prefs[idx] & (1 << player_index) ) && // is accepting sound from the sender
1529 !( Net_players[idx].p_info.options.flags & MLO_FLAG_NO_VOICE ) ){ // is accepting sound periods
1531 multi_io_send(&Net_players[idx], data, packet_size);
1536 case MULTI_MSG_TARGET:
1537 SDL_assert(target != NULL);
1538 if(!(target->p_info.options.flags & MLO_FLAG_NO_VOICE)){
1539 multi_io_send(target, data, packet_size);
1545 // find the stream to apply incoming sound data to, freeing up old ones as necessary
1546 int multi_voice_get_stream(int stream_id)
1548 int idx,max_diff_index;
1549 fix cur_time,max_diff;
1551 // first check to see if this stream exists
1552 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
1553 if(Multi_voice_stream[idx].stream_id == (ubyte)stream_id){
1558 // if we got to this point, we didn't find the matching stream, so we should try and find an empty stream
1559 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
1560 if(Multi_voice_stream[idx].token_stamp == -1){
1565 #ifdef MULTI_VOICE_VERBOSE
1566 nprintf(("Network","MULTI VOICE : going to blast old voice stream while looking for a free one - beware!!\n"));
1569 // if we got to this point, we should free up the oldest stream we have
1570 cur_time = timer_get_fixed_seconds();
1571 max_diff_index = -1;
1573 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
1574 if(((max_diff_index == -1) || ((cur_time - Multi_voice_stream[idx].stream_last_heard) > max_diff)) && (Multi_voice_stream[idx].token_stamp != -1)){
1575 max_diff_index = idx;
1576 max_diff = cur_time - Multi_voice_stream[idx].stream_last_heard;
1580 // if we found the oldest
1581 if(max_diff_index != -1){
1582 // flush the old stream
1583 multi_voice_flush_old_stream(max_diff_index);
1585 return max_diff_index;
1588 // some other fail condition
1592 // is the given sound stream playing (compares uncompressed sound size with current playback position)
1593 int multi_voice_stream_playing(int stream_index)
1595 // if the handle is invalid, it can't be playing
1597 if(Multi_voice_stream[stream_index].stream_snd_handle < 0){
1601 // if the sound is playing and the buffer is past the uncompressed size, its effectively done
1602 if(ds_get_play_position(ds_get_channel(Multi_voice_stream[stream_index].stream_snd_handle)) >= (DWORD)Multi_voice_stream[stream_index].stream_uc_size){
1611 // tack on pre and post sounds to a sound stream (pass -1 for either if no sound is wanted)
1612 // return final buffer size
1613 int multi_voice_mix(int post_sound,char *data,int cur_size,int max_size)
1617 // if the user passed -1 for both pre and post sounds, don't do a thing
1618 if(post_sound == -1){
1622 // get the sizes of the additional sounds
1625 if(post_sound >= 0){
1626 post_sound = snd_load(&Snds[post_sound]);
1627 if(post_sound >= 0){
1628 if(snd_size(post_sound,&post_size) == -1){
1638 // if we have a "post" sound to add
1640 if((max_size - cur_size) > post_size){
1641 // copy in the sound
1642 if ( !snd_get_data(post_sound,data + cur_size) ) {
1643 // increment the cur_size
1644 cur_size += post_size;
1649 // return the size of the new buffer
1653 // max size of a sound chunk which we can fit into a packet
1654 int multi_voice_max_chunk_size(int msg_mode)
1658 // all headers contain the following data
1659 header_size = 1 + // messaging mode
1661 2 + // packet uncompressed size
1662 2 + // compressed size
1665 // if we're targeting a specific player
1666 if(msg_mode == MULTI_MSG_TARGET){
1667 header_size += 2; // targeted player's object net_signature
1670 header_size += 4; // my address (4 bytes in TCP)
1672 // calculate max chunk size
1673 return (MAX_PACKET_SIZE - // max freespace packet size
1675 1 - // voice packet code subtype
1676 header_size); // calculated header size
1679 // --------------------------------------------------------------------------------------------------
1680 // MULTI VOICE / RTVOICE INTERFACE
1683 // process the "next" chunk of standalone valid sound data from the rtvoice system
1684 void multi_voice_process_next_chunk()
1686 int sound_size,uncompressed_size;
1690 // we'd better not ever get here is we can't record voice
1691 SDL_assert(Multi_voice_can_record);
1694 rtvoice_get_data((unsigned char**)&Multi_voice_record_buffer,&sound_size,&uncompressed_size,&d_gain);
1696 // if we've reached the max # of packets for this stream, bail
1697 if(Multi_voice_current_stream_index >= (MULTI_VOICE_ACCUM_BUFFER_COUNT - 1)){
1698 nprintf(("Network","MULTI VOICE : Forcing stream to stop on the record size!!!\n"));
1700 // mark me as no longer recording
1701 Multi_voice_recording = 0;
1703 Multi_voice_current_stream_sent = -1;
1705 // stop the recording process
1706 rtvoice_stop_recording();
1708 #ifdef MULTI_VOICE_POST_DECOMPRESS
1709 multi_voice_player_send_stream();
1712 // play my sound locally as well
1713 #ifdef MULTI_VOICE_LOCAL_ECHO
1714 multi_voice_alg_play_window(0);
1716 // release the token back to the server
1717 multi_voice_release_token();
1719 // unset the timestamp so we don't still think we're still recording
1720 Multi_voice_recording_stamp = -1;
1725 // pack the data locally as well (so I can hear myself)
1726 str = &Multi_voice_stream[0];
1727 memcpy(str->accum_buffer[Multi_voice_current_stream_index],Multi_voice_record_buffer,sound_size);
1728 str->stream_from = Net_player->player_id;
1729 str->accum_buffer_flags[Multi_voice_current_stream_index] = 1;
1730 str->accum_buffer_usize[Multi_voice_current_stream_index] = (ushort)uncompressed_size;
1731 str->accum_buffer_csize[Multi_voice_current_stream_index] = (ushort)sound_size;
1732 str->accum_buffer_gain[Multi_voice_current_stream_index] = d_gain;
1734 // increment the stream index
1735 Multi_voice_current_stream_index++;
1739 // --------------------------------------------------------------------------------------------------
1740 // MULTI VOICE PACKET HANDLERS
1743 // send a dummy packet in the place of a too-large data packet
1744 void multi_voice_send_dummy_packet()
1746 ubyte data[10],code,msg_mode;
1747 int packet_size,target_index;
1749 // build the header and add the opcode
1750 BUILD_HEADER(VOICE_PACKET);
1751 code = (ubyte)MV_CODE_DATA_DUMMY;
1754 msg_mode = (ubyte)Multi_voice_send_mode;
1755 // get the specific target if we're in MSG_TARGET mode
1757 if(msg_mode == MULTI_MSG_TARGET){
1758 if(Player_ai->target_objnum != -1){
1759 target_index = multi_find_player_by_object(&Objects[Player_ai->target_objnum]);
1760 if(target_index == -1){
1768 if(msg_mode == MULTI_MSG_TARGET){
1769 ADD_USHORT(Objects[Net_players[target_index].player->objnum].net_signature);
1772 // add the voice stream id
1773 ADD_DATA(Multi_voice_stream_id);
1775 // send to the server or rebroadcast if I _am_ the server
1776 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1777 multi_voice_route_data(data,packet_size,MY_NET_PLAYER_NUM,(int)msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
1779 multi_io_send(Net_player, data, packet_size);
1783 // process a dummy data packet
1784 int multi_voice_process_data_dummy(ubyte *data)
1790 // get the stream id
1791 GET_DATA(stream_id);
1793 // get the proper stream index
1794 stream_index = multi_voice_get_stream((int)stream_id);
1796 // set the token timestamp
1797 Multi_voice_stream[stream_index].token_stamp = timestamp(MULTI_VOICE_TOKEN_TIMEOUT);
1799 // set the last heard time
1800 Multi_voice_stream[stream_index].stream_last_heard = timer_get_fixed_seconds();
1802 // set the timeout timestamp
1803 Multi_voice_stamps[stream_index] = timestamp(MV_ALG_TIMEOUT);
1805 // return bytes processed
1809 // process a player preferences packet, return bytes processed
1810 int multi_voice_process_player_prefs(ubyte *data,int player_index)
1817 // set all channels active
1818 Multi_voice_player_prefs[player_index] = 0xffffffff;
1820 // get all muted players
1825 // get the player to mute
1826 mute_index = find_player_id(mute_id);
1827 if(mute_index != -1){
1828 #ifdef MULTI_VOICE_VERBOSE
1829 nprintf(("Network","Player %s muting player %s\n",Net_players[player_index].player->callsign,Net_players[mute_index].player->callsign));
1832 Multi_voice_player_prefs[player_index] &= ~(1<<mute_index);
1835 // get the next stop value
1839 // return bytes processed
1843 // process an incoming voice packet of some kind or another
1844 void multi_voice_process_packet(ubyte *data, header *hinfo)
1846 ubyte code,msg_mode;
1848 int player_index,stream_index,target_index;
1849 int offset = HEADER_LENGTH;
1851 // find out who is sending this data
1852 player_index = find_player_id(hinfo->id);
1857 // process the packet
1859 // I don't have the token anymore
1860 case MV_CODE_TAKE_TOKEN:
1861 // we should never have the token if we cannot record
1862 if(!Multi_voice_can_record){
1866 Multi_voice_token = 0;
1869 // I have been denied the token
1870 case MV_CODE_DENY_TOKEN:
1871 // set the "denied" timestamp
1872 Multi_voice_denied_stamp = timestamp(MULTI_VOICE_DENIED_TIME);
1875 // I now have the token
1876 case MV_CODE_GIVE_TOKEN:
1877 GET_DATA(Multi_voice_stream_id);
1879 // we should never get the token if we cannot record
1880 if(!Multi_voice_can_record){
1884 // if we no longer have the keydown, automatically release the token
1885 if(!Multi_voice_keydown){
1886 multi_voice_release_token();
1888 Multi_voice_token = 1;
1892 // a request for the token from a player
1893 case MV_CODE_REQUEST_TOKEN:
1894 if(player_index >= 0){
1895 multi_voice_process_token_request(player_index);
1899 // a player gave up the token
1900 case MV_CODE_RELEASE_TOKEN:
1901 if(player_index >= 0){
1902 stream_index = multi_voice_find_token(player_index);
1907 if(stream_index >= 0){
1908 // set the token as having been released
1909 Multi_voice_stream[stream_index].token_status = MULTI_VOICE_TOKEN_INDEX_RELEASED;
1911 // timestamp this guy so that he can't get the token back immediately
1912 Net_players[player_index].s_info.voice_token_timestamp = timestamp(Netgame.options.voice_token_wait);
1916 // a player has set prefs for himself
1917 case MV_CODE_PLAYER_PREFS:
1918 SDL_assert(player_index != -1);
1919 offset += multi_voice_process_player_prefs(data+offset,player_index);
1924 #ifdef MULTI_VOICE_VERBOSE
1925 nprintf(("Network","VOICE : PROC DATA\n"));
1927 // get routing information
1930 if(msg_mode == MULTI_MSG_TARGET){
1931 GET_USHORT(target_sig);
1932 target_index = multi_find_player_by_net_signature(target_sig);
1933 SDL_assert(target_index != -1);
1936 offset += multi_voice_process_data(data+offset,player_index,msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
1938 // if we're the server of the game, we should also route this data to all other players
1939 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1940 multi_voice_route_data(data,offset,player_index,msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
1944 // a data dummy packet
1945 case MV_CODE_DATA_DUMMY:
1946 #ifdef MULTI_VOICE_VERBOSE
1947 nprintf(("Network","VOICE : PROC DATA DUMMY\n"));
1949 // get routing information
1952 if(msg_mode == MULTI_MSG_TARGET){
1953 GET_USHORT(target_sig);
1954 target_index = multi_find_player_by_net_signature(target_sig);
1955 SDL_assert(target_index != -1);
1958 offset += multi_voice_process_data_dummy(data+offset);
1960 // if we're the server of the game, we should also route this data to all other players
1961 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
1962 multi_voice_route_data(data,offset,player_index,msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
1969 // send all pending voice packets
1970 void multi_voice_client_send_pending()
1972 ubyte data[MAX_PACKET_SIZE],code;
1973 ubyte msg_mode,chunk_index;
1974 ushort uc_size,chunk_size;
1975 int max_chunk_size,sent,target_index;
1980 // if we're not recording
1981 if(!Multi_voice_recording || (Multi_voice_current_stream_sent < 0) || (Multi_voice_current_stream_sent > Multi_voice_current_stream_index)){
1985 // stream all buffered up packets
1986 str = &Multi_voice_stream[0];
1987 while(Multi_voice_current_stream_sent < Multi_voice_current_stream_index){
1988 sent = Multi_voice_current_stream_sent++;
1990 // get the current messaging mode
1991 msg_mode = (ubyte)Multi_voice_send_mode;
1993 // if the size of this voice chunk will fit in the packet
1994 max_chunk_size = multi_voice_max_chunk_size(Multi_voice_send_mode);
1995 if(str->accum_buffer_csize[sent] > max_chunk_size){
1996 #ifdef MULTI_VOICE_VERBOSE
1997 nprintf(("Network","MULTI VOICE : streamed packet size too large!!\n"));
2000 Multi_voice_current_stream_sent++;
2002 // send a dummy data packet instead
2003 multi_voice_send_dummy_packet();
2008 #ifdef MULTI_VOICE_VERBOSE
2009 nprintf(("Network","MULTI VOICE : PACKET %d %d\n",(int)str->accum_buffer_csize[sent],(int)str->accum_buffer_usize[sent]));
2012 // get the specific target if we're in MSG_TARGET mode
2014 if(msg_mode == MULTI_MSG_TARGET){
2015 if(Player_ai->target_objnum != -1){
2016 target_index = multi_find_player_by_object(&Objects[Player_ai->target_objnum]);
2017 if(target_index == -1){
2025 // go through the data and send all of it
2026 code = MV_CODE_DATA;
2029 // if this packet is small enough to fit within a psnet data packet
2030 BUILD_HEADER(VOICE_PACKET);
2032 // add the packet code type
2035 // add the routing data and any necessary targeting information
2037 if(msg_mode == MULTI_MSG_TARGET){
2038 SDL_assert(Game_mode & GM_IN_MISSION);
2039 ADD_USHORT(Objects[Net_players[target_index].player->objnum].net_signature);
2043 ADD_SHORT(Net_player->player_id);
2045 // add the current stream id#
2046 ADD_DATA(Multi_voice_stream_id);
2048 //SDL_assert(str->accum_buffer_usize[sent] < MULTI_VOICE_MAX_BUFFER_SIZE); // always true
2049 uc_size = (ushort)str->accum_buffer_usize[sent];
2050 ADD_USHORT(uc_size);
2052 // add the chunk index
2053 chunk_index = (ubyte)sent;
2054 ADD_DATA(chunk_index);
2056 // size of the sound data
2057 chunk_size = (ushort)str->accum_buffer_csize[sent];
2058 ADD_USHORT(chunk_size);
2061 gain = (float)str->accum_buffer_gain[sent];
2064 // add the chunk of data
2065 memcpy(data+packet_size, str->accum_buffer[sent],chunk_size);
2066 packet_size += chunk_size;
2068 // send to the server or rebroadcast if I _am_ the server
2069 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
2070 multi_voice_route_data(data,packet_size,MY_NET_PLAYER_NUM,(int)msg_mode,(target_index == -1) ? NULL : &Net_players[target_index]);
2072 multi_io_send(Net_player, data, packet_size);
2078 // --------------------------------------------------------------------------------------------------
2079 // MULTI VOICE ALGORITHM stuff
2082 // process and play the current window of sound stream data we have. reset the window for the next incoming data as well
2083 void multi_voice_alg_play_window(int stream_index)
2085 int idx,buffer_offset;
2088 #ifdef MULTI_VOICE_VERBOSE
2089 nprintf(("Network","MULTI VOICE : PLAYING STREAM %d\n",stream_index));
2092 // get a pointer to the stream
2093 st = &Multi_voice_stream[stream_index];
2095 // don't play anything back if we can't hear sound
2096 if(Multi_voice_can_play){
2097 // first, pack all the accum buffers into the playback buffer
2098 #ifdef MULTI_VOICE_PRE_DECOMPRESS
2099 buffer_offset = Multi_voice_pre_sound_size;
2100 nprintf(("Network","VOICE : pre sound size %d\n",Multi_voice_pre_sound_size));
2101 for(idx=0;idx<MULTI_VOICE_ACCUM_BUFFER_COUNT;idx++){
2102 // if the flag is set, uncompress the data into the playback buffer
2103 if(st->accum_buffer_flags[idx]){
2104 // first, uncompress the data
2105 rtvoice_uncompress(st->accum_buffer[idx],(int)st->accum_buffer_csize[idx],st->accum_buffer_gain[idx],(ubyte*)Multi_voice_playback_buffer+buffer_offset,st->accum_buffer_usize[idx]);
2107 // increment the buffer offset
2108 buffer_offset += st->accum_buffer_usize[idx];
2112 #ifdef MULTI_VOICE_POST_DECOMPRESS
2114 for(idx=0;idx<MULTI_VOICE_ACCUM_BUFFER_COUNT;idx++){
2115 // if the flag is set, copy the data
2116 if(st->accum_buffer_flags[idx]){
2117 memcpy(Multi_voice_unpack_buffer+buffer_offset,st->accum_buffer[idx],st->accum_buffer_csize[idx]);
2118 buffer_offset += st->accum_buffer_csize[idx];
2122 // decompress the whole shebang
2123 rtvoice_uncompress((ubyte*)Multi_voice_unpack_buffer,buffer_offset,st->accum_buffer_gain[0],(ubyte*)Multi_voice_playback_buffer,st->accum_buffer_usize[0]);
2124 buffer_offset = st->accum_buffer_usize[0];
2127 // mix in the SND_CUE_VOICE and the SND_END_VOICE game sounds
2128 buffer_offset = multi_voice_mix(MULTI_VOICE_POST_SOUND,Multi_voice_playback_buffer,buffer_offset,MULTI_VOICE_MAX_BUFFER_SIZE);
2130 SDL_assert(Multi_voice_stream[stream_index].stream_rtvoice_handle != -1);
2132 // kill any previously playing sounds
2133 rtvoice_stop_playback(Multi_voice_stream[stream_index].stream_rtvoice_handle);
2134 Multi_voice_stream[stream_index].stream_snd_handle = -1;
2136 // if we can play sound and we know who this is from, display it
2137 if(Multi_voice_can_play){
2138 char voice_msg[256];
2139 int player_index = find_player_id(Multi_voice_stream[stream_index].stream_from);
2141 if(player_index != -1){
2142 SDL_snprintf(voice_msg, SDL_arraysize(voice_msg), XSTR("<%s is speaking>", 712), Net_players[player_index].player->callsign);
2144 // display a chat message (write to the correct spot - hud, standalone gui, chatbox, etc)
2145 multi_display_chat_msg(voice_msg,player_index,0);
2149 // now call the rtvoice playback functions
2150 Multi_voice_stream[stream_index].stream_snd_handle = rtvoice_play_uncompressed(Multi_voice_stream[stream_index].stream_rtvoice_handle,(unsigned char*)Multi_voice_playback_buffer,buffer_offset);
2151 Multi_voice_stream[stream_index].stream_start_time = timer_get_fixed_seconds();
2154 // unset the stamp so that its not "free"
2155 Multi_voice_stamps[stream_index] = -1;
2157 // flush the stream (will also grab the token back, if the server)
2158 multi_voice_flush_old_stream(stream_index);
2161 // decision function which decides if we should play the current block of sound we have
2162 int multi_voice_alg_should_play(int stream_index)
2164 // if the timestamp has expired, play the sound
2165 if((Multi_voice_stamps[stream_index] != -1) && timestamp_elapsed(Multi_voice_stamps[stream_index])){
2166 #ifdef MULTI_VOICE_VERBOSE
2167 nprintf(("Network","MULTI VOICE : DECIDE, TIMEOUT\n"));
2175 // process incoming sound data in whatever way necessary (this function should take care of playing data when necessary)
2176 void multi_voice_alg_process_data(int /*player_index*/, int stream_index, ushort /*chunk_index*/, ushort /*chunk_size*/)
2178 // update the timestamp for this window
2179 Multi_voice_stamps[stream_index] = timestamp(MV_ALG_TIMEOUT);
2182 // process existing streams
2183 void multi_voice_alg_process_streams()
2188 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
2189 // determine if we should play this window of data
2190 if((Multi_voice_stamps[idx] != -1) && multi_voice_alg_should_play(idx)){
2191 // determine who this stream came from
2192 player_index = find_player_id(Multi_voice_stream[idx].stream_from);
2194 // server should check his own settings here
2195 if((Net_player->flags & NETINFO_FLAG_AM_MASTER) && ((Net_player->p_info.options.flags & MLO_FLAG_NO_VOICE) || (player_index == -1) || !(Multi_voice_player_prefs[MY_NET_PLAYER_NUM] & (1<<player_index))) ){
2196 // unset the stamp so that its not "free"
2197 Multi_voice_stamps[idx] = -1;
2199 // flush the stream (will also grab the token back, if the server)
2200 multi_voice_flush_old_stream(idx);
2202 nprintf(("Network","Server not playing sound because of set options!\n"));
2204 // play the current sound
2206 multi_voice_alg_play_window(idx);
2212 // we are going to flush the current stream because we have started to receive data for a new one. do something first
2213 void multi_voice_alg_flush_old_stream(int stream_index)
2215 // just unset the heard from timestamp for now
2216 Multi_voice_stamps[stream_index] = -1;
2219 // initialize the smart algorithm
2220 void multi_voice_alg_init()
2224 for(idx=0;idx<MULTI_VOICE_MAX_STREAMS;idx++){
2225 Multi_voice_stamps[idx] = -1;
2230 // --------------------------------------------------------------------------------------------------
2231 // MULTI VOICE TESTING FUNCTIONS
2234 #define MV_TEST_RECORD_TIME 3000 // recording time in ms for testing voice
2235 int Multi_voice_test_record_stamp = -1;
2236 int Multi_voice_test_packet_tossed = 0;
2238 // process the next chunk of voice data
2239 void multi_voice_test_process_next_chunk()
2241 unsigned char *outbuf,*outbuf_raw;
2242 int compressed_size,uncompressed_size,raw_size;
2245 // if the test recording stamp is -1, we should stop
2246 if(Multi_voice_test_record_stamp == -1){
2247 rtvoice_stop_recording();
2251 // if the recording timestamp has elapsed, stop the whole thing
2252 if(timestamp_elapsed(Multi_voice_test_record_stamp)){
2253 nprintf(("Network","Stopping voice test recording\n"));
2255 rtvoice_stop_recording();
2257 Multi_voice_test_record_stamp = -1;
2258 Multi_voice_test_packet_tossed = 0;
2262 // otherwise get the compressed and uncompressed data and do something interesting with it
2263 rtvoice_get_data(&outbuf,&compressed_size,&uncompressed_size,&gain,&outbuf_raw,&raw_size);
2265 // determine whether the packet would have been dropped
2266 if(compressed_size > multi_voice_max_chunk_size(MULTI_MSG_ALL)){
2267 Multi_voice_test_packet_tossed = 1;
2269 Multi_voice_test_packet_tossed = 0;
2272 // send the raw output buffer to the voice options screen
2273 options_multi_set_voice_data(outbuf_raw,raw_size,outbuf,compressed_size,uncompressed_size,gain);
2276 // start recording voice locally for playback testing
2277 void multi_voice_test_record_start()
2279 // if there is test recording going on already, don't do anything
2280 if(Multi_voice_test_record_stamp != -1){
2284 // stop any playback which may be occuring
2285 rtvoice_stop_playback_all();
2287 // stop any recording which may be occuring
2288 rtvoice_stop_recording();
2290 // set the timestamp
2291 Multi_voice_test_record_stamp = timestamp(MV_TEST_RECORD_TIME);
2293 // start the recording of voice
2294 rtvoice_start_recording(multi_voice_test_process_next_chunk);
2297 // force stop any recording voice test
2298 void multi_voice_test_record_stop()
2300 Multi_voice_test_record_stamp = -1;
2301 Multi_voice_test_packet_tossed = 0;
2302 rtvoice_stop_recording();
2305 // return if the test recording is going on
2306 int multi_voice_test_recording()
2308 return (Multi_voice_test_record_stamp == -1) ? 0 : 1;
2311 // call this function if multi_voice_test_recording() is true to process various odds and ends of the test recording
2312 void multi_voice_test_process()
2314 // if we're not recording, do nothing
2315 if(Multi_voice_test_record_stamp == -1){
2319 // check to see if the timestamp has elapsed
2320 if(timestamp_elapsed(Multi_voice_test_record_stamp)){
2321 Multi_voice_test_record_stamp = -1;
2322 Multi_voice_test_packet_tossed = 0;
2326 // get a playback buffer handle (return -1 if none exist - bad)
2327 int multi_voice_test_get_playback_buffer()
2329 // return voice stream 0
2330 SDL_assert(Multi_voice_stream[0].stream_snd_handle == -1);
2331 SDL_assert(Multi_voice_stream[0].stream_rtvoice_handle != -1);
2333 return Multi_voice_stream[0].stream_rtvoice_handle;
2336 // return whether the last sampled chunk would have been too large to fit in a packet
2337 int multi_voice_test_packet_tossed()
2339 return Multi_voice_test_packet_tossed;