aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/neomedia/RTCPConnectorInputStream.java
blob: 1c85b33ff073ce3663c2f0f92ead622f9c9e561b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.neomedia;

import java.io.*;
import java.net.*;
import java.util.*;

import net.java.sip.communicator.service.neomedia.event.*;

/**
 * @author Bing SU (nova.su@gmail.com)
 * @author Lubomir Marinov
 * @author Sebastien Vincent
 */
public class RTCPConnectorInputStream extends RTPConnectorInputStream
{
    /**
     * List of feedback listeners;
     */
    private List<RTCPFeedbackListener> listeners =
        new ArrayList<RTCPFeedbackListener>();

    /**
     * Initializes a new <tt>RTCPConnectorInputStream</tt> which is to receive
     * packet data from a specific UDP socket.
     *
     * @param socket the UDP socket the new instance is to receive data from
     */
    public RTCPConnectorInputStream(DatagramSocket socket)
    {
        super(socket);
    }

    /**
     * Add an <tt>RTCPFeedbackListener</tt>.
     *
     * @param listener object that will listen to incoming RTCP feedback
     * messages.
     */
    public void addRTCPFeedbackListener(RTCPFeedbackListener listener)
    {
        if(!listeners.contains(listener))
        {
            listeners.add(listener);
        }
    }

    /**
     * Remove an <tt>RTCPFeedbackListener</tt>.
     *
     * @param listener object to remove from listening RTCP feedback messages.
     */
    public void removeRTCPFeedbackListener(RTCPFeedbackListener listener)
    {
        if(listeners.contains(listener))
        {
            listeners.remove(listener);
        }
    }

    /**
     * Copies the content of the most recently received packet into
     * <tt>inBuffer</tt>.
     *
     * @param inBuffer the <tt>byte[]</tt> that we'd like to copy the content
     * of the packet to.
     * @param offset the position where we are supposed to start writing in
     * <tt>inBuffer</tt>.
     * @param length the number of <tt>byte</tt>s available for writing in
     * <tt>inBuffer</tt>.
     *
     * @return the number of bytes read
     *
     * @throws IOException if <tt>length</tt> is less than the size of the
     * packet.
     */
    public int read(byte[] inBuffer, int offset, int length)
        throws IOException
    {
        if (ioError)
            return -1;

        int pktLength = pkt.getLength();

        if (length < pktLength)
            throw
                new IOException("Input buffer not big enough for " + pktLength);

        /* check if RTCP feedback message */

        /* Feedback message size is minimum 12 bytes:
         * Version/Padding/Feedback message type: 1 byte
         * Payload type: 1 byte
         * Length: 2 bytes
         * SSRC of packet sender: 4 bytes
         * SSRC of media source: 4 bytes
         */
        if(pktLength >= 12)
        {
            byte data[] = pkt.getBuffer();
            int fmt = 0;
            int pt = 0;

            /* get FMT field (last 5 bits of first byte) */
            fmt = (data[0] & 0x1F);
            pt |= (data[1] & 0xFF);

            RTCPFeedbackEvent evt = new RTCPFeedbackEvent(this, fmt, pt);

            /* notify feedback listeners */
            for(RTCPFeedbackListener l : listeners)
            {
                l.feedbackReceived(evt);
            }
        }

        System.arraycopy(
                pkt.getBuffer(), pkt.getOffset(), inBuffer, offset, pktLength);

        return pktLength;
    }
}