Browse Source

Make audio work

Includes some reorganization and cleanup
Florian Meißner 10 years ago
parent
commit
7cd8f51eb4
  1. 5
      CMakeLists.txt
  2. 11
      Sources/Plasma/Apps/plClient/CMakeLists.txt
  3. 21
      Sources/Plasma/Apps/plClient/plClient.cpp
  4. 2
      Sources/Plasma/Apps/plClient/plClient.h
  5. 4
      Sources/Plasma/FeatureLib/pfMoviePlayer/CMakeLists.txt
  6. 264
      Sources/Plasma/FeatureLib/pfMoviePlayer/plMoviePlayer.cpp
  7. 19
      Sources/Plasma/FeatureLib/pfMoviePlayer/plMoviePlayer.h
  8. 87
      Sources/Plasma/PubUtilLib/plAudio/plWin32VideoSound.cpp
  9. 13
      Sources/Plasma/PubUtilLib/plAudio/plWin32VideoSound.h
  10. 9
      cmake/FindOpus.cmake
  11. 8
      cmake/FindVPX.cmake

5
CMakeLists.txt

@ -51,6 +51,11 @@ if(WIN32)
find_package(DirectX REQUIRED) find_package(DirectX REQUIRED)
endif(WIN32) endif(WIN32)
if (VPX_FOUND AND Opus_FOUND)
set(VIDEO_AVAILABLE TRUE)
add_definitions(-DVIDEO_AVAILABLE)
endif()
include(PrecompiledHeader) #Precompiled Header helper macros include(PrecompiledHeader) #Precompiled Header helper macros
# libCurl isn't smart enough to detect this for us, so we have to configure it ourselves # libCurl isn't smart enough to detect this for us, so we have to configure it ourselves

11
Sources/Plasma/Apps/plClient/CMakeLists.txt

@ -12,6 +12,11 @@ include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${PYTHON_INCLUDE_DIR}) include_directories(${PYTHON_INCLUDE_DIR})
include_directories(${CURL_INCLUDE_DIR}) include_directories(${CURL_INCLUDE_DIR})
if (VIDEO_AVAILABLE)
include_directories(${VPX_INCLUDE_DIR})
include_directories(${Opus_INCLUDE_DIR})
endif (VIDEO_AVAILABLE)
# Test for Python Interpreter, which will be used for extra build scripts if available # Test for Python Interpreter, which will be used for extra build scripts if available
find_package(PythonInterp) find_package(PythonInterp)
if(PYTHONINTERP_FOUND) if(PYTHONINTERP_FOUND)
@ -161,15 +166,15 @@ target_link_libraries(plClient ${Ogg_LIBRARIES})
target_link_libraries(plClient ${Vorbis_LIBRARIES}) target_link_libraries(plClient ${Vorbis_LIBRARIES})
target_link_libraries(plClient ${DirectX_LIBRARIES}) target_link_libraries(plClient ${DirectX_LIBRARIES})
target_link_libraries(plClient ${CURL_LIBRARY}) target_link_libraries(plClient ${CURL_LIBRARY})
target_link_libraries(plClient ${Opus_LIBRARIES})
if(USE_VLD) if(USE_VLD)
target_link_libraries(plClient ${VLD_LIBRARY}) target_link_libraries(plClient ${VLD_LIBRARY})
endif() endif()
if (VPX_AVAILABLE) if (VIDEO_AVAILABLE)
target_link_libraries(plClient ${VPX_LIBRARY}) target_link_libraries(plClient ${VPX_LIBRARY})
endif (VPX_AVAILABLE) target_link_libraries(plClient ${Opus_LIBRARIES})
endif (VIDEO_AVAILABLE)
if (WIN32) if (WIN32)
target_link_libraries(plClient rpcrt4) target_link_libraries(plClient rpcrt4)

21
Sources/Plasma/Apps/plClient/plClient.cpp

@ -825,18 +825,18 @@ bool plClient::IHandleMovieMsg(plMovieMsg* mov)
if (mov->GetFileName().IsEmpty()) if (mov->GetFileName().IsEmpty())
return true; return true;
int i = fMovies.GetCount(); size_t i = fMovies.size();
if (!(mov->GetCmd() & plMovieMsg::kMake)) if (!(mov->GetCmd() & plMovieMsg::kMake))
{ {
for (i = 0; i < fMovies.GetCount(); i++) for (i = 0; i < fMovies.size(); i++)
{ {
if (mov->GetFileName().CompareI(fMovies[i]->GetFileName().AsString()) == 0) if (mov->GetFileName().CompareI(fMovies[i]->GetFileName().AsString()) == 0)
break; break;
} }
} }
if (i == fMovies.GetCount()) if (i == fMovies.size())
{ {
fMovies.Append(new plMoviePlayer); fMovies.push_back(new plMoviePlayer());
fMovies[i]->SetFileName(mov->GetFileName()); fMovies[i]->SetFileName(mov->GetFileName());
} }
@ -892,7 +892,8 @@ bool plClient::IHandleMovieMsg(plMovieMsg* mov)
if (!fMovies[i]->GetFileName().IsValid()) if (!fMovies[i]->GetFileName().IsValid())
{ {
delete fMovies[i]; delete fMovies[i];
fMovies.Remove(i); fMovies[i] = fMovies.back();
fMovies.pop_back();
} }
return true; return true;
} }
@ -1827,13 +1828,13 @@ bool plClient::IDraw()
void plClient::IServiceMovies() void plClient::IServiceMovies()
{ {
int i; for (size_t i = 0; i < fMovies.size(); i++)
for (i = 0; i < fMovies.GetCount(); i++)
{ {
if (!fMovies[i]->NextFrame()) if (!fMovies[i]->NextFrame())
{ {
delete fMovies[i]; delete fMovies[i];
fMovies.Remove(i); fMovies[i] = fMovies.back();
fMovies.pop_back();
i--; i--;
} }
} }
@ -1841,9 +1842,9 @@ void plClient::IServiceMovies()
void plClient::IKillMovies() void plClient::IKillMovies()
{ {
for (int i = 0; i < fMovies.GetCount(); i++) for (size_t i = 0; i < fMovies.size(); i++)
delete fMovies[i]; delete fMovies[i];
fMovies.Reset(); fMovies.clear();
} }
bool plClient::IPlayIntroMovie(const char* movieName, float endDelay, float posX, float posY, float scaleX, float scaleY, float volume /* = 1.0 */) bool plClient::IPlayIntroMovie(const char* movieName, float endDelay, float posX, float posY, float scaleX, float scaleY, float volume /* = 1.0 */)

2
Sources/Plasma/Apps/plClient/plClient.h

@ -147,7 +147,7 @@ protected:
int fQuality; int fQuality;
bool fQuitIntro; bool fQuitIntro;
hsTArray<plMoviePlayer*> fMovies; std::vector<plMoviePlayer*> fMovies;
plMessagePumpProc fMessagePumpProc; plMessagePumpProc fMessagePumpProc;

4
Sources/Plasma/FeatureLib/pfMoviePlayer/CMakeLists.txt

@ -3,10 +3,10 @@ include_directories(../../NucleusLib)
include_directories(../../NucleusLib/inc) include_directories(../../NucleusLib/inc)
include_directories(../../PubUtilLib) include_directories(../../PubUtilLib)
if (VPX_AVAILABLE) if (VIDEO_AVAILABLE)
include_directories(${VPX_INCLUDE_DIR}) include_directories(${VPX_INCLUDE_DIR})
include_directories(${Opus_INCLUDE_DIR}) include_directories(${Opus_INCLUDE_DIR})
endif (VPX_AVAILABLE) endif (VIDEO_AVAILABLE)
set(pfMoviePlayer_SOURCES set(pfMoviePlayer_SOURCES
plMoviePlayer.cpp plMoviePlayer.cpp

264
Sources/Plasma/FeatureLib/pfMoviePlayer/plMoviePlayer.cpp

@ -41,18 +41,15 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
*==LICENSE==*/ *==LICENSE==*/
#include "plMoviePlayer.h" #include "plMoviePlayer.h"
#include <tuple>
#include <memory>
#ifdef VPX_AVAILABLE #ifdef VIDEO_AVAILABLE
# define VPX_CODEC_DISABLE_COMPAT 1 # define VPX_CODEC_DISABLE_COMPAT 1
# include <vpx/vpx_decoder.h> # include <vpx/vpx_decoder.h>
# include <vpx/vp8dx.h> # include <vpx/vp8dx.h>
# define iface (vpx_codec_vp9_dx()) # define iface (vpx_codec_vp9_dx())
# include <opus.h>
#endif #endif
#include <opus.h>
#include "plGImage/plMipmap.h" #include "plGImage/plMipmap.h"
#include "pnKeyedObject/plUoid.h" #include "pnKeyedObject/plUoid.h"
#include "plPipeline/hsGDeviceRef.h" #include "plPipeline/hsGDeviceRef.h"
@ -61,12 +58,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "hsResMgr.h" #include "hsResMgr.h"
#include "hsTimer.h" #include "hsTimer.h"
#include "plAudio/plWin32VideoSound.h" #include "plAudio/plWin32VideoSound.h"
#include "../Apps/plClient/plClient.h"
#include "plScene/plSceneNode.h"
#include "pnSceneObject/plSceneObject.h"
#include "pnSceneObject/plAudioInterface.h"
#include "pnMessage/plSoundMsg.h"
#include "plgDispatch.h"
#include "webm/mkvreader.hpp" #include "webm/mkvreader.hpp"
#include "webm/mkvparser.hpp" #include "webm/mkvparser.hpp"
@ -88,6 +79,7 @@ class VPX
{ {
VPX() { } VPX() { }
#ifdef VIDEO_AVAILABLE
public: public:
vpx_codec_ctx_t codec; vpx_codec_ctx_t codec;
@ -121,6 +113,7 @@ public:
// if this proves false, move decoder function into IProcessVideoFrame // if this proves false, move decoder function into IProcessVideoFrame
return vpx_codec_get_frame(&codec, &iter); return vpx_codec_get_frame(&codec, &iter);
} }
#endif
}; };
// ===================================================== // =====================================================
@ -133,57 +126,46 @@ class TrackMgr
bool PeekNextBlockEntry(const std::unique_ptr<mkvparser::Segment>& segment) bool PeekNextBlockEntry(const std::unique_ptr<mkvparser::Segment>& segment)
{ {
// Assume that if blk_entry == nullptr, we need to start from the beginning // Assume that if blk_entry == nullptr, we need to start from the beginning
// Load the current cluster
const mkvparser::Cluster* cluster; const mkvparser::Cluster* cluster;
if (blk_entry) if (blk_entry)
cluster = blk_entry->GetCluster(); cluster = blk_entry->GetCluster();
else else
cluster = segment->GetFirst(); cluster = segment->GetFirst();
while (true) // As long as we have clusters, they contain blocks that we have to process
{ while (cluster && !cluster->EOS()) {
if (cluster->EOS() && cluster != NULL) // If we have no block yet, get the first one, otherwise the next one
{ if (!blk_entry)
cluster = segment->GetNext(cluster);
blk_entry = nullptr;
if (!cluster)
return false;
}
if (blk_entry)
{
SAFE_OP(cluster->GetNext(blk_entry, blk_entry), "get next block");
}
else if (cluster->m_pSegment != NULL)
{ {
SAFE_OP(cluster->GetFirst(blk_entry), "get first block"); SAFE_OP(cluster->GetFirst(blk_entry), "get first block");
} }
else else
{ {
blk_entry = nullptr; SAFE_OP(cluster->GetNext(blk_entry, blk_entry), "get next block");
return false; //reached end of movie. I hope.
} }
if (blk_entry) // Are there any blocks left?
{ while (blk_entry && !blk_entry->EOS()) {
if (blk_entry->EOS()) // Is this the next block we want for our track? Awesome, we're done!
continue;
if (blk_entry->GetBlock()->GetTrackNumber() == number) if (blk_entry->GetBlock()->GetTrackNumber() == number)
return true; return true;
SAFE_OP(cluster->GetNext(blk_entry, blk_entry), "get next block");
} }
else
{ // No blocks left, go to next cluster
cluster = segment->GetNext(cluster); blk_entry = nullptr;
blk_entry = nullptr; cluster = segment->GetNext(cluster);
if (!cluster)
return false;
}
} }
return false; // if this happens, boom.
// That's it, nothing left...
return false;
} }
public: public:
uint32_t number; int32_t number;
TrackMgr(uint32_t num) : blk_entry(nullptr), valid(true), number(num) { } TrackMgr(int32_t num) : blk_entry(nullptr), valid(true), number(num) { }
bool GetFrames(plMoviePlayer* p, int64_t movieTime, std::vector<blkbuf_t>& frames) bool GetFrames(plMoviePlayer* p, int64_t movieTime, std::vector<blkbuf_t>& frames)
{ {
@ -204,7 +186,8 @@ public:
data.Read(p->fReader, buf); data.Read(p->fReader, buf);
frames.push_back(std::make_tuple(std::unique_ptr<uint8_t>(buf), static_cast<int32_t>(data.len))); frames.push_back(std::make_tuple(std::unique_ptr<uint8_t>(buf), static_cast<int32_t>(data.len)));
} }
} else }
else
{ {
blk_entry = prev; blk_entry = prev;
return true; return true;
@ -213,42 +196,6 @@ public:
} }
return true; return true;
} }
#if 0
bool Advance(plMoviePlayer* p, int64_t movieTime=0)
{
if (!valid)
return false;
// This keeps us from getting behind due to freezes
// Assumption: Audio will not skip ahead in time. FIXME?
while ((valid = PeekNextBlockEntry(p->fSegment)))
{
const mkvparser::Block* blk = blk_entry->GetBlock();
if (blk->GetTime(blk_entry->GetCluster()) < movieTime)
continue;
else
return true;
}
return false; // ran out of blocks
}
int32_t GetBlockData(plMoviePlayer* p, std::vector<blkbuf_t>& frames) const
{
const mkvparser::Block* block = blk_entry->GetBlock();
// Return the frames
frames.reserve(block->GetFrameCount());
for (int32_t i = 0; i < block->GetFrameCount(); ++i)
{
const mkvparser::Block::Frame frame = block->GetFrame(i);
std::shared_ptr<uint8_t> data(new uint8_t[frame.len]);
frame.Read(p->fReader, data.get());
frames.push_back(std::make_tuple(data, frame.len));
}
return block->GetFrameCount();
}
#endif 0
}; };
// ===================================================== // =====================================================
@ -257,44 +204,27 @@ plMoviePlayer::plMoviePlayer() :
fPlate(nullptr), fPlate(nullptr),
fTexture(nullptr), fTexture(nullptr),
fReader(nullptr), fReader(nullptr),
fTimeScale(0),
fStartTime(0), fStartTime(0),
fAudioPlayer(),
fPosition(hsPoint2()), fPosition(hsPoint2()),
fAudioInterface(), fPlaying(false),
fPlaying(true), fOpusDecoder(nullptr)
fOpusDecoder(nil)
{ {
fScale.Set(1.0f, 1.0f); fScale.Set(1.0f, 1.0f);
fAudioSound = std::shared_ptr<plWin32VideoSound>(new plWin32VideoSound());
fAudioPlayer.SetSound(fAudioSound);
plSceneNode* sceneNode = plClient::GetInstance()->GetCurrentScene();
if (sceneNode != nullptr)
{
hsTArray<plSceneObject*>& sceneObjects = sceneNode->GetSceneObjects();
for (int i = 0; i < sceneObjects.GetCount(); ++i)
{
if (sceneObjects[i]->GetAudioInterface() == nullptr)
{
fAudioInterface.ISetAudible(&fAudioPlayer);
sceneObjects[i]->SetAudioInterface(&fAudioInterface);
break;
}
}
}
} }
plMoviePlayer::~plMoviePlayer() plMoviePlayer::~plMoviePlayer()
{ {
opus_decoder_destroy(fOpusDecoder);
if (fPlate) if (fPlate)
// The plPlate owns the Mipmap Texture, so it destroys it for us // The plPlate owns the Mipmap Texture, so it destroys it for us
plPlateManager::Instance().DestroyPlate(fPlate); plPlateManager::Instance().DestroyPlate(fPlate);
#ifdef VPX_AVAILABLE #ifdef VIDEO_AVAILABLE
if (fOpusDecoder)
opus_decoder_destroy(fOpusDecoder);
if (fReader) if (fReader)
{
fReader->Close(); fReader->Close();
delete fReader;
}
#endif #endif
} }
@ -305,7 +235,7 @@ int64_t plMoviePlayer::GetMovieTime() const
bool plMoviePlayer::IOpenMovie() bool plMoviePlayer::IOpenMovie()
{ {
#ifdef VPX_AVAILABLE #ifdef VIDEO_AVAILABLE
if (!plFileInfo(fMoviePath).Exists()) if (!plFileInfo(fMoviePath).Exists())
{ {
hsAssert(false, "Tried to play a movie that doesn't exist"); hsAssert(false, "Tried to play a movie that doesn't exist");
@ -326,9 +256,6 @@ bool plMoviePlayer::IOpenMovie()
SAFE_OP(seg->Load(), "load segment from webm"); SAFE_OP(seg->Load(), "load segment from webm");
fSegment.reset(seg); fSegment.reset(seg);
// Just in case someone gives us a weird file, find out the timecode offset
fTimeScale = fSegment->GetInfo()->GetTimeCodeScale();
// TODO: Figure out video and audio based on current language // TODO: Figure out video and audio based on current language
// For now... just take the first one. // For now... just take the first one.
const mkvparser::Tracks* tracks = fSegment->GetTracks(); const mkvparser::Tracks* tracks = fSegment->GetTracks();
@ -360,8 +287,9 @@ bool plMoviePlayer::IOpenMovie()
#endif #endif
} }
bool plMoviePlayer::IProcessVideoFrame(const std::vector<blkbuf_t>& frames) void plMoviePlayer::IProcessVideoFrame(const std::vector<blkbuf_t>& frames)
{ {
#ifdef VIDEO_AVAILABLE
vpx_image_t* img = nullptr; vpx_image_t* img = nullptr;
// We have to decode all the frames, but we only want to display the most recent one to the user. // We have to decode all the frames, but we only want to display the most recent one to the user.
@ -390,34 +318,45 @@ bool plMoviePlayer::IProcessVideoFrame(const std::vector<blkbuf_t>& frames)
// Flush new data to the device // Flush new data to the device
if (fTexture->GetDeviceRef()) if (fTexture->GetDeviceRef())
fTexture->GetDeviceRef()->SetDirty(true); fTexture->GetDeviceRef()->SetDirty(true);
return true;
} }
return false; #endif
} }
bool plMoviePlayer::Start() void plMoviePlayer::IProcessAudioFrame(const std::vector<blkbuf_t>& frames)
{ {
plSceneNode* sceneNode = plClient::GetInstance()->GetCurrentScene(); #ifdef VIDEO_AVAILABLE
if (sceneNode != nullptr) const unsigned char* data = nullptr;
sceneNode->GetKey(); int32_t size = 0;
for (auto it = frames.begin(); it != frames.end(); ++it)
{
const std::unique_ptr<uint8_t>& buf = std::get<0>(*it);
data = buf.get();
size = std::get<1>(*it);
#ifdef VPX_AVAILABLE static const int frameSize = 5760; //max packet duration at 48kHz
const mkvparser::AudioTrack* audio = static_cast<const mkvparser::AudioTrack*>(fSegment->GetTracks()->GetTrackByNumber(fAudioTrack->number));
int16_t* pcm = new int16_t[frameSize * audio->GetChannels() * sizeof(int16_t)];
int samples = opus_decode(fOpusDecoder, data, size, pcm, frameSize, 0);
if (samples < 0)
hsAssert(false, "opus error");
fAudioSound->UpdateSoundBuffer(reinterpret_cast<uint8_t*>(pcm), samples * audio->GetChannels() * sizeof(int16_t));
}
#endif
}
bool plMoviePlayer::Start()
{
#ifdef VIDEO_AVAILABLE
if (!IOpenMovie()) if (!IOpenMovie())
return false; return false;
hsAssert(fVideoTrack, "nil video track -- expect bad things to happen!"); hsAssert(fVideoTrack, "nil video track -- expect bad things to happen!");
// Initialize VP8 // Initialize VPX
if (VPX* vpx = VPX::Create()) if (VPX* vpx = VPX::Create())
fVpx.reset(vpx); fVpx.reset(vpx);
else else
return false; return false;
//initialize opus
int error;
fOpusDecoder = opus_decoder_create(48000, 1, &error);
if (error != OPUS_OK)
hsAssert(false, "Error occured initalizing opus");
// Need to figure out scaling based on pipe size. // Need to figure out scaling based on pipe size.
plPlateManager& plateMgr = plPlateManager::Instance(); plPlateManager& plateMgr = plPlateManager::Instance();
const mkvparser::VideoTrack* video = static_cast<const mkvparser::VideoTrack*>(fSegment->GetTracks()->GetTrackByNumber(fVideoTrack->number)); const mkvparser::VideoTrack* video = static_cast<const mkvparser::VideoTrack*>(fSegment->GetTracks()->GetTrackByNumber(fVideoTrack->number));
@ -428,19 +367,34 @@ bool plMoviePlayer::Start()
fPlate->SetVisible(true); fPlate->SetVisible(true);
fTexture = fPlate->CreateMaterial(static_cast<uint32_t>(video->GetWidth()), static_cast<uint32_t>(video->GetHeight()), nullptr); fTexture = fPlate->CreateMaterial(static_cast<uint32_t>(video->GetWidth()), static_cast<uint32_t>(video->GetHeight()), nullptr);
//initialize opus
const mkvparser::AudioTrack* audio = static_cast<const mkvparser::AudioTrack*>(fSegment->GetTracks()->GetTrackByNumber(fAudioTrack->number));
plWAVHeader header;
header.fFormatTag = plWAVHeader::kPCMFormatTag;
header.fNumChannels = audio->GetChannels();
header.fBitsPerSample = audio->GetBitDepth() == 8 ? 8 : 16;
header.fNumSamplesPerSec = 48000; // OPUS specs say we shall always decode at 48kHz
header.fBlockAlign = header.fNumChannels * header.fBitsPerSample / 2;
header.fAvgBytesPerSec = header.fNumSamplesPerSec * header.fBlockAlign;
fAudioSound.reset(new plWin32VideoSound(header));
int error;
fOpusDecoder = opus_decoder_create(48000, audio->GetChannels(), &error);
if (error != OPUS_OK)
hsAssert(false, "Error occured initalizing opus");
fPlaying = true; fPlaying = true;
return true; return true;
#else #else
return false; return false;
#endif // VPX_AVAILABLE #endif // VIDEO_AVAILABLE
} }
bool plMoviePlayer::NextFrame() bool plMoviePlayer::NextFrame()
{ {
if (fPlaying) if (fPlaying)
{ {
#ifdef VPX_AVAILABLE #ifdef VIDEO_AVAILABLE
// Get our current timecode // Get our current timecode
int64_t movieTime = 0; int64_t movieTime = 0;
if (fStartTime == 0) if (fStartTime == 0)
@ -450,23 +404,21 @@ bool plMoviePlayer::NextFrame()
std::vector<blkbuf_t> audio; std::vector<blkbuf_t> audio;
std::vector<blkbuf_t> video; std::vector<blkbuf_t> video;
uint8_t tracksWithData = 0;
if (fAudioTrack)
{ {
uint8_t tracksWithData = 0; if (fAudioTrack->GetFrames(this, movieTime, audio))
if (fAudioTrack) tracksWithData++;
{ }
if (fAudioTrack->GetFrames(this, movieTime, audio)) if (fVideoTrack)
tracksWithData++; {
} if (fVideoTrack->GetFrames(this, movieTime, video))
if (fVideoTrack) tracksWithData++;
{ }
if (fVideoTrack->GetFrames(this, movieTime, video)) if (!tracksWithData)
tracksWithData++; {
} Stop();
if (tracksWithData == 0) return false;
{
Stop();
return false;
}
} }
// Show our mess // Show our mess
@ -476,7 +428,7 @@ bool plMoviePlayer::NextFrame()
return true; return true;
#else #else
return false; return false;
#endif // VPX_AVAILABLE #endif // VIDEO_AVAILABLE
} }
return false; return false;
} }
@ -484,36 +436,10 @@ bool plMoviePlayer::NextFrame()
bool plMoviePlayer::Stop() bool plMoviePlayer::Stop()
{ {
fPlaying = false; fPlaying = false;
if (fAudioSound)
fAudioSound->Stop();
for (int i = 0; i < fCallbacks.size(); i++) for (int i = 0; i < fCallbacks.size(); i++)
fCallbacks[i]->Send(); fCallbacks[i]->Send();
fCallbacks.clear(); fCallbacks.clear();
return false;
}
bool plMoviePlayer::IProcessAudioFrame(const std::vector<blkbuf_t>& frames)
{
const unsigned char* data = NULL;
int32_t size = 0;
for (auto it = frames.begin(); it != frames.end(); ++it)
{
const std::unique_ptr<uint8_t>& buf = std::get<0>(*it);
data = buf.get();
size = std::get<1>(*it);
int error;
const int frameSize = 5760; //max packet duration at 48kHz
opus_int16* pcm = new opus_int16[frameSize * 1 * sizeof(opus_int16)];
error = opus_decode(fOpusDecoder, data, size, pcm, frameSize, 0);
if (error < 0)
hsAssert(false, "opus error");
fAudioSound->UpdateSoundBuffer(reinterpret_cast<unsigned char*>(pcm), error);
if (plClient::GetInstance()->GetCurrentScene() != nullptr)
{
plSoundMsg* soundMsg = new plSoundMsg();
soundMsg->SetCmd(plSoundMsg::kPlay);
soundMsg->SetBCastFlag(plMessage::kBCastByType);
fAudioInterface.MsgReceive(soundMsg);
}
}
return true; return true;
} }

19
Sources/Plasma/FeatureLib/pfMoviePlayer/plMoviePlayer.h

@ -48,16 +48,11 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
#include "hsPoint2.h" #include "hsPoint2.h"
#include "hsColorRGBA.h" #include "hsColorRGBA.h"
#include "plMessage/plMovieMsg.h" #include "plMessage/plMovieMsg.h"
#include "plAudio/plWin32VideoSound.h"
#include "plAudible/plVideoAudible.h"
#include "pnSceneObject/plAudioInterface.h"
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <tuple> #include <tuple>
#include <opus.h>
namespace mkvparser namespace mkvparser
{ {
class BlockEntry; class BlockEntry;
@ -79,23 +74,21 @@ protected:
mkvparser::MkvReader* fReader; mkvparser::MkvReader* fReader;
std::unique_ptr<mkvparser::Segment> fSegment; std::unique_ptr<mkvparser::Segment> fSegment;
std::unique_ptr<class TrackMgr> fAudioTrack, fVideoTrack; // TODO: vector of tracks? std::unique_ptr<class TrackMgr> fAudioTrack, fVideoTrack; // TODO: vector of tracks?
std::unique_ptr<class plWin32VideoSound> fAudioSound;
std::unique_ptr<class VPX> fVpx; std::unique_ptr<class VPX> fVpx;
int64_t fTimeScale, fStartTime; class OpusDecoder* fOpusDecoder;
int64_t fStartTime;
hsPoint2 fPosition, fScale; hsPoint2 fPosition, fScale;
plFileName fMoviePath; plFileName fMoviePath;
OpusDecoder* fOpusDecoder;
std::shared_ptr<plWin32VideoSound> fAudioSound;
plVideoAudible fAudioPlayer;
plAudioInterface fAudioInterface;
bool fPlaying; bool fPlaying;
int64_t GetMovieTime() const; int64_t GetMovieTime() const;
bool IOpenMovie(); bool IOpenMovie();
bool IProcessVideoFrame(const std::vector<blkbuf_t>& frames); void IProcessVideoFrame(const std::vector<blkbuf_t>& frames);
bool IProcessAudioFrame(const std::vector<blkbuf_t>& frames); void IProcessAudioFrame(const std::vector<blkbuf_t>& frames);
public: public:
plMoviePlayer(); plMoviePlayer();

87
Sources/Plasma/PubUtilLib/plAudio/plWin32VideoSound.cpp

@ -1,47 +1,98 @@
#include <iostream> /*==LICENSE==*
#include <fstream>
#include "hsResMgr.h" CyanWorlds.com Engine - MMOG client, server and tools
#include "plFormat.h" Copyright (C) 2011 Cyan Worlds, Inc.
#include "plDSoundBuffer.h" This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#include "plWin32VideoSound.h" #include "plWin32VideoSound.h"
#include "hsResMgr.h"
#include "plFormat.h"
#include "plDSoundBuffer.h"
static int uniqueID = 0; static int uniqueID = 0;
plWin32VideoSound::plWin32VideoSound() : plWin32Sound() plWin32VideoSound::plWin32VideoSound(plWAVHeader& header) : plWin32Sound()
{ {
plWAVHeader header; fCurrVolume = 1.0f;
header.fFormatTag = 0x1; fDesiredVol = 1.0f;
header.fNumChannels = 1; fSoftVolume = 1.0f;
header.fBitsPerSample = 16; fType = kGUISound;
fDSoundBuffer = new plDSoundBuffer(0, header, false, false, false, true); fDSoundBuffer = new plDSoundBuffer(0, header, false, false, false, true);
fDSoundBuffer->SetupVoiceSource(); fDSoundBuffer->SetupVoiceSource();
fDSoundBuffer->SetScalarVolume(1.0f);
uniqueID++; uniqueID++;
hsgResMgr::ResMgr()->NewKey(plFormat("videosound#{}", uniqueID), this, plLocation::kGlobalFixedLoc); hsgResMgr::ResMgr()->NewKey(plFormat("videosound#{}", uniqueID), this, plLocation::kGlobalFixedLoc);
fSoftVolume = 1.0f;
} }
plWin32VideoSound::~plWin32VideoSound() plWin32VideoSound::~plWin32VideoSound()
{ {
delete fDSoundBuffer; if (fDSoundBuffer)
delete fDSoundBuffer;
} }
void plWin32VideoSound::UpdateSoundBuffer(unsigned char* buffer, size_t size) void plWin32VideoSound::UpdateSoundBuffer(void* buffer, size_t size)
{ {
unsigned int bufferID = 0; uint32_t bufferId;
uint32_t chunk;
if (fDSoundBuffer->GetAvailableBufferId(&bufferID)) fDSoundBuffer->UnQueueVoiceBuffers();
while (size > 0)
{ {
fDSoundBuffer->VoiceFillBuffer(buffer, size, bufferID); chunk = size < STREAM_BUFFER_SIZE ? size : STREAM_BUFFER_SIZE;
IActuallyPlay(); if (!fDSoundBuffer->GetAvailableBufferId(&bufferId))
break;
fDSoundBuffer->VoiceFillBuffer(buffer, chunk, bufferId);
size -= chunk;
} }
IActuallyPlay();
} }
void plWin32VideoSound::IDerivedActuallyPlay() void plWin32VideoSound::IDerivedActuallyPlay()
{ {
if (!fDSoundBuffer->IsPlaying()) if (!fReallyPlaying)
{
fDSoundBuffer->Play(); fDSoundBuffer->Play();
fReallyPlaying = true;
}
} }
bool plWin32VideoSound::LoadSound(bool is3D) bool plWin32VideoSound::LoadSound(bool is3D)

13
Sources/Plasma/PubUtilLib/plAudio/plWin32VideoSound.h

@ -33,12 +33,13 @@ the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work. work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at: or by snail mail at:
Cyan Worlds, Inc. Cyan Worlds, Inc.
14617 N Newport Hwy 14617 N Newport Hwy
Mead, WA 99021 Mead, WA 99021
*==LICENSE==*/ *==LICENSE==*/
#ifndef plWin32VideoSound_h #ifndef plWin32VideoSound_h
#define plWin32VideoSound_h #define plWin32VideoSound_h
@ -47,10 +48,10 @@ Mead, WA 99021
class plWin32VideoSound : public plWin32Sound class plWin32VideoSound : public plWin32Sound
{ {
public: public:
plWin32VideoSound(); plWin32VideoSound(plWAVHeader& header);
virtual ~plWin32VideoSound(); virtual ~plWin32VideoSound();
void UpdateSoundBuffer(unsigned char* buffer, size_t size); void UpdateSoundBuffer(void* buffer, size_t size);
protected: protected:
void IDerivedActuallyPlay(void); void IDerivedActuallyPlay(void);

9
cmake/FindOpus.cmake

@ -1,8 +1,7 @@
if(Opus_INCLUDE_DIR AND Opus_LIBRARY) if(Opus_INCLUDE_DIR AND Opus_LIBRARY)
set(Opus_FIND_QUIETLY TRUE) set(Opus_FIND_QUIETLY TRUE)
endif() endif()
find_path(Opus_INCLUDE_DIR opus.h find_path(Opus_INCLUDE_DIR opus.h
/usr/local/include /usr/local/include
/usr/include /usr/include
@ -20,11 +19,11 @@ find_library(Silk_LIBRARY NAMES silk_common
set(Opus_LIBRARIES set(Opus_LIBRARIES
${Opus_LIBRARY} ${Opus_LIBRARY}
${Celt_LIBRARY} ${Celt_LIBRARY}
${Silk_LIBRARY}) ${Silk_LIBRARY}
)
if(Opus_INCLUDE_DIR AND Opus_LIBRARY) if(Opus_INCLUDE_DIR AND Opus_LIBRARY)
set(Opus_FOUND TRUE) set(Opus_FOUND TRUE)
endif() endif()
if (Opus_FOUND) if (Opus_FOUND)

8
cmake/FindVPX.cmake

@ -7,12 +7,10 @@ find_path(VPX_INCLUDE_DIR vpx/vp8.h
/usr/include /usr/include
) )
find_library(VPX_LIBRARY NAMES vpxmt vpx find_library(VPX_LIBRARY NAMES vpxmt vpxmd vpx
PATHS /usr/local/lib /usr/lib PATHS /usr/local/lib /usr/lib
) )
# If everything has been found, we have movie support! if(VPX_INCLUDE_DIR AND VPX_LIBRARY)
if (VPX_INCLUDE_DIR AND VPX_LIBRARY) set(VPX_FOUND TRUE)
set(VPX_AVAILABLE TRUE)
add_definitions(-DVPX_AVAILABLE)
endif() endif()

Loading…
Cancel
Save