File: ffmpeg_glue.h

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (96 lines) | stat: -rw-r--r-- 3,389 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// FFmpegGlue is an interface between FFmpeg and Chrome used to proxy FFmpeg's
// read and seek requests to Chrome's internal data structures.  The glue works
// through the AVIO interface provided by FFmpeg.
//
// AVIO works through a special AVIOContext created through avio_alloc_context()
// which is attached to the AVFormatContext used for demuxing.  The AVIO context
// is initialized with read and seek methods which FFmpeg calls when necessary.
//
// During OpenContext() FFmpegGlue will tell FFmpeg to use Chrome's AVIO context
// by passing NULL in for the filename parameter to avformat_open_input().  All
// FFmpeg operations using the configured AVFormatContext will then redirect
// reads and seeks through the glue.
//
// The glue in turn processes those read and seek requests using the
// FFmpegURLProtocol provided during construction.

#ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
#define MEDIA_FILTERS_FFMPEG_GLUE_H_

#include <stdint.h>

#include <memory>

#include "base/logging.h"
#include "base/macros.h"
#include "media/base/container_names.h"
#include "media/base/media_export.h"
#include "media/ffmpeg/ffmpeg_deleters.h"

struct AVFormatContext;
struct AVIOContext;

namespace media {

class MEDIA_EXPORT FFmpegURLProtocol {
 public:
  // Read the given amount of bytes into data, returns the number of bytes read
  // if successful, kReadError otherwise.
  virtual int Read(int size, uint8_t* data) = 0;

  // Returns true and the current file position for this file, false if the
  // file position could not be retrieved.
  virtual bool GetPosition(int64_t* position_out) = 0;

  // Returns true if the file position could be set, false otherwise.
  virtual bool SetPosition(int64_t position) = 0;

  // Returns true and the file size, false if the file size could not be
  // retrieved.
  virtual bool GetSize(int64_t* size_out) = 0;

  // Returns false if this protocol supports random seeking.
  virtual bool IsStreaming() = 0;
};

class MEDIA_EXPORT FFmpegGlue {
 public:
  static void InitializeFFmpeg();

  // See file documentation for usage.  |protocol| must outlive FFmpegGlue.
  explicit FFmpegGlue(FFmpegURLProtocol* protocol);
  ~FFmpegGlue();

  // Opens an AVFormatContext specially prepared to process reads and seeks
  // through the FFmpegURLProtocol provided during construction.
  // |is_local_file| is an optional parameter used for metrics reporting.
  bool OpenContext(bool is_local_file = false);
  AVFormatContext* format_context() { return format_context_; }
  // Returns the container name.
  // Note that it is only available after calling OpenContext.
  container_names::MediaContainerName container() const {
    DCHECK(open_called_);
    return container_;
  }

  // Used on Android to switch to using the native MediaPlayer to play HLS.
  bool detected_hls() { return detected_hls_; }

 private:
  bool open_called_ = false;
  bool detected_hls_ = false;
  AVFormatContext* format_context_ = nullptr;
  std::unique_ptr<AVIOContext, ScopedPtrAVFree> avio_context_;
  container_names::MediaContainerName container_ =
      container_names::CONTAINER_UNKNOWN;

  DISALLOW_COPY_AND_ASSIGN(FFmpegGlue);
};

}  // namespace media

#endif  // MEDIA_FILTERS_FFMPEG_GLUE_H_