Sirikata
liboh/plugins/sdlaudio/FFmpegGlue.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_FFMPEG_GLUE_HPP_
00006 #define _SIRIKATA_SDLAUDIO_FFMPEG_GLUE_HPP_
00007 
00008 #include <sirikata/core/util/Singleton.hpp>
00009 #include <boost/thread.hpp>
00010 
00011 namespace Sirikata {
00012 namespace SDL {
00013 
00014 // This borrows heavily from Chromium's code:
00015 //
00016 //  Copyright (c) 2011 The Chromium Authors. All rights reserved.
00017 //  Use of this source code is governed by a BSD-style license that can be
00018 //  found in the LICENSE file.
00019 //
00020 // See chromium's src/media/filters/ffmpeg_glue*.
00021 
00022 class FFmpegGlue;
00023 class FFmpegURLProtocol;
00024 
00028 class FFmpegGlue : public AutoSingleton<FFmpegGlue> {
00029 public:
00030     // Reference counting to know when it's safe to clean this up
00031     void ref();
00032     void unref();
00033 
00034     // Adds a FFmpegProtocol to the FFmpeg glue layer and returns a unique string
00035     // that can be passed to FFmpeg to identify the data source.
00036     String addProtocol(FFmpegURLProtocol* protocol);
00037 
00038     // Removes a FFmpegProtocol from the FFmpeg glue layer.  Using strings from
00039     // previously added FFmpegProtocols will no longer work.
00040     void removeProtocol(FFmpegURLProtocol* protocol);
00041 
00042     // Assigns the FFmpegProtocol identified with by the given key to
00043     // |protocol|, or assigns NULL if no such FFmpegProtocol could be found.
00044     void getProtocol(const String& key,
00045         FFmpegURLProtocol** protocol);
00046 protected:
00047     FFmpegGlue();
00048     ~FFmpegGlue();
00049     friend class AutoSingleton<FFmpegGlue>;
00050     friend std::auto_ptr<FFmpegGlue>::~auto_ptr();
00051     friend void std::auto_ptr<FFmpegGlue>::reset(FFmpegGlue*);
00052 
00053     // Returns the unique key for this data source, which can be passed to
00054     // av_open_input_file as the filename.
00055     String getProtocolKey(FFmpegURLProtocol* protocol);
00056 
00057     int32 mRefCount;
00058 
00059     typedef boost::mutex Mutex;
00060     typedef boost::lock_guard<Mutex> Lock;
00061     Mutex mMutex;
00062     // Map between keys and FFmpegProtocol references.
00063     typedef std::map<String, FFmpegURLProtocol*> ProtocolMap;
00064     ProtocolMap mProtocols;
00065 };
00066 
00067 class FFmpegURLProtocol {
00068 public:
00069     FFmpegURLProtocol() {}
00070     virtual ~FFmpegURLProtocol() {}
00071 
00072     // Get a user-readable name for this, e.g. the filename or original URL.
00073     virtual String name() const = 0;
00074 
00075     // Read the given amount of bytes into data, returns the number of bytes read
00076     // if successful, kReadError otherwise.
00077     virtual size_t read(size_t size, uint8* data) = 0;
00078 
00079     // Returns true and the current file position for this file, false if the
00080     // file position could not be retrieved.
00081     virtual bool getPosition(int64* position_out) = 0;
00082 
00083     // Returns true if the file position could be set, false otherwise.
00084     virtual bool setPosition(int64 position) = 0;
00085 
00086     // Returns true and the file size, false if the file size could not be
00087     // retrieved.
00088     virtual bool getSize(int64* size_out) = 0;
00089 
00090     // Returns false if this protocol supports random seeking.
00091     virtual bool isStreaming() = 0;
00092 
00093 private:
00094     FFmpegURLProtocol(const FFmpegURLProtocol&);
00095     FFmpegURLProtocol& operator=(const FFmpegURLProtocol&);
00096 };
00097 
00098 } // namespace SDL
00099 } // namespace Sirikata
00100 
00101 #endif //_SIRIKATA_SDLAUDIO_FFMPEG_GLUE_HPP_