Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // FFmpegGlue is an adapter for FFmpeg's URLProtocol interface that allows us to | 5 // FFmpegGlue is an interface between FFmpeg and Chrome used to proxy FFmpeg's |
| 6 // use a DataSource implementation with FFmpeg. For convenience we use FFmpeg's | 6 // read and seek requests to Chrome's internal data structures. The glue works |
| 7 // avformat_open_input() function, which analyzes the filename given to it and | 7 // through the AVIO interface provided by FFmpeg. |
| 8 // automatically initializes the appropriate URLProtocol. | |
| 9 // | 8 // |
| 10 // Since the DataSource is already open by time we call avformat_open_input(), | 9 // AVIO works through a special AVIOContext created through avio_alloc_context() |
| 11 // we need a way for avformat_open_input() to find the correct DataSource | 10 // which is attached to the AVFormatContext used for demuxing. The AVIO context |
| 12 // instance. The solution is to maintain a map of "filenames" to DataSource | 11 // is initialized with read and seek methods which FFmpeg calls when necessary. |
| 13 // instances, where filenames are actually just a unique identifier. For | |
| 14 // simplicity, FFmpegGlue is registered as an HTTP handler and generates | |
| 15 // filenames based on the memory address of the DataSource, i.e., | |
| 16 // http://0xc0bf4870. Since there may be multiple FFmpegDemuxers active at one | |
| 17 // time, FFmpegGlue is a thread-safe singleton. | |
| 18 // | 12 // |
| 19 // Usage: FFmpegDemuxer adds the DataSource to FFmpegGlue's map and is given a | 13 // During OpenContext() FFmpegGlue will tell FFmpeg to use Chrome's AVIO context |
| 20 // filename to pass to avformat_open_input(). FFmpegDemuxer calls | 14 // by passing NULL in for the filename parameter to avformat_open_input(). All |
| 21 // avformat_open_input() with the filename, which results in FFmpegGlue | 15 // FFmpeg operations using the configured AVFormatContext will then redirect |
| 22 // returning the DataSource as a URLProtocol instance to FFmpeg. Since | 16 // reads and seeks through the glue. |
| 23 // FFmpegGlue is only needed for opening files, when avformat_open_input() | 17 // |
| 24 // returns FFmpegDemuxer removes the DataSource from FFmpegGlue's map. | 18 // The glue in turn processes those read and seek requests using the |
| 19 // FFmpegURLProtocol provided during construction. | |
| 20 // | |
| 21 // FFmpegGlue is also responsible for initializing FFmpeg, which is done once | |
| 22 // per process. Initialization includes: turning off log messages, registering | |
| 23 // a lock manager, and finally registering all demuxers and codecs. | |
| 25 | 24 |
| 26 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_ | 25 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| 27 #define MEDIA_FILTERS_FFMPEG_GLUE_H_ | 26 #define MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| 28 | 27 |
| 29 #include <map> | 28 #include "base/basictypes.h" |
| 30 #include <string> | 29 #include "base/memory/scoped_ptr.h" |
| 31 | |
| 32 #include "base/memory/singleton.h" | |
| 33 #include "base/synchronization/lock.h" | |
| 34 #include "media/base/media_export.h" | 30 #include "media/base/media_export.h" |
| 35 | 31 |
| 36 struct URLProtocol; | 32 struct AVFormatContext; |
| 33 struct AVIOContext; | |
| 37 | 34 |
| 38 namespace media { | 35 namespace media { |
| 39 | 36 |
| 37 class ScopedPtrAVFree; | |
| 38 | |
| 40 class MEDIA_EXPORT FFmpegURLProtocol { | 39 class MEDIA_EXPORT FFmpegURLProtocol { |
| 41 public: | 40 public: |
| 42 FFmpegURLProtocol() {} | |
| 43 | |
| 44 // Read the given amount of bytes into data, returns the number of bytes read | 41 // Read the given amount of bytes into data, returns the number of bytes read |
| 45 // if successful, kReadError otherwise. | 42 // if successful, kReadError otherwise. |
| 46 virtual size_t Read(size_t size, uint8* data) = 0; | 43 virtual int Read(int size, uint8* data) = 0; |
| 47 | 44 |
| 48 // Returns true and the current file position for this file, false if the | 45 // Returns true and the current file position for this file, false if the |
| 49 // file position could not be retrieved. | 46 // file position could not be retrieved. |
| 50 virtual bool GetPosition(int64* position_out) = 0; | 47 virtual bool GetPosition(int64* position_out) = 0; |
| 51 | 48 |
| 52 // Returns true if the file position could be set, false otherwise. | 49 // Returns true if the file position could be set, false otherwise. |
| 53 virtual bool SetPosition(int64 position) = 0; | 50 virtual bool SetPosition(int64 position) = 0; |
| 54 | 51 |
| 55 // Returns true and the file size, false if the file size could not be | 52 // Returns true and the file size, false if the file size could not be |
| 56 // retrieved. | 53 // retrieved. |
| 57 virtual bool GetSize(int64* size_out) = 0; | 54 virtual bool GetSize(int64* size_out) = 0; |
| 58 | 55 |
| 59 // Returns false if this protocol supports random seeking. | 56 // Returns false if this protocol supports random seeking. |
| 60 virtual bool IsStreaming() = 0; | 57 virtual bool IsStreaming() = 0; |
| 61 | |
| 62 protected: | |
| 63 virtual ~FFmpegURLProtocol() {} | |
| 64 | |
| 65 private: | |
| 66 DISALLOW_COPY_AND_ASSIGN(FFmpegURLProtocol); | |
| 67 }; | 58 }; |
| 68 | 59 |
| 69 class MEDIA_EXPORT FFmpegGlue { | 60 class MEDIA_EXPORT FFmpegGlue { |
| 70 public: | 61 public: |
| 71 // Returns the singleton instance. | 62 static void InitializeFFmpeg(); |
| 72 static FFmpegGlue* GetInstance(); | |
| 73 | 63 |
| 74 // Adds a FFmpegProtocol to the FFmpeg glue layer and returns a unique string | 64 explicit FFmpegGlue(FFmpegURLProtocol* protocol); |
|
scherkus (not reviewing)
2012/10/02 01:36:35
seems like you may want to mention that the lifeti
DaleCurtis
2012/10/02 01:46:30
Whoops, documented everywhere in the unit tests, b
| |
| 75 // that can be passed to FFmpeg to identify the data source. | 65 ~FFmpegGlue(); |
| 76 std::string AddProtocol(FFmpegURLProtocol* protocol); | |
| 77 | 66 |
| 78 // Removes a FFmpegProtocol from the FFmpeg glue layer. Using strings from | 67 // Opens an AVFormatContext specially prepared to process reads and seeks |
| 79 // previously added FFmpegProtocols will no longer work. | 68 // through the FFmpegURLProtocol provided during construction. |
| 80 void RemoveProtocol(FFmpegURLProtocol* protocol); | 69 bool OpenContext(); |
| 81 | 70 AVFormatContext* format_context() { return format_context_; } |
| 82 // Assigns the FFmpegProtocol identified with by the given key to | |
| 83 // |protocol|, or assigns NULL if no such FFmpegProtocol could be found. | |
| 84 void GetProtocol(const std::string& key, | |
| 85 FFmpegURLProtocol** protocol); | |
| 86 | 71 |
| 87 private: | 72 private: |
| 88 // Only allow Singleton to create and delete FFmpegGlue. | 73 bool open_called_; |
| 89 friend struct DefaultSingletonTraits<FFmpegGlue>; | 74 AVFormatContext* format_context_; |
| 90 FFmpegGlue(); | 75 scoped_ptr_malloc<AVIOContext, ScopedPtrAVFree> avio_context_; |
| 91 virtual ~FFmpegGlue(); | |
| 92 | |
| 93 // Returns the unique key for this data source, which can be passed to | |
| 94 // avformat_open_input() as the filename. | |
| 95 std::string GetProtocolKey(FFmpegURLProtocol* protocol); | |
| 96 | |
| 97 // Mutual exclusion while adding/removing items from the map. | |
| 98 base::Lock lock_; | |
| 99 | |
| 100 // Map between keys and FFmpegProtocol references. | |
| 101 typedef std::map<std::string, FFmpegURLProtocol*> ProtocolMap; | |
| 102 ProtocolMap protocols_; | |
| 103 | |
| 104 friend class FFmpegGlueTest; | |
| 105 static URLProtocol* url_protocol(); | |
| 106 | 76 |
| 107 DISALLOW_COPY_AND_ASSIGN(FFmpegGlue); | 77 DISALLOW_COPY_AND_ASSIGN(FFmpegGlue); |
| 108 }; | 78 }; |
| 109 | 79 |
| 110 } // namespace media | 80 } // namespace media |
| 111 | 81 |
| 112 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_ | 82 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| OLD | NEW |