Skip to content

Commit

Permalink
srtp_stream_hash_t
Browse files Browse the repository at this point in the history
Store SRTP streams in several lists rather than in a single one, making
those effectively smaller than the current single one.

Each hash entry is a srtp_stream_list_t, being this one where the
streams are ultimately stored as they are currently.
  • Loading branch information
jmillan committed Nov 17, 2023
1 parent 1f0a5de commit 29eeb9a
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 39 deletions.
3 changes: 2 additions & 1 deletion include/srtp_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern "C" {
typedef struct srtp_stream_ctx_t_ srtp_stream_ctx_t;
typedef srtp_stream_ctx_t *srtp_stream_t;
typedef struct srtp_stream_list_ctx_t_ *srtp_stream_list_t;
typedef struct srtp_stream_hash_t_ *srtp_stream_hash_t;

/*
* the following declarations are libSRTP internal functions
Expand Down Expand Up @@ -158,7 +159,7 @@ typedef struct srtp_stream_ctx_t_ {
* an srtp_ctx_t holds a stream list and a service description
*/
typedef struct srtp_ctx_t_ {
srtp_stream_list_t stream_list; /* linked list of streams */
srtp_stream_hash_t stream_hash; /* hash of stream lists */
struct srtp_stream_ctx_t_ *stream_template; /* act as template for other */
/* streams */
void *user_data; /* user custom data */
Expand Down
49 changes: 49 additions & 0 deletions include/stream_list_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,55 @@ void srtp_stream_list_for_each(srtp_stream_list_t list,
int (*callback)(srtp_stream_t, void *),
void *data);

/**
* allocate and initialize a stream hash instance
*/
srtp_err_status_t srtp_stream_hash_alloc(srtp_stream_hash_t *hash_ptr);

/**
* deallocate a stream hash instance
*
* the underlying lists must be empty or else an error is returned.
*/
srtp_err_status_t srtp_stream_hash_dealloc(srtp_stream_hash_t hash);

/**
* insert a stream into the hash
*
* returns srtp_err_status_alloc_fail if insertion failed due to unavailable
* capacity in the hash. if operation succeeds, srtp_err_status_ok is returned
*
* if another stream with the same SSRC already exists in the hash,
* behavior is undefined. if the SSRC field is mutated while the
* stream is inserted, further operations have undefined behavior
*/
srtp_err_status_t srtp_stream_hash_insert(srtp_stream_hash_t hash,
srtp_stream_t stream);

/*
* look up the stream corresponding to the specified SSRC and return it.
* if no such SSRC is found, NULL is returned.
*/
srtp_stream_t srtp_stream_hash_get(srtp_stream_hash_t hash, uint32_t ssrc);

/**
* remove the stream from the hash.
*
* The stream to be removed is referenced "by value", i.e., by the pointer to be
* removed from the hash. This pointer is obtained using `srtp_stream_hash_get`
* or as callback parameter in `srtp_stream_hash_for_each`.
*/
void srtp_stream_hash_remove(srtp_stream_hash_t hash, srtp_stream_t stream);

/**
* iterate through all stored streams. while iterating, it is allowed to delete
* the current element; any other mutation to the hash is undefined behavior.
* returning non-zero from callback aborts the iteration.
*/
void srtp_stream_hash_for_each(srtp_stream_hash_t hash,
int (*callback)(srtp_stream_t, void *),
void *data);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 29eeb9a

Please sign in to comment.