Search in sources :

Example 1 with AudioPacket

use of com.jcumulus.server.rtmfp.packet.AudioPacket in project Openfire by igniterealtime.

the class MemberSender method sendData.

public synchronized boolean sendData(int[] dataToSend) {
    if (dtmfKeyToSend != null) {
        if (telephoneEventPayload != 0) {
            sendDtmfKey();
            return true;
        } else {
            if (Logger.logLevel >= Logger.LOG_INFO) {
                Logger.println("Call " + cp + " Telephone event payload not supported.  " + "Can't send " + dtmfKeyToSend);
            }
            dtmfKeyToSend = null;
        }
    }
    long start = System.nanoTime();
    if (dataToSend == null) {
        if (Logger.logLevel == -77) {
            Logger.println("Call " + cp + " no data to send");
        }
        if (comfortNoiseType == CN_USE_PAYLOAD) {
            if (senderPacket.getRtpPayload() != RtpPacket.COMFORT_PAYLOAD) {
                if (Logger.logLevel == -77) {
                    Logger.println("Call " + cp + " sending comfort payload");
                }
                if (relayChannel == null) {
                    if (cp.getRtmfpSendStream() == null && "SIP".equals(cp.getProtocol()))
                        sendComfortNoisePayload();
                } else
                    relayChannel.sendComfortNoisePayload();
            }
        }
        mustSetMarkBit = true;
        return false;
    }
    if (senderPacket.getRtpPayload() == RtpPacket.COMFORT_PAYLOAD) {
        // account for pause
        senderPacket.adjustRtpTimestamp();
    }
    senderPacket.setRtpPayload(myMediaInfo.getPayload());
    if (mustSetMarkBit) {
        if (Logger.logLevel == -77 || Logger.logLevel >= Logger.LOG_MOREINFO) {
            Logger.println("Setting MARK for " + cp);
        }
        if (Logger.logLevel != -77) {
            /*
                 * Adjust RTP timestamp to account for long pause
                 */
            if (timePreviousPacketSent != 0) {
                senderPacket.adjustRtpTimestamp(System.currentTimeMillis() - timePreviousPacketSent);
            }
        }
        senderPacket.setMark();
        /* Set MARK_BIT */
        mustSetMarkBit = false;
    }
    if (outputVolume != 1.0) {
        callHandler.getMember().adjustVolume(dataToSend, outputVolume);
    }
    dataToSend = normalize(dataToSend);
    try {
        /*
	     * Resample if needed
	     */
        if (outSampleRateConverter != null) {
            dataToSend = outSampleRateConverter.resample(dataToSend);
        }
    } catch (IOException e) {
        Logger.println("Call " + cp + " can't resample data to send! " + e.getMessage());
        callHandler.cancelRequest("Call " + cp + " can't resample data to send! " + e.getMessage());
        return false;
    }
    byte[] rtpData = senderPacket.getData();
    if (Logger.logLevel == -37) {
        boolean silence = true;
        for (int i = RtpPacket.HEADER_SIZE; i < rtpData.length - RtpPacket.HEADER_SIZE; i++) {
            if (rtpData[i] != 0) {
                silence = false;
                break;
            }
        }
        if (silence) {
            //Logger.println("Call " + cp + " sending silence");
            return false;
        }
    }
    //Util.dump("Call " + cp + " sending data " + dataToSend.length,
    //    dataToSend, 0, 8);
    //Logger.println("Call " + cp + " Sending data...");
    byte[] opusBytes = null;
    if (myMediaInfo.getEncoding() == RtpPacket.PCMU_ENCODING) {
        /*
	     * Convert to ulaw
	     */
        AudioConversion.linearToUlaw(dataToSend, rtpData, RtpPacket.HEADER_SIZE);
        senderPacket.setLength(rtpData.length);
    //Util.dump("Call " + cp + " sending ulaw data " + rtpData.length,
    //    rtpData, 0, 16);
    } else if (myMediaInfo.getEncoding() == RtpPacket.SPEEX_ENCODING) {
        try {
            if (Logger.logLevel >= Logger.LOG_MOREDETAIL) {
                Logger.writeFile("Call " + cp + " speex encoding data ");
            }
            int length = speexEncoder.encode(dataToSend, rtpData, RtpPacket.HEADER_SIZE);
            senderPacket.setLength(length + RtpPacket.HEADER_SIZE);
        } catch (SpeexException e) {
            Logger.println("Call " + this + ":  " + e.getMessage());
            return false;
        }
    } else if (myMediaInfo.getEncoding() == RtpPacket.PCM_ENCODING) {
        if (relayChannel != null && relayChannel.encode()) {
            byte[] input = AudioConversion.littleEndianIntsToBytes(dataToSend);
            byte[] output = new byte[Opus.MAX_PACKET];
            int outLength = Opus.encode(opusEncoder, input, 0, frameSizeInSamplesPerChannel, output, 0, output.length);
            opusBytes = new byte[outLength];
            System.arraycopy(output, 0, opusBytes, 0, outLength);
            System.arraycopy(output, 0, rtpData, RtpPacket.HEADER_SIZE, outLength);
            senderPacket.setLength(outLength + RtpPacket.HEADER_SIZE);
        //Logger.println("RtpPacket.PCM_ENCODING " + outLength);
        }
    } else {
        AudioConversion.intsToBytes(dataToSend, rtpData, RtpPacket.HEADER_SIZE);
    }
    recordPacket(rtpData, senderPacket.getLength());
    recordAudio(rtpData, RtpPacket.HEADER_SIZE, senderPacket.getLength() - RtpPacket.HEADER_SIZE);
    /*
	 * Encrypt data if required
	 */
    if (needToEncrypt()) {
        encrypt(rtpData, senderPacket.getLength());
    }
    if (Logger.logLevel == -78) {
        Logger.println("Call " + cp + " sending data from socket " + datagramChannel.socket().getLocalAddress() + ":" + datagramChannel.socket().getLocalPort() + " to " + senderPacket.getSocketAddress());
    }
    if (relayChannel == null) {
        if (// RTMFP
        cp.getRtmfpSendStream() != null) {
            if (RtmfpCallAgent.publishHandlers.containsKey(cp.getRtmfpSendStream())) {
                int ts = (int) (System.currentTimeMillis() - startTime);
                byte[] rtmfp = new byte[rtpData.length + 1 - RtpPacket.HEADER_SIZE];
                rtmfp[0] = (byte) 130;
                System.arraycopy(rtpData, RtpPacket.HEADER_SIZE, rtmfp, 1, rtmfp.length - 1);
                RtmfpCallAgent.publishHandlers.get(cp.getRtmfpSendStream()).B(ts, new AudioPacket(rtmfp, rtmfp.length), 0);
            }
        } else if ("SIP".equals(cp.getProtocol())) {
            if (cp.getInputTreatment() == null) {
                try {
                    senderPacket.setSocketAddress(memberAddress);
                    datagramChannel.send(ByteBuffer.wrap(senderPacket.getData(), 0, senderPacket.getLength()), memberAddress);
                    if (Logger.logLevel >= Logger.LOG_MOREDETAIL) {
                        Logger.writeFile("Call " + cp + " back from sending data");
                    }
                } catch (Exception e) {
                    if (!done) {
                        Logger.error("Call " + cp + " sendData " + e.getMessage());
                        e.printStackTrace();
                    }
                    return false;
                }
            }
        } else {
            return true;
        }
    } else {
        try {
            if (relayChannel.encode())
                relayChannel.pushAudio(senderPacket.getData(), opusBytes);
            else
                relayChannel.pushAudio(dataToSend);
        } catch (Exception e) {
            return false;
        }
    }
    senderPacket.setBuffer(rtpData);
    if (Logger.logLevel >= Logger.LOG_DEBUG) {
        log(true);
    }
    timePreviousPacketSent = System.currentTimeMillis();
    if (Logger.logLevel >= Logger.LOG_MOREDETAIL) {
        Logger.println("Call " + cp + " sendLength " + rtpData.length);
    }
    totalTimeToGetData += (System.nanoTime() - start);
    packetsSent++;
    senderPacket.updateRtpHeader(rtpData.length);
    return true;
}
Also used : IOException(java.io.IOException) AudioPacket(com.jcumulus.server.rtmfp.packet.AudioPacket) SpeexException(com.sun.voip.SpeexException) SpeexException(com.sun.voip.SpeexException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Aggregations

AudioPacket (com.jcumulus.server.rtmfp.packet.AudioPacket)1 SpeexException (com.sun.voip.SpeexException)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1