rtp_stream.cpp 6.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**
 * @file FILENAME
 * @brief BRIEF DESCRIPTION
 * @copyright Copyright (C) YEAR Elphel Inc.
 * @author AUTHOR <EMAIL>
 *
 * @par License:
 *  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 3 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, see <http://www.gnu.org/licenses/>.
*/

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#include <arpa/inet.h>
#include <iostream>
#include "rtp_stream.h"
#include "helper.h"

using namespace std;

#define CNAME "elphel353"

#undef RTP_DEBUG
//#define RTP_DEBUG

#ifdef RTP_DEBUG
	#define D(a) a
#else
	#define D(a)
#endif

RTP_Stream::RTP_Stream(void) {
	_play = false;
	pthread_mutex_init(&pthm_flow, NULL);
	stream_name = "unknown";
	rtp_socket = NULL;
	rtcp_socket = NULL;
	sem_init(&sem_play, 0, 0);
	pth_id = -1;
}

RTP_Stream::~RTP_Stream() {
//cerr << "RTP_Stream::~RTP_Stream() for stream " << stream_name << endl;
	if(pth_id >= 0)
		pthread_cancel(pth);
	if(rtp_socket != NULL)
		delete rtp_socket;
	if(rtcp_socket != NULL)
		delete rtcp_socket;
	sem_destroy(&sem_play);
	pthread_mutex_destroy(&pthm_flow);
}

void RTP_Stream::init_pthread(void *__this) {
	pthread_attr_t tattr;
	pthread_attr_init(&tattr);
	pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
	pth_id = pthread_create(&pth, &tattr, RTP_Stream::pthread_f, (void *)__this);
}

void *RTP_Stream::pthread_f(void *_this) {
	RTP_Stream *__this = (RTP_Stream *)_this;
	return __this->thread();
}

void RTP_Stream::Start(string ip, int port, int ttl) {
D(	cerr << " new " << stream_name << " UDP socket at port: " << port << endl;)
	pthread_mutex_lock(&pthm_flow);
	if(!_play) {
		rtp_socket = new Socket(ip, port, Socket::TYPE_UDP, ttl);
		rtcp_socket = new Socket(ip, port + 1, Socket::TYPE_UDP, ttl);
		rtp_packets = 0;
		rtp_octets = 0;
		rtcp_tv.tv_sec = 0;
		rtcp_tv.tv_usec = 0;
//		rtcp_delay = 2500000;	// in usec
//		rtcp_delay = 1250000;	// in usec
		rtcp_delay = 2000000;	// in usec
		_play = true;
		/// unlock semaphore - 'play' event
		sem_post(&sem_play);
	}
	pthread_mutex_unlock(&pthm_flow);
}

void RTP_Stream::Stop(void) {
D(	cerr << __FILE__<< ":"<< __FUNCTION__ << ":" <<__LINE__ <<endl;)
//cerr << "RTP_Stream::Stop() for stream " << stream_name << " - begin" << endl;
	pthread_mutex_lock(&pthm_flow);
	if(_play) {
//cerr << "RTP_Stream::Stop() for stream " << stream_name << " - in progress" << endl;
		/// reset semaphore
		sem_init(&sem_play, 0, 0);
		_play = false;
//		delete rtcp_socket;
		if(rtp_socket != NULL) {
			delete rtp_socket;
			rtp_socket = NULL;
		}
		if(rtcp_socket != NULL) {
			delete rtcp_socket;
			rtcp_socket = NULL;
		}
	}
	pthread_mutex_unlock(&pthm_flow);
//cerr << "RTP_Stream::Stop() for stream " << stream_name << " - end" << endl;
}

/**
 * @brief Thread that invokes the video (and audio too) frame acquisition/ transmission
 *        In the current implementation video process() is blocking, but may be made
 *        non-blocking again later (through poll()). In addition to bool result
 *        (false if no frames are available, there are now long process(), with
 *        additional <0 result for the video frame change/stream shut down detected
 *        in Video class.
 * @return never
 */
void *RTP_Stream::thread(void) {
D(	cerr << "RTP_Stream::thread(void)" <<endl;)
	for(;;) {
		pthread_mutex_lock(&pthm_flow);
		if(_play) {
			long f = process();
			if(f > 0)
				rtcp();
			// process() and rtcp() use sockets
			pthread_mutex_unlock(&pthm_flow);
			if(f < 0) {
D(				cerr << __FILE__<< ":"<< __FUNCTION__ << ":" <<__LINE__<< "process exception detected: " << f << endl;)
//				cerr << "Stop() from thread for stream " << stream_name << endl;
				Stop();
			}
		} else { /// wait for 'play' event semaphore
			pthread_mutex_unlock(&pthm_flow);
			sem_wait(&sem_play);
		}
	}
	return NULL;
}

void RTP_Stream::rtcp(void) {
	// check time for next one RTCP...
	if(f_tv.tv_sec == 0 && f_tv.tv_usec == 0)
		return;
	long td = time_delta_us(f_tv, rtcp_tv);
	if(td < 0) {
		rtcp_tv = f_tv;
		return;
	}
	if(td < rtcp_delay)
		return;
	rtcp_tv = f_tv;
	rtcp_send_sdes();
	rtcp_send_sr();
}

void RTP_Stream::rtcp_send_sr(void) {
	char packet[8 + 20]; // by RTP RFC 3550, for SR RTCP packet needed 8 + 20 bytes
	int packet_len = sizeof(packet);
	uint16_t us;
	uint32_t ul;

	// RTCP header
	packet[0] = 0x81;
	packet[1] = 200;	// SR
	us = htons(((packet_len) / 4) - 1);
	memcpy((void *)&packet[2], (void *)&us, 2);
	memcpy((void *)&packet[4], (void *)&SSRC, 4);
	// NTP timestamp is a fixed point 32.32 format time
	ul = htonl(f_tv.tv_sec);
	memcpy((void *)&packet[8], (void *)&ul, 4);
	double d = f_tv.tv_usec;
	d /= 1000000.0;
	d *= 65536.0;
	d *= 4096.0;
	uint32_t f = (uint32_t)d;
	if(f > 0x0FFFFFFF)
		f = 0x0FFFFFFF;
	f <<= 4;
	ul = htonl(f);
	memcpy((void *)&packet[12], (void *)&ul, 4);
	ul = htonl(timestamp);
	memcpy((void *)&packet[16], (void *)&ul, 4);
	ul = htonl(rtp_packets);
	memcpy((void *)&packet[20], (void *)&ul, 4);
	ul = htonl(rtp_octets);
	memcpy((void *)&packet[24], (void *)&ul, 4);
	rtcp_socket->send(packet, packet_len);
}

void RTP_Stream::rtcp_send_sdes(void) {
	char packet[8 + 4 + 128]; // by RTP RFC 3550, for SDES RTCP packet needed 8 + 4 + ... bytes, 
				  // so get additional 128 bytes for 126 CNAME field
	int packet_len = 0;
        int padding=0;

	uint16_t us;
	const char *cname = CNAME;
	int cname_len = strlen(cname);
        bzero((void *)packet, 140); //8+4+128

	// RTCP header
	packet[0] = 0x81;
	packet[1] = 202;
	memcpy((void *)&packet[4], (void *)&SSRC, 4);
	packet_len += 8;

	// SDES fields
	packet[8] = 0x01;
	memcpy((void *)&packet[10], (void *)cname, cname_len);
	packet_len += 2; // + cname_len;
        // calculate common length SDES 
        padding=(cname_len+2)%4;
	if(padding) 
		cname_len += (4-padding); 
	packet[9] = cname_len; 
        // each chunk  MUST be terminated by one or more null octets(RFC3350)
	packet_len += (cname_len+4);

	us = htons((packet_len / 4) - 1);
	memcpy((void *)&packet[2], (void *)&us, 2);

	rtcp_socket->send(packet, packet_len);
}