summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xtinyDAV/src/tdav.c7
-rwxr-xr-xtinyMEDIA/include/tinymedia/tmedia_codec.h1
-rwxr-xr-xtinyMEDIA/src/tmedia_codec.c35
3 files changed, 30 insertions, 13 deletions
diff --git a/tinyDAV/src/tdav.c b/tinyDAV/src/tdav.c
index 0fa72e8..e264f2b 100755
--- a/tinyDAV/src/tdav.c
+++ b/tinyDAV/src/tdav.c
@@ -473,14 +473,15 @@ int tdav_codec_set_priority(tdav_codec_id_t codec_id, int priority)
{
tsk_size_t i;
- if(priority < 0) {
+ if (priority < 0) {
TSK_DEBUG_ERROR("Invalid parameter");
return -1;
}
- for(i = 0; i < __codec_plugins_all_count && __codec_plugins_all[i]; ++i) {
+ for (i = 0; i < __codec_plugins_all_count && __codec_plugins_all[i]; ++i) {
if(__codec_plugins_all[i]->codec_id == codec_id) {
const struct tmedia_codec_plugin_def_s *codec_decl_1, *codec_decl_2;
- priority = TSK_MIN(priority, (int)__codec_plugins_all_count-1);
+ tsk_size_t max = tmedia_codec_plugin_registered_count(__codec_plugins_all, __codec_plugins_all_count);
+ priority = TSK_CLAMP(0, priority, (int)(max > 0 ? (max - 1) : 0));
codec_decl_1 = __codec_plugins_all[priority];
codec_decl_2 = __codec_plugins_all[i];
diff --git a/tinyMEDIA/include/tinymedia/tmedia_codec.h b/tinyMEDIA/include/tinymedia/tmedia_codec.h
index 6dcd2be..63e6fc9 100755
--- a/tinyMEDIA/include/tinymedia/tmedia_codec.h
+++ b/tinyMEDIA/include/tinymedia/tmedia_codec.h
@@ -314,6 +314,7 @@ TINYMEDIA_API tsk_bool_t tmedia_codec_plugin_is_registered(const tmedia_codec_pl
TINYMEDIA_API tsk_bool_t tmedia_codec_plugin_is_registered_2(tmedia_codec_id_t codec_id);
TINYMEDIA_API int tmedia_codec_plugin_registered_get_all(const struct tmedia_codec_plugin_def_s*(** plugins)[TMED_CODEC_MAX_PLUGINS], tsk_size_t* count);
TINYMEDIA_API const struct tmedia_codec_plugin_def_s* tmedia_codec_plugin_registered_get_const(tmedia_codec_id_t codec_id);
+TINYMEDIA_API tsk_size_t tmedia_codec_plugin_registered_count(const struct tmedia_codec_plugin_def_s** plugins, tsk_size_t count);
TINYMEDIA_API int tmedia_codec_plugin_unregister(const tmedia_codec_plugin_def_t* plugin);
TINYMEDIA_API int tmedia_codec_plugin_unregister_all();
TINYMEDIA_API tmedia_codec_t* tmedia_codec_create(const char* format);
diff --git a/tinyMEDIA/src/tmedia_codec.c b/tinyMEDIA/src/tmedia_codec.c
index a5fb8e2..0470a04 100755
--- a/tinyMEDIA/src/tmedia_codec.c
+++ b/tinyMEDIA/src/tmedia_codec.c
@@ -302,37 +302,43 @@ int tmedia_codec_plugin_register(const tmedia_codec_plugin_def_t* plugin)
int tmedia_codec_plugin_register_2(const tmedia_codec_plugin_def_t* plugin, int prio)
{
- tsk_size_t count = 0;
+ tsk_size_t index = 0, max;
tsk_bool_t already_registered = tsk_false;
const tmedia_codec_plugin_def_t* tmp;
- if(!plugin || tsk_strnullORempty(plugin->name) || tsk_strnullORempty(plugin->format) || (prio + 1) >= TMED_CODEC_MAX_PLUGINS) {
+ if (!plugin || tsk_strnullORempty(plugin->name) || tsk_strnullORempty(plugin->format) || (prio + 1) >= TMED_CODEC_MAX_PLUGINS) {
TSK_DEBUG_ERROR("Invalid parameter");
return -1;
}
// count codecs and found if already registered
- while(__tmedia_codec_plugins[count]) {
- if(__tmedia_codec_plugins[count] == plugin) {
+ while (__tmedia_codec_plugins[index]) {
+ if (__tmedia_codec_plugins[index] == plugin) {
already_registered = tsk_true;
}
- ++count;
+ ++index;
}
- if(count >= TMED_CODEC_MAX_PLUGINS) {
+ if (index >= TMED_CODEC_MAX_PLUGINS) {
TSK_DEBUG_ERROR("No room");
return -1;
}
+ // clamp prio (must be done here before unregistering the plugin)
+ max = tmedia_codec_plugin_registered_count(__tmedia_codec_plugins, sizeof(__tmedia_codec_plugins)/sizeof(__tmedia_codec_plugins[0]));
+ prio = TSK_CLAMP(0, prio, (int)(max > 0 ? (max - 1) : 0));
+
// unregister and compact
- if(already_registered) {
- if(tmedia_codec_plugin_unregister(plugin) == 0) {
- --count;
+ if (already_registered) {
+ if (tmedia_codec_plugin_unregister(plugin) == 0) {
+ --index;
}
}
+
+
// put current plugin at prio and old (which was at prio) at the end
tmp = __tmedia_codec_plugins[prio];
- __tmedia_codec_plugins[count] = tmp;// put old codec add prio to the end of the list
+ __tmedia_codec_plugins[index] = tmp;// put old codec add prio to the end of the list
__tmedia_codec_plugins[prio] = plugin;
return 0;
@@ -399,6 +405,15 @@ const struct tmedia_codec_plugin_def_s* tmedia_codec_plugin_registered_get_const
}
/**@ingroup tmedia_codec_group
+*/
+tsk_size_t tmedia_codec_plugin_registered_count(const struct tmedia_codec_plugin_def_s** plugins, tsk_size_t count)
+{
+ tsk_size_t i;
+ for (i = 0; i < count && plugins[i]; ++i) ;
+ return i;
+}
+
+/**@ingroup tmedia_codec_group
* UnRegisters a codec plugin.
* @param plugin the definition of the plugin.
* @retval 0 if succeed and non-zero error code otherwise.
OpenPOWER on IntegriCloud