[rtmpdump] r65 - rtmp2.c rtmp2.h rtmpd2.c

hyc subversion at mplayerhq.hu
Wed Dec 16 09:30:52 CET 2009


Author: hyc
Date: Wed Dec 16 09:30:52 2009
New Revision: 65

Log:
Back to allocated packet bodies, oh well

Modified:
   rtmp2.c
   rtmp2.h
   rtmpd2.c

Modified: rtmp2.c
==============================================================================
--- rtmp2.c	Wed Dec 16 07:26:45 2009	(r64)
+++ rtmp2.c	Wed Dec 16 09:30:52 2009	(r65)
@@ -139,6 +139,23 @@ RTMPPacket_Reset(RTMPPacket * p)
   p->m_nBytesRead = 0;
 }
 
+bool
+RTMPPacket_Alloc(RTMPPacket * p, int nSize)
+{
+  p->m_body = calloc(1, nSize);
+  if (!p->m_body)
+    return false;
+  p->m_nBytesRead = 0;
+  return true;
+}
+
+void
+RTMPPacket_Free(RTMPPacket * p)
+{
+  free(p->m_body);
+  p->m_body = NULL;
+}
+
 void
 RTMPPacket_Dump(RTMPPacket * p)
 {
@@ -452,7 +469,7 @@ SocksNegotiate(RTMP * r)
 bool
 RTMP_ConnectStream(RTMP * r, double seekTime, uint32_t dLength)
 {
-  RTMPPacket packet;
+  RTMPPacket packet = { 0 };
   if (seekTime >= -2.0)
     r->Link.seekTime = seekTime;
 
@@ -461,24 +478,22 @@ RTMP_ConnectStream(RTMP * r, double seek
 
   r->m_mediaChannel = 0;
 
-  RTMPPacket_Reset(&packet);
   while (!r->m_bPlaying && RTMP_IsConnected(r) && ReadPacket(r, &packet))
     {
-      if (!RTMPPacket_IsReady(&packet))
+      if (RTMPPacket_IsReady(&packet))
 	{
-	  continue;
-	}
+	  if ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) ||
+	      (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) ||
+	      (packet.m_packetType == RTMP_PACKET_TYPE_INFO))
+	    {
+	      Log(LOGDEBUG, "%s, received FLV packet before play()!",
+		  __FUNCTION__);
+	      break;
+	    }
 
-      if ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) ||
-	  (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) ||
-	  (packet.m_packetType == RTMP_PACKET_TYPE_INFO))
-	{
-	  Log(LOGDEBUG, "%s, received FLV packet before play()!",
-	      __FUNCTION__);
-	  break;
+	  HandlePacket(r, &packet);
+	  RTMPPacket_Free(&packet);
 	}
-
-      HandlePacket(r, &packet);
     }
 
   return r->m_bPlaying;
@@ -529,7 +544,6 @@ RTMP_GetNextMediaPacket(RTMP * r, RTMPPa
 {
   int bHasMediaPacket = 0;
 
-  RTMPPacket_Reset(packet);
   while (!bHasMediaPacket && RTMP_IsConnected(r) && ReadPacket(r, packet))
     {
       if (!RTMPPacket_IsReady(packet))
@@ -541,6 +555,7 @@ RTMP_GetNextMediaPacket(RTMP * r, RTMPPa
 
       if (!bHasMediaPacket)
 	{
+	  RTMPPacket_Free(packet);
 	}
       else if (r->m_pausing == 3)
 	{
@@ -651,7 +666,6 @@ HandlePacket(RTMP * r, RTMPPacket * pack
 
 	if (HandleInvoke(r, packet->m_body + 1, packet->m_nBodySize - 1) == 1)
 	  bHasMediaPacket = 2;
-	RTMPPacket_Reset(packet);
 	break;
       }
     case 0x12:
@@ -675,7 +689,6 @@ HandlePacket(RTMP * r, RTMPPacket * pack
 
       if (HandleInvoke(r, packet->m_body, packet->m_nBodySize) == 1)
 	bHasMediaPacket = 2;
-      RTMPPacket_Reset(packet);
       break;
 
     case 0x16:
@@ -866,6 +879,7 @@ static bool
 SendConnectPacket(RTMP * r)
 {
   RTMPPacket packet;
+  char pbuf[4096];
 
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
@@ -873,6 +887,7 @@ SendConnectPacket(RTMP * r)
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_connect);
@@ -920,12 +935,15 @@ static bool
 SendBGHasStream(RTMP * r, double dId, AVal * playpath)
 {
   RTMPPacket packet;
+  char pbuf[1024];
+
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_bgHasStream);
@@ -945,12 +963,15 @@ static bool
 SendCreateStream(RTMP * r, double dStreamId)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_createStream);
@@ -968,12 +989,14 @@ static bool
 SendFCSubscribe(RTMP * r, AVal * subscribepath)
 {
   RTMPPacket packet;
+  char pbuf[256];
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   Log(LOGDEBUG, "FCSubscribe: %s", subscribepath);
   char *enc = packet.m_body;
@@ -993,6 +1016,7 @@ static bool
 SendDeleteStream(RTMP * r, double dStreamId)
 {
   RTMPPacket packet;
+  char pbuf[256];
 
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
@@ -1000,6 +1024,7 @@ SendDeleteStream(RTMP * r, double dStrea
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_deleteStream);
@@ -1019,12 +1044,15 @@ bool
 RTMP_SendPause(RTMP * r, bool DoPause, double dTime)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x08;	// video channel 
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// invoke
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_pause);
@@ -1044,12 +1072,15 @@ static bool
 SendSeek(RTMP * r, double dTime)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x08;	// video channel 
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// invoke
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_seek);
@@ -1066,12 +1097,15 @@ static bool
 SendServerBW(RTMP * r)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x02;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
   packet.m_packetType = 0x05;	// Server BW
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   packet.m_nBodySize = 4;
 
@@ -1083,12 +1117,15 @@ static bool
 SendBytesReceived(RTMP * r)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x02;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x03;	// bytes in
   packet.m_nInfoField1 = 0;
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   packet.m_nBodySize = 4;
 
@@ -1105,12 +1142,15 @@ static bool
 SendCheckBW(RTMP * r)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField1 = RTMP_GetTime();
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av__checkbw);
@@ -1129,12 +1169,15 @@ static bool
 SendCheckBWResult(RTMP * r, double txn)
 {
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x03;	// control channel (invoke)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField1 = 0x16 * r->m_nBWCheckCounter;	// temp inc value. till we figure it out.
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av__result);
@@ -1153,12 +1196,15 @@ static bool
 SendPlay(RTMP * r)
 {
   RTMPPacket packet;
+  char pbuf[1024];
+
   packet.m_nChannel = 0x08;	// we make 8 our stream channel
   packet.m_headerType = RTMP_PACKET_SIZE_LARGE;
   packet.m_packetType = 0x14;	// INVOKE
   packet.m_nInfoField2 = r->m_stream_id;	//0x01000000;
   packet.m_nInfoField1 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   char *enc = packet.m_body;
   enc += AMF_EncodeString(enc, &av_play);
@@ -1221,12 +1267,15 @@ SendCtrl(RTMP * r, short nType, unsigned
   Log(LOGDEBUG, "sending ctrl. type: 0x%04x", (unsigned short) nType);
 
   RTMPPacket packet;
+  char pbuf[256];
+
   packet.m_nChannel = 0x02;	// control channel (ping)
   packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
   packet.m_packetType = 0x04;	// ctrl
   packet.m_nInfoField1 = RTMP_GetTime();
   packet.m_nInfoField2 = 0;
   packet.m_hasAbsTimestamp = 0;
+  packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE;
 
   int nSize = (nType == 0x03 ? 10 : 6);	// type 3 is the buffer time and requires all 3 parameters. all in all 10 bytes.
   if (nType == 0x1B)
@@ -1714,9 +1763,6 @@ ReadPacket(RTMP * r, RTMPPacket * packet
       return false;
     }
 
-  if (packet->m_nBodySize && RTMPPacket_IsReady(packet))
-    RTMPPacket_Reset(packet);
-
   packet->m_headerType = (type & 0xc0) >> 6;
   packet->m_nChannel = (type & 0x3f);
   if (packet->m_nChannel == 0)
@@ -1750,16 +1796,16 @@ ReadPacket(RTMP * r, RTMPPacket * packet
   if (nSize == RTMP_LARGE_HEADER_SIZE)	// if we get a full header the timestamp is absolute
     packet->m_hasAbsTimestamp = true;
 
-  if (nSize < RTMP_LARGE_HEADER_SIZE)
+  else if (nSize < RTMP_LARGE_HEADER_SIZE)
     {				// using values from the last message of this channel
       if (r->m_vecChannelsIn[packet->m_nChannel])
 	memcpy(packet, r->m_vecChannelsIn[packet->m_nChannel],
-	       offsetof(RTMPPacket, m_header));
+	       sizeof(RTMPPacket));
     }
 
   nSize--;
 
-  char *header = packet->m_header;
+  char header[RTMP_LARGE_HEADER_SIZE] = { 0 };
   if (nSize > 0 && ReadN(r, header, nSize) != nSize)
     {
       Log(LOGERROR, "%s, failed to read RTMP packet header. type: %x",
@@ -1768,21 +1814,37 @@ ReadPacket(RTMP * r, RTMPPacket * packet
     }
 
   if (nSize >= 3)
-    packet->m_nInfoField1 = AMF_DecodeInt24(header);
+    {
+      packet->m_nInfoField1 = AMF_DecodeInt24(header);
 
-  //Log(LOGDEBUG, "%s, reading RTMP packet chunk on channel %x, headersz %i, timestamp %i, abs timestamp %i", __FUNCTION__, packet.m_nChannel, nSize, packet.m_nInfoField1, packet.m_hasAbsTimestamp); 
+      //Log(LOGDEBUG, "%s, reading RTMP packet chunk on channel %x, headersz %i, timestamp %i, abs timestamp %i", __FUNCTION__, packet.m_nChannel, nSize, packet.m_nInfoField1, packet.m_hasAbsTimestamp); 
 
-  if (nSize >= 6)
-    {
-      packet->m_nBodySize = AMF_DecodeInt24(header + 3);
-      packet->m_nBytesRead = 0;
-    }
+      if (nSize >= 6)
+	{
+	  packet->m_nBodySize = AMF_DecodeInt24(header + 3);
+	  packet->m_nBytesRead = 0;
+	  RTMPPacket_Free(packet);
 
-  if (nSize > 6)
-    packet->m_packetType = header[6];
+	  if (nSize > 6)
+	    {
+	      packet->m_packetType = header[6];
 
-  if (nSize == 11)
-    packet->m_nInfoField2 = ReadInt32LE(header + 7);
+	      if (nSize == 11)
+		packet->m_nInfoField2 = ReadInt32LE(header + 7);
+	    }
+	}
+    }
+
+  bool didAlloc = false;
+  if (packet->m_nBodySize > 0 && packet->m_body == NULL)
+    {
+      if (!RTMPPacket_Alloc(packet, packet->m_nBodySize))
+	{
+	  Log(LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__);
+	  return false;
+	}
+      didAlloc = true;
+    }
 
   int nToRead = packet->m_nBodySize - packet->m_nBytesRead;
   int nChunk = r->m_chunkSize;
@@ -1800,10 +1862,8 @@ ReadPacket(RTMP * r, RTMPPacket * packet
 
   // keep the packet as ref for other packets on this channel
   if (!r->m_vecChannelsIn[packet->m_nChannel])
-    r->m_vecChannelsIn[packet->m_nChannel] =
-      malloc(offsetof(RTMPPacket, m_header));
-  memcpy(r->m_vecChannelsIn[packet->m_nChannel], packet,
-	 offsetof(RTMPPacket, m_header));
+    r->m_vecChannelsIn[packet->m_nChannel] = malloc(sizeof(RTMPPacket));
+  memcpy(r->m_vecChannelsIn[packet->m_nChannel], packet, sizeof(RTMPPacket));
 
   if (RTMPPacket_IsReady(packet))
     {
@@ -1817,9 +1877,14 @@ ReadPacket(RTMP * r, RTMPPacket * packet
 
       // reset the data from the stored packet. we keep the header since we may use it later if a new packet for this channel
       // arrives and requests to re-use some info (small packet header)
+      r->m_vecChannelsIn[packet->m_nChannel]->m_body = NULL;
       r->m_vecChannelsIn[packet->m_nChannel]->m_nBytesRead = 0;
       r->m_vecChannelsIn[packet->m_nChannel]->m_hasAbsTimestamp = false;	// can only be false if we reuse header
     }
+  else
+    {
+      packet->m_body = NULL;	/* so it won't be erased on free */
+    }
 
   return true;
 }
@@ -2015,10 +2080,8 @@ SendRTMP(RTMP * r, RTMPPacket * packet, 
     }
 
   if (!r->m_vecChannelsOut[packet->m_nChannel])
-    r->m_vecChannelsOut[packet->m_nChannel] =
-      malloc(offsetof(RTMPPacket, m_header));
-  memcpy(r->m_vecChannelsOut[packet->m_nChannel], packet,
-	 offsetof(RTMPPacket, m_header));
+    r->m_vecChannelsOut[packet->m_nChannel] = malloc(sizeof(RTMPPacket));
+  memcpy(r->m_vecChannelsOut[packet->m_nChannel], packet, sizeof(RTMPPacket));
   return true;
 }
 
@@ -2044,6 +2107,7 @@ RTMP_Close(RTMP * r)
     {
       if (r->m_vecChannelsIn[i])
 	{
+	  RTMPPacket_Free(r->m_vecChannelsIn[i]);
 	  free(r->m_vecChannelsIn[i]);
 	  r->m_vecChannelsIn[i] = NULL;
 	}

Modified: rtmp2.h
==============================================================================
--- rtmp2.h	Wed Dec 16 07:26:45 2009	(r64)
+++ rtmp2.h	Wed Dec 16 09:30:52 2009	(r65)
@@ -84,12 +84,14 @@ typedef struct RTMPPacket
   uint32_t m_nTimeStamp;	// absolute timestamp
   uint32_t m_nBodySize;
   uint32_t m_nBytesRead;
-  char m_header[RTMP_MAX_HEADER_SIZE];
-  char m_body[16384];
+  char *m_body;
 } RTMPPacket;
 
-void RTMPPacket_Reset(RTMPPacket * p);
-void RTMPPacket_Dump(RTMPPacket * p);
+void RTMPPacket_Reset(RTMPPacket *p);
+void RTMPPacket_Dump(RTMPPacket *p);
+bool RTMPPacket_Alloc(RTMPPacket *p, int nSize);
+void RTMPPacket_Free(RTMPPacket *p);
+
 #define RTMPPacket_IsReady(a)	((a)->m_nBytesRead == (a)->m_nBodySize)
 
 typedef struct RTMP_LNK

Modified: rtmpd2.c
==============================================================================
--- rtmpd2.c	Wed Dec 16 07:26:45 2009	(r64)
+++ rtmpd2.c	Wed Dec 16 09:30:52 2009	(r65)
@@ -154,55 +154,58 @@ WriteStream(RTMP * rtmp, char **buf,	// 
   static bool bFoundFlvKeyframe = false;
 
   uint32_t prevTagSize = 0;
-  int rtnGetNextMediaPacket = 0;
-  char pbuf[131072];
-  RTMPPacket *packet = (RTMPPacket *) pbuf;
+  int rtnGetNextMediaPacket = 0, ret = -1;
+  RTMPPacket packet = { 0 };
 
-  rtnGetNextMediaPacket = RTMP_GetNextMediaPacket(rtmp, packet);
-  if (rtnGetNextMediaPacket)
+  rtnGetNextMediaPacket = RTMP_GetNextMediaPacket(rtmp, &packet);
+  while (rtnGetNextMediaPacket)
     {
-      char *packetBody = packet->m_body;
-      unsigned int nPacketLen = packet->m_nBodySize;
+      char *packetBody = packet.m_body;
+      unsigned int nPacketLen = packet.m_nBodySize;
 
       // Return -3 if this was completed nicely with invoke message Play.Stop or Play.Complete
       if (rtnGetNextMediaPacket == 2)
 	{
 	  Log(LOGDEBUG,
 	      "Got Play.Complete or Play.Stop from server. Assuming stream is complete");
-	  return -3;
+	  ret = -3;
+	  break;
 	}
 
       // skip video info/command packets
-      if (packet->m_packetType == 0x09 &&
+      if (packet.m_packetType == 0x09 &&
 	  nPacketLen == 2 && ((*packetBody & 0xf0) == 0x50))
 	{
-	  return 0;
+	  ret = 0;
+	  break;
 	}
 
-      if (packet->m_packetType == 0x09 && nPacketLen <= 5)
+      if (packet.m_packetType == 0x09 && nPacketLen <= 5)
 	{
 	  Log(LOGWARNING, "ignoring too small video packet: size: %d",
 	      nPacketLen);
-	  return 0;
+	  ret = 0;
+	  break;
 	}
-      if (packet->m_packetType == 0x08 && nPacketLen <= 1)
+      if (packet.m_packetType == 0x08 && nPacketLen <= 1)
 	{
 	  Log(LOGWARNING, "ignoring too small audio packet: size: %d",
 	      nPacketLen);
-	  return 0;
+	  ret = 0;
+	  break;
 	}
 #ifdef _DEBUG
       Log(LOGDEBUG, "type: %02X, size: %d, TS: %d ms, abs TS: %d",
-	  packet->m_packetType, nPacketLen, packet->m_nTimeStamp,
-	  packet->m_hasAbsTimestamp);
-      if (packet->m_packetType == 0x09)
+	  packet.m_packetType, nPacketLen, packet.m_nTimeStamp,
+	  packet.m_hasAbsTimestamp);
+      if (packet.m_packetType == 0x09)
 	Log(LOGDEBUG, "frametype: %02X", (*packetBody & 0xf0));
 #endif
 
       // check the header if we get one
-      if (bResume && packet->m_nTimeStamp == 0)
+      if (bResume && packet.m_nTimeStamp == 0)
 	{
-	  if (nMetaHeaderSize > 0 && packet->m_packetType == 0x12)
+	  if (nMetaHeaderSize > 0 && packet.m_packetType == 0x12)
 	    {
 
 	      AMFObject metaObj;
@@ -220,9 +223,12 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 			  (memcmp(metaHeader, packetBody, nMetaHeaderSize) !=
 			   0))
 			{
-			  return -2;
+			  ret = -2;
 			}
 		    }
+		  AMF_Reset(&metaObj);
+		  if (ret == -2)
+		    break;
 		}
 	    }
 
@@ -232,7 +238,7 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 	    {
 
 	      // video or audio data
-	      if (packet->m_packetType == initialFrameType
+	      if (packet.m_packetType == initialFrameType
 		  && nInitialFrameSize == nPacketLen)
 		{
 		  // we don't compare the sizes since the packet can contain several FLV packets, just make
@@ -242,7 +248,8 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 		    {
 		      Log(LOGDEBUG, "Checked keyframe successfully!");
 		      bFoundKeyframe = true;
-		      return 0;	// ignore it! (what about audio data after it? it is handled by ignoring all 0ms frames, see below)
+		      ret = 0;	// ignore it! (what about audio data after it? it is handled by ignoring all 0ms frames, see below)
+		      break;
 		    }
 		}
 
@@ -250,7 +257,7 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 	      // it is also included in the first FLV stream chunk and we have to compare it and
 	      // filter it out !!
 	      //
-	      if (packet->m_packetType == 0x16)
+	      if (packet.m_packetType == 0x16)
 		{
 		  // basically we have to find the keyframe with the correct TS being nResumeTS
 		  unsigned int pos = 0;
@@ -283,7 +290,8 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 				{
 				  Log(LOGERROR,
 				      "FLV Stream: Keyframe doesn't match!");
-				  return -2;
+				  ret = -2;
+				  break;
 				}
 			      bFoundFlvKeyframe = true;
 
@@ -293,7 +301,8 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 				{
 				  Log(LOGWARNING,
 				      "Non skipable packet since it doesn't end with chunk, stream corrupt!");
-				  return -2;
+				  ret = -2;
+				  break;
 				}
 			      packetBody += (pos + 11 + dataSize + 4);
 			      nPacketLen -= (pos + 11 + dataSize + 4);
@@ -319,13 +328,14 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 		    {
 		      Log(LOGERROR,
 			  "Couldn't find the seeked keyframe in this chunk!");
-		      return 0;
+		      ret = 0;
+		      break;
 		    }
 		}
 	    }
 	}
 
-      if (bResume && packet->m_nTimeStamp > 0
+      if (bResume && packet.m_nTimeStamp > 0
 	  && (bFoundFlvKeyframe || bFoundKeyframe))
 	{
 	  // another problem is that the server can actually change from 09/08 video/audio packets to an FLV stream
@@ -340,34 +350,39 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 	}
 
       // skip till we find out keyframe (seeking might put us somewhere before it)
-      if (bResume && !bFoundKeyframe && packet->m_packetType != 0x16)
+      if (bResume && !bFoundKeyframe && packet.m_packetType != 0x16)
 	{
 	  Log(LOGWARNING,
 	      "Stream does not start with requested frame, ignoring data... ");
 	  nIgnoredFrameCounter++;
 	  if (nIgnoredFrameCounter > MAX_IGNORED_FRAMES)
-	    return -2;		// fatal error, couldn't continue stream
-	  return 0;
+	    ret = -2;		// fatal error, couldn't continue stream
+	  else
+	    ret = 0;
+	  break;
 	}
       // ok, do the same for FLV streams
-      if (bResume && !bFoundFlvKeyframe && packet->m_packetType == 0x16)
+      if (bResume && !bFoundFlvKeyframe && packet.m_packetType == 0x16)
 	{
 	  Log(LOGWARNING,
 	      "Stream does not start with requested FLV frame, ignoring data... ");
 	  nIgnoredFlvFrameCounter++;
 	  if (nIgnoredFlvFrameCounter > MAX_IGNORED_FRAMES)
-	    return -2;
-	  return 0;
+	    ret = -2;
+	  else
+	    ret = 0;
+	  break;
 	}
 
       // if bResume, we continue a stream, we have to ignore the 0ms frames since these are the first keyframes, we've got these
       // so don't mess around with multiple copies sent by the server to us! (if the keyframe is found at a later position
       // there is only one copy and it will be ignored by the preceding if clause)
-      if (!bStopIgnoring && bResume && packet->m_packetType != 0x16)
+      if (!bStopIgnoring && bResume && packet.m_packetType != 0x16)
 	{			// exclude type 0x16 (FLV) since it can conatin several FLV packets
-	  if (packet->m_nTimeStamp == 0)
+	  if (packet.m_nTimeStamp == 0)
 	    {
-	      return 0;
+	      ret = 0;
+	      break;
 	    }
 	  else
 	    {
@@ -378,9 +393,9 @@ WriteStream(RTMP * rtmp, char **buf,	// 
       // calculate packet size and reallocate buffer if necessary
       unsigned int size = nPacketLen
 	+
-	((packet->m_packetType == 0x08 || packet->m_packetType == 0x09
-	  || packet->m_packetType ==
-	  0x12) ? 11 : 0) + (packet->m_packetType != 0x16 ? 4 : 0);
+	((packet.m_packetType == 0x08 || packet.m_packetType == 0x09
+	  || packet.m_packetType ==
+	  0x12) ? 11 : 0) + (packet.m_packetType != 0x16 ? 4 : 0);
 
       if (size + 4 > len)
 	{			// the extra 4 is for the case of an FLV stream without a last prevTagSize (we need extra 4 bytes to append it)
@@ -388,7 +403,8 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 	  if (*buf == 0)
 	    {
 	      Log(LOGERROR, "Couldn't reallocate memory!");
-	      return -1;	// fatal error
+	      ret = -1;		// fatal error
+	      break;
 	    }
 	}
       char *ptr = *buf;
@@ -397,18 +413,18 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 
       // audio (0x08), video (0x09) or metadata (0x12) packets :
       // construct 11 byte header then add rtmp packet's data
-      if (packet->m_packetType == 0x08 || packet->m_packetType == 0x09
-	  || packet->m_packetType == 0x12)
+      if (packet.m_packetType == 0x08 || packet.m_packetType == 0x09
+	  || packet.m_packetType == 0x12)
 	{
 	  // set data type
 	  *dataType |=
-	    (((packet->m_packetType == 0x08) << 2) | (packet->m_packetType ==
-						      0x09));
+	    (((packet.m_packetType == 0x08) << 2) | (packet.m_packetType ==
+						     0x09));
 
-	  nTimeStamp = nResumeTS + packet->m_nTimeStamp;
+	  nTimeStamp = nResumeTS + packet.m_nTimeStamp;
 	  prevTagSize = 11 + nPacketLen;
 
-	  *ptr = packet->m_packetType;
+	  *ptr = packet.m_packetType;
 	  ptr++;
 	  ptr += AMF_EncodeInt24(ptr, nPacketLen);
 
@@ -441,7 +457,7 @@ WriteStream(RTMP * rtmp, char **buf,	// 
       unsigned int len = nPacketLen;
 
       // correct tagSize and obtain timestamp if we have an FLV stream
-      if (packet->m_packetType == 0x16)
+      if (packet.m_packetType == 0x16)
 	{
 	  unsigned int pos = 0;
 
@@ -467,7 +483,8 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 		      Log(LOGERROR,
 			  "Wrong data size (%lu), stream corrupted, aborting!",
 			  dataSize);
-		      return -2;
+		      ret = -2;
+		      break;
 		    }
 		  Log(LOGWARNING, "No tagSize found, appending!");
 
@@ -507,7 +524,7 @@ WriteStream(RTMP * rtmp, char **buf,	// 
 	}
       ptr += len;
 
-      if (packet->m_packetType != 0x16)
+      if (packet.m_packetType != 0x16)
 	{			// FLV tag packets contain their own prevTagSize
 	  AMF_EncodeInt32(ptr, prevTagSize);
 	  //ptr += 4;
@@ -517,13 +534,15 @@ WriteStream(RTMP * rtmp, char **buf,	// 
       // Update ext timestamp with this absolute offset in non-live mode otherwise report the relative one
       // LogPrintf("\nDEBUG: type: %02X, size: %d, pktTS: %dms, TS: %dms, bLiveStream: %d", packet.m_packetType, nPacketLen, packet.m_nTimeStamp, nTimeStamp, bLiveStream);
       if (tsm)
-	*tsm = bLiveStream ? packet->m_nTimeStamp : nTimeStamp;
+	*tsm = bLiveStream ? packet.m_nTimeStamp : nTimeStamp;
 
 
-      return size;
+      ret = size;
+      break;
     }
 
-  return -1;			// no more media packets
+  RTMPPacket_Free(&packet);
+  return ret;			// no more media packets
 }
 
 int


More information about the rtmpdump mailing list