ZLMediaKit/webrtc/RtpExt.h

143 lines
5.0 KiB
C++
Raw Permalink Normal View History

2021-05-07 11:19:12 +08:00
/*
2021-05-06 16:25:18 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* 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.
*/
#ifndef ZLMEDIAKIT_RTPEXT_H
#define ZLMEDIAKIT_RTPEXT_H
#include <stdint.h>
#include <map>
#include <string>
#include "Common/macros.h"
#include "Rtsp/Rtsp.h"
using namespace std;
using namespace mediakit;
2021-07-06 17:54:10 +08:00
#define RTP_EXT_MAP(XX) \
XX(ssrc_audio_level, "urn:ietf:params:rtp-hdrext:ssrc-audio-level") \
XX(abs_send_time, "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time") \
XX(transport_cc, "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01") \
XX(sdes_mid, "urn:ietf:params:rtp-hdrext:sdes:mid") \
XX(sdes_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id") \
XX(sdes_repaired_rtp_stream_id, "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id") \
XX(video_timing, "http://www.webrtc.org/experiments/rtp-hdrext/video-timing") \
XX(color_space, "http://www.webrtc.org/experiments/rtp-hdrext/color-space") \
XX(csrc_audio_level, "urn:ietf:params:rtp-hdrext:csrc-audio-level") \
XX(framemarking, "http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07") \
XX(video_content_type, "http://www.webrtc.org/experiments/rtp-hdrext/video-content-type") \
XX(playout_delay, "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay") \
XX(video_orientation, "urn:3gpp:video-orientation") \
XX(toffset, "urn:ietf:params:rtp-hdrext:toffset") \
XX(encrypt, "urn:ietf:params:rtp-hdrext:encrypt")
2021-05-07 12:00:26 +08:00
enum class RtpExtType : uint8_t {
padding = 0,
2021-07-06 17:54:10 +08:00
#define XX(type, uri) type,
RTP_EXT_MAP(XX)
#undef XX
reserved = encrypt,
2021-05-07 12:00:26 +08:00
};
class RtcMedia;
2021-05-13 01:29:55 +08:00
//使用次对象的方法前需保证RtpHeader内存未释放
class RtpExt {
2021-05-06 16:25:18 +08:00
public:
2021-05-07 18:33:45 +08:00
template<typename Type>
2021-05-08 10:32:08 +08:00
friend void appendExt(map<uint8_t, RtpExt> &ret, uint8_t *ptr, const uint8_t *end);
2021-10-06 22:17:38 +08:00
friend class RtpExtContext;
2021-05-07 12:00:26 +08:00
~RtpExt() = default;
2021-05-07 14:56:01 +08:00
2021-05-08 10:32:08 +08:00
static map<uint8_t/*id*/, RtpExt/*data*/> getExtValue(const RtpHeader *header);
2021-05-07 14:01:24 +08:00
static RtpExtType getExtType(const string &url);
static const string& getExtUrl(RtpExtType type);
2021-05-07 14:56:01 +08:00
static const char *getExtName(RtpExtType type);
2021-05-07 12:00:26 +08:00
2021-05-08 10:32:08 +08:00
void setType(RtpExtType type);
2021-05-16 21:17:55 +08:00
RtpExtType getType() const;
2021-05-07 14:56:01 +08:00
string dumpString() const;
2021-05-07 12:00:26 +08:00
2021-05-07 17:47:53 +08:00
uint8_t getAudioLevel(bool *vad) const;
uint32_t getAbsSendTime() const;
uint16_t getTransportCCSeq() const;
2021-05-13 01:29:55 +08:00
string getSdesMid() const;
2021-05-07 17:47:53 +08:00
string getRtpStreamId() const;
string getRepairedRtpStreamId() const;
void getVideoTiming(uint8_t &flags,
uint16_t &encode_start,
uint16_t &encode_finish,
uint16_t &packetization_complete,
uint16_t &last_pkt_left_pacer,
uint16_t &reserved_net0,
uint16_t &reserved_net1) const;
uint8_t getVideoContentType() const;
void getVideoOrientation(bool &camera_bit,
bool &flip_bit,
bool &first_rotation,
bool &second_rotation) const;
void getPlayoutDelay(uint16_t &min_delay, uint16_t &max_delay) const;
uint32_t getTransmissionOffset() const;
uint8_t getFramemarkingTID() const;
2021-05-07 18:33:45 +08:00
void setExtId(uint8_t ext_id);
2021-05-08 11:37:54 +08:00
void clearExt();
2021-10-06 22:17:38 +08:00
operator bool () const;
2021-05-07 18:33:45 +08:00
private:
2021-10-06 22:17:38 +08:00
RtpExt() = default;
2021-05-08 10:32:08 +08:00
RtpExt(void *ptr, bool one_byte_ext, const char *str, size_t size);
2021-05-13 01:29:55 +08:00
const char *data() const;
size_t size() const;
2021-10-06 22:10:54 +08:00
const uint8_t& operator[](size_t pos) const;
2021-05-13 01:29:55 +08:00
operator std::string() const;
2021-05-07 18:33:45 +08:00
2021-05-07 12:00:26 +08:00
private:
2021-05-13 01:29:55 +08:00
void *_ext = nullptr;
const char *_data;
size_t _size;
2021-05-07 18:33:45 +08:00
bool _one_byte_ext = true;
2021-05-08 10:32:08 +08:00
RtpExtType _type = RtpExtType::padding;
2021-05-06 16:25:18 +08:00
};
2021-06-25 15:31:13 +08:00
class RtcMedia;
class RtpExtContext {
public:
using Ptr = std::shared_ptr<RtpExtContext>;
2021-06-25 16:57:38 +08:00
using OnGetRtp = function<void(uint8_t pt, uint32_t ssrc, const string &rid)>;
2021-06-25 15:31:13 +08:00
RtpExtContext(const RtcMedia &media);
~RtpExtContext() = default;
2021-06-25 16:57:38 +08:00
void setOnGetRtp(OnGetRtp cb);
2021-06-25 15:31:13 +08:00
string getRid(uint32_t ssrc) const;
2021-06-25 18:08:21 +08:00
void setRid(uint32_t ssrc, const string &rid);
2021-10-06 22:17:38 +08:00
RtpExt changeRtpExtId(const RtpHeader *header, bool is_recv, string *rid_ptr = nullptr, RtpExtType type = RtpExtType::padding);
2021-06-25 15:31:13 +08:00
private:
2021-06-25 16:57:38 +08:00
void onGetRtp(uint8_t pt, uint32_t ssrc, const string &rid);
private:
OnGetRtp _cb;
2021-06-25 15:31:13 +08:00
//发送rtp时需要修改rtp ext id
map<RtpExtType, uint8_t> _rtp_ext_type_to_id;
//接收rtp时需要修改rtp ext id
unordered_map<uint8_t, RtpExtType> _rtp_ext_id_to_type;
//ssrc --> rid
unordered_map<uint32_t/*simulcast ssrc*/, string/*rid*/> _ssrc_to_rid;
};
2021-05-06 16:25:18 +08:00
#endif //ZLMEDIAKIT_RTPEXT_H