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
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// FFmpegGlue is also responsible for initializing FFmpeg, which is done once
22// per process.  Initialization includes: turning off log messages, registering
23// a lock manager, and finally registering all demuxers and codecs.
24
25#ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
26#define MEDIA_FILTERS_FFMPEG_GLUE_H_
27
28#include "base/basictypes.h"
29#include "base/memory/scoped_ptr.h"
30#include "media/base/media_export.h"
31#include "media/ffmpeg/ffmpeg_deleters.h"
32
33struct AVFormatContext;
34struct AVIOContext;
35
36namespace media {
37
38class MEDIA_EXPORT FFmpegURLProtocol {
39 public:
40  // Read the given amount of bytes into data, returns the number of bytes read
41  // if successful, kReadError otherwise.
42  virtual int Read(int size, uint8* data) = 0;
43
44  // Returns true and the current file position for this file, false if the
45  // file position could not be retrieved.
46  virtual bool GetPosition(int64* position_out) = 0;
47
48  // Returns true if the file position could be set, false otherwise.
49  virtual bool SetPosition(int64 position) = 0;
50
51  // Returns true and the file size, false if the file size could not be
52  // retrieved.
53  virtual bool GetSize(int64* size_out) = 0;
54
55  // Returns false if this protocol supports random seeking.
56  virtual bool IsStreaming() = 0;
57};
58
59class MEDIA_EXPORT FFmpegGlue {
60 public:
61  static void InitializeFFmpeg();
62
63  // See file documentation for usage.  |protocol| must outlive FFmpegGlue.
64  explicit FFmpegGlue(FFmpegURLProtocol* protocol);
65  ~FFmpegGlue();
66
67  // Opens an AVFormatContext specially prepared to process reads and seeks
68  // through the FFmpegURLProtocol provided during construction.
69  bool OpenContext();
70  AVFormatContext* format_context() { return format_context_; }
71
72 private:
73  bool open_called_;
74  AVFormatContext* format_context_;
75  scoped_ptr<AVIOContext, ScopedPtrAVFree> avio_context_;
76
77  DISALLOW_COPY_AND_ASSIGN(FFmpegGlue);
78};
79
80}  // namespace media
81
82#endif  // MEDIA_FILTERS_FFMPEG_GLUE_H_
83