home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////
- // Most of the code in this file was simply copied from the play
- // program from the University of Minnesota Gopher client.
-
- #import "Sound.h"
-
- #define SOUNDBUFSIZE 1400 /* A pretty good value for ethernet */
-
- // This is a flag indicating whether we are currently playing a sound.
- mutex_t soundIsPlaying;
-
- // This is a flag indicating that a currently playing sound should stop asap.
- mutex_t soundShouldStop;
- int soundShouldStopFlag;
-
- // Make it global so it can be freed by other functions.
- static ThreadArgument *threadArgument;
-
- static int currentsocket;
-
- // Put this here to avoid "warning: Objective-C text in `.c' file".
- extern int NXRunAlertPanel(const char *title, const char *msg, const char *defaultButton, const char *alternateButton, const char *otherButton, ...);
-
-
- ////////////////////////////////////////////////////////////
- // This function is thread-forked. It must free the argument
- // before leaving.
-
- int PlaySound(ThreadArgument *theArgument)
- {
- FILE *Play;
- int j;
- char buf[SOUNDBUFSIZE];
-
- threadArgument = theArgument;
-
- // Check that we're the only thread.
- if (mutex_try_lock(soundIsPlaying) == 0)
- {
- fprintf(stderr, "Error : too many threads.\n");
- free(theArgument);
- close(currentsocket);
- cthread_exit(0);
- }
-
-
- currentsocket = create_socket(theArgument->hostname, theArgument->portnumber);
- if ( currentsocket < 0)
- {
- switch (currentsocket)
- {
- case -1 :
- fprintf(stderr, "Unknown host\n");
- break;
- case -2 :
- fprintf(stderr, "Socket call failed\n");
- break;
- case -3 :
- fprintf(stderr, "Connect call failed\n");
- break;
- default :
- fprintf(stderr, "Unknown network error\n");
- break;
- }
- free(theArgument);
- close(currentsocket);
- mutex_unlock(soundIsPlaying);
- cthread_exit(0);
- }
-
- writestring(currentsocket, theArgument->filename);
- writestring(currentsocket, "\r\n");
-
- Play = popen(PlayCommand, "w");
- while(1)
- {
- j = read(currentsocket, buf, SOUNDBUFSIZE);
- if (j == 0)
- break;
- fwrite(buf, 1, j, Play);
- if (mutex_try_lock(soundShouldStop) != 0)
- if (soundShouldStopFlag)
- {
- soundShouldStopFlag = 0;
- mutex_unlock(soundShouldStop);
- break;
- }
- else
- mutex_unlock(soundShouldStop);
- }
-
- free(theArgument);
- close(currentsocket);
- fclose(Play);
- #ifdef DEBUG
- fprintf(stderr, "SoundPlay exiting.\n");
- #endif /* DEBUG */
-
- sleep(2); // Wait for play to exit
- mutex_unlock(soundIsPlaying);
- cthread_exit(0);
-
- return 0;
- }