summaryrefslogtreecommitdiff
path: root/service/src/com/android/telephony/imsmedia/lib/libimsmedia/core/text/nodes/TextRtpPayloadDecoderNode.cpp
blob: 901f88c685f4332ba90cc6ea893818de1e8f7fd8 (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
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
/**
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <TextRtpPayloadDecoderNode.h>
#include <TextConfig.h>
#include <ImsMediaTrace.h>
#include <list>

TextRtpPayloadDecoderNode::TextRtpPayloadDecoderNode(BaseSessionCallback* callback) :
        BaseNode(callback)
{
    mCodecType = TextConfig::TEXT_CODEC_NONE;
}

TextRtpPayloadDecoderNode::~TextRtpPayloadDecoderNode() {}

kBaseNodeId TextRtpPayloadDecoderNode::GetNodeId()
{
    return kNodeIdTextPayloadDecoder;
}

ImsMediaResult TextRtpPayloadDecoderNode::Start()
{
    IMLOGD1("[Start] codec[%d]", mCodecType);

    if (mCodecType == TextConfig::TEXT_CODEC_NONE)
    {
        return RESULT_INVALID_PARAM;
    }

    mNodeState = kNodeStateRunning;
    return RESULT_SUCCESS;
}

void TextRtpPayloadDecoderNode::Stop()
{
    IMLOGD0("[Stop]");
    mNodeState = kNodeStateStopped;
}

bool TextRtpPayloadDecoderNode::IsRunTime()
{
    return true;
}

bool TextRtpPayloadDecoderNode::IsSourceNode()
{
    return false;
}

void TextRtpPayloadDecoderNode::OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* data,
        uint32_t size, uint32_t timestamp, bool mark, uint32_t seqNum, ImsMediaSubType dataType,
        uint32_t arrivalTime)
{
    (void)dataType;
    (void)arrivalTime;

    if (subtype == MEDIASUBTYPE_REFRESHED)
    {
        SendDataToRearNode(subtype, nullptr, size, 0, 0, 0, MEDIASUBTYPE_UNDEFINED);
        return;
    }

    switch (mCodecType)
    {
        case TextConfig::TEXT_T140:
        case TextConfig::TEXT_T140_RED:
            DecodeT140(data, size, subtype, timestamp, mark, seqNum);
            break;
        default:
            IMLOGE1("[OnDataFromFrontNode invalid codec type[%u]", mCodecType);
            break;
    }
}

void TextRtpPayloadDecoderNode::SetConfig(void* config)
{
    if (config == nullptr)
    {
        return;
    }

    TextConfig* pConfig = reinterpret_cast<TextConfig*>(config);
    mCodecType = pConfig->getCodecType();
}

bool TextRtpPayloadDecoderNode::IsSameConfig(void* config)
{
    if (config == nullptr)
    {
        return true;
    }

    TextConfig* pConfig = reinterpret_cast<TextConfig*>(config);

    return (mCodecType == pConfig->getCodecType());
}

void TextRtpPayloadDecoderNode::DecodeT140(uint8_t* data, uint32_t size, ImsMediaSubType subtype,
        uint32_t timestamp, bool mark, uint32_t seq)
{
    IMLOGD_PACKET5(IM_PACKET_LOG_PH,
            "[DecodeT140] subtype[%u], size[%u], timestamp[%d], mark[%d], seq[%d]", subtype, size,
            timestamp, mark, seq);

    if (subtype == MEDIASUBTYPE_BITSTREAM_T140 || subtype == MEDIASUBTYPE_BITSTREAM_T140_RED)
    {
        std::list<uint32_t> listTimestampOffset;
        std::list<uint32_t> listLength;
        uint32_t readByte = 0;
        uint16_t redundantCount = 0;

        mBitReader.SetBuffer(data, size);

        /*
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |1|   T140 PT   |  timestamp offset of "R"  | "R" block length  |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        |0|   T140 PT   | "R" T.140 encoded redundant data              |
        +-+-+-+-+-+-+-+-+                               +---------------+
        |                                               |
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        */

        // Primary Data Only
        if (subtype == MEDIASUBTYPE_BITSTREAM_T140)
        {
            SendDataToRearNode(MEDIASUBTYPE_BITSTREAM_T140, data, size, timestamp, mark, seq);
            return;
        }

        // Redundant data included
        while (mBitReader.Read(1) == 1)  // redundant flag bit
        {
            uint32_t payloadType = mBitReader.Read(7);  // T140 payload type
            uint32_t timestampOffset = mBitReader.Read(14);
            uint32_t length = mBitReader.Read(10);

            listTimestampOffset.push_back(timestampOffset);  // timestamp offset
            listLength.push_back(length);                    // block length

            IMLOGD_PACKET3(IM_PACKET_LOG_PH, "[DecodeT140] PT[%u], TSOffset[%u], size[%u]",
                    payloadType, timestampOffset, length);
            readByte += 4;
            redundantCount++;
        }

        mBitReader.Read(7);  // T140 payload type (111)
        readByte += 1;

        // redundant data
        while (listTimestampOffset.size() > 0)
        {
            uint32_t redundantTimestamp = listTimestampOffset.front();
            uint32_t redundantLength = listLength.front();

            // read redundant payload
            mBitReader.ReadByteBuffer(mPayload, redundantLength * 8);
            readByte += redundantLength;

            uint16_t redundantSeqNum = seq - redundantCount;

            IMLOGD_PACKET3(IM_PACKET_LOG_PH, "[DecodeT140] red TS[%u], size[%u], seq[%u]",
                    timestamp - redundantTimestamp, redundantLength, redundantSeqNum);
            SendDataToRearNode(MEDIASUBTYPE_BITSTREAM_T140, mPayload, redundantLength,
                    timestamp - redundantTimestamp, mark, redundantSeqNum);

            redundantCount--;
            listTimestampOffset.pop_front();
            listLength.pop_front();
        }

        // primary data
        if (size - readByte > 0)
        {
            mBitReader.ReadByteBuffer(mPayload, (size - readByte) * 8);
        }

        SendDataToRearNode(
                MEDIASUBTYPE_BITSTREAM_T140, mPayload, (size - readByte), timestamp, mark, seq);
    }
    else
    {
        IMLOGW1("[DecodeT140] INVALID media sub type[%u]", subtype);
    }
}