Skip to content

Commit 4a7b1ac

Browse files
committed
add RTCAudioFrame.
1 parent 80a9d67 commit 4a7b1ac

File tree

4 files changed

+115
-81
lines changed

4 files changed

+115
-81
lines changed

BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ rtc_shared_library("libwebrtc") {
7676
"include/base/scoped_ref_ptr.h",
7777
"include/libwebrtc.h",
7878
"include/rtc_audio_device.h",
79+
"include/rtc_audio_frame.h",
7980
"include/rtc_audio_source.h",
8081
"include/rtc_audio_track.h",
8182
"include/rtc_data_channel.h",
@@ -105,6 +106,8 @@ rtc_shared_library("libwebrtc") {
105106
"src/libwebrtc.cc",
106107
"src/rtc_audio_device_impl.cc",
107108
"src/rtc_audio_device_impl.h",
109+
"src/rtc_audio_frame_impl.cc",
110+
"src/rtc_audio_frame_impl.h",
108111
"src/rtc_audio_source_impl.cc",
109112
"src/rtc_audio_source_impl.h",
110113
"src/rtc_audio_track_impl.cc",

include/rtc_audio_frame.h

Lines changed: 13 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,40 @@
1-
#ifndef AUDIO_FRAME_HXX
2-
#define AUDIO_FRAME_HXX
1+
#ifndef LIB_WEBRTC_RTC_AUDIO_FRAME_HXX
2+
#define LIB_WEBRTC_RTC_AUDIO_FRAME_HXX
33

4-
#include "media_manager_types.h"
4+
#include "rtc_types.h"
55

6-
namespace b2bua {
6+
namespace libwebrtc {
77

8-
class AudioFrame {
8+
class RTCAudioFrame : public RefCountInterface {
99
public:
10-
/**
11-
* @brief Creates a new instance of AudioFrame.
12-
* @return AudioFrame*: a pointer to the newly created AudioFrame.
13-
*/
14-
MEDIA_MANAGER_API static AudioFrame* Create();
10+
LIB_WEBRTC_API static scoped_refptr<RTCAudioFrame> Create();
1511

16-
/**
17-
* @brief Creates a new instance of AudioFrame with specified parameters.
18-
* @param id: the unique identifier of the frame.
19-
* @param timestamp: the timestamp of the frame.
20-
* @param data: a pointer to the audio data buffer.
21-
* @param samples_per_channel: the number of samples per channel.
22-
* @param sample_rate_hz: the sample rate in Hz.
23-
* @param num_channels: the number of audio channels.
24-
* @return AudioFrame*: a pointer to the newly created AudioFrame.
25-
*/
26-
MEDIA_MANAGER_API static AudioFrame* Create(int id, uint32_t timestamp,
27-
const int16_t* data,
28-
size_t samples_per_channel,
29-
int sample_rate_hz,
30-
size_t num_channels = 1);
31-
32-
/**
33-
* @brief Releases the memory of this AudioFrame.
34-
*/
35-
virtual void Release() = 0;
12+
LIB_WEBRTC_API static scoped_refptr<RTCAudioFrame> Create(
13+
uint32_t timestamp, const int16_t* data, size_t samples_per_channel,
14+
int sample_rate_hz, size_t num_channels = 1);
3615

3716
public:
38-
/**
39-
* @brief Updates the audio frame with specified parameters.
40-
* @param id: the unique identifier of the frame.
41-
* @param timestamp: the timestamp of the frame.
42-
* @param data: a pointer to the audio data buffer.
43-
* @param samples_per_channel: the number of samples per channel.
44-
* @param sample_rate_hz: the sample rate in Hz.
45-
* @param num_channels: the number of audio channels.
46-
*/
47-
virtual void UpdateFrame(int id, uint32_t timestamp, const int16_t* data,
17+
virtual void UpdateFrame(uint32_t timestamp, const int16_t* data,
4818
size_t samples_per_channel, int sample_rate_hz,
4919
size_t num_channels = 1) = 0;
5020

51-
/**
52-
* @brief Copies the contents of another AudioFrame.
53-
* @param src: the source AudioFrame to copy from.
54-
*/
55-
virtual void CopyFrom(const AudioFrame& src) = 0;
21+
virtual void CopyFrom(const scoped_refptr<RTCAudioFrame> src) = 0;
5622

57-
/**
58-
* @brief Adds another AudioFrame to this one.
59-
* @param frame_to_add: the AudioFrame to add.
60-
*/
61-
virtual void Add(const AudioFrame& frame_to_add) = 0;
23+
virtual void Add(const scoped_refptr<RTCAudioFrame> frame_to_add) = 0;
6224

63-
/**
64-
* @brief Mutes the audio data in this AudioFrame.
65-
*/
6625
virtual void Mute() = 0;
6726

68-
/**
69-
* @brief Returns a pointer to the audio data buffer.
70-
* @return const int16_t*: a pointer to the audio data buffer.
71-
*/
7227
virtual const int16_t* data() = 0;
7328

74-
/**
75-
* @brief Returns the number of samples per channel.
76-
* @return size_t: the number of samples per channel.
77-
*/
7829
virtual size_t samples_per_channel() = 0;
7930

80-
/**
81-
* @brief Returns the sample rate in Hz.
82-
* @return int: the sample rate in Hz.
83-
*/
8431
virtual int sample_rate_hz() = 0;
8532

86-
/**
87-
* @brief Returns the number of audio channels.
88-
* @return size_t: the number of audio channels.
89-
*/
9033
virtual size_t num_channels() = 0;
9134

92-
/**
93-
* @brief Returns the timestamp of the AudioFrame.
94-
* @return uint32_t: the timestamp of the AudioFrame.
95-
*/
9635
virtual uint32_t timestamp() = 0;
97-
98-
/**
99-
* @brief Returns the unique identifier of the AudioFrame.
100-
* @return int: the unique identifier of the AudioFrame.
101-
*/
102-
103-
virtual int id() = 0;
10436
};
10537

106-
}; // namespace b2bua
38+
} // namespace libwebrtc
10739

10840
#endif

src/rtc_audio_frame_impl.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "rtc_audio_frame_impl.h"
2+
3+
#include "audio/utility/audio_frame_operations.h"
4+
5+
namespace libwebrtc {
6+
7+
scoped_refptr<RTCAudioFrame> RTCAudioFrame::Create() {
8+
return scoped_refptr<RTCAudioFrame>(
9+
new RefCountedObject<RTCAudioFrameImpl>());
10+
}
11+
12+
scoped_refptr<RTCAudioFrame> RTCAudioFrame::Create(
13+
uint32_t timestamp, const int16_t* data, size_t samples_per_channel,
14+
int sample_rate_hz, size_t num_channels /*= 1*/) {
15+
auto audio_frame =
16+
scoped_refptr<RTCAudioFrame>(new RefCountedObject<RTCAudioFrameImpl>());
17+
audio_frame->UpdateFrame(timestamp, data, samples_per_channel, sample_rate_hz,
18+
num_channels);
19+
return audio_frame;
20+
}
21+
22+
RTCAudioFrameImpl::RTCAudioFrameImpl(webrtc::AudioFrame& buffer) {
23+
buffer_.CopyFrom(buffer);
24+
}
25+
26+
RTCAudioFrameImpl::~RTCAudioFrameImpl() {}
27+
28+
void RTCAudioFrameImpl::UpdateFrame(uint32_t timestamp, const int16_t* data,
29+
size_t samples_per_channel,
30+
int sample_rate_hz,
31+
size_t num_channels /*= 1*/) {
32+
buffer_.UpdateFrame(timestamp, data, samples_per_channel, sample_rate_hz,
33+
webrtc::AudioFrame::kNormalSpeech,
34+
webrtc::AudioFrame::kVadUnknown, num_channels);
35+
}
36+
37+
void RTCAudioFrameImpl::CopyFrom(const scoped_refptr<RTCAudioFrame> src) {
38+
RTCAudioFrameImpl* frame = static_cast<RTCAudioFrameImpl*>(src.get());
39+
buffer_.CopyFrom(frame->buffer_);
40+
}
41+
42+
void RTCAudioFrameImpl::Add(const scoped_refptr<RTCAudioFrame> frame_to_add) {
43+
RTCAudioFrameImpl* frame =
44+
static_cast<RTCAudioFrameImpl*>(frame_to_add.get());
45+
webrtc::AudioFrameOperations::Add(frame->buffer_, &buffer_);
46+
}
47+
48+
void RTCAudioFrameImpl::Mute() { buffer_.Mute(); }
49+
50+
const int16_t* RTCAudioFrameImpl::data() { return buffer_.data(); }
51+
52+
size_t RTCAudioFrameImpl::samples_per_channel() {
53+
return buffer_.samples_per_channel_;
54+
}
55+
56+
int RTCAudioFrameImpl::sample_rate_hz() { return buffer_.sample_rate_hz_; }
57+
58+
size_t RTCAudioFrameImpl::num_channels() { return buffer_.num_channels_; }
59+
60+
uint32_t RTCAudioFrameImpl::timestamp() { return buffer_.timestamp_; }
61+
62+
} // namespace libwebrtc

src/rtc_audio_frame_impl.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "api/audio/audio_frame.h"
2+
#include "include/rtc_audio_frame.h"
3+
4+
namespace libwebrtc {
5+
6+
class RTCAudioFrameImpl : public RTCAudioFrame {
7+
public:
8+
RTCAudioFrameImpl() {}
9+
RTCAudioFrameImpl(webrtc::AudioFrame& buffer);
10+
~RTCAudioFrameImpl();
11+
12+
public:
13+
virtual void UpdateFrame(uint32_t timestamp, const int16_t* data,
14+
size_t samples_per_channel, int sample_rate_hz,
15+
size_t num_channels = 1) override;
16+
17+
virtual void CopyFrom(const scoped_refptr<RTCAudioFrame> src) override;
18+
19+
virtual void Add(const scoped_refptr<RTCAudioFrame> frame_to_add) override;
20+
21+
virtual void Mute() override;
22+
23+
virtual const int16_t* data() override;
24+
25+
virtual size_t samples_per_channel() override;
26+
27+
virtual int sample_rate_hz() override;
28+
29+
virtual size_t num_channels() override;
30+
31+
virtual uint32_t timestamp() override;
32+
33+
private:
34+
webrtc::AudioFrame buffer_;
35+
};
36+
37+
} // namespace libwebrtc

0 commit comments

Comments
 (0)