Skip to content

Commit

Permalink
perf(ActiveDecoder): speedup lock usage
Browse files Browse the repository at this point in the history
Signed-off-by: pingkai <pingkai010@gmail.com>
(cherry picked from commit c2750b9)
  • Loading branch information
pingkai committed Feb 25, 2020
1 parent f1eecdc commit bc72c04
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
30 changes: 15 additions & 15 deletions framework/codec/ActiveDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,25 @@ int ActiveDecoder::decode_func()
int needWait = 0;
int ret;
int64_t pts = INT64_MIN;
bool empty;
{
std::unique_lock<std::mutex> uMutex(mMutex);
empty = mInputQueue.empty();
}

if (!empty) {
if (!mPacket) {
std::unique_lock<std::mutex> uMutex(mMutex);

if (mInputQueue.front().get()) {
pts = mInputQueue.front().get()->getInfo().pts;
ret = enqueue_decoder(mInputQueue.front());
} else {
ret = 0;
if (!mInputQueue.empty()) {
mPacket = move(mInputQueue.front());
mInputQueue.pop();
}
}

if (mPacket) {
pts = mPacket->getInfo().pts;
ret = enqueue_decoder(mPacket);

if (ret == -EAGAIN) {
needWait++;
} else {
mInputQueue.pop();
// assert(mPacket == nullptr);
mPacket = nullptr;

if (ret == STATUS_EOS) {
bDecoderEOS = true;
Expand Down Expand Up @@ -318,6 +317,7 @@ void ActiveDecoder::flush()
mHoldingQueue.pop();
}

mPacket = nullptr;
#endif
clean_error();
flush_decoder();
Expand Down Expand Up @@ -355,15 +355,15 @@ int ActiveDecoder::extract_decoder()
int count = 0;
int size;
{
std::unique_lock <std::mutex> uMutex(mMutex);
std::unique_lock<std::mutex> uMutex(mMutex);
size = mOutputQueue.size();
}

if (size < maxOutQueueSize) {
int ret = 0;

do {
unique_ptr <IAFFrame> pFrame{};
unique_ptr<IAFFrame> pFrame{};
ret = dequeue_decoder(pFrame);

if (ret < 0 || ret == STATUS_EOS) {
Expand All @@ -379,7 +379,7 @@ int ActiveDecoder::extract_decoder()
}

if (pFrame) {
std::unique_lock <std::mutex> uMutex(mMutex);
std::unique_lock<std::mutex> uMutex(mMutex);
mOutputQueue.push(move(pFrame));
count++;
}
Expand Down
3 changes: 2 additions & 1 deletion framework/codec/ActiveDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ActiveDecoder : public Cicada::IDecoder {
protected:
#if AF_HAVE_PTHREAD
afThread *mDecodeThread = nullptr;
std::atomic_bool mRunning{false};
std::atomic_bool mRunning{false};
#endif
private:

Expand All @@ -86,6 +86,7 @@ class ActiveDecoder : public Cicada::IDecoder {
int maxOutQueueSize = 2;
std::mutex mMutex{};
std::mutex mSleepMutex{};
std::unique_ptr<IAFPacket> mPacket{nullptr};
#endif
bool bHolding = false;
std::queue<std::unique_ptr<IAFPacket>> mHoldingQueue{};
Expand Down
1 change: 1 addition & 0 deletions framework/codec/avcodecDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ namespace Cicada {
ret = avcodec_send_packet(mPDecoder->codecCont, pkt);

if (0 == ret) {
pPacket = nullptr;
} else if (ret == AVERROR(EAGAIN)) {
} else if (ret == AVERROR_EOF) {
AF_LOGD("Decode EOF\n");
Expand Down

0 comments on commit bc72c04

Please sign in to comment.