From db9f2f23ea1d0a23f544cf584766d7dbd6fbbc4f Mon Sep 17 00:00:00 2001
From: Bent Bisballe Nyeng <deva@aasimon.org>
Date: Tue, 3 Dec 2013 13:16:42 +0100
Subject: Added some error handling. Return CSRC and ts.

---
 src/lrtp.cc | 45 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/src/lrtp.cc b/src/lrtp.cc
index 1f0b11d..f974331 100644
--- a/src/lrtp.cc
+++ b/src/lrtp.cc
@@ -39,6 +39,8 @@
 
 #include "srtp.h"
 
+//#define SKIP_SRTP
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -222,7 +224,9 @@ int lrtp_pack(struct lrtp_t *lrtp, char *packet, size_t maxsize)
 
   if(rtp.isValid()) {
     size_t size = rtp.packet(packet, maxsize);
+#ifndef SKIP_SRTP
     size = lrtp->srtp->encrypt(packet, size);
+#endif/*SKIP_SRTP*/
     lrtp->seq++;
     return size;
   }
@@ -246,6 +250,9 @@ int lrtp_dequeue_frame(struct lrtp_t *lrtp,
 
   memcpy(frame, f->data, f->size);
 
+  *csrc = f->csrc;
+  *ts = f->ts;
+
   lrtp->framelist.pop_front();
 
   free(f->data);
@@ -257,32 +264,56 @@ int lrtp_dequeue_frame(struct lrtp_t *lrtp,
 EXPORT
 int lrtp_unpack(struct lrtp_t *lrtp, const char *packet, size_t size)
 {
-  if(!lrtp) return UNPACK_MISSING_HANDLE;
+  if(!lrtp) return -UNPACK_MISSING_HANDLE;
 
   // TODO: Check lrtp magic word
 
-  char pkg[16*1024];
+  char pkg[size];
   memcpy(pkg, packet, size);
 
-  size = lrtp->srtp->decrypt(pkg, size);
+  int ret = 0;
+
+#ifndef SKIP_SRTP
+  ret = lrtp->srtp->decrypt(pkg, size);
+
+  if(ret < 0) {
+    printf("lrtp_unpack::decrypt error: %d\n", ret);
+    return -1;
+  }
+
+  size = ret;
+#endif/*SKIP_SRTP*/
 
   RTP rtp;
   rtp.fromPacket(pkg, size);
   std::list<csrc_t> csrcs = rtp.getCSrcs();
-  if(csrcs.size() == 0) return UNPACK_MISSING_CSRC;
-  if(csrcs.size() > 1) return UNPACK_TOO_MANY_CSRCS;
+  if(csrcs.size() == 0) return -UNPACK_MISSING_CSRC;
+  if(csrcs.size() > 1) return -UNPACK_TOO_MANY_CSRCS;
 
   csrc_t csrc = *csrcs.begin();
 
   struct lrtp_profile_t *profile = NULL;
   if(lrtp->profiles.find(csrc) == lrtp->profiles.end()) {
-    return UNPACK_MISSING_PROFILE;
+    return -UNPACK_MISSING_PROFILE;
   }
 
   profile = lrtp->profiles[csrc];
 
   std::list<outputframe_t*> framelist;
-  int ret = profile->unpack(profile, rtp, framelist);
+  ret = profile->unpack(profile, rtp, framelist);
+
+  if(ret < 0) {
+    printf("lrtp_unpack::Profile unpack failed: %d\n", ret);
+    return -1;
+  }
+
+  // Make sure all frames in the list have the correct csrc.
+  std::list<outputframe_t*>::iterator fi = framelist.begin();
+  while(fi != framelist.end()) {
+    (*fi)->csrc = csrc;
+    fi++;
+  }
+
   lrtp->framelist.splice(lrtp->framelist.end(), framelist);
 
   return ret;
-- 
cgit v1.2.3