|
| 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 | +} |
0 commit comments