Revert "[M120 Migration]Fix for crash during chrome exit"
[platform/framework/web/chromium-efl.git] / media / filters / ffmpeg_glue.h
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.
4
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.
8 //
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.
12 //
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.
17 //
18 // The glue in turn processes those read and seek requests using the
19 // FFmpegURLProtocol provided during construction.
20
21 #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
22 #define MEDIA_FILTERS_FFMPEG_GLUE_H_
23
24 #include <stdint.h>
25
26 #include <memory>
27
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"
33
34 struct AVFormatContext;
35 struct AVIOContext;
36
37 namespace media {
38
39 class MEDIA_EXPORT FFmpegURLProtocol {
40  public:
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;
44
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;
48
49   // Returns true if the file position could be set, false otherwise.
50   virtual bool SetPosition(int64_t position) = 0;
51
52   // Returns true and the file size, false if the file size could not be
53   // retrieved.
54   virtual bool GetSize(int64_t* size_out) = 0;
55
56   // Returns false if this protocol supports random seeking.
57   virtual bool IsStreaming() = 0;
58 };
59
60 class MEDIA_EXPORT FFmpegGlue {
61  public:
62   // See file documentation for usage.  |protocol| must outlive FFmpegGlue.
63   explicit FFmpegGlue(FFmpegURLProtocol* protocol);
64
65   FFmpegGlue(const FFmpegGlue&) = delete;
66   FFmpegGlue& operator=(const FFmpegGlue&) = delete;
67
68   ~FFmpegGlue();
69
70   // Returns the list of allowed decoders for audio/video respectively.
71   static const char* GetAllowedAudioDecoders();
72   static const char* GetAllowedVideoDecoders();
73
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 {
82     DCHECK(open_called_);
83     return container_;
84   }
85
86   // Used on Android to switch to using the native MediaPlayer to play HLS.
87   bool detected_hls() { return detected_hls_; }
88
89  private:
90   bool open_called_ = false;
91   bool detected_hls_ = false;
92
93   // This field is not a raw_ptr<> because it was filtered by the rewriter for:
94   // #addr-of
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;
99 };
100
101 }  // namespace media
102
103 #endif  // MEDIA_FILTERS_FFMPEG_GLUE_H_