mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-03-16 11:00:52 +08:00
改成异步解码,并修复内存泄露问题
This commit is contained in:
@@ -10,11 +10,9 @@
|
||||
|
||||
#ifndef FFMpegDecoder_H_
|
||||
#define FFMpegDecoder_H_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include "Extension/Frame.h"
|
||||
#include "Extension/Track.h"
|
||||
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Common/MediaSink.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -25,9 +23,6 @@ extern "C" {
|
||||
}
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
using namespace mediakit;
|
||||
|
||||
class FFmpegFrame {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FFmpegFrame>;
|
||||
@@ -36,14 +31,13 @@ public:
|
||||
~FFmpegFrame();
|
||||
|
||||
AVFrame *get() const;
|
||||
void fillPicture(AVPixelFormat target_format, int target_width, int target_height);
|
||||
|
||||
private:
|
||||
char *_data = nullptr;
|
||||
std::shared_ptr<AVFrame> _frame;
|
||||
};
|
||||
|
||||
class FFmpegSwr{
|
||||
class FFmpegSwr {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FFmpegSwr>;
|
||||
|
||||
@@ -58,33 +52,65 @@ private:
|
||||
int _target_samplerate;
|
||||
AVSampleFormat _target_format;
|
||||
SwrContext *_ctx = nullptr;
|
||||
ResourcePool<FFmpegFrame> _frame_pool;
|
||||
};
|
||||
|
||||
class FFmpegDecoder : public FrameWriterInterface {
|
||||
class TaskManager {
|
||||
public:
|
||||
TaskManager() = default;
|
||||
~TaskManager();
|
||||
|
||||
protected:
|
||||
void startThread(const string &name);
|
||||
void stopThread();
|
||||
|
||||
void addEncodeTask(function<void()> task);
|
||||
void addDecodeTask(bool key_frame, function<void()> task);
|
||||
bool isEnabled() const;
|
||||
|
||||
private:
|
||||
void onThreadRun(const string &name);
|
||||
void pushExit();
|
||||
|
||||
private:
|
||||
class ThreadExitException : public std::runtime_error {
|
||||
public:
|
||||
ThreadExitException() : std::runtime_error("exit") {}
|
||||
~ThreadExitException() = default;
|
||||
};
|
||||
|
||||
private:
|
||||
bool _decode_drop_start = false;
|
||||
bool _exit = false;
|
||||
mutex _task_mtx;
|
||||
semaphore _sem;
|
||||
List<function<void()> > _task;
|
||||
std::shared_ptr<thread> _thread;
|
||||
};
|
||||
|
||||
class FFmpegDecoder : public FrameWriterInterface, private TaskManager {
|
||||
public:
|
||||
using Ptr = std::shared_ptr<FFmpegDecoder>;
|
||||
using onDec = function<void(const FFmpegFrame::Ptr &)>;
|
||||
|
||||
FFmpegDecoder(const Track::Ptr &track);
|
||||
~FFmpegDecoder() {}
|
||||
~FFmpegDecoder();
|
||||
|
||||
void inputFrame(const Frame::Ptr &frame) override;
|
||||
void inputFrame(const char *data, size_t size, uint32_t dts, uint32_t pts);
|
||||
|
||||
void setOnDecode(onDec cb);
|
||||
void flush();
|
||||
const AVCodecContext *getContext() const;
|
||||
|
||||
private:
|
||||
void onDecode(const FFmpegFrame::Ptr &frame);
|
||||
void inputFrame_l(const Frame::Ptr &frame);
|
||||
void decodeFrame(const char *data, size_t size, uint32_t dts, uint32_t pts);
|
||||
|
||||
private:
|
||||
bool _do_merger = false;
|
||||
Ticker _ticker;
|
||||
onDec _cb;
|
||||
FFmpegSwr::Ptr _swr;
|
||||
ResourcePool<FFmpegFrame> _frame_pool;
|
||||
std::shared_ptr<AVCodecContext> _context;
|
||||
FrameMerger _merger{FrameMerger::h264_prefix};
|
||||
};
|
||||
|
||||
#endif /* FFMpegDecoder_H_ */
|
||||
|
||||
Reference in New Issue
Block a user