Skip to content

Commit

Permalink
Fixed pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
sapharow committed Feb 24, 2017
1 parent 008ae56 commit 2b7dfbf
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 90 deletions.
6 changes: 5 additions & 1 deletion include/capture/fileSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,6 +28,10 @@ namespace fp {
String m_FileName;
FILE* m_File;
size_t m_FileSize;

std::vector<uint8_t> m_Content;
size_t m_Offset = 0;
bool m_Preload;
};

}
Expand Down
3 changes: 0 additions & 3 deletions include/transcode/coreIVVideoTranscoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
33 changes: 26 additions & 7 deletions src/capture/fileSource.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <capture/fileSource.h>
#include <vector>
#include <memory.h>

namespace fp {
namespace cap {

FileSource::FileSource(const String& fileName)
FileSource::FileSource(const String& fileName, bool preload)
: m_FileName(fileName)
, m_Preload(preload)
{
}

Expand All @@ -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<size_t>(m_Content.size() - m_Offset, size);
memcpy(buffer, m_Content.data() + m_Offset, nBytesToRead);
m_Offset += nBytesToRead;
return nBytesToRead;
}
}
}
return 0;
}

void FileSource::doStart() {
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
76 changes: 2 additions & 74 deletions src/transcode/coreIVVideoTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<omx::Tunnel>();

// 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);

Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -390,27 +363,16 @@ 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);

m_PipelineSet = true;
}
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");
Expand All @@ -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() {
Expand All @@ -483,9 +414,6 @@ namespace fp {
}
}

if (m_File) {
fclose(m_File);
}
m_Tunnel = nullptr;
}

Expand Down

0 comments on commit 2b7dfbf

Please sign in to comment.