| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | // Copyright (C) 2018 F4EXB                                                      //
 | 
					
						
							|  |  |  | // written by Edouard Griffiths                                                  //
 | 
					
						
							|  |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 as version 3 of the License, or                  //
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:32:15 +02:00
										 |  |  | // (at your option) any later version.                                           //
 | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | //                                                                               //
 | 
					
						
							|  |  |  | // 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 V3 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/>.          //
 | 
					
						
							|  |  |  | ///////////////////////////////////////////////////////////////////////////////////
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "rtpsink.h"
 | 
					
						
							|  |  |  | #include "dsp/dsptypes.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  | RTPSink::RTPSink(QUdpSocket *udpSocket, int sampleRate, bool stereo) : | 
					
						
							| 
									
										
										
										
											2018-03-08 00:16:24 +01:00
										 |  |  |     m_payloadType(stereo ? RTPSink::PayloadL16Stereo : RTPSink::PayloadL16Mono), | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |     m_sampleRate(sampleRate), | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     m_sampleBytes(0), | 
					
						
							|  |  |  |     m_packetSamples(0), | 
					
						
							|  |  |  |     m_bufferSize(0), | 
					
						
							|  |  |  |     m_sampleBufferIndex(0), | 
					
						
							|  |  |  |     m_byteBuffer(0), | 
					
						
							| 
									
										
										
										
											2022-09-15 21:59:42 +02:00
										 |  |  |     m_destport(9998) | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  | 	m_rtpSessionParams.SetOwnTimestampUnit(1.0 / (double) m_sampleRate); | 
					
						
							|  |  |  |     m_rtpTransmissionParams.SetRTCPMultiplexing(true); // do not allocate another socket for RTCP
 | 
					
						
							| 
									
										
										
										
											2018-03-08 00:16:24 +01:00
										 |  |  |     m_rtpTransmissionParams.SetUseExistingSockets(udpSocket, udpSocket); | 
					
						
							| 
									
										
										
										
											2018-02-20 14:50:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     int status = m_rtpTransmitter.Init(); | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qCritical("RTPSink::RTPSink: cannot initialize transmitter: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  |         m_valid = false; | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qDebug("RTPSink::RTPSink: initialized transmitter: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-20 14:50:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  |     m_rtpTransmitter.Create(m_rtpSessionParams.GetMaximumPacketSize(), &m_rtpTransmissionParams); | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     qDebug("RTPSink::RTPSink: created transmitter: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  |     status = m_rtpSession.Create(m_rtpSessionParams, &m_rtpTransmitter); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qCritical("RTPSink::RTPSink: cannot create session: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-02-20 14:50:21 +01:00
										 |  |  |         m_valid = false; | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qDebug("RTPSink::RTPSink: created session: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |     setPayloadInformation(m_payloadType, m_sampleRate); | 
					
						
							| 
									
										
										
										
											2018-02-20 14:50:21 +01:00
										 |  |  |     m_valid = true; | 
					
						
							| 
									
										
										
										
											2018-02-21 00:28:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     uint32_t endianTest32 = 1; | 
					
						
							|  |  |  |     uint8_t *ptr = (uint8_t*) &endianTest32; | 
					
						
							|  |  |  |     m_endianReverse = (*ptr == 1); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RTPSink::~RTPSink() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     qrtplib::RTPTime delay = qrtplib::RTPTime(10.0); | 
					
						
							| 
									
										
										
										
											2018-02-20 19:18:23 +01:00
										 |  |  |     m_rtpSession.BYEDestroy(delay, "Time's up", 9); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_byteBuffer) { | 
					
						
							|  |  |  |         delete[] m_byteBuffer; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  | void RTPSink::setPayloadInformation(PayloadType payloadType, int sampleRate) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t timestampinc; | 
					
						
							|  |  |  |     QMutexLocker locker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |     qDebug("RTPSink::setPayloadInformation: payloadType: %d sampleRate: %d", payloadType, sampleRate); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     switch (payloadType) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |     case PayloadPCMA8: | 
					
						
							|  |  |  |         m_sampleBytes = 1; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(8); | 
					
						
							|  |  |  |         m_packetSamples = m_sampleRate / 50; // 20ms packet samples
 | 
					
						
							| 
									
										
										
										
											2019-02-13 10:34:36 +01:00
										 |  |  |         timestampinc = m_sampleRate / 50;    // 1 channel
 | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case PayloadPCMU8: | 
					
						
							|  |  |  |         m_sampleBytes = 1; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(0); | 
					
						
							|  |  |  |         m_packetSamples = m_sampleRate / 50; // 20ms packet samples
 | 
					
						
							| 
									
										
										
										
											2019-02-13 10:34:36 +01:00
										 |  |  |         timestampinc = m_sampleRate / 50;    // 1 channel
 | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case PayloadL8: | 
					
						
							|  |  |  |         m_sampleBytes = 1; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(96); | 
					
						
							|  |  |  |         m_packetSamples = m_sampleRate / 50; // 20ms packet samples
 | 
					
						
							|  |  |  |         timestampinc = m_sampleRate / 50;    // 1 channel
 | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |     case PayloadL16Stereo: | 
					
						
							|  |  |  |         m_sampleBytes = 4; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(96); | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |         m_packetSamples = m_sampleRate / 50; // 20ms packet samples
 | 
					
						
							| 
									
										
										
										
											2019-02-13 10:34:36 +01:00
										 |  |  |         timestampinc = m_sampleRate / 100;   // 2 channels
 | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-02-17 03:40:11 +01:00
										 |  |  |     case PayloadG722: | 
					
						
							|  |  |  |         m_sampleBytes = 1; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(9); | 
					
						
							| 
									
										
										
										
											2019-02-18 22:06:43 +01:00
										 |  |  |         m_packetSamples = 160; // Fixed 8 kB/s 20ms packet samples
 | 
					
						
							|  |  |  |         timestampinc = 160;    // 1 channel
 | 
					
						
							| 
									
										
										
										
											2019-02-17 03:40:11 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2019-02-18 02:30:43 +01:00
										 |  |  |     case PayloadOpus: | 
					
						
							|  |  |  |         m_sampleBytes = 1; | 
					
						
							| 
									
										
										
										
											2019-02-19 00:36:32 +01:00
										 |  |  |         m_rtpSession.SetDefaultPayloadType(96); | 
					
						
							|  |  |  |         m_packetSamples = 160; // Payload size is 160 bytes
 | 
					
						
							|  |  |  |         timestampinc = 960;    // But increment is 960
 | 
					
						
							| 
									
										
										
										
											2019-02-18 02:30:43 +01:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |     case PayloadL16Mono: | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         m_sampleBytes = 2; | 
					
						
							|  |  |  |         m_rtpSession.SetDefaultPayloadType(96); | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |         m_packetSamples = m_sampleRate / 50; // 20ms packet samples
 | 
					
						
							| 
									
										
										
										
											2019-02-13 10:34:36 +01:00
										 |  |  |         timestampinc = m_sampleRate / 50;    // 1 channel
 | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_bufferSize = m_packetSamples * m_sampleBytes; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_byteBuffer) { | 
					
						
							|  |  |  |         delete[] m_byteBuffer; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_byteBuffer = new uint8_t[m_bufferSize]; | 
					
						
							|  |  |  |     m_sampleBufferIndex = 0; | 
					
						
							|  |  |  |     m_payloadType = payloadType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int status = m_rtpSession.SetTimestampUnit(1.0 / (double) m_sampleRate); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qCritical("RTPSink::setPayloadInformation: cannot set timestamp unit: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qDebug("RTPSink::setPayloadInformation: timestamp unit set to %f: %s", | 
					
						
							| 
									
										
										
										
											2018-03-27 00:09:52 +02:00
										 |  |  |                1.0 / (double) m_sampleRate, | 
					
						
							|  |  |  |                qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     status = m_rtpSession.SetDefaultMark(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qCritical("RTPSink::setPayloadInformation: cannot set default mark: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qDebug("RTPSink::setPayloadInformation: set default mark to false: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-10 05:49:18 +01:00
										 |  |  |     status = m_rtpSession.SetDefaultTimestampIncrement(timestampinc); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qCritical("RTPSink::setPayloadInformation: cannot set default timestamp increment: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qDebug("RTPSink::setPayloadInformation: set default timestamp increment to %d: %s", timestampinc, qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 19:50:02 +02:00
										 |  |  |     int maximumPacketSize = m_bufferSize+20; // was +40
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (maximumPacketSize < RTP_MINPACKETSIZE) { | 
					
						
							|  |  |  |         maximumPacketSize += m_bufferSize; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |     status = m_rtpSession.SetMaximumPacketSize(maximumPacketSize); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qCritical("RTPSink::setPayloadInformation: cannot set maximum packet size: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-27 09:04:10 +02:00
										 |  |  |         qDebug("RTPSink::setPayloadInformation: set maximum packet size to %d bytes: %s", maximumPacketSize, qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RTPSink::setDestination(const QString& address, uint16_t port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_rtpSession.ClearDestinations(); | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     m_rtpSession.DeleteDestination(qrtplib::RTPAddress(m_destip, m_destport)); | 
					
						
							|  |  |  |     m_destip.setAddress(address); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     m_destport = port; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     int status = m_rtpSession.AddDestination(qrtplib::RTPAddress(m_destip, m_destport)); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qCritical("RTPSink::setDestination: cannot set destination address: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RTPSink::deleteDestination(const QString& address, uint16_t port) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     QHostAddress destip(address); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     int status =  m_rtpSession.DeleteDestination(qrtplib::RTPAddress(destip, port)); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qCritical("RTPSink::deleteDestination: cannot delete destination address: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RTPSink::addDestination(const QString& address, uint16_t port) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     QHostAddress destip(address); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |     int status = m_rtpSession.AddDestination(qrtplib::RTPAddress(destip, port)); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |         qCritical("RTPSink::addDestination: cannot add destination address: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         qDebug("RTPSink::addDestination: destination address set to %s:%d: %s", | 
					
						
							|  |  |  |                 address.toStdString().c_str(), | 
					
						
							|  |  |  |                 port, | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |                 qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  | void RTPSink::write(const uint8_t *sampleByte) | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     QMutexLocker locker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_sampleBufferIndex < m_packetSamples) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |         writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], | 
					
						
							|  |  |  |                 sampleByte, | 
					
						
							|  |  |  |                 elemLength(m_payloadType), | 
					
						
							|  |  |  |                 m_sampleBytes, | 
					
						
							|  |  |  |                 m_endianReverse); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |         m_sampleBufferIndex++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int status = m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (status < 0) { | 
					
						
							| 
									
										
										
										
											2018-03-06 02:23:47 +01:00
										 |  |  |             qCritical("RTPSink::write: cannot write packet: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 03:40:11 +01:00
										 |  |  |         writeNetBuf(&m_byteBuffer[0], | 
					
						
							|  |  |  |             sampleByte, | 
					
						
							|  |  |  |             elemLength(m_payloadType), | 
					
						
							|  |  |  |             m_sampleBytes, | 
					
						
							| 
									
										
										
										
											2019-02-13 10:34:36 +01:00
										 |  |  |             m_endianReverse); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |         m_sampleBufferIndex = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 01:19:02 +02:00
										 |  |  | void RTPSink::write(const uint8_t *sampleByteL, const uint8_t *sampleByteR) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QMutexLocker locker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_sampleBufferIndex < m_packetSamples) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], | 
					
						
							|  |  |  |                 sampleByteL, | 
					
						
							|  |  |  |                 elemLength(m_payloadType), | 
					
						
							|  |  |  |                 m_sampleBytes, | 
					
						
							|  |  |  |                 m_endianReverse); | 
					
						
							|  |  |  |         writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes + elemLength(m_payloadType)], | 
					
						
							|  |  |  |                 sampleByteR, | 
					
						
							|  |  |  |                 elemLength(m_payloadType), | 
					
						
							|  |  |  |                 m_sampleBytes, | 
					
						
							|  |  |  |                 m_endianReverse); | 
					
						
							|  |  |  |         m_sampleBufferIndex++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         int status = m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (status < 0) { | 
					
						
							|  |  |  |             qCritical("RTPSink::write: cannot write packet: %s", qrtplib::RTPGetErrorString(status).c_str()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         writeNetBuf(&m_byteBuffer[0], sampleByteL,  elemLength(m_payloadType), m_sampleBytes, m_endianReverse); | 
					
						
							|  |  |  |         writeNetBuf(&m_byteBuffer[2], sampleByteR,  elemLength(m_payloadType), m_sampleBytes, m_endianReverse); | 
					
						
							|  |  |  |         m_sampleBufferIndex = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  | void RTPSink::write(const uint8_t *samples, int nbSamples) | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     int samplesIndex = 0; | 
					
						
							|  |  |  |     QMutexLocker locker(&m_mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // fill remainder of buffer and send it
 | 
					
						
							|  |  |  |     if (m_sampleBufferIndex + nbSamples > m_packetSamples) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |         writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |                 &samples[samplesIndex*m_sampleBytes], | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |                 elemLength(m_payloadType), | 
					
						
							|  |  |  |                 (m_packetSamples - m_sampleBufferIndex)*m_sampleBytes, | 
					
						
							|  |  |  |                 m_endianReverse); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |         m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize); | 
					
						
							|  |  |  |         nbSamples -= (m_packetSamples - m_sampleBufferIndex); | 
					
						
							|  |  |  |         m_sampleBufferIndex = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // send complete packets
 | 
					
						
							|  |  |  |     while (nbSamples > m_packetSamples) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |         writeNetBuf(m_byteBuffer, | 
					
						
							|  |  |  |                 samples, | 
					
						
							|  |  |  |                 elemLength(m_payloadType), | 
					
						
							|  |  |  |                 m_bufferSize, | 
					
						
							|  |  |  |                 m_endianReverse); | 
					
						
							|  |  |  |         m_rtpSession.SendPacket((const void *) m_byteBuffer, (std::size_t) m_bufferSize); | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |         samplesIndex += m_packetSamples; | 
					
						
							|  |  |  |         nbSamples -= m_packetSamples; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // copy remainder of input to buffer
 | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |     writeNetBuf(&m_byteBuffer[m_sampleBufferIndex*m_sampleBytes], | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  |             &samples[samplesIndex*m_sampleBytes], | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |             elemLength(m_payloadType), | 
					
						
							|  |  |  |             nbSamples*m_sampleBytes,m_endianReverse); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RTPSink::writeNetBuf(uint8_t *dest, const uint8_t *src, unsigned int elemLen, unsigned int bytesLen, bool endianReverse) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (unsigned int i = 0; i < bytesLen; i += elemLen) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         memcpy(&dest[i], &src[i], elemLen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (endianReverse) { | 
					
						
							|  |  |  |             std::reverse(&dest[i], &dest[i+elemLen]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | unsigned int RTPSink::elemLength(PayloadType payloadType) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (payloadType) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |     case PayloadPCMA8: | 
					
						
							|  |  |  |     case PayloadPCMU8: | 
					
						
							| 
									
										
										
										
											2019-02-19 00:36:32 +01:00
										 |  |  |     case PayloadG722: | 
					
						
							|  |  |  |     case PayloadOpus: | 
					
						
							|  |  |  |     case PayloadL8: | 
					
						
							| 
									
										
										
										
											2019-02-13 07:53:38 +01:00
										 |  |  |         return sizeof(int8_t); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2018-01-31 02:03:23 +01:00
										 |  |  |     case PayloadL16Stereo: | 
					
						
							|  |  |  |     case PayloadL16Mono: | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         return sizeof(int16_t); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-31 00:40:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 |