ffmpeg_glue.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // FFmpegGlue is an interface between FFmpeg and Chrome used to proxy FFmpeg's
  5. // read and seek requests to Chrome's internal data structures. The glue works
  6. // through the AVIO interface provided by FFmpeg.
  7. //
  8. // AVIO works through a special AVIOContext created through avio_alloc_context()
  9. // which is attached to the AVFormatContext used for demuxing. The AVIO context
  10. // is initialized with read and seek methods which FFmpeg calls when necessary.
  11. //
  12. // During OpenContext() FFmpegGlue will tell FFmpeg to use Chrome's AVIO context
  13. // by passing NULL in for the filename parameter to avformat_open_input(). All
  14. // FFmpeg operations using the configured AVFormatContext will then redirect
  15. // reads and seeks through the glue.
  16. //
  17. // The glue in turn processes those read and seek requests using the
  18. // FFmpegURLProtocol provided during construction.
  19. #ifndef MEDIA_FILTERS_FFMPEG_GLUE_H_
  20. #define MEDIA_FILTERS_FFMPEG_GLUE_H_
  21. #include <stdint.h>
  22. #include <memory>
  23. #include "base/check.h"
  24. #include "media/base/container_names.h"
  25. #include "media/base/media_export.h"
  26. #include "media/ffmpeg/ffmpeg_deleters.h"
  27. struct AVFormatContext;
  28. struct AVIOContext;
  29. namespace media {
  30. class MEDIA_EXPORT FFmpegURLProtocol {
  31. public:
  32. // Read the given amount of bytes into data, returns the number of bytes read
  33. // if successful, kReadError otherwise.
  34. virtual int Read(int size, uint8_t* data) = 0;
  35. // Returns true and the current file position for this file, false if the
  36. // file position could not be retrieved.
  37. virtual bool GetPosition(int64_t* position_out) = 0;
  38. // Returns true if the file position could be set, false otherwise.
  39. virtual bool SetPosition(int64_t position) = 0;
  40. // Returns true and the file size, false if the file size could not be
  41. // retrieved.
  42. virtual bool GetSize(int64_t* size_out) = 0;
  43. // Returns false if this protocol supports random seeking.
  44. virtual bool IsStreaming() = 0;
  45. };
  46. class MEDIA_EXPORT FFmpegGlue {
  47. public:
  48. // See file documentation for usage. |protocol| must outlive FFmpegGlue.
  49. explicit FFmpegGlue(FFmpegURLProtocol* protocol);
  50. FFmpegGlue(const FFmpegGlue&) = delete;
  51. FFmpegGlue& operator=(const FFmpegGlue&) = delete;
  52. ~FFmpegGlue();
  53. // Opens an AVFormatContext specially prepared to process reads and seeks
  54. // through the FFmpegURLProtocol provided during construction.
  55. // |is_local_file| is an optional parameter used for metrics reporting.
  56. bool OpenContext(bool is_local_file = false);
  57. AVFormatContext* format_context() { return format_context_; }
  58. // Returns the container name.
  59. // Note that it is only available after calling OpenContext.
  60. container_names::MediaContainerName container() const {
  61. DCHECK(open_called_);
  62. return container_;
  63. }
  64. // Used on Android to switch to using the native MediaPlayer to play HLS.
  65. bool detected_hls() { return detected_hls_; }
  66. private:
  67. bool open_called_ = false;
  68. bool detected_hls_ = false;
  69. AVFormatContext* format_context_ = nullptr;
  70. std::unique_ptr<AVIOContext, ScopedPtrAVFree> avio_context_;
  71. container_names::MediaContainerName container_ =
  72. container_names::CONTAINER_UNKNOWN;
  73. };
  74. } // namespace media
  75. #endif // MEDIA_FILTERS_FFMPEG_GLUE_H_