Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement async callback for Android mediacodec #4105

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pjmedia/build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export _LDFLAGS := $(APP_THIRD_PARTY_LIBS) \
#
export PJMEDIA_SRCDIR = ../src/pjmedia
export PJMEDIA_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \
alaw_ulaw.o alaw_ulaw_table.o avi_player.o \
alaw_ulaw.o alaw_ulaw_table.o avi_player.o atomic_queue.o \
bidirectional.o clock_thread.o codec.o conference.o \
conf_switch.o converter.o converter_libswscale.o converter_libyuv.o \
delaybuf.o echo_common.o \
Expand Down
81 changes: 81 additions & 0 deletions pjmedia/include/pjmedia/atomic_queue.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2024 Teluu Inc. (http://www.teluu.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __ATOMIC_QUEUE_HPP__
#define __ATOMIC_QUEUE_HPP__

#include <atomic>

/* Atomic queue (ring buffer) for single consumer & single producer.
*
* Producer invokes 'put(item)' to put an item to the back of the queue and
* consumer invokes 'get(item)' to get an item from the head of the queue.
*
* For producer, there is write pointer 'ptrWrite' that will be incremented
* every time a item is queued to the back of the queue. If the queue is
* almost full (the write pointer is right before the read pointer) the
* producer will forcefully discard the oldest item in the head of the
* queue by incrementing read pointer.
*
* For consumer, there is read pointer 'ptrRead' that will be incremented
* every time a item is fetched from the head of the queue, only if the
* pointer is not modified by producer (in case of queue full).
*/
class AtomicQueue {
public:
/**
* Constructor
*/
AtomicQueue(unsigned max_item_cnt, unsigned item_size,
const char* name_);

/**
* Destructor
*/
~AtomicQueue();

/**
* Get a item from the head of the queue
*/
bool get(void* item);

/**
* Put a item to the back of the queue
*/
void put(void* item);

private:
unsigned maxItemCnt;
unsigned itemSize;
std::atomic<unsigned> ptrWrite;
std::atomic<unsigned> ptrRead;
char *buffer;
const char *name;

/* Increment read pointer, only if producer not incemented it already.
* Producer may increment the read pointer if the write pointer is
* right before the read pointer (buffer almost full).
*/
bool inc_ptr_read_if_not_yet(unsigned old_ptr);

/* Increment write pointer */
unsigned inc_ptr_write(unsigned old_ptr);

AtomicQueue() {}
};

#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Headers in include dir are meant to be public API, so by convention it should use a specific prefix such as pj or pjmedia.
  • If this is public API, IMO, the interface better uses C (as used by the rest), e.g: pj_atomic_queue, and perhaps part of PJLIB instead of PJMEDIA, note that the implementation can still use C++.

104 changes: 1 addition & 103 deletions pjmedia/src/pjmedia-audiodev/oboe_dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <pjmedia/circbuf.h>
#include <pjmedia/errno.h>
#include <pjmedia/event.h>
#include <pjmedia/atomic_queue.hpp>

#if defined(PJMEDIA_AUDIO_DEV_HAS_OBOE) && PJMEDIA_AUDIO_DEV_HAS_OBOE != 0

Expand All @@ -38,9 +39,6 @@
#include <android/log.h>
#include <oboe/Oboe.h>

#include <atomic>


/* Device info */
typedef struct aud_dev_info
{
Expand Down Expand Up @@ -525,106 +523,6 @@ static pj_status_t oboe_default_param(pjmedia_aud_dev_factory *ff,
return PJ_SUCCESS;
}


/* Atomic queue (ring buffer) for single consumer & single producer.
*
* Producer invokes 'put(frame)' to put a frame to the back of the queue and
* consumer invokes 'get(frame)' to get a frame from the head of the queue.
*
* For producer, there is write pointer 'ptrWrite' that will be incremented
* every time a frame is queued to the back of the queue. If the queue is
* almost full (the write pointer is right before the read pointer) the
* producer will forcefully discard the oldest frame in the head of the
* queue by incrementing read pointer.
*
* For consumer, there is read pointer 'ptrRead' that will be incremented
* every time a frame is fetched from the head of the queue, only if the
* pointer is not modified by producer (in case of queue full).
*/
class AtomicQueue {
public:

AtomicQueue(unsigned max_frame_cnt, unsigned frame_size,
const char* name_= "") :
maxFrameCnt(max_frame_cnt), frameSize(frame_size),
ptrWrite(0), ptrRead(0),
buffer(NULL), name(name_)
{
buffer = new char[maxFrameCnt * frameSize];

/* Surpress warning when debugging log is disabled */
PJ_UNUSED_ARG(name);
}

~AtomicQueue() {
delete [] buffer;
}

/* Get a frame from the head of the queue */
bool get(void* frame) {
if (ptrRead == ptrWrite)
return false;

unsigned cur_ptr = ptrRead;
void *p = &buffer[cur_ptr * frameSize];
pj_memcpy(frame, p, frameSize);
inc_ptr_read_if_not_yet(cur_ptr);

//__android_log_print(ANDROID_LOG_INFO, name,
// "GET: ptrRead=%d ptrWrite=%d\n",
// ptrRead.load(), ptrWrite.load());
return true;
}

/* Put a frame to the back of the queue */
void put(void* frame) {
unsigned cur_ptr = ptrWrite;
void *p = &buffer[cur_ptr * frameSize];
pj_memcpy(p, frame, frameSize);
unsigned next_ptr = inc_ptr_write(cur_ptr);

/* Increment read pointer if next write is overlapping (next_ptr == read ptr) */
unsigned next_read_ptr = (next_ptr == maxFrameCnt-1)? 0 : (next_ptr+1);
ptrRead.compare_exchange_strong(next_ptr, next_read_ptr);

//__android_log_print(ANDROID_LOG_INFO, name,
// "PUT: ptrRead=%d ptrWrite=%d\n",
// ptrRead.load(), ptrWrite.load());
}

private:

unsigned maxFrameCnt;
unsigned frameSize;
std::atomic<unsigned> ptrWrite;
std::atomic<unsigned> ptrRead;
char *buffer;
const char *name;

/* Increment read pointer, only if producer not incemented it already.
* Producer may increment the read pointer if the write pointer is
* right before the read pointer (buffer almost full).
*/
bool inc_ptr_read_if_not_yet(unsigned old_ptr) {
unsigned new_ptr = (old_ptr == maxFrameCnt-1)? 0 : (old_ptr+1);
return ptrRead.compare_exchange_strong(old_ptr, new_ptr);
}

/* Increment write pointer */
unsigned inc_ptr_write(unsigned old_ptr) {
unsigned new_ptr = (old_ptr == maxFrameCnt-1)? 0 : (old_ptr+1);
if (ptrWrite.compare_exchange_strong(old_ptr, new_ptr))
return new_ptr;

/* Should never happen */
pj_assert(!"There is more than one producer!");
return old_ptr;
}

AtomicQueue() {}
};


/* Interface to Oboe */
class MyOboeEngine : oboe::AudioStreamDataCallback,
oboe::AudioStreamErrorCallback
Expand Down
Loading
Loading