rfc3984.c 9.25 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

Simon Morlat's avatar
Simon Morlat committed
20

aymeric's avatar
aymeric committed
21 22 23 24 25 26 27
#include "mediastreamer2/rfc3984.h"
#include "mediastreamer2/msfilter.h"

#define TYPE_FU_A 28    /*fragmented unit 0x1C*/
#define TYPE_STAP_A 24  /*single time aggregation packet  0x18*/


28
static MS2_INLINE void nal_header_init(uint8_t *h, uint8_t nri, uint8_t type){
aymeric's avatar
aymeric committed
29 30 31
	*h=((nri&0x3)<<5) | (type & ((1<<5)-1));
}

32
static MS2_INLINE uint8_t nal_header_get_type(const uint8_t *h){
aymeric's avatar
aymeric committed
33 34 35
	return (*h) & ((1<<5)-1);
}

36
static MS2_INLINE uint8_t nal_header_get_nri(const uint8_t *h){
aymeric's avatar
aymeric committed
37 38 39
	return ((*h) >> 5) & 0x3;
}

40
Rfc3984Context *rfc3984_new(void){
41 42
	Rfc3984Context *ctx=ms_new0(Rfc3984Context,1);
	rfc3984_init(ctx);
43 44 45 46 47 48 49 50
	return ctx;
}

void rfc3984_destroy(Rfc3984Context *ctx){
	rfc3984_uninit (ctx);
	ms_free(ctx);
}

aymeric's avatar
aymeric committed
51 52 53 54 55 56 57 58 59 60 61 62 63
void rfc3984_init(Rfc3984Context *ctx){
	ms_queue_init(&ctx->q);
	ctx->m=NULL;
	ctx->maxsz=ms_get_payload_max_size();
	ctx->mode=0;
	ctx->last_ts=0x943FEA43;/*some random value*/
	ctx->stap_a_allowed=TRUE;
}

void rfc3984_set_max_payload_size(Rfc3984Context *ctx, int size){
	ctx->maxsz=size;
}

64
static void send_packet(Rfc3984Context *ctx, MSQueue *rtpq, uint32_t ts, mblk_t *m, bool_t marker){
aymeric's avatar
aymeric committed
65 66
	mblk_set_timestamp_info(m,ts);
	mblk_set_marker_info(m,marker);
67
	mblk_set_cseq(m, ctx->ref_cseq++);
aymeric's avatar
aymeric committed
68 69 70
	ms_queue_put(rtpq,m);
}

71 72
static void put_nal_size(mblk_t *m, size_t sz){
	uint16_t size=htons((uint16_t)sz);
aymeric's avatar
aymeric committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	*(uint16_t*)m->b_wptr=size;
	m->b_wptr+=2;
}

static mblk_t * prepend_stapa(mblk_t *m){
	mblk_t *hm=allocb(3,0);
	nal_header_init(hm->b_wptr,nal_header_get_nri(m->b_rptr),TYPE_STAP_A);
	hm->b_wptr+=1;
	put_nal_size(hm,msgdsize(m));
	hm->b_cont=m;
	return hm;
}

static mblk_t * concat_nalus(mblk_t *m1, mblk_t *m2){
	mblk_t *l=allocb(2,0);
	/*eventually append a stap-A header to m1, if not already done*/
	if (nal_header_get_type(m1->b_rptr)!=TYPE_STAP_A){
		m1=prepend_stapa(m1);
	}
	put_nal_size(l,msgdsize(m2));
	l->b_cont=m2;
	concatb(m1,l);
	return m1;
}

static mblk_t *prepend_fu_indicator_and_header(mblk_t *m, uint8_t indicator,
	bool_t start, bool_t end, uint8_t type){
	mblk_t *h=allocb(2,0);
	h->b_wptr[0]=indicator;
	h->b_wptr[1]=((start&0x1)<<7)|((end&0x1)<<6)|type;
	h->b_wptr+=2;
	h->b_cont=m;
	if (start) m->b_rptr++;/*skip original nalu header */
	return h;
}

109
static void frag_nalu_and_send(Rfc3984Context *ctx, MSQueue *rtpq, uint32_t ts, mblk_t *nalu, bool_t marker, int maxsize){
aymeric's avatar
aymeric committed
110 111 112 113 114 115 116 117 118 119 120 121 122
	mblk_t *m;
	int payload_max_size=maxsize-2;/*minus FUA header*/
	uint8_t fu_indicator;
	uint8_t type=nal_header_get_type(nalu->b_rptr);
	uint8_t nri=nal_header_get_nri(nalu->b_rptr);
	bool_t start=TRUE;

	nal_header_init(&fu_indicator,nri,TYPE_FU_A);
	while(nalu->b_wptr-nalu->b_rptr>payload_max_size){
		m=dupb(nalu);
		nalu->b_rptr+=payload_max_size;
		m->b_wptr=nalu->b_rptr;
		m=prepend_fu_indicator_and_header(m,fu_indicator,start,FALSE,type);
123
		send_packet(ctx, rtpq,ts,m,FALSE);
aymeric's avatar
aymeric committed
124 125 126 127
		start=FALSE;
	}
	/*send last packet */
	m=prepend_fu_indicator_and_header(nalu,fu_indicator,FALSE,TRUE,type);
128
	send_packet(ctx, rtpq,ts,m,marker);
aymeric's avatar
aymeric committed
129 130 131 132 133 134 135 136
}

static void rfc3984_pack_mode_0(Rfc3984Context *ctx, MSQueue *naluq, MSQueue *rtpq, uint32_t ts){
	mblk_t *m;
	bool_t end;
	int size;
	while((m=ms_queue_get(naluq))!=NULL){
		end=ms_queue_empty(naluq);
137
		size=(int)(m->b_wptr-m->b_rptr);
aymeric's avatar
aymeric committed
138 139 140
		if (size>ctx->maxsz){
			ms_warning("This H264 packet does not fit into mtu: size=%i",size);
		}
141
		send_packet(ctx, rtpq,ts,m,end);
aymeric's avatar
aymeric committed
142 143 144 145 146 147 148 149 150 151
	}
}

/*process NALUs and pack them into rtp payloads */
static void rfc3984_pack_mode_1(Rfc3984Context *ctx, MSQueue *naluq, MSQueue *rtpq, uint32_t ts){
	mblk_t *m,*prevm=NULL;
	int prevsz=0,sz;
	bool_t end;
	while((m=ms_queue_get(naluq))!=NULL){
		end=ms_queue_empty(naluq);
152
		sz=(int)(m->b_wptr-m->b_rptr);
aymeric's avatar
aymeric committed
153 154 155 156 157 158 159 160 161 162 163 164 165
		if (ctx->stap_a_allowed){
			if (prevm!=NULL){
				if ((prevsz+sz)<(ctx->maxsz-2)){
					prevm=concat_nalus(prevm,m);
					m=NULL;
					prevsz+=sz+2;/*+2 for the stapa size field*/
					continue;
				}else{
					/*send prevm packet: either single nal or STAP-A*/
					if (prevm->b_cont!=NULL){
						ms_debug("Sending STAP-A");
					}else
						ms_debug("Sending previous msg as single NAL");
166
					send_packet(ctx, rtpq,ts,prevm,FALSE);
aymeric's avatar
aymeric committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
					prevm=NULL;
					prevsz=0;
				}
			}
			if (sz<(ctx->maxsz/2)){
				/*try to aggregate it with next packet*/
				prevm=m;
				prevsz=sz+3; /*STAP-A header + size*/
				m=NULL;
			}else{
				
				/*send as single nal or FU-A*/
				if (sz>ctx->maxsz){
					ms_debug("Sending FU-A packets");
181
					frag_nalu_and_send(ctx, rtpq,ts,m,end, ctx->maxsz);
aymeric's avatar
aymeric committed
182 183
				}else{
					ms_debug("Sending Single NAL");
184
					send_packet(ctx, rtpq,ts,m,end);
aymeric's avatar
aymeric committed
185 186 187 188 189
				}
			}
		}else{
			if (sz>ctx->maxsz){
				ms_debug("Sending FU-A packets");
190
				frag_nalu_and_send(ctx, rtpq,ts,m,end, ctx->maxsz);
aymeric's avatar
aymeric committed
191 192
			}else{
				ms_debug("Sending Single NAL");
193
				send_packet(ctx, rtpq,ts,m,end);
aymeric's avatar
aymeric committed
194 195 196 197 198
			}
		}
	}
	if (prevm){
		ms_debug("Sending Single NAL (2)");
199
		send_packet(ctx, rtpq,ts,prevm,TRUE);
aymeric's avatar
aymeric committed
200 201 202 203 204 205 206 207 208 209 210 211 212
	}
}

static mblk_t * aggregate_fua(Rfc3984Context *ctx, mblk_t *im){
	mblk_t *om=NULL;
	uint8_t fu_header;
	uint8_t nri,type;
	bool_t start,end;
	fu_header=im->b_rptr[1];
	type=nal_header_get_type(&fu_header);
	start=fu_header>>7;
	end=(fu_header>>6)&0x1;
	if (start){
213
		mblk_t *new_header;
aymeric's avatar
aymeric committed
214 215 216 217 218 219 220
		nri=nal_header_get_nri(im->b_rptr);
		if (ctx->m!=NULL){
			ms_error("receiving FU-A start while previous FU-A is not "
				"finished");
			freemsg(ctx->m);
			ctx->m=NULL;
		}
221 222 223 224 225 226 227 228
		im->b_rptr+=2; /*skip the nal header and the fu header*/
		new_header=allocb(1,0); /* allocate small fragment to put the correct nal header, this is to avoid to write on the buffer
					which can break processing of other users of the buffers */
		nal_header_init(new_header->b_wptr,nri,type);
		new_header->b_wptr++;
		mblk_meta_copy(im,new_header);
		concatb(new_header,im);
		ctx->m=new_header;
aymeric's avatar
aymeric committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	}else{
		if (ctx->m!=NULL){
			im->b_rptr+=2;
			concatb(ctx->m,im);
		}else{
			ms_error("Receiving continuation FU packet but no start.");
			freemsg(im);
		}
	}
	if (end && ctx->m){
		msgpullup(ctx->m,-1);
		om=ctx->m;
		ctx->m=NULL;
	}
	return om;
}

/*process incoming rtp data and output NALUs, whenever possible*/
247
int rfc3984_unpack(Rfc3984Context *ctx, mblk_t *im, MSQueue *out){
aymeric's avatar
aymeric committed
248 249 250 251
	uint8_t type=nal_header_get_type(im->b_rptr);
	uint8_t *p;
	int marker = mblk_get_marker_info(im);
	uint32_t ts=mblk_get_timestamp_info(im);
252 253
	uint16_t cseq=mblk_get_cseq(im);
	int res = 0;
aymeric's avatar
aymeric committed
254 255 256 257 258 259

	if (ctx->last_ts!=ts){
		/*a new frame is arriving, in case the marker bit was not set in previous frame, output it now*/
		/* unless this is a FU-A (workarond some other apps bugs)*/
		ctx->last_ts=ts;
		if (ctx->m==NULL){
Simon Morlat's avatar
Simon Morlat committed
260
			bool_t out_without_marker = FALSE;
aymeric's avatar
aymeric committed
261 262
			while(!ms_queue_empty(&ctx->q)){
				ms_queue_put(out,ms_queue_get(&ctx->q));
Simon Morlat's avatar
Simon Morlat committed
263
				out_without_marker = TRUE;
264
				res = -1;
aymeric's avatar
aymeric committed
265
			}
Simon Morlat's avatar
Simon Morlat committed
266
			if (out_without_marker) ms_warning("Incomplete H264 frame (missing marker bit)");
aymeric's avatar
aymeric committed
267 268 269 270 271
		}
	}

	if (im->b_cont) msgpullup(im,-1);

272 273 274 275 276 277 278 279 280 281 282 283
	if (!ctx->initialized_ref_cseq) {
		ctx->initialized_ref_cseq = TRUE;
		ctx->ref_cseq = cseq;
	} else {
		ctx->ref_cseq++;
		if (ctx->ref_cseq != cseq) {
			ms_message("sequence inconsistency detected (diff=%i)",(int)(cseq - ctx->ref_cseq));
			ctx->ref_cseq = cseq;
			res = -1;
		}
	}

aymeric's avatar
aymeric committed
284 285 286 287 288
	if (type==TYPE_STAP_A){
		/*split into nalus*/
		uint16_t sz;
		uint8_t *buf=(uint8_t*)&sz;
		mblk_t *nal;
unknown's avatar
unknown committed
289 290
	
		ms_debug("Receiving STAP-A");
aymeric's avatar
aymeric committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		for(p=im->b_rptr+1;p<im->b_wptr;){
			buf[0]=p[0];
			buf[1]=p[1];
			sz=ntohs(sz);
			nal=dupb(im);
			p+=2;
			nal->b_rptr=p;
			p+=sz;
			nal->b_wptr=p;
			if (p>im->b_wptr){
				ms_error("Malformed STAP-A packet");
				freemsg(nal);
				break;
			}
			ms_queue_put(&ctx->q,nal);
		}
		freemsg(im);
	}else if (type==TYPE_FU_A){
		mblk_t *o=aggregate_fua(ctx,im);
unknown's avatar
unknown committed
310
		ms_debug("Receiving FU-A");
aymeric's avatar
aymeric committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		if (o) ms_queue_put(&ctx->q,o);
	}else{
		if (ctx->m){
			/*discontinued FU-A, purge it*/
			freemsg(ctx->m);
			ctx->m=NULL;
		}
		/*single nal unit*/
		ms_debug("Receiving single NAL");
		ms_queue_put(&ctx->q,im);
	}

	if (marker){
		ctx->last_ts=ts;
		ms_debug("Marker bit set");
		/*end of frame, output everything*/
		while(!ms_queue_empty(&ctx->q)){
			ms_queue_put(out,ms_queue_get(&ctx->q));
		}
	}
331 332

	return res;
aymeric's avatar
aymeric committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}


void rfc3984_pack(Rfc3984Context *ctx, MSQueue *naluq, MSQueue *rtpq, uint32_t ts){
	switch(ctx->mode){
		case 0:
			rfc3984_pack_mode_0(ctx,naluq,rtpq,ts);
			break;
		case 1:
			rfc3984_pack_mode_1(ctx,naluq,rtpq,ts);
			break;
		default:
			ms_error("Bad or unsupported mode %i",ctx->mode);
	}
}

void rfc3984_uninit(Rfc3984Context *ctx){
	ms_queue_flush(&ctx->q);
	if (ctx->m) freemsg(ctx->m);
	ctx->m=NULL;
}

void rfc3984_set_mode(Rfc3984Context *ctx, int mode){
	ctx->mode=mode;
}

void rfc3984_enable_stap_a(Rfc3984Context *ctx, bool_t yesno){
	ctx->stap_a_allowed=yesno;
}