-
Notifications
You must be signed in to change notification settings - Fork 774
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
trengginas
wants to merge
5
commits into
master
Choose a base branch
from
async-mediacodec
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee60ba1
Implement async callback for Android mediacodec
trengginas 8b97e99
move atomic_queue to pjlib
trengginas 42b8d75
Change the interface
trengginas 0672e06
Add checks and doc.
trengginas d7b56f6
Modification based on comments
trengginas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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 __PJ_ATOMIC_QUEUE_H__ | ||
#define __PJ_ATOMIC_QUEUE_H__ | ||
|
||
/** | ||
* @file atomic_queue.h | ||
* @brief Atomic Queue operations | ||
* @{ | ||
* | ||
* Atomic queue for a single consumer and producer. | ||
* This cyclic queue employs a ring buffer for storage, maintaining read/write | ||
* pointers without locks. It’s designed for one producer and one consumer, | ||
* as having multiple producers/consumers could lead to conflicts with the | ||
* read/write pointers. | ||
* The producer uses #pj_atomic_queue_put() to add an item to the back | ||
* of the queue, while the consumer uses #pj_atomic_queue_get() | ||
* to retrieve an item from the front. | ||
*/ | ||
|
||
#include <pj/types.h> | ||
|
||
PJ_BEGIN_DECL | ||
|
||
/** | ||
* Create a new Atomic Queue for single consumer and single producer case. | ||
* | ||
* @param pool The pool to allocate the atomic queue structure. | ||
* @param max_item_cnt The maximum number of items that can be stored. | ||
* @param item_size The size of each item. | ||
* @param name The name of the queue. | ||
* @param atomic_queue Pointer to hold the newly created Atomic Queue. | ||
* | ||
* @return PJ_SUCCESS on success. | ||
*/ | ||
PJ_DECL(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool, | ||
unsigned max_item_cnt, | ||
unsigned item_size, | ||
const char *name, | ||
pj_atomic_queue_t **atomic_queue); | ||
|
||
/** | ||
* Destroy the Atomic Queue. | ||
* | ||
* @param atomic_queue The Atomic Queue to be destroyed. | ||
* | ||
* @return PJ_SUCCESS if success. | ||
*/ | ||
PJ_DECL(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue); | ||
|
||
/** | ||
* Put an item 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 | ||
* the read pointer. | ||
* | ||
* @param atomic_queue The Atomic Queue. | ||
* @param item The pointer to the data to store. | ||
* | ||
* @return PJ_SUCCESS if success. | ||
*/ | ||
PJ_DECL(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue, | ||
void *item); | ||
|
||
/** | ||
* Get an item from the head of the queue. | ||
* | ||
* @param atomic_queue The Atomic Queue. | ||
* @param item The pointer to data to get the data. | ||
* | ||
* @return PJ_SUCCESS if success. | ||
*/ | ||
PJ_DECL(pj_status_t) pj_atomic_queue_get(pj_atomic_queue_t *atomic_queue, | ||
void *item); | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
||
PJ_END_DECL | ||
|
||
#endif | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* 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 | ||
*/ | ||
#include <pj/assert.h> | ||
#include <pj/atomic_queue.h> | ||
#include <pj/errno.h> | ||
#include <pj/pool.h> | ||
#include <pj/string.h> | ||
#include <atomic> | ||
|
||
#if 0 | ||
# define TRACE_(arg) PJ_LOG(4,arg) | ||
#else | ||
# define TRACE_(expr) | ||
#endif | ||
class AtomicQueue { | ||
public: | ||
/** | ||
* Constructor | ||
*/ | ||
AtomicQueue(unsigned maxItemCnt, unsigned itemSize, const char* name = ""): | ||
maxItemCnt_(maxItemCnt), | ||
itemSize_(itemSize), | ||
ptrWrite(0), | ||
ptrRead(0), | ||
buffer(NULL), | ||
name_(name) | ||
{ | ||
buffer = new char[maxItemCnt_ * itemSize_]; | ||
|
||
/* Surpress warning when debugging log is disabled */ | ||
PJ_UNUSED_ARG(name_); | ||
|
||
TRACE_((name_, "Created AtomicQueue: maxItemCnt=%d itemSize=%d", | ||
maxItemCnt_, itemSize_)); | ||
} | ||
|
||
/** | ||
* Destructor | ||
*/ | ||
~AtomicQueue() | ||
{ | ||
delete [] buffer; | ||
} | ||
|
||
/** | ||
* Get a item from the head of the queue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an item. |
||
*/ | ||
bool get(void* item) | ||
{ | ||
if (ptrRead == ptrWrite) | ||
return false; | ||
|
||
unsigned cur_ptr = ptrRead; | ||
void *p = &buffer[cur_ptr * itemSize_]; | ||
pj_memcpy(item, p, itemSize_); | ||
inc_ptrRead_if_not_yet(cur_ptr); | ||
|
||
TRACE_((name_, "GET: ptrRead=%d ptrWrite=%d\n", | ||
ptrRead.load(), ptrWrite.load())); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Put a item to the back of the queue | ||
*/ | ||
void put(void* item) | ||
{ | ||
unsigned cur_ptr = ptrWrite; | ||
void *p = &buffer[cur_ptr * itemSize_]; | ||
pj_memcpy(p, item, itemSize_); | ||
unsigned next_ptr = inc_ptrWrite(cur_ptr); | ||
|
||
/* Increment read pointer if next write is overlapping | ||
* (next_ptr == read ptr) | ||
*/ | ||
unsigned next_read_ptr = (next_ptr == maxItemCnt_-1)? 0 : (next_ptr+1); | ||
ptrRead.compare_exchange_strong(next_ptr, next_read_ptr); | ||
|
||
TRACE_((name_, "PUT: ptrRead=%d ptrWrite=%d\n", | ||
ptrRead.load(), ptrWrite.load())); | ||
} | ||
|
||
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_ptrRead_if_not_yet(unsigned old_ptr) | ||
{ | ||
unsigned new_ptr = (old_ptr == maxItemCnt_-1)? 0 : (old_ptr+1); | ||
return ptrRead.compare_exchange_strong(old_ptr, new_ptr); | ||
} | ||
|
||
/* Increment write pointer */ | ||
unsigned inc_ptrWrite(unsigned old_ptr) | ||
{ | ||
unsigned new_ptr = (old_ptr == maxItemCnt_-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() {} | ||
}; | ||
|
||
struct pj_atomic_queue_t | ||
{ | ||
AtomicQueue *aQ; | ||
}; | ||
|
||
PJ_DEF(pj_status_t) pj_atomic_queue_create(pj_pool_t *pool, | ||
unsigned max_item_cnt, | ||
unsigned item_size, | ||
const char *name, | ||
pj_atomic_queue_t **atomic_queue) | ||
{ | ||
pj_atomic_queue_t *aqueue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add pre-verification/ |
||
|
||
PJ_ASSERT_RETURN(pool, PJ_EINVAL); | ||
aqueue = PJ_POOL_ZALLOC_T(pool, pj_atomic_queue_t); | ||
aqueue->aQ = new AtomicQueue(max_item_cnt, item_size, name); | ||
*atomic_queue = aqueue; | ||
return PJ_SUCCESS; | ||
} | ||
|
||
PJ_DEF(pj_status_t) pj_atomic_queue_destroy(pj_atomic_queue_t *atomic_queue) | ||
{ | ||
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ, PJ_EINVAL); | ||
delete atomic_queue->aQ; | ||
atomic_queue->aQ = NULL; | ||
return PJ_SUCCESS; | ||
} | ||
|
||
/** | ||
* For producer, there is write pointer 'ptrWrite' that will be incremented | ||
* every time a item is queued to the back of the queue. | ||
*/ | ||
PJ_DEF(pj_status_t) pj_atomic_queue_put(pj_atomic_queue_t *atomic_queue, | ||
void *item) | ||
{ | ||
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL); | ||
atomic_queue->aQ->put(item); | ||
return PJ_SUCCESS; | ||
} | ||
|
||
/** | ||
* 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). | ||
*/ | ||
PJ_DEF(pj_status_t) pj_atomic_queue_get(pj_atomic_queue_t *atomic_queue, | ||
void *item) | ||
{ | ||
PJ_ASSERT_RETURN(atomic_queue && atomic_queue->aQ && item, PJ_EINVAL); | ||
if (atomic_queue->aQ->get(item)) | ||
return PJ_SUCCESS; | ||
else | ||
return PJ_ENOTFOUND; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is definitely still C++ (with C naming style :)
Please check
ioqueue_symbian.cpp
which useioqueue.h
.