📄 rtp.c
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose. In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/
/*!
*****************************************************************************
*
* \file rtp.c
*
* \brief
* Functions to handle RTP headers and packets per RFC1889 and RTP NAL spec
* Functions support little endian systems only (Intel, not Motorola/Sparc)
*
* \date
* 30 September 2001
*
* \author
* Stephan Wenger stewe@cs.tu-berlin.de
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <memory.h>
#include <malloc.h>
#include "rtp.h"
#include "elements.h"
#include "defines.h"
#include "header.h"
#include "global.h"
// A little trick to avoid those horrible #if TRACE all over the source code
#if TRACE
#define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
#else
#define SYMTRACESTRING(s) // to nothing
#endif
int CurrentRTPTimestamp = 0; //! The RTP timestamp of the current packet,
//! incremented with all P and I frames
int CurrentRTPSequenceNumber = 0; //! The RTP sequence number of the current packet
//! incremented by one for each sent packet
/*!
*****************************************************************************
*
* \brief
* ComposeRTPpacket composes the complete RTP packet using the various
* structure members of the RTPpacket_t structure
*
* \return
* 0 in case of success
* negative error code in case of failure
*
* \para Parameters
* Caller is responsible to allocate enough memory for the generated packet
* in parameter->packet. Typically a malloc of 12+paylen bytes is sufficient
*
* \para Side effects
* none
*
* \para Other Notes
* Function contains assert() tests for debug purposes (consistency checks
* for RTP header fields
*
* \date
* 30 Spetember 2001
*
* \author
* Stephan Wenger stewe@cs.tu-berlin.de
*****************************************************************************/
int ComposeRTPPacket (RTPpacket_t *p)
{
// Consistency checks through assert, only used for debug purposes
assert (p->v == 2);
assert (p->p == 0);
assert (p->x == 0);
assert (p->cc == 0); // mixer designers need to change this one
assert (p->m == 0 || p->m == 1);
assert (p->pt < 128);
assert (p->seq < 65536);
assert (p->payload != NULL);
assert (p->paylen < 65536 - 40); // 2**16 -40 for IP/UDP/RTP header
assert (p->packet != NULL);
// Compose RTP header, little endian
p->packet[0] = ( (p->v)
| (p->p << 2)
| (p->x << 3)
| (p->cc << 4) );
p->packet[1] = ( (p->m)
| (p->pt << 1) );
p->packet[2] = p->seq & 0xff;
p->packet[3] = (p->seq >> 8) & 0xff;
memcpy (&p->packet[4], &p->timestamp, 4); // change to shifts for unified byte sex
memcpy (&p->packet[8], &p->ssrc, 4);// change to shifts for unified byte sex
// Copy payload
memcpy (&p->packet[12], p->payload, p->paylen);
p->packlen = p->paylen+12;
return 0;
}
/*!
*****************************************************************************
*
* \brief
* WriteRTPPacket writes the supplied RTP packet to the output file
*
* \return
* 0 in case of access
* <0 in case of write failure (typically fatal)
*
* \para Parameters
* p: the RTP packet to be written (after ComposeRTPPacket() )
* f: output file
*
* \para Side effects
* none
*
* \date
* October 23, 2001
*
* \author
* Stephan Wenger stewe@cs.tu-berlin.de
*****************************************************************************/
int WriteRTPPacket (RTPpacket_t *p, FILE *f)
{
int intime = -1;
assert (f != NULL);
assert (p != NULL);
if (1 != fwrite (&p->packlen, 4, 1, f))
return -1;
if (1 != fwrite (&intime, 4, 1, f))
return -1;
if (1 != fwrite (p->packet, p->packlen, 1, f))
return -1;
return 0;
}
/*!
*****************************************************************************
*
* \brief
* ComposeHeaderPacketPayload generates the payload for a header packet
* using the inp-> structure contents. The header packet contains
* definitions for a single parameter set 0, which is used for all
* slices of the picture
*
* \return
* len of the genberated payload in bytes
* <0 in case of failure (typically fatal)
*
* \para Parameters
* p: the payload of the RTP packet to be written
*
* \para Side effects
* none
*
* \note
* This function should be revisited and checked in case of additional
* bit stream parameters that affect the picture header (or higher
* entitries). Typical examples are more entropy coding schemes, other
* motion vector resolutiuon, and optional elements
*
* \date
* October 23, 2001
*
* \author
* Stephan Wenger stewe@cs.tu-berlin.de
*****************************************************************************/
int ComposeHeaderPacketPayload (byte *payload)
{
int slen=0;
int multpred;
assert (img->width%16 == 0);
assert (img->height%16 == 0);
#ifdef _ADDITIONAL_REFERENCE_FRAME_
if (input->no_multpred <= 1 && input->add_ref_frame == 0)
#else
if (input->no_multpred <= 1)
#endif
multpred=FALSE;
else
multpred=TRUE; // multiple reference frames in motion search
slen = snprintf (payload, MAXRTPPAYLOADLEN,
"a=H26L (0) MaxPicID %d\
\na=H26L (0) UseMultpred %d\
\na=H26L (0) MaxPn %d\
\na=H26L (0) BufCycle %d\
\na=H26L (0) PixAspectRatioX 1\
\na=H26L (0) PixAspectRatioY 1\
\na=H26L (0) DisplayWindowOffsetTop 0\
\na=H26L (0) DisplayWindowOffsetBottom 0\
\na=H26L (0) DisplayWindowOffsetRight 0\
\na=H26L (0) DisplayWindowOffsetLeft 0\
\na=H26L (0) XSizeMB %d\
\na=H26L (0) YSizeMB %d\
\na=H26L (0) EntropyCoding %s\
\na=H26L (0) MotionResolution %s\
\na=H26L (0) PartitioningType %s\
\na=H26L (0) IntraPredictionType %s\
\na=H26L (0) HRCParameters 0\
\
\na=H26L (-1) FramesToBeEncoded %d\
\na=H26L (-1) FrameSkip %d\
\na=H26L (-1) SequenceFileName %s\
\na=H26L (0) NumberBFrames %d\
%c%c",
256,
multpred==TRUE?1:0,
input->no_multpred,
input->no_multpred,
input->img_width/16,
input->img_height/16,
input->symbol_mode==UVLC?"UVLC":"CABAC",
input->mv_res==0?"quater":"eigth",
input->partition_mode==0?"one":"three",
input->UseConstrainedIntraPred==0?"InterPredicted":"NotInterPredicted",
input->no_frames,
input->jumpd,
input->infile,
input->successive_Bframe,
4, // Unix Control D EOF symbol
26); // MS-DOS/Windows Control Z EOF Symbol
return slen;
}
/*!
*****************************************************************************
*
* \brief
* void RTPSequenceHeader (FILE *out) write the RTP Sequence header in the
* form of a header packet into the outfile. It is assumed that the
* input-> structure is already filled (config file is parsed)
*
* \return
* length of sequence header
*
* \para Parameters
* out: fiel pointer of the output file
*
* \para Side effects
* header written, statistics are updated, RTP sequence number updated
*
* \para Note
* This function uses alloca() to reserve memry on the stack -- no freeing and
* no return value check necessary, error lead to stack overflows
*
* \date
* October 24, 2001
*
* \author
* Stephan Wenger stewe@cs.tu-berlin.de
*****************************************************************************/
int RTPSequenceHeader (FILE *out)
{
RTPpacket_t *p;
assert (out != NULL);
// Set RTP structure elements and alloca() memory foor the buffers
p = (RTPpacket_t *) alloca (sizeof (RTPpacket_t));
p->packet=alloca (MAXRTPPACKETSIZE);
p->payload=alloca (MAXRTPPACKETSIZE);
p->v=2;
p->p=0;
p->x=0;
p->cc=0;
p->m=1;
p->pt=H26LPAYLOADTYPE;
p->seq=CurrentRTPSequenceNumber++;
p->timestamp=CurrentRTPTimestamp;
p->ssrc=H26LSSRC;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -