Ignore:
Timestamp:
12/23/06 17:07:21 (17 years ago)
Author:
r2d
Message:
  • removed file info bug
  • improved thread reactivity
File:
1 edited

Legend:

Unmodified
Added
Removed
  • winamp-musepack/trunk/winamp-musepack/mpc_player.cpp

    r186 r188  
    99mpc_player::mpc_player(In_Module * in_mod)
    1010{
     11        init(in_mod);
     12}
     13
     14mpc_player::mpc_player(char * fn, In_Module * in_mod)
     15{
     16        init(in_mod);
     17        openFile(fn);
     18}
     19
     20mpc_player::~mpc_player(void)
     21{
     22        closeFile();
     23}
     24
     25void mpc_player::init(In_Module * in_mod)
     26{
    1127        thread_handle=INVALID_HANDLE_VALUE;
    1228        killDecodeThread=0;
     
    1430        demux = 0;
    1531        mod = in_mod;
    16 }
    17 
    18 mpc_player::~mpc_player(void)
     32        wait_event = 0;
     33}
     34
     35int mpc_player::openFile(char * fn)
    1936{
    2037        closeFile();
    21 }
    22 
    23 int mpc_player::openFile(char * fn)
    24 {
    25         mpc_status err;
    26 
    27     err = mpc_reader_init_stdio(&reader, fn);
     38
     39    mpc_status err = mpc_reader_init_stdio(&reader, fn);
    2840    if(err < 0) return 1;
    2941
     
    3547
    3648    mpc_demux_get_info(demux,  &si);
     49        strcpy(lastfn, fn);
    3750        return 0;
    3851}
     
    4558                mpc_reader_exit_stdio(&reader);
    4659        }
     60}
     61
     62void mpc_player::setOutputTime(int time_in_ms)
     63{
     64        seek_offset = time_in_ms;
     65        if (wait_event) SetEvent(wait_event);
    4766}
    4867
     
    7392{
    7493        int done = 0;
     94
     95        wait_event = CreateEvent(0, FALSE, FALSE, 0);
    7596
    7697        while (!killDecodeThread)
     
    92113                                break;
    93114                        }
    94                         Sleep(10);              // give a little CPU time back to the system.
     115                        WaitForSingleObject(wait_event, 100);           // give a little CPU time back to the system.
    95116                } else if (mod->outMod->CanWrite() >= (MPC_FRAME_LENGTH * sizeof(short) * si.channels)*(mod->dsp_isactive()?2:1)) {
    96117                        // CanWrite() returns the number of bytes you can write, so we check that
     
    105126
    106127                        if(frame.bits == -1) {
    107                                 done=1;
     128                                done = 1;
    108129                        } else {
    109130                                short output_buffer[MPC_FRAME_LENGTH * 2]; // default 2 channels
     
    124145                                mod->outMod->Write((char*)output_buffer, frame.samples * sizeof(short) * si.channels);
    125146                        }
    126                 } else Sleep(20);
    127         }
     147                } else WaitForSingleObject(wait_event, 1000);
     148        }
     149
     150        CloseHandle(wait_event);
     151        wait_event = 0;
     152
    128153        return 0;
    129154}
    130155
    131 void mpc_player::getFileInfo(char *filename, char *title, int *length_in_ms)
    132 {
    133         if (!filename || !*filename) {
    134                 if (length_in_ms) *length_in_ms = getLength();
    135                 if (title) {
    136                         char *p = lastfn + strlen(lastfn);
    137                         while (*p != '\\' && p >= lastfn) p--;
    138                         strcpy(title,++p);
    139                 }
    140         } else {
    141                 if (length_in_ms) {
    142                         if (openFile(filename) == 0) {
    143                                 closeFile();
    144                                 *length_in_ms = getLength();
    145                         } else {
    146                                 *length_in_ms = -1000; // the default is unknown file length (-1000).
    147                         }
    148                 }
    149                 if (title) {
    150                         char *p = filename + strlen(filename);
    151                         while (*p != '\\' && p >= filename) p--;
    152                         strcpy(title,++p);
    153                 }
     156void mpc_player::getFileInfo(char *title, int *length_in_ms)
     157{
     158        if (length_in_ms) *length_in_ms = getLength();
     159        if (title) {
     160                char *p = lastfn + strlen(lastfn);
     161                while (*p != '\\' && p >= lastfn) p--;
     162                strcpy(title,++p);
    154163        }
    155164}
     
    158167void mpc_player::stop(void)
    159168{
    160         if (thread_handle != INVALID_HANDLE_VALUE)
    161         {
    162                 killDecodeThread=1;
    163                 if (WaitForSingleObject(thread_handle,10000) == WAIT_TIMEOUT)
    164                 {
     169        if (thread_handle != INVALID_HANDLE_VALUE) {
     170                killDecodeThread = 1;
     171                if (wait_event) SetEvent(wait_event);
     172                if (WaitForSingleObject(thread_handle,10000) == WAIT_TIMEOUT) {
    165173                        MessageBox(mod->hMainWindow,"error asking thread to die!\n",
    166                                 "error killing decode thread",0);
    167                         TerminateThread(thread_handle,0);
     174                                "error killing decode thread", 0);
     175                        TerminateThread(thread_handle, 0);
    168176                }
    169177                CloseHandle(thread_handle);
     
    189197
    190198        if (openFile(fn) != 0) return 1;
    191        
    192         strcpy(lastfn,fn);
    193199
    194200        // -1 and -1 are to specify buffer and prebuffer lengths.
Note: See TracChangeset for help on using the changeset viewer.