From da54bf841a777cc4ba8c89cf2170445110f93274 Mon Sep 17 00:00:00 2001 From: roleo Date: Sat, 5 Feb 2022 17:48:38 +0100 Subject: [PATCH] Add support for ulaw audio --- src/xop/G711USource.cpp | 73 +++++++++++++++++++++++++++++++++++++++++ src/xop/G711USource.h | 39 ++++++++++++++++++++++ src/xop/media.h | 2 +- 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/xop/G711USource.cpp create mode 100644 src/xop/G711USource.h diff --git a/src/xop/G711USource.cpp b/src/xop/G711USource.cpp new file mode 100644 index 0000000..e9b2fc9 --- /dev/null +++ b/src/xop/G711USource.cpp @@ -0,0 +1,73 @@ +#if defined(WIN32) || defined(_WIN32) +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif +#endif +#include "G711USource.h" +#include +#include +#if defined(__linux) || defined(__linux__) +#include +#endif + +using namespace xop; +using namespace std; + +G711USource::G711USource() +{ + payload_ = 0; + media_type_ = PCMU; + clock_rate_ = 8000; +} + +G711USource* G711USource::CreateNew() +{ + return new G711USource(); +} + +G711USource::~G711USource() +{ + +} + +string G711USource::GetMediaDescription(uint16_t port) +{ + char buf[100] = {0}; + sprintf(buf, "m=audio %hu RTP/AVP 0", port); + return string(buf); +} + +string G711USource::GetAttribute() +{ + return string("a=rtpmap:0 PCMU/8000/1"); +} + +bool G711USource::HandleFrame(MediaChannelId channel_id, AVFrame frame) +{ + if (frame.buffer.size() > MAX_RTP_PAYLOAD_SIZE) { + return false; + } + + uint8_t *frame_buf = frame.buffer.data(); + uint32_t frame_size = frame.buffer.size(); + + RtpPacket rtp_pkt; + rtp_pkt.type = frame.type; + rtp_pkt.timestamp = frame.timestamp; + rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE; + rtp_pkt.last = 1; + + memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE, frame_buf, frame_size); + + if (send_frame_callback_) { + send_frame_callback_(channel_id, rtp_pkt); + } + + return true; +} + +int64_t G711USource::GetTimestamp() +{ + auto time_point = chrono::time_point_cast(chrono::steady_clock::now()); + return (int64_t)((time_point.time_since_epoch().count()+500)/1000*8); +} diff --git a/src/xop/G711USource.h b/src/xop/G711USource.h new file mode 100644 index 0000000..100b66a --- /dev/null +++ b/src/xop/G711USource.h @@ -0,0 +1,39 @@ +#ifndef XOP_G711U_SOURCE_H +#define XOP_G711U_SOURCE_H + +#include "MediaSource.h" +#include "rtp.h" + +namespace xop +{ + +class G711USource : public MediaSource +{ +public: + static G711USource* CreateNew(); + virtual ~G711USource(); + + uint32_t GetSampleRate() const + { return samplerate_; } + + uint32_t GetChannels() const + { return channels_; } + + virtual std::string GetMediaDescription(uint16_t port=0); + + virtual std::string GetAttribute(); + + bool HandleFrame(MediaChannelId channel_id, AVFrame frame); + + static int64_t GetTimestamp(); + +private: + G711USource(); + + uint32_t samplerate_ = 8000; + uint32_t channels_ = 1; +}; + +} + +#endif diff --git a/src/xop/media.h b/src/xop/media.h index c6b2631..1a03345 100644 --- a/src/xop/media.h +++ b/src/xop/media.h @@ -13,7 +13,7 @@ namespace xop /* RTSP服务支持的媒体类型 */ enum MediaType { - //PCMU = 0, + PCMU = 0, PCMA = 8, H264 = 96, AAC = 37,