From 09a0a0946e4ac28d7191af5f030309d580d5a05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Tue, 30 Jul 2019 22:27:08 +0200 Subject: [PATCH] Remove malloc in chiaki_frame_processor_fec() --- lib/include/chiaki/frameprocessor.h | 8 +++++++- lib/src/frameprocessor.c | 21 +++++++-------------- lib/src/videoreceiver.c | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/include/chiaki/frameprocessor.h b/lib/include/chiaki/frameprocessor.h index 957a187..aae7172 100644 --- a/lib/include/chiaki/frameprocessor.h +++ b/lib/include/chiaki/frameprocessor.h @@ -48,7 +48,8 @@ typedef struct chiaki_frame_processor_t typedef enum chiaki_frame_flush_result_t { CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_SUCCESS = 0, CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FEC_SUCCESS = 1, - CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FAILED = 2 + CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FEC_FAILED = 2, + CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FAILED = 3 } ChiakiFrameProcessorFlushResult; CHIAKI_EXPORT void chiaki_frame_processor_init(ChiakiFrameProcessor *frame_processor, ChiakiLog *log); @@ -56,6 +57,11 @@ CHIAKI_EXPORT void chiaki_frame_processor_fini(ChiakiFrameProcessor *frame_proce CHIAKI_EXPORT ChiakiErrorCode chiaki_frame_processor_alloc_frame(ChiakiFrameProcessor *frame_processor, ChiakiTakionAVPacket *packet); CHIAKI_EXPORT ChiakiErrorCode chiaki_frame_processor_put_unit(ChiakiFrameProcessor *frame_processor, ChiakiTakionAVPacket *packet); + +/** + * @param frame unless CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FAILED returned, will receive a pointer into the internal buffer of frame_processor. + * MUST NOT be used after the next call to this frame processor! + */ CHIAKI_EXPORT ChiakiFrameProcessorFlushResult chiaki_frame_processor_flush(ChiakiFrameProcessor *frame_processor, uint8_t **frame, size_t *frame_size); static inline bool chiaki_frame_processor_flush_possible(ChiakiFrameProcessor *frame_processor) diff --git a/lib/src/frameprocessor.c b/lib/src/frameprocessor.c index f2c83a7..7653961 100644 --- a/lib/src/frameprocessor.c +++ b/lib/src/frameprocessor.c @@ -18,10 +18,11 @@ #include #include +#include + #include #include - #define UNIT_SLOTS_MAX 256 @@ -169,9 +170,6 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_frame_processor_put_unit(ChiakiFrameProcess return CHIAKI_ERR_SUCCESS; } - -#include - static ChiakiErrorCode chiaki_frame_processor_fec(ChiakiFrameProcessor *frame_processor) { CHIAKI_LOGI(frame_processor->log, "Frame Processor received %u+%u / %u+%u units, attempting FEC", @@ -252,11 +250,7 @@ CHIAKI_EXPORT ChiakiFrameProcessorFlushResult chiaki_frame_processor_flush(Chiak result = CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FEC_SUCCESS; } - uint8_t *buf = malloc(frame_processor->frame_buf_size); // TODO: this should come from outside instead of mallocing all the time - if(!buf) - return CHIAKI_FRAME_PROCESSOR_FLUSH_RESULT_FAILED; - - size_t buf_size = 0; + size_t cur = 0; for(size_t i=0; iunits_source_expected; i++) { ChiakiFrameUnit *unit = frame_processor->unit_slots + i; @@ -268,12 +262,11 @@ CHIAKI_EXPORT ChiakiFrameProcessorFlushResult chiaki_frame_processor_flush(Chiak } size_t part_size = unit->data_size - 2; uint8_t *buf_ptr = frame_processor->frame_buf + i*frame_processor->buf_size_per_unit; - //CHIAKI_LOGD(frame_processor->log, "unit size: %#zx, in buf: %#x", unit->data_size, frame_processor->buf_size_per_unit - (unsigned int)ntohs(*((uint16_t *)buf_ptr))); - memcpy(buf + buf_size, buf_ptr + 2, part_size); - buf_size += part_size; + memmove(frame_processor->frame_buf + cur, buf_ptr + 2, part_size); + cur += part_size; } - *frame = buf; - *frame_size = buf_size; + *frame = frame_processor->frame_buf; + *frame_size = cur; return result; } diff --git a/lib/src/videoreceiver.c b/lib/src/videoreceiver.c index 6234d36..c1729cf 100644 --- a/lib/src/videoreceiver.c +++ b/lib/src/videoreceiver.c @@ -138,7 +138,7 @@ static ChiakiErrorCode chiaki_video_receiver_flush_frame(ChiakiVideoReceiver *vi if(video_receiver->session->video_sample_cb) video_receiver->session->video_sample_cb(frame, frame_size, video_receiver->session->video_sample_cb_user); - free(frame); + video_receiver->frame_index_prev = video_receiver->frame_index_cur; return CHIAKI_ERR_SUCCESS; } \ No newline at end of file