1 // Copyright 2012 The Chromium Authors
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 interface between FFmpeg and Chrome used to proxy FFmpeg's
6 // read and seek requests to Chrome's internal data structures. The glue works
7 // through the AVIO interface provided by FFmpeg.
9 // AVIO works through a special AVIOContext created through avio_alloc_context()
10 // which is attached to the AVFormatContext used for demuxing. The AVIO context
11 // is initialized with read and seek methods which FFmpeg calls when necessary.
13 // During OpenContext() FFmpegGlue will tell FFmpeg to use Chrome's AVIO context
14 // by passing NULL in for the filename parameter to avformat_open_input(). All
15 // FFmpeg operations using the configured AVFormatContext will then redirect
16 // reads and seeks through the glue.
18 // The glue in turn processes those read and seek requests using the
19 // FFmpegURLProtocol provided during construction.
21 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
22 #define MEDIA_FILTERS_FFMPEG_GLUE_H_
28 #include "base/check.h"
29 #include "base/memory/raw_ptr_exclusion.h"
30 #include "media/base/container_names.h"
31 #include "media/base/media_export.h"
32 #include "media/ffmpeg/ffmpeg_deleters.h"
34 struct AVFormatContext;
39 class MEDIA_EXPORT FFmpegURLProtocol {
41 // Read the given amount of bytes into data, returns the number of bytes read
42 // if successful, kReadError otherwise.
43 virtual int Read(int size, uint8_t* data) = 0;
45 // Returns true and the current file position for this file, false if the
46 // file position could not be retrieved.
47 virtual bool GetPosition(int64_t* position_out) = 0;
49 // Returns true if the file position could be set, false otherwise.
50 virtual bool SetPosition(int64_t position) = 0;
52 // Returns true and the file size, false if the file size could not be
54 virtual bool GetSize(int64_t* size_out) = 0;
56 // Returns false if this protocol supports random seeking.
57 virtual bool IsStreaming() = 0;
60 class MEDIA_EXPORT FFmpegGlue {
62 // See file documentation for usage. |protocol| must outlive FFmpegGlue.
63 explicit FFmpegGlue(FFmpegURLProtocol* protocol);
65 FFmpegGlue(const FFmpegGlue&) = delete;
66 FFmpegGlue& operator=(const FFmpegGlue&) = delete;
70 // Returns the list of allowed decoders for audio/video respectively.
71 static const char* GetAllowedAudioDecoders();
72 static const char* GetAllowedVideoDecoders();
74 // Opens an AVFormatContext specially prepared to process reads and seeks
75 // through the FFmpegURLProtocol provided during construction.
76 // |is_local_file| is an optional parameter used for metrics reporting.
77 bool OpenContext(bool is_local_file = false);
78 AVFormatContext* format_context() { return format_context_; }
79 // Returns the container name.
80 // Note that it is only available after calling OpenContext.
81 container_names::MediaContainerName container() const {
86 // Used on Android to switch to using the native MediaPlayer to play HLS.
87 bool detected_hls() { return detected_hls_; }
90 bool open_called_ = false;
91 bool detected_hls_ = false;
93 // This field is not a raw_ptr<> because it was filtered by the rewriter for:
95 RAW_PTR_EXCLUSION AVFormatContext* format_context_ = nullptr;
96 std::unique_ptr<AVIOContext, ScopedPtrAVFree> avio_context_;
97 container_names::MediaContainerName container_ =
98 container_names::MediaContainerName::kContainerUnknown;
103 #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_