Ticket #5093: game_kit_sound.patch

File game_kit_sound.patch, 8.2 KB (added by bmkx, 14 years ago)

Proposal of some more fixes

  • src/kits/game/GameSoundDevice.cpp

     
    180180
    181181status_t
    182182BGameSoundDevice::CreateBuffer(gs_id* sound, const void* object,
    183     const gs_audio_format* format)
     183    const gs_audio_format* format, size_t inBufferFrameCount,
     184    size_t inBufferCount)
    184185{
    185186    if (!object || !sound)
    186187        return B_BAD_VALUE;
     
    189190    int32 position = AllocateSound();
    190191
    191192    if (position >= 0) {
    192         fSounds[position] = new StreamingSoundBuffer(format, object);
     193        fSounds[position] = new StreamingSoundBuffer(format, object,
     194            inBufferFrameCount, inBufferCount);
    193195        err = fSounds[position]->Connect(&fConnection->producer);
    194196    }
    195197
  • src/kits/game/GameSoundDevice.h

     
    5151                                                 int64 frames);
    5252    virtual status_t                CreateBuffer(gs_id * sound,
    5353                                                 const void * object,
    54                                                  const gs_audio_format * format);
     54                                                 const gs_audio_format * format,
     55                                                 size_t inBufferFrameCount = 0,
     56                                                 size_t inBufferCount = 0);
    5557    virtual void                    ReleaseBuffer(gs_id sound);
    5658   
    5759    virtual status_t                Buffer(gs_id sound,
     
    7375            void                    SetInitError(status_t error);
    7476               
    7577            gs_audio_format         fFormat;                                           
     78
    7679private:
    7780            status_t                Connect();
    7881            int32                   AllocateSound();
  • src/kits/game/GameSoundBuffer.cpp

     
    3232#include <MediaAddOn.h>
    3333#include <MediaTheme.h>
    3434#include <TimeSource.h>
     35#include <BufferGroup.h>
    3536
    3637#include "GameProducer.h"
    3738#include "GameSoundBuffer.h"
     
    270271    pan[1] = fPanLeft;
    271272   
    272273    char * buffer = new char[fFrameSize * frames];
    273            
     274
    274275    FillBuffer(buffer, frames);
    275276   
    276277    switch (fFormat.format) {
     
    531532
    532533// StreamingSoundBuffer ------------------------------------------------------
    533534StreamingSoundBuffer::StreamingSoundBuffer(const gs_audio_format * format,
    534                         const void * streamHook)
     535    const void * streamHook, size_t inBufferFrameCount,
     536    size_t inBufferCount)
    535537    :
    536538    GameSoundBuffer(format),
    537539    fStreamHook(const_cast<void *>(streamHook))
    538540{
     541    if (inBufferFrameCount != 0 && inBufferCount  != 0) {
     542        BBufferGroup *bufferGroup = new BBufferGroup(inBufferFrameCount
     543                * fFrameSize, inBufferCount);
     544        fNode->SetBufferGroup(fConnection->source, bufferGroup);
     545    }
    539546}
    540547
    541548
     
    552559    size_t bytes = fFrameSize * frames;
    553560    object->FillBuffer(buffer, bytes);
    554561}
    555 
  • src/kits/game/GameProducer.cpp

     
    234234    media_node_id id;
    235235    FindLatencyFor(fOutput.destination, &fLatency, &id);
    236236
     237    if (!fBufferGroup)
     238        fBufferSize = fOutput.format.u.raw_audio.buffer_size;
     239            // Have to set it before latency calculating
     240
    237241    // Use a dry run to see how long it takes me to fill a buffer of data
    238242       
    239243    // The first step to setup the buffer
    240244    bigtime_t start, produceLatency;
    241     int32 frames = int32(fOutput.format.u.raw_audio.buffer_size / fFrameSize);
     245    int32 frames = int32(fBufferSize / fFrameSize);
    242246    float* data = new float[frames * 2];
    243247   
    244248    // Second, fill the buffer
     
    263267    // Set up the buffer group for our connection, as long as nobody handed us a
    264268    // buffer group (via SetBufferGroup()) prior to this. 
    265269    if (!fBufferGroup) {
    266         size_t size = fOutput.format.u.raw_audio.buffer_size;
    267270        int32 count = int32(fLatency / BufferDuration() + 2);
    268         fBufferGroup = new BBufferGroup(size, count);
     271        fBufferGroup = new BBufferGroup(fBufferSize, count);
    269272    }
    270273}
    271274
     
    312315    if (newGroup != NULL) {
    313316        // we were given a valid group; just use that one from now on
    314317        fBufferGroup = newGroup;
     318
     319        // get buffer length from the first buffer
     320        BBuffer *buffers[1];
     321        if (newGroup->GetBufferList(1, buffers) != B_OK)
     322            return B_BAD_VALUE;
     323        fBufferSize = buffers[0]->SizeAvailable();
    315324    } else {
    316325        // we were passed a NULL group pointer; that means we construct
    317326        // our own buffer group to use from now on
    318         size_t size = fOutput.format.u.raw_audio.buffer_size;
     327        fBufferSize = fOutput.format.u.raw_audio.buffer_size;
    319328        int32 count = int32(fLatency / BufferDuration() + 2);
    320         fBufferGroup = new BBufferGroup(size, count);
     329        fBufferGroup = new BBufferGroup(fBufferSize, count);
    321330    }
    322331
    323332    return B_OK;
     
    356365            // The other run modes dictate various strategies for sacrificing data quality
    357366            // in the interests of timely data delivery.  The way *we* do this is to skip
    358367            // a buffer, which catches us up in time by one buffer duration.
    359             size_t nSamples = fOutput.format.u.raw_audio.buffer_size / fFrameSize;
     368            size_t nSamples = fBufferSize / fFrameSize;
    360369            fFramesSent += nSamples;
    361370        }
    362371    }
     
    478487                }
    479488               
    480489                // track how much media we've delivered so far
    481                 size_t nFrames = fOutput.format.u.raw_audio.buffer_size / fFrameSize;
     490                size_t nFrames = fBufferSize / fFrameSize;
    482491                fFramesSent += nFrames;
    483492
    484493                // The buffer is on its way; now schedule the next one to go
     
    497506
    498507BBuffer*
    499508GameProducer::FillNextBuffer(bigtime_t event_time)
    500 {   
     509{
    501510    // get a buffer from our buffer group
    502     BBuffer* buf = fBufferGroup->RequestBuffer(fOutput.format.u.raw_audio.buffer_size, BufferDuration());
     511    BBuffer* buf = fBufferGroup->RequestBuffer(fBufferSize, BufferDuration());
    503512
    504513    // if we fail to get a buffer (for example, if the request times out), we skip this
    505514    // buffer and go on to the next, to avoid locking up the control thread
     
    507516        return NULL;
    508517
    509518    // we need to discribe the buffer
    510     int64 frames = int64(fOutput.format.u.raw_audio.buffer_size / fFrameSize);
    511     memset(buf->Data(), 0, fOutput.format.u.raw_audio.buffer_size);
     519    int64 frames = int64(fBufferSize / fFrameSize);
     520    memset(buf->Data(), 0, fBufferSize);
    512521       
    513522    // now fill the buffer with data, continuing where the last buffer left off
    514523    fObject->Play(buf->Data(), frames);
     
    516525    // fill in the buffer header
    517526    media_header* hdr = buf->Header();
    518527    hdr->type = B_MEDIA_RAW_AUDIO;
    519     hdr->size_used = fOutput.format.u.raw_audio.buffer_size;
     528    hdr->size_used = fBufferSize;
    520529    hdr->time_source = TimeSource()->ID();
    521530   
    522531    bigtime_t stamp;
  • src/kits/game/GameSoundBuffer.h

     
    6565           
    6666            size_t                  fFrameSize;
    6767           
     68            Connection *            fConnection;
     69            GameProducer *          fNode;
     70
    6871private:
    6972           
    70             Connection *            fConnection;
    71             GameProducer *          fNode;
    7273            bool                    fIsConnected;
    7374            bool                    fIsPlaying;
    7475           
     
    103104class StreamingSoundBuffer : public GameSoundBuffer {
    104105public:
    105106                                StreamingSoundBuffer(const gs_audio_format * format,
    106                                                      const void * streamHook);
     107                                                     const void * streamHook,
     108                                                     size_t inBufferFrameCount,
     109                                                     size_t inBufferCount);
    107110    virtual                     ~StreamingSoundBuffer();
    108111   
    109112protected:
  • src/kits/game/GameProducer.h

     
    146146    size_t              fFrameSize;
    147147    int64               fFramesSent;
    148148    GameSoundBuffer *   fObject;   
     149    size_t              fBufferSize;
    149150};
    150151
    151152#endif
  • src/kits/game/StreamingGameSound.cpp

     
    106106                          size_t inBufferCount)
    107107{
    108108    gs_id sound;
    109     status_t error = Device()->CreateBuffer(&sound, this, format);
     109    status_t error = Device()->CreateBuffer(&sound, this, format,
     110        inBufferFrameCount, inBufferCount);
    110111    if (error != B_OK) return error;
    111112   
    112113    return BGameSound::Init(sound);