Commit 72c2df05 by ziyue

改成异步解码,并修复内存泄露问题

parent 895dc04f
...@@ -10,11 +10,9 @@ ...@@ -10,11 +10,9 @@
#ifndef FFMpegDecoder_H_ #ifndef FFMpegDecoder_H_
#define FFMpegDecoder_H_ #define FFMpegDecoder_H_
#include <string>
#include <memory> #include "Util/TimeTicker.h"
#include <stdexcept> #include "Common/MediaSink.h"
#include "Extension/Frame.h"
#include "Extension/Track.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
...@@ -25,9 +23,6 @@ extern "C" { ...@@ -25,9 +23,6 @@ extern "C" {
} }
#endif #endif
using namespace std;
using namespace mediakit;
class FFmpegFrame { class FFmpegFrame {
public: public:
using Ptr = std::shared_ptr<FFmpegFrame>; using Ptr = std::shared_ptr<FFmpegFrame>;
...@@ -36,14 +31,13 @@ public: ...@@ -36,14 +31,13 @@ public:
~FFmpegFrame(); ~FFmpegFrame();
AVFrame *get() const; AVFrame *get() const;
void fillPicture(AVPixelFormat target_format, int target_width, int target_height);
private: private:
char *_data = nullptr; char *_data = nullptr;
std::shared_ptr<AVFrame> _frame; std::shared_ptr<AVFrame> _frame;
}; };
class FFmpegSwr{ class FFmpegSwr {
public: public:
using Ptr = std::shared_ptr<FFmpegSwr>; using Ptr = std::shared_ptr<FFmpegSwr>;
...@@ -58,33 +52,65 @@ private: ...@@ -58,33 +52,65 @@ private:
int _target_samplerate; int _target_samplerate;
AVSampleFormat _target_format; AVSampleFormat _target_format;
SwrContext *_ctx = nullptr; 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: public:
using Ptr = std::shared_ptr<FFmpegDecoder>; using Ptr = std::shared_ptr<FFmpegDecoder>;
using onDec = function<void(const FFmpegFrame::Ptr &)>; using onDec = function<void(const FFmpegFrame::Ptr &)>;
FFmpegDecoder(const Track::Ptr &track); FFmpegDecoder(const Track::Ptr &track);
~FFmpegDecoder() {} ~FFmpegDecoder();
void inputFrame(const Frame::Ptr &frame) override; 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 setOnDecode(onDec cb);
void flush(); void flush();
const AVCodecContext *getContext() const; const AVCodecContext *getContext() const;
private: private:
void onDecode(const FFmpegFrame::Ptr &frame); 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: private:
bool _do_merger = false;
Ticker _ticker; Ticker _ticker;
onDec _cb; onDec _cb;
FFmpegSwr::Ptr _swr;
ResourcePool<FFmpegFrame> _frame_pool;
std::shared_ptr<AVCodecContext> _context; std::shared_ptr<AVCodecContext> _context;
FrameMerger _merger{FrameMerger::h264_prefix};
}; };
#endif /* FFMpegDecoder_H_ */ #endif /* FFMpegDecoder_H_ */
......
...@@ -83,11 +83,8 @@ int main(int argc, char *argv[]) { ...@@ -83,11 +83,8 @@ int main(int argc, char *argv[]) {
return true; return true;
}); });
}); });
auto merger = std::make_shared<FrameMerger>(FrameMerger::h264_prefix); auto delegate = std::make_shared<FrameWriterInterfaceHelper>([decoder](const Frame::Ptr &frame) {
auto delegate = std::make_shared<FrameWriterInterfaceHelper>([decoder, merger](const Frame::Ptr &frame) { decoder->inputFrame(frame);
merger->inputFrame(frame, [&](uint32_t dts, uint32_t pts, const Buffer::Ptr &buffer, bool have_idr) {
decoder->inputFrame(buffer->data(), buffer->size(), dts, pts);
});
}); });
videoTrack->addDelegate(delegate); videoTrack->addDelegate(delegate);
} }
...@@ -97,8 +94,16 @@ int main(int argc, char *argv[]) { ...@@ -97,8 +94,16 @@ int main(int argc, char *argv[]) {
auto audio_player = std::make_shared<AudioPlayer>(); auto audio_player = std::make_shared<AudioPlayer>();
//FFmpeg解码时已经统一转换为16位整型pcm //FFmpeg解码时已经统一转换为16位整型pcm
audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16); audio_player->setup(audioTrack->getAudioSampleRate(), audioTrack->getAudioChannel(), AUDIO_S16);
decoder->setOnDecode([audio_player](const FFmpegFrame::Ptr &pcm) { FFmpegSwr::Ptr swr;
audio_player->playPCM((const char *) (pcm->get()->data[0]), pcm->get()->linesize[0]);
decoder->setOnDecode([audio_player, swr](const FFmpegFrame::Ptr &frame) mutable{
if (!swr) {
swr = std::make_shared<FFmpegSwr>(AV_SAMPLE_FMT_S16, frame->get()->channels,
frame->get()->channel_layout, frame->get()->sample_rate);
}
auto pcm = swr->inputFrame(frame);
auto len = pcm->get()->nb_samples * pcm->get()->channels * av_get_bytes_per_sample((enum AVSampleFormat)pcm->get()->format);
audio_player->playPCM((const char *) (pcm->get()->data[0]), len);
}); });
auto audio_delegate = std::make_shared<FrameWriterInterfaceHelper>( [decoder](const Frame::Ptr &frame) { auto audio_delegate = std::make_shared<FrameWriterInterfaceHelper>( [decoder](const Frame::Ptr &frame) {
decoder->inputFrame(frame); decoder->inputFrame(frame);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论