decoder.h 2.31 KB
Newer Older
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
// Copyright 2004-present Facebook. All Rights Reserved.

#pragma once

#include "seekable_buffer.h"
#include "stream.h"

namespace ffmpeg {

/**
 * Class uses FFMPEG library to decode media streams.
 * Media bytes can be explicitly provided through read-callback
 * or fetched internally by FFMPEG library
 */
class Decoder : public MediaDecoder {
 public:
  Decoder();
  ~Decoder() override;

  // MediaDecoder overrides
  bool init(const DecoderParameters& params, DecoderInCallback&& in) override;
  int decode_all(const DecoderOutCallback& callback) override;
  void shutdown() override;
  void interrupt() override;

 protected:
  // function does actual work, derived class calls it in working thread
  // periodically. On success method returns 0, ENOADATA on EOF and error on
  // unrecoverable error.
  int getBytes(size_t workingTimeInMs = 100);

  // Derived class must override method and consume the provided message
  virtual void push(DecoderOutputMessage&& buffer) = 0;

  // Fires on init call
  virtual void onInit() {}

 public:
  // C-style FFMPEG API requires C/static methods for callbacks
  static void logFunction(void* avcl, int level, const char* cfmt, va_list vl);
  static int shutdownFunction(void* ctx);
  static int readFunction(void* opaque, uint8_t* buf, int size);
  static int64_t seekFunction(void* opaque, int64_t offset, int whence);
  // can be called by any classes or API
  static void initOnce();

  int* getPrintPrefix() {
    return &printPrefix;
  }

 private:
  // mark below function for a proper invocation
  virtual bool enableLogLevel(int level) const;
  virtual void logCallback(int level, const std::string& message);
  virtual int readCallback(uint8_t* buf, int size);
  virtual int64_t seekCallback(int64_t offset, int whence);
  virtual int shutdownCallback();

  bool activateStreams();
  Stream* findByIndex(int streamIndex) const;
  Stream* findByType(const MediaFormat& format) const;
  int processPacket(Stream* stream, AVPacket* packet);
  void flushStreams();
  void cleanUp();

 private:
  DecoderParameters params_;
  SeekableBuffer seekableBuffer_;
  int printPrefix{1};

  std::atomic<bool> interrupted_{false};
  AVFormatContext* inputCtx_{nullptr};
  AVIOContext* avioCtx_{nullptr};
  std::unordered_map<ssize_t, std::unique_ptr<Stream>> streams_;
  bool outOfRange_{false};
};
} // namespace ffmpeg