|
41 | 41 |
|
42 | 42 | #include "Lib/BaseType.h"
|
43 | 43 | #include "OpenALDevice/OpenALAudioManager.h"
|
44 |
| -//#include "OpenALAudioDevice/OpenALAudioStream.h" |
| 44 | +#include "OpenALDevice/OpenALAudioStream.h" |
45 | 45 | #include "OpenALAudioFileCache.h"
|
46 | 46 |
|
47 | 47 | #include "Common/AudioAffect.h"
|
|
55 | 55 | #include "Common/GameSounds.h"
|
56 | 56 | #include "Common/CRCDebug.h"
|
57 | 57 | #include "Common/GlobalData.h"
|
58 |
| -#include "Common/ScopedMutex.h" |
59 | 58 |
|
60 | 59 | #include "GameClient/DebugDisplay.h"
|
61 | 60 | #include "GameClient/Drawable.h"
|
@@ -632,7 +631,7 @@ void OpenALAudioManager::pauseAudio(AudioAffect which)
|
632 | 631 | AudioRequest *req = (*ait);
|
633 | 632 | if( req && req->m_request == AR_Play )
|
634 | 633 | {
|
635 |
| - req->deleteInstance(); |
| 634 | + deleteInstance(req); |
636 | 635 | ait = m_audioRequests.erase(ait);
|
637 | 636 | }
|
638 | 637 | else
|
@@ -767,54 +766,54 @@ void OpenALAudioManager::playAudioEvent(AudioEventRTS* event)
|
767 | 766 |
|
768 | 767 | OpenALAudioStream* stream;
|
769 | 768 | if (!handleToKill || foundSoundToReplace) {
|
770 |
| - //stream = new OpenALAudioStream; |
771 |
| - //// When we need more data ask FFmpeg for more data. |
772 |
| - //stream->setRequireDataCallback([ffmpegFile, stream]() { |
773 |
| - // ffmpegFile->decodePacket(); |
774 |
| - // }); |
775 |
| - // |
776 |
| - //// When we receive a frame from FFmpeg, send it to OpenAL. |
777 |
| - //ffmpegFile->setFrameCallback([stream](AVFrame* frame, int stream_idx, int stream_type, void* user_data) { |
778 |
| - // if (stream_type != AVMEDIA_TYPE_AUDIO) { |
779 |
| - // return; |
780 |
| - // } |
781 |
| - |
782 |
| - // DEBUG_LOG(("Received audio frame\n")); |
783 |
| - |
784 |
| - // AVSampleFormat sampleFmt = static_cast<AVSampleFormat>(frame->format); |
785 |
| - // const int bytesPerSample = av_get_bytes_per_sample(sampleFmt); |
786 |
| - // ALenum format = OpenALAudioManager::getALFormat(frame->ch_layout.nb_channels, bytesPerSample * 8); |
787 |
| - // const int frameSize = |
788 |
| - // av_samples_get_buffer_size(NULL, frame->ch_layout.nb_channels, frame->nb_samples, sampleFmt, 1); |
789 |
| - // uint8_t* frameData = frame->data[0]; |
790 |
| - |
791 |
| - // // We need to interleave the samples if the format is planar |
792 |
| - // if (av_sample_fmt_is_planar(static_cast<AVSampleFormat>(frame->format))) { |
793 |
| - // uint8_t* audioBuffer = static_cast<uint8_t*>(av_malloc(frameSize)); |
794 |
| - |
795 |
| - // // Write the samples into our audio buffer |
796 |
| - // for (int sample_idx = 0; sample_idx < frame->nb_samples; sample_idx++) |
797 |
| - // { |
798 |
| - // int byte_offset = sample_idx * bytesPerSample; |
799 |
| - // for (int channel_idx = 0; channel_idx < frame->ch_layout.nb_channels; channel_idx++) |
800 |
| - // { |
801 |
| - // uint8_t* dst = &audioBuffer[byte_offset * frame->ch_layout.nb_channels + channel_idx * bytesPerSample]; |
802 |
| - // uint8_t* src = &frame->data[channel_idx][byte_offset]; |
803 |
| - // memcpy(dst, src, bytesPerSample); |
804 |
| - // } |
805 |
| - // } |
806 |
| - // stream->bufferData(audioBuffer, frameSize, format, frame->sample_rate); |
807 |
| - // av_freep(&audioBuffer); |
808 |
| - // } |
809 |
| - // else |
810 |
| - // stream->bufferData(frameData, frameSize, format, frame->sample_rate); |
811 |
| - // }); |
| 769 | + stream = new OpenALAudioStream; |
| 770 | + // When we need more data ask FFmpeg for more data. |
| 771 | + stream->setRequireDataCallback([ffmpegFile, stream]() { |
| 772 | + ffmpegFile->decodePacket(); |
| 773 | + }); |
| 774 | + |
| 775 | + // When we receive a frame from FFmpeg, send it to OpenAL. |
| 776 | + ffmpegFile->setFrameCallback([stream](AVFrame* frame, int stream_idx, int stream_type, void* user_data) { |
| 777 | + if (stream_type != AVMEDIA_TYPE_AUDIO) { |
| 778 | + return; |
| 779 | + } |
| 780 | + |
| 781 | + DEBUG_LOG(("Received audio frame\n")); |
| 782 | + |
| 783 | + AVSampleFormat sampleFmt = static_cast<AVSampleFormat>(frame->format); |
| 784 | + const int bytesPerSample = av_get_bytes_per_sample(sampleFmt); |
| 785 | + ALenum format = OpenALAudioManager::getALFormat(frame->ch_layout.nb_channels, bytesPerSample * 8); |
| 786 | + const int frameSize = |
| 787 | + av_samples_get_buffer_size(NULL, frame->ch_layout.nb_channels, frame->nb_samples, sampleFmt, 1); |
| 788 | + uint8_t* frameData = frame->data[0]; |
| 789 | + |
| 790 | + // We need to interleave the samples if the format is planar |
| 791 | + if (av_sample_fmt_is_planar(static_cast<AVSampleFormat>(frame->format))) { |
| 792 | + uint8_t* audioBuffer = static_cast<uint8_t*>(av_malloc(frameSize)); |
| 793 | + |
| 794 | + // Write the samples into our audio buffer |
| 795 | + for (int sample_idx = 0; sample_idx < frame->nb_samples; sample_idx++) |
| 796 | + { |
| 797 | + int byte_offset = sample_idx * bytesPerSample; |
| 798 | + for (int channel_idx = 0; channel_idx < frame->ch_layout.nb_channels; channel_idx++) |
| 799 | + { |
| 800 | + uint8_t* dst = &audioBuffer[byte_offset * frame->ch_layout.nb_channels + channel_idx * bytesPerSample]; |
| 801 | + uint8_t* src = &frame->data[channel_idx][byte_offset]; |
| 802 | + memcpy(dst, src, bytesPerSample); |
| 803 | + } |
| 804 | + } |
| 805 | + stream->bufferData(audioBuffer, frameSize, format, frame->sample_rate); |
| 806 | + av_freep(&audioBuffer); |
| 807 | + } |
| 808 | + else |
| 809 | + stream->bufferData(frameData, frameSize, format, frame->sample_rate); |
| 810 | + }); |
812 | 811 |
|
813 | 812 | // Decode packets before starting the stream.
|
814 |
| - //for (int i = 0; i < AL_STREAM_BUFFER_COUNT; i++) { |
815 |
| - // if (!ffmpegFile->decodePacket()) |
816 |
| - // break; |
817 |
| - //} |
| 813 | + for (int i = 0; i < AL_STREAM_BUFFER_COUNT; i++) { |
| 814 | + if (!ffmpegFile->decodePacket()) |
| 815 | + break; |
| 816 | + } |
818 | 817 | }
|
819 | 818 | else {
|
820 | 819 | stream = NULL;
|
@@ -1061,7 +1060,7 @@ void OpenALAudioManager::killAudioEventImmediately(AudioHandle audioEvent)
|
1061 | 1060 | AudioRequest *req = (*ait);
|
1062 | 1061 | if( req && req->m_request == AR_Play && req->m_handleToInteractOn == audioEvent )
|
1063 | 1062 | {
|
1064 |
| - req->deleteInstance(); |
| 1063 | + deleteInstance(req); |
1065 | 1064 | ait = m_audioRequests.erase(ait);
|
1066 | 1065 | return;
|
1067 | 1066 | }
|
@@ -2305,7 +2304,7 @@ void OpenALAudioManager::processRequestList(void)
|
2305 | 2304 | if (!req->m_requiresCheckForSample || checkForSample(req)) {
|
2306 | 2305 | processRequest(req);
|
2307 | 2306 | }
|
2308 |
| - req->deleteInstance(); |
| 2307 | + deleteInstance(req); |
2309 | 2308 | it = m_audioRequests.erase(it);
|
2310 | 2309 | }
|
2311 | 2310 | }
|
@@ -2945,7 +2944,7 @@ void* OpenALAudioManager::getHandleForBink(void)
|
2945 | 2944 | {
|
2946 | 2945 | if (!m_binkAudio) {
|
2947 | 2946 | DEBUG_LOG(("Creating Bink audio stream\n"));
|
2948 |
| - //m_binkAudio = NEW OpenALAudioStream; |
| 2947 | + m_binkAudio = NEW OpenALAudioStream; |
2949 | 2948 | }
|
2950 | 2949 | return m_binkAudio;
|
2951 | 2950 | }
|
|
0 commit comments