RtpDecoder.cpp 2 KB
Newer Older
Gemfield committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
9
 */
Gemfield committed
10

11
#if defined(ENABLE_RTPPROXY)
Gemfield committed
12 13
#include "Util/logger.h"
#include "RtpDecoder.h"
14
#include "rtp-payload.h"
Gemfield committed
15 16
using namespace toolkit;

17 18
namespace mediakit{

xiongziliang committed
19
RtpDecoder::RtpDecoder(const char *codec) {
Gemfield committed
20
    _buffer = std::make_shared<BufferRaw>();
xiongziliang committed
21
    _codec = codec;
Gemfield committed
22 23 24 25 26 27 28 29 30
}

RtpDecoder::~RtpDecoder() {
    if(_rtp_decoder){
        rtp_payload_decode_destroy(_rtp_decoder);
        _rtp_decoder = nullptr;
    }
}

31
void RtpDecoder::decodeRtp(const void *data, int bytes) {
Gemfield committed
32 33 34 35 36 37 38 39 40 41 42 43
    if(!_rtp_decoder){
        static rtp_payload_t s_func= {
                [](void* param, int bytes){
                    RtpDecoder *obj = (RtpDecoder *)param;
                    obj->_buffer->setCapacity(bytes);
                    return (void *)obj->_buffer->data();
                },
                [](void* param, void* packet){
                    //do nothing
                },
                [](void* param, const void *packet, int bytes, uint32_t timestamp, int flags){
                    RtpDecoder *obj = (RtpDecoder *)param;
xiongziliang committed
44
                    obj->onRtpDecode((uint8_t *)packet, bytes, timestamp, flags);
Gemfield committed
45 46 47 48 49
                }
        };

        uint8_t rtp_type = 0x7F & ((uint8_t *) data)[1];
        InfoL << "rtp type:" << (int) rtp_type;
xiongziliang committed
50
        _rtp_decoder = rtp_payload_decode_create(rtp_type, _codec.data(), &s_func, this);
Gemfield committed
51 52 53 54
        if (!_rtp_decoder) {
            WarnL << "unsupported rtp type:" << (int) rtp_type << ",size:" << bytes << ",hexdump" << hexdump(data, bytes > 16 ? 16 : bytes);
        }
    }
55

Gemfield committed
56 57 58 59
    if(_rtp_decoder){
        rtp_payload_decode_input(_rtp_decoder,data,bytes);
    }
}
60 61 62

}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)