Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ulaw audio #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/xop/G711USource.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <chrono>
#if defined(__linux) || defined(__linux__)
#include <sys/time.h>
#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::microseconds>(chrono::steady_clock::now());
return (int64_t)((time_point.time_since_epoch().count()+500)/1000*8);
}
39 changes: 39 additions & 0 deletions src/xop/G711USource.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/xop/media.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace xop
/* RTSP服务支持的媒体类型 */
enum MediaType
{
//PCMU = 0,
PCMU = 0,
PCMA = 8,
H264 = 96,
AAC = 37,
Expand Down