Sirikata
liboh/plugins/sdlaudio/SDLAudio.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved.
00002 // Use of this source code is governed by a BSD-style license that can
00003 // be found in the LICENSE file.
00004 
00005 #ifndef _SIRIKATA_SDLAUDIO_SDLAUDIO_HPP_
00006 #define _SIRIKATA_SDLAUDIO_SDLAUDIO_HPP_
00007 
00008 #include <sirikata/oh/Simulation.hpp>
00009 #include <sirikata/core/service/Context.hpp>
00010 #include <sirikata/core/transfer/TransferMediator.hpp>
00011 #include <sirikata/core/transfer/ResourceDownloadTask.hpp>
00012 #include <sirikata/core/util/Liveness.hpp>
00013 
00014 namespace Sirikata {
00015 namespace SDL {
00016 
00017 class FFmpegAudioStream;
00018 typedef std::tr1::shared_ptr<FFmpegAudioStream> FFmpegAudioStreamPtr;
00019 
00020 class AudioSimulation : public Simulation,
00021                         public Liveness
00022 
00023 {
00024 public:
00025     AudioSimulation(Context* ctx, Network::IOStrandPtr aStrand);
00026     virtual ~AudioSimulation();
00027 
00028 
00029     // Service Interface
00030     virtual void start();
00031     virtual void stop();
00032 
00033     // Invokable Interface
00034     virtual boost::any invoke(std::vector<boost::any>& params);
00035 
00036 
00037 
00038     // Mixing interface, public for the mixing callback function
00039     void mix(uint8* raw_stream, int32 len);
00040 
00041 private:
00042     void iStart(Liveness::Token lt);
00043     void iStop(Liveness::Token lt);
00044 
00045     bool initialized;
00046 
00047     // Indicates whether basic initialization was successful, i.e. whether we're
00048     // going to be able to do any operations.
00049     bool ready() const;
00050 
00051     void handleFinishedDownload(
00052         Transfer::ResourceDownloadTaskPtr taskptr,
00053         Transfer::TransferRequestPtr request,
00054         Transfer::DenseDataPtr response);
00055 
00056     void iHandleFinishedDownload(
00057         Liveness::Token lt,
00058         Transfer::ResourceDownloadTaskPtr taskptr,
00059         Transfer::TransferRequestPtr request,
00060         Transfer::DenseDataPtr response);
00061 
00062     Network::IOStrandPtr audioStrand;
00063 
00064 
00065     Context* mContext;
00066     bool mInitializedAudio;
00067     bool mOpenedAudio;
00068 
00069     typedef uint32 ClipHandle;
00070     ClipHandle mClipHandleSource;
00071 
00072     // Mixing callbacks come from SDL's thread, download handlers come from
00073     // transfer thread, so we need to be careful about managing access.
00074     typedef boost::mutex Mutex;
00075     typedef boost::unique_lock<Mutex> Lock;
00076     Mutex mMutex;
00077 
00078     Transfer::TransferPoolPtr mTransferPool;
00079     struct DownloadTask {
00080         Transfer::ResourceDownloadTaskPtr task;
00081         std::set<ClipHandle> waiting;
00082     };
00083     typedef std::map<Transfer::URI, DownloadTask> DownloadTaskMap;
00084     DownloadTaskMap mDownloads;
00085 
00086     // ClipHandles are used to uniquely identify playing clips
00087     struct Clip {
00088         FFmpegAudioStreamPtr stream;
00089         bool paused;
00090         float32 volume;
00091         bool loop;
00092     };
00093     typedef std::map<ClipHandle, Clip> ClipMap;
00094     ClipMap mClips;
00095 
00096     bool mPlaying;
00097 };
00098 
00099 } //namespace SDL
00100 } //namespace Sirikata
00101 
00102 #endif //_SIRIKATA_SDLAUDIO_SDLAUDIO_HPP_