From 2b7dfbf246ae8ff8b4e5a2c43582bed77dd4d0de Mon Sep 17 00:00:00 2001 From: Iskandar Safarov Date: Fri, 24 Feb 2017 11:45:32 +0000 Subject: [PATCH] Fixed pipeline --- include/capture/fileSource.h | 6 +- include/transcode/coreIVVideoTranscoder.h | 3 - src/capture/fileSource.cpp | 33 +++++++--- src/main.cpp | 10 +-- src/transcode/coreIVVideoTranscoder.cpp | 76 +---------------------- 5 files changed, 38 insertions(+), 90 deletions(-) diff --git a/include/capture/fileSource.h b/include/capture/fileSource.h index 2315c06..ca5f01a 100644 --- a/include/capture/fileSource.h +++ b/include/capture/fileSource.h @@ -17,7 +17,7 @@ namespace fp { * Create file source * @param[in] fileName File source name */ - FileSource(const String& fileName); + FileSource(const String& fileName, bool preload); ~FileSource() override; protected: @@ -28,6 +28,10 @@ namespace fp { String m_FileName; FILE* m_File; size_t m_FileSize; + + std::vector m_Content; + size_t m_Offset = 0; + bool m_Preload; }; } diff --git a/include/transcode/coreIVVideoTranscoder.h b/include/transcode/coreIVVideoTranscoder.h index 17ba797..28c61dc 100644 --- a/include/transcode/coreIVVideoTranscoder.h +++ b/include/transcode/coreIVVideoTranscoder.h @@ -61,13 +61,10 @@ namespace fp { omx::TunnelRef m_Tunnel; omx::ComponentRef m_Decoder; omx::ComponentRef m_Encoder; - omx::ComponentRef m_Scheduler; - omx::ComponentRef m_Clock; bool m_PipelineSet = false; bool m_FirstPacket = false; bool m_PipelineNeedsToSetup = false; - FILE* m_File = nullptr; std::mutex m_Mutex; bool m_EncoderSet = false; }; diff --git a/src/capture/fileSource.cpp b/src/capture/fileSource.cpp index 66388f1..af0fe9b 100644 --- a/src/capture/fileSource.cpp +++ b/src/capture/fileSource.cpp @@ -1,11 +1,13 @@ #include #include +#include namespace fp { namespace cap { - FileSource::FileSource(const String& fileName) + FileSource::FileSource(const String& fileName, bool preload) : m_FileName(fileName) + , m_Preload(preload) { } @@ -17,14 +19,24 @@ namespace fp { } size_t FileSource::readDataInto(uint8_t* buffer, size_t size) { - if (ferror(m_File)) { - throw std::runtime_error("Error reading from file"); - } - if (feof(m_File)) { - return 0; + if (m_File) { + if (ferror(m_File)) { + throw std::runtime_error("Error reading from file"); + } + if (!feof(m_File)) { + return fread(buffer, 1, (size / 188) * 188, m_File); + } } else { - return fread(buffer, 1, (size / 188) * 188, m_File); + if (m_Preload) { + if (m_Content.size() > m_Offset) { + size_t nBytesToRead = std::min(m_Content.size() - m_Offset, size); + memcpy(buffer, m_Content.data() + m_Offset, nBytesToRead); + m_Offset += nBytesToRead; + return nBytesToRead; + } + } } + return 0; } void FileSource::doStart() { @@ -54,6 +66,13 @@ namespace fp { throw std::runtime_error("File has incorrect format"); } fseek(m_File, offset, SEEK_SET); + + if (m_Preload) { + m_Content.resize(m_FileSize - offset); + fread(m_Content.data(), 1, m_Content.size(), m_File); + fclose(m_File); + m_File = nullptr; + } } void FileSource::doStop() { diff --git a/src/main.cpp b/src/main.cpp index e855da5..7b7ac8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,14 +50,14 @@ class MyEncodedStream : public fp::VideoStream { break; } -// m_File = fopen(buffer, "wb"); + m_File = fopen(buffer, "wb"); } void supplyFrame(const uint8_t* data, size_t size, Metadata* metadata) override { if (m_File) { fwrite(data, 1, size, m_File); } - } + } private: FILE* m_File = nullptr; @@ -149,7 +149,7 @@ class MyVideoStream : public fp::VideoStream { class MySource : public fp::cap::FileSource { public: - MySource(const fp::String& fname) : fp::cap::FileSource(fname) {} + MySource(const fp::String& fname) : fp::cap::FileSource(fname, true) {} // MySource(const fp::String& fname) : fp::cap::DVBSource(0,0,0) {} fp::AudioStreamRef createAudioStream(fp::AudioStreamMeta* meta) override { @@ -175,8 +175,8 @@ class MySource : public fp::cap::FileSource { int main(int argc, char **argv) { - MySource src("mpt-smart-travels-classical-clip.dvb"); -// MySource src("football.dvb"); +// MySource src("mpt-smart-travels-classical-clip.dvb"); + MySource src("football.dvb"); // fp::cap::DVBSource src(DVB_ADAPTER, DVB_FRONTEND, DVB_DEMUX); src.start(); diff --git a/src/transcode/coreIVVideoTranscoder.cpp b/src/transcode/coreIVVideoTranscoder.cpp index 38a0585..d73bf0b 100644 --- a/src/transcode/coreIVVideoTranscoder.cpp +++ b/src/transcode/coreIVVideoTranscoder.cpp @@ -54,9 +54,7 @@ namespace fp { CoreIVVideoTranscoder::CoreIVVideoTranscoder(StreamType inputType, const VideoStreamRef& output) : VideoTranscoder(inputType, output) - { - m_File = fopen("test.h264", "wb"); - } + { } static String getCompressionFormatStr(OMX_VIDEO_CODINGTYPE coding) { switch (coding) { @@ -237,52 +235,30 @@ namespace fp { } void CoreIVVideoTranscoder::onFillBufferDone(const omx::ComponentRef& component, OMX_BUFFERHEADERTYPE* buf) { - printf("!!!! Callback onFillBufferDone (%s)\n", component->name().c_str()); if (buf) { if (buf->nFilledLen) { - printf("\tencoder: filled output buffer with %u bytes of data\n", buf->nFilledLen); - fwrite(buf->pBuffer, 1, buf->nFilledLen, m_File); + output()->supplyFrame(buf->pBuffer, buf->nFilledLen); buf->nFilledLen = 0; - } else { - printf("Got empty buffer!!!\n"); } } } void CoreIVVideoTranscoder::onEmptyBufferDone(const omx::ComponentRef& component, OMX_BUFFERHEADERTYPE* pBuffer) { - printf("!!!! Callback onEmptyBufferDone (%s)\n", component->name().c_str()); } bool CoreIVVideoTranscoder::init() { // Destroy previous tunnel m_Decoder = nullptr; m_Encoder = nullptr; - m_Clock = nullptr; -// m_Scheduler = nullptr; m_Tunnel = nullptr; // Create components list m_Decoder = createDecoderComponent(true, false); m_Encoder = createEncoderComponent(false, true); - m_Clock = createClockComponent(); - m_Scheduler = createSchedulerComponent(); // Create tunnel m_Tunnel = std::make_shared(); - // Configure clock - OMX_TIME_CONFIG_CLOCKSTATETYPE cstate; - memset(&cstate, 0, sizeof(cstate)); - cstate.nSize = sizeof(cstate); - cstate.nVersion.nVersion = OMX_VERSION; - cstate.eState = OMX_TIME_ClockStateWaitingForStartTime; - cstate.nWaitMask = 1; - m_Clock->setParameter(OMX_IndexConfigTimeClockState, &cstate); - - // Setup clock -> scheduler tunnel - m_Tunnel->set(m_Clock, 80, m_Scheduler, 12); - m_Clock->changeState(OMX_StateExecuting); - // Setup decoder m_Decoder->changeState(OMX_StateIdle); @@ -323,8 +299,6 @@ namespace fp { if (!m_Decoder) { throw std::runtime_error("Decoder is not initialised"); } - printf("--- Frame supply start ---------------\n"); -#if 1 if (m_PipelineNeedsToSetup) { if (!m_PipelineSet) { printf("Setup pipeline\n"); @@ -341,7 +315,6 @@ namespace fp { def.format.video.nFrameWidth, def.format.video.nFrameHeight, def.format.video.xFramerate >> 16, def.format.video.xFramerate & 0xffff, getCompressionFormatStr(def.format.video.eCompressionFormat).c_str(), getColorFormatStr(def.format.video.eColorFormat).c_str()); -// m_Encoder->changeState(OMX_StateIdle); printf("Get encoder input format\n"); // Get input settings from encoder OMX_VIDEO_PORTDEFINITIONTYPE decoderVideoDef = def.format.video; @@ -390,15 +363,7 @@ namespace fp { def.nPortIndex = 201; m_Encoder->getParameter(OMX_IndexParamPortDefinition, &def); -#if 1 m_Tunnel->set(m_Decoder, 131, m_Encoder, 200, 1000); -#else - // Set Scheduler -> Encoder - m_Tunnel->set(m_Decoder, 131, m_Scheduler, 10); - m_Scheduler->changeState(OMX_StateExecuting); - - m_Tunnel->set(m_Scheduler, 11, m_Encoder, 200, 1000); -#endif m_Encoder->enablePortBuffers(201); m_Encoder->changeState(OMX_StateExecuting); @@ -406,11 +371,8 @@ namespace fp { } m_PipelineNeedsToSetup = false; } -#endif - printf("\tdecoder: getting input buffer\n"); OMX_BUFFERHEADERTYPE* buf = m_Decoder->getInputBuffer(130, true); - printf("\tdecoder: got input buffer %p\n", buf); if (buf) { if (buf->nAllocLen < size) { throw std::runtime_error("Buffer size is less than frame size"); @@ -428,45 +390,14 @@ namespace fp { buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN; } - printf("\tdecoder: emptying input buffer\n"); m_Decoder->emptyBuffer(buf); - printf("\tdecoder: emptied input buffer\n"); } if (m_EncoderSet) { while ( (buf = m_Encoder->getOutputBuffer(201, false)) != nullptr ) { - printf("\tencoder: filling output buffer\n"); - m_Encoder->fillBuffer(buf); - printf("\tencoder: filled output buffer\n"); - } - } - -#if 1 -#endif - -#if 0 -// if (m_PipelineSet) - { - printf("\tencoder: getting buffer\n"); - buf = m_Encoder->getOutputBuffer(201, false); - if (buf) { - printf("\tencoder: got buffer %p\n", buf); - printf("\tencoder: filling buffer\n"); m_Encoder->fillBuffer(buf); - printf("\tencoder: filled buffer\n"); - } else { - printf("\tencoder: got null buffer, skipped\n"); - } - /* - if (buf) { - } else { - printf("\tencoder: got null buffer\n"); } - */ } -#endif - static int i = 0; - printf("... Supplied frame %d ................\n", i++); } CoreIVVideoTranscoder::~CoreIVVideoTranscoder() { @@ -483,9 +414,6 @@ namespace fp { } } - if (m_File) { - fclose(m_File); - } m_Tunnel = nullptr; }