srpcgen: Use 'const char*' for string parameters
[chromium-blink-merge.git] / media / filters / ffmpeg_glue.h
blob257915eecb6ad3c1b30e460e700a4a61875f2f9a
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // FFmpegGlue is an adapter for FFmpeg's URLProtocol interface that allows us to
6 // use a DataSource implementation with FFmpeg. For convenience we use FFmpeg's
7 // av_open_input_file function, which analyzes the filename given to it and
8 // automatically initializes the appropriate URLProtocol.
9 //
10 // Since the DataSource is already open by time we call av_open_input_file, we
11 // need a way for av_open_input_file to find the correct DataSource instance.
12 // The solution is to maintain a map of "filenames" to DataSource instances,
13 // where filenames are actually just a unique identifier. For simplicity,
14 // FFmpegGlue is registered as an HTTP handler and generates filenames based on
15 // the memory address of the DataSource, i.e., http://0xc0bf4870. Since there
16 // may be multiple FFmpegDemuxers active at one time, FFmpegGlue is a
17 // thread-safe singleton.
19 // Usage: FFmpegDemuxer adds the DataSource to FFmpegGlue's map and is given a
20 // filename to pass to av_open_input_file. FFmpegDemuxer calls
21 // av_open_input_file with the filename, which results in FFmpegGlue returning
22 // the DataSource as a URLProtocol instance to FFmpeg. Since FFmpegGlue is only
23 // needed for opening files, when av_open_input_file returns FFmpegDemuxer
24 // removes the DataSource from FFmpegGlue's map.
26 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
27 #define MEDIA_FILTERS_FFMPEG_GLUE_H_
29 #include <map>
30 #include <string>
32 #include "base/memory/singleton.h"
33 #include "base/synchronization/lock.h"
34 #include "media/base/media_export.h"
36 struct URLProtocol;
38 namespace media {
40 class MEDIA_EXPORT FFmpegURLProtocol {
41 public:
42 FFmpegURLProtocol() {}
44 virtual ~FFmpegURLProtocol() {}
46 // Read the given amount of bytes into data, returns the number of bytes read
47 // if successful, kReadError otherwise.
48 virtual size_t Read(size_t size, uint8* data) = 0;
50 // Returns true and the current file position for this file, false if the
51 // file position could not be retrieved.
52 virtual bool GetPosition(int64* position_out) = 0;
54 // Returns true if the file position could be set, false otherwise.
55 virtual bool SetPosition(int64 position) = 0;
57 // Returns true and the file size, false if the file size could not be
58 // retrieved.
59 virtual bool GetSize(int64* size_out) = 0;
61 // Returns false if this protocol supports random seeking.
62 virtual bool IsStreaming() = 0;
64 private:
65 DISALLOW_COPY_AND_ASSIGN(FFmpegURLProtocol);
68 class MEDIA_EXPORT FFmpegGlue {
69 public:
70 // Returns the singleton instance.
71 static FFmpegGlue* GetInstance();
73 // Adds a FFmpegProtocol to the FFmpeg glue layer and returns a unique string
74 // that can be passed to FFmpeg to identify the data source.
75 std::string AddProtocol(FFmpegURLProtocol* protocol);
77 // Removes a FFmpegProtocol from the FFmpeg glue layer. Using strings from
78 // previously added FFmpegProtocols will no longer work.
79 void RemoveProtocol(FFmpegURLProtocol* protocol);
81 // Assigns the FFmpegProtocol identified with by the given key to
82 // |protocol|, or assigns NULL if no such FFmpegProtocol could be found.
83 void GetProtocol(const std::string& key,
84 FFmpegURLProtocol** protocol);
86 private:
87 // Only allow Singleton to create and delete FFmpegGlue.
88 friend struct DefaultSingletonTraits<FFmpegGlue>;
89 FFmpegGlue();
90 virtual ~FFmpegGlue();
92 // Returns the unique key for this data source, which can be passed to
93 // av_open_input_file as the filename.
94 std::string GetProtocolKey(FFmpegURLProtocol* protocol);
96 // Mutual exclusion while adding/removing items from the map.
97 base::Lock lock_;
99 // Map between keys and FFmpegProtocol references.
100 typedef std::map<std::string, FFmpegURLProtocol*> ProtocolMap;
101 ProtocolMap protocols_;
103 friend class FFmpegGlueTest;
104 static URLProtocol* url_protocol();
106 DISALLOW_COPY_AND_ASSIGN(FFmpegGlue);
109 } // namespace media
111 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_