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 adapter for FFmpeg's URLProtocol interface that allows us to |
|
Ami GONE FROM CHROMIUM
2012/09/27 03:24:45
overhaul file-level comment
DaleCurtis
2012/10/02 01:24:23
Done.
| |
| 6 // use a DataSource implementation with FFmpeg. For convenience we use FFmpeg's | 6 // use a DataSource implementation with FFmpeg. For convenience we use FFmpeg's |
| 7 // avformat_open_input() function, which analyzes the filename given to it and | 7 // avformat_open_input() function, which analyzes the filename given to it and |
| 8 // automatically initializes the appropriate URLProtocol. | 8 // automatically initializes the appropriate URLProtocol. |
| 9 // | 9 // |
| 10 // Since the DataSource is already open by time we call avformat_open_input(), | 10 // Since the DataSource is already open by time we call avformat_open_input(), |
| 11 // we need a way for avformat_open_input() to find the correct DataSource | 11 // we need a way for avformat_open_input() to find the correct DataSource |
| 12 // instance. The solution is to maintain a map of "filenames" to DataSource | 12 // instance. The solution is to maintain a map of "filenames" to DataSource |
| 13 // instances, where filenames are actually just a unique identifier. For | 13 // instances, where filenames are actually just a unique identifier. For |
| 14 // simplicity, FFmpegGlue is registered as an HTTP handler and generates | 14 // simplicity, FFmpegGlue is registered as an HTTP handler and generates |
| 15 // filenames based on the memory address of the DataSource, i.e., | 15 // filenames based on the memory address of the DataSource, i.e., |
| 16 // http://0xc0bf4870. Since there may be multiple FFmpegDemuxers active at one | 16 // http://0xc0bf4870. Since there may be multiple FFmpegDemuxers active at one |
| 17 // time, FFmpegGlue is a thread-safe singleton. | 17 // time, FFmpegGlue is a thread-safe singleton. |
| 18 // | 18 // |
| 19 // Usage: FFmpegDemuxer adds the DataSource to FFmpegGlue's map and is given a | 19 // Usage: FFmpegDemuxer adds the DataSource to FFmpegGlue's map and is given a |
| 20 // filename to pass to avformat_open_input(). FFmpegDemuxer calls | 20 // filename to pass to avformat_open_input(). FFmpegDemuxer calls |
| 21 // avformat_open_input() with the filename, which results in FFmpegGlue | 21 // avformat_open_input() with the filename, which results in FFmpegGlue |
| 22 // returning the DataSource as a URLProtocol instance to FFmpeg. Since | 22 // returning the DataSource as a URLProtocol instance to FFmpeg. Since |
| 23 // FFmpegGlue is only needed for opening files, when avformat_open_input() | 23 // FFmpegGlue is only needed for opening files, when avformat_open_input() |
| 24 // returns FFmpegDemuxer removes the DataSource from FFmpegGlue's map. | 24 // returns FFmpegDemuxer removes the DataSource from FFmpegGlue's map. |
| 25 | 25 |
| 26 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_ | 26 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| 27 #define MEDIA_FILTERS_FFMPEG_GLUE_H_ | 27 #define MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| 28 | 28 |
| 29 #include <map> | 29 #include "base/basictypes.h" |
| 30 #include <string> | 30 #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" | 31 #include "media/base/media_export.h" |
| 35 | 32 |
| 36 struct URLProtocol; | 33 struct AVFormatContext; |
| 34 struct AVIOContext; | |
| 37 | 35 |
| 38 namespace media { | 36 namespace media { |
| 39 | 37 |
| 38 class ScopedPtrAVFree; | |
| 39 | |
| 40 class MEDIA_EXPORT FFmpegURLProtocol { | 40 class MEDIA_EXPORT FFmpegURLProtocol { |
|
Ami GONE FROM CHROMIUM
2012/09/27 03:24:45
wants renaming?
| |
| 41 public: | 41 public: |
| 42 FFmpegURLProtocol() {} | |
| 43 | |
| 44 // Read the given amount of bytes into data, returns the number of bytes read | 42 // Read the given amount of bytes into data, returns the number of bytes read |
| 45 // if successful, kReadError otherwise. | 43 // if successful, kReadError otherwise. |
| 46 virtual size_t Read(size_t size, uint8* data) = 0; | 44 virtual int Read(int size, uint8* data) = 0; |
| 47 | 45 |
| 48 // Returns true and the current file position for this file, false if the | 46 // Returns true and the current file position for this file, false if the |
| 49 // file position could not be retrieved. | 47 // file position could not be retrieved. |
| 50 virtual bool GetPosition(int64* position_out) = 0; | 48 virtual bool GetPosition(int64* position_out) = 0; |
| 51 | 49 |
| 52 // Returns true if the file position could be set, false otherwise. | 50 // Returns true if the file position could be set, false otherwise. |
| 53 virtual bool SetPosition(int64 position) = 0; | 51 virtual bool SetPosition(int64 position) = 0; |
| 54 | 52 |
| 55 // Returns true and the file size, false if the file size could not be | 53 // Returns true and the file size, false if the file size could not be |
| 56 // retrieved. | 54 // retrieved. |
| 57 virtual bool GetSize(int64* size_out) = 0; | 55 virtual bool GetSize(int64* size_out) = 0; |
| 58 | 56 |
| 59 // Returns false if this protocol supports random seeking. | 57 // Returns false if this protocol supports random seeking. |
| 60 virtual bool IsStreaming() = 0; | 58 virtual bool IsStreaming() = 0; |
| 61 | |
| 62 protected: | |
| 63 virtual ~FFmpegURLProtocol() {} | |
| 64 | |
| 65 private: | |
| 66 DISALLOW_COPY_AND_ASSIGN(FFmpegURLProtocol); | |
| 67 }; | 59 }; |
| 68 | 60 |
| 69 class MEDIA_EXPORT FFmpegGlue { | 61 class MEDIA_EXPORT FFmpegGlue { |
| 70 public: | 62 public: |
| 71 // Returns the singleton instance. | 63 static void InitializeFFmpeg(); |
| 72 static FFmpegGlue* GetInstance(); | |
| 73 | 64 |
| 74 // Adds a FFmpegProtocol to the FFmpeg glue layer and returns a unique string | 65 explicit FFmpegGlue(FFmpegURLProtocol* protocol); |
| 75 // that can be passed to FFmpeg to identify the data source. | 66 virtual ~FFmpegGlue(); |
|
Ami GONE FROM CHROMIUM
2012/09/27 03:24:45
why virtual?
DaleCurtis
2012/10/02 01:24:23
Done.
| |
| 76 std::string AddProtocol(FFmpegURLProtocol* protocol); | |
| 77 | 67 |
| 78 // Removes a FFmpegProtocol from the FFmpeg glue layer. Using strings from | 68 int OpenContext(); |
|
Ami GONE FROM CHROMIUM
2012/09/27 03:24:45
why not return a bool for success?
(here and elsew
DaleCurtis
2012/10/02 01:24:23
Done.
| |
| 79 // previously added FFmpegProtocols will no longer work. | 69 AVFormatContext* format_context() { return format_context_; } |
| 80 void RemoveProtocol(FFmpegURLProtocol* protocol); | |
| 81 | |
| 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 | 70 |
| 87 private: | 71 private: |
| 88 // Only allow Singleton to create and delete FFmpegGlue. | 72 bool open_called_; |
| 89 friend struct DefaultSingletonTraits<FFmpegGlue>; | 73 AVFormatContext* format_context_; |
| 90 FFmpegGlue(); | 74 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 | |
| 107 DISALLOW_COPY_AND_ASSIGN(FFmpegGlue); | |
| 108 }; | 75 }; |
| 109 | 76 |
| 110 } // namespace media | 77 } // namespace media |
| 111 | 78 |
| 112 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_ | 79 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_ |
| OLD | NEW |