session: refactor add and find source

Make functions to find and add a source to the hashtable.
This commit is contained in:
Wim Taymans 2013-07-25 15:56:39 +02:00
parent adb0d68c07
commit 0060e1d45d

View file

@ -1331,6 +1331,21 @@ check_collision (RTPSession * sess, RTPSource * source,
return TRUE; return TRUE;
} }
static RTPSource *
find_source (RTPSession * sess, guint32 ssrc)
{
return g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
GINT_TO_POINTER (ssrc));
}
static void
add_source (RTPSession * sess, RTPSource * src)
{
g_hash_table_insert (sess->ssrcs[sess->mask_idx],
GINT_TO_POINTER (src->ssrc), src);
/* we have one more source now */
sess->total_sources++;
}
/* must be called with the session lock, the returned source needs to be /* must be called with the session lock, the returned source needs to be
* unreffed after usage. */ * unreffed after usage. */
@ -1340,12 +1355,13 @@ obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
{ {
RTPSource *source; RTPSource *source;
source = source = find_source (sess, ssrc);
g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
if (source == NULL) { if (source == NULL) {
/* make new Source in probation and insert */ /* make new Source in probation and insert */
source = rtp_source_new (ssrc); source = rtp_source_new (ssrc);
GST_DEBUG ("creating new source %08x %p", ssrc, source);
/* for RTP packets we need to set the source in probation. Receiving RTCP /* for RTP packets we need to set the source in probation. Receiving RTCP
* packets of an SSRC, on the other hand, is a strong indication that we * packets of an SSRC, on the other hand, is a strong indication that we
* are dealing with a valid source. */ * are dealing with a valid source. */
@ -1365,11 +1381,7 @@ obtain_source (RTPSession * sess, guint32 ssrc, gboolean * created,
/* configure a callback on the source */ /* configure a callback on the source */
rtp_source_set_callbacks (source, &callbacks, sess); rtp_source_set_callbacks (source, &callbacks, sess);
g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc), add_source (sess, source);
source);
/* we have one more source now */
sess->total_sources++;
*created = TRUE; *created = TRUE;
} else { } else {
*created = FALSE; *created = FALSE;
@ -1482,14 +1494,9 @@ rtp_session_add_source (RTPSession * sess, RTPSource * src)
g_return_val_if_fail (src != NULL, FALSE); g_return_val_if_fail (src != NULL, FALSE);
RTP_SESSION_LOCK (sess); RTP_SESSION_LOCK (sess);
find = find = find_source (sess, src->ssrc);
g_hash_table_lookup (sess->ssrcs[sess->mask_idx],
GINT_TO_POINTER (src->ssrc));
if (find == NULL) { if (find == NULL) {
g_hash_table_insert (sess->ssrcs[sess->mask_idx], add_source (sess, src);
GINT_TO_POINTER (src->ssrc), src);
/* we have one more source now */
sess->total_sources++;
result = TRUE; result = TRUE;
} }
RTP_SESSION_UNLOCK (sess); RTP_SESSION_UNLOCK (sess);
@ -1560,8 +1567,7 @@ rtp_session_get_source_by_ssrc (RTPSession * sess, guint32 ssrc)
g_return_val_if_fail (RTP_IS_SESSION (sess), NULL); g_return_val_if_fail (RTP_IS_SESSION (sess), NULL);
RTP_SESSION_LOCK (sess); RTP_SESSION_LOCK (sess);
result = result = find_source (sess, ssrc);
g_hash_table_lookup (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc));
if (result) if (result)
g_object_ref (result); g_object_ref (result);
RTP_SESSION_UNLOCK (sess); RTP_SESSION_UNLOCK (sess);
@ -1579,8 +1585,7 @@ rtp_session_create_new_ssrc (RTPSession * sess)
ssrc = g_random_int (); ssrc = g_random_int ();
/* see if it exists in the session, we're done if it doesn't */ /* see if it exists in the session, we're done if it doesn't */
if (g_hash_table_lookup (sess->ssrcs[sess->mask_idx], if (find_source (sess, ssrc) == NULL)
GINT_TO_POINTER (ssrc)) == NULL)
break; break;
} }
return ssrc; return ssrc;
@ -1608,10 +1613,7 @@ rtp_session_create_source (RTPSession * sess)
rtp_source_set_callbacks (source, &callbacks, sess); rtp_source_set_callbacks (source, &callbacks, sess);
/* we need an additional ref for the source in the hashtable */ /* we need an additional ref for the source in the hashtable */
g_object_ref (source); g_object_ref (source);
g_hash_table_insert (sess->ssrcs[sess->mask_idx], GINT_TO_POINTER (ssrc), add_source (sess, source);
source);
/* we have one more source now */
sess->total_sources++;
RTP_SESSION_UNLOCK (sess); RTP_SESSION_UNLOCK (sess);
return source; return source;
@ -2156,8 +2158,7 @@ rtp_session_process_pli (RTPSession * sess, guint32 sender_ssrc,
if (!sess->callbacks.request_key_unit) if (!sess->callbacks.request_key_unit)
return; return;
src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx], src = find_source (sess, sender_ssrc);
GINT_TO_POINTER (sender_ssrc));
if (!src) if (!src)
return; return;
@ -2179,8 +2180,7 @@ rtp_session_process_fir (RTPSession * sess, guint32 sender_ssrc,
if (fci_length < 8) if (fci_length < 8)
return; return;
src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx], src = find_source (sess, sender_ssrc);
GINT_TO_POINTER (sender_ssrc));
/* Hack because Google fails to set the sender_ssrc correctly */ /* Hack because Google fails to set the sender_ssrc correctly */
if (!src && sender_ssrc == 1) { if (!src && sender_ssrc == 1) {
@ -2253,8 +2253,7 @@ rtp_session_process_feedback (RTPSession * sess, GstRTCPPacket * packet,
} }
if (sess->rtcp_feedback_retention_window) { if (sess->rtcp_feedback_retention_window) {
RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx], RTPSource *src = find_source (sess, media_ssrc);
GINT_TO_POINTER (media_ssrc));
if (src) if (src)
rtp_source_retain_rtcp_packet (src, packet, arrival->running_time); rtp_source_retain_rtcp_packet (src, packet, arrival->running_time);
@ -3300,8 +3299,7 @@ gboolean
rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now, rtp_session_request_key_unit (RTPSession * sess, guint32 ssrc, GstClockTime now,
gboolean fir, gint count) gboolean fir, gint count)
{ {
RTPSource *src = g_hash_table_lookup (sess->ssrcs[sess->mask_idx], RTPSource *src = find_source (sess, ssrc);
GUINT_TO_POINTER (ssrc));
if (!src) if (!src)
return FALSE; return FALSE;