All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.peers.media.DtmfFactory Maven / Gradle / Ivy

/*
    This file is part of Peers, a java SIP softphone.

    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
    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 .
    
    Copyright 2010 Yohann Martineau
 */

package net.sourceforge.peers.media;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.peers.rtp.DtmfRtpPacket;
import net.sourceforge.peers.rtp.RFC4733;
import net.sourceforge.peers.rtp.RtpPacket;

public class DtmfFactory {

	public static String parseDtmf(RtpPacket rtpPacket) {
		byte[] data = rtpPacket.getData();
		String value = "";
		int dtmfValue = data[0];
		if (dtmfValue == 10) {
			value = "*";
		} else if (dtmfValue == 11) {
			value = "#";
		} else {
			value = "" + dtmfValue;
		}
		return value;
	}

	public List createDtmfPackets(char digit) {
		List packets = new ArrayList();
		byte[] data = new byte[4];
		// RFC4733
		if (digit == '*') {
			data[0] = 10;
		} else if (digit == '#') {
			data[0] = 11;
		} else if (digit >= 'A' && digit <= 'D') {
			data[0] = (byte) (digit - 53);
		} else {
			data[0] = (byte) (digit - 48);
		}
		data[1] = 10; // volume 10
		// Set Duration to 160
		// duration 8 bits
		data[2] = 0;
		// duration 8 bits
		data[3] = -96;

		DtmfRtpPacket rtpPacket = new DtmfRtpPacket();
		rtpPacket.setData(data);
		rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
		rtpPacket.setMarker(true);
		packets.add(rtpPacket);

		// two classical packets

		rtpPacket = new DtmfRtpPacket(rtpPacket);
		// set duration to 320
		data = data.clone();
		data[2] = 1;
		data[3] = 64;
		rtpPacket.setData(data);
		rtpPacket.setMarker(false);
		rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
		packets.add(rtpPacket);

		rtpPacket = new DtmfRtpPacket(rtpPacket);
		// set duration to 320
		data = data.clone();
		data[2] = 1;
		data[3] = -32;
		rtpPacket.setData(data);
		rtpPacket.setMarker(false);
		rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
		packets.add(rtpPacket);

		data = data.clone();
		// create three end event packets
		data[1] = -0x76; // end event flag + volume set to 10
		// set Duration to 640
		data[2] = 2; // duration 8 bits
		data[3] = -128; // duration 8 bits
		for (int r = 0; r < 3; r++) {
			rtpPacket = new DtmfRtpPacket(rtpPacket);
			rtpPacket.setData(data);
			rtpPacket.setMarker(false);
			rtpPacket.setPayloadType(RFC4733.PAYLOAD_TYPE_TELEPHONE_EVENT);
			packets.add(rtpPacket);
		}

		return packets;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy