Skip to content

Commit ec9035e

Browse files
Merge pull request #10 from roleoroleo/g711u
Add support for ulaw audio
2 parents f434765 + da54bf8 commit ec9035e

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

src/xop/G711USource.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#if defined(WIN32) || defined(_WIN32)
2+
#ifndef _CRT_SECURE_NO_WARNINGS
3+
#define _CRT_SECURE_NO_WARNINGS
4+
#endif
5+
#endif
6+
#include "G711USource.h"
7+
#include <cstdio>
8+
#include <chrono>
9+
#if defined(__linux) || defined(__linux__)
10+
#include <sys/time.h>
11+
#endif
12+
13+
using namespace xop;
14+
using namespace std;
15+
16+
G711USource::G711USource()
17+
{
18+
payload_ = 0;
19+
media_type_ = PCMU;
20+
clock_rate_ = 8000;
21+
}
22+
23+
G711USource* G711USource::CreateNew()
24+
{
25+
return new G711USource();
26+
}
27+
28+
G711USource::~G711USource()
29+
{
30+
31+
}
32+
33+
string G711USource::GetMediaDescription(uint16_t port)
34+
{
35+
char buf[100] = {0};
36+
sprintf(buf, "m=audio %hu RTP/AVP 0", port);
37+
return string(buf);
38+
}
39+
40+
string G711USource::GetAttribute()
41+
{
42+
return string("a=rtpmap:0 PCMU/8000/1");
43+
}
44+
45+
bool G711USource::HandleFrame(MediaChannelId channel_id, AVFrame frame)
46+
{
47+
if (frame.buffer.size() > MAX_RTP_PAYLOAD_SIZE) {
48+
return false;
49+
}
50+
51+
uint8_t *frame_buf = frame.buffer.data();
52+
uint32_t frame_size = frame.buffer.size();
53+
54+
RtpPacket rtp_pkt;
55+
rtp_pkt.type = frame.type;
56+
rtp_pkt.timestamp = frame.timestamp;
57+
rtp_pkt.size = frame_size + RTP_TCP_HEAD_SIZE + RTP_HEADER_SIZE;
58+
rtp_pkt.last = 1;
59+
60+
memcpy(rtp_pkt.data.get()+RTP_TCP_HEAD_SIZE+RTP_HEADER_SIZE, frame_buf, frame_size);
61+
62+
if (send_frame_callback_) {
63+
send_frame_callback_(channel_id, rtp_pkt);
64+
}
65+
66+
return true;
67+
}
68+
69+
int64_t G711USource::GetTimestamp()
70+
{
71+
auto time_point = chrono::time_point_cast<chrono::microseconds>(chrono::steady_clock::now());
72+
return (int64_t)((time_point.time_since_epoch().count()+500)/1000*8);
73+
}

src/xop/G711USource.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef XOP_G711U_SOURCE_H
2+
#define XOP_G711U_SOURCE_H
3+
4+
#include "MediaSource.h"
5+
#include "rtp.h"
6+
7+
namespace xop
8+
{
9+
10+
class G711USource : public MediaSource
11+
{
12+
public:
13+
static G711USource* CreateNew();
14+
virtual ~G711USource();
15+
16+
uint32_t GetSampleRate() const
17+
{ return samplerate_; }
18+
19+
uint32_t GetChannels() const
20+
{ return channels_; }
21+
22+
virtual std::string GetMediaDescription(uint16_t port=0);
23+
24+
virtual std::string GetAttribute();
25+
26+
bool HandleFrame(MediaChannelId channel_id, AVFrame frame);
27+
28+
static int64_t GetTimestamp();
29+
30+
private:
31+
G711USource();
32+
33+
uint32_t samplerate_ = 8000;
34+
uint32_t channels_ = 1;
35+
};
36+
37+
}
38+
39+
#endif

src/xop/media.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace xop
1313
/* RTSP服务支持的媒体类型 */
1414
enum MediaType
1515
{
16-
//PCMU = 0,
16+
PCMU = 0,
1717
PCMA = 8,
1818
H264 = 96,
1919
AAC = 37,

0 commit comments

Comments
 (0)