Skip to content

Commit

Permalink
add RTCAudioFrame.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Aug 15, 2024
1 parent 80a9d67 commit 4a7b1ac
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 81 deletions.
3 changes: 3 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ rtc_shared_library("libwebrtc") {
"include/base/scoped_ref_ptr.h",
"include/libwebrtc.h",
"include/rtc_audio_device.h",
"include/rtc_audio_frame.h",
"include/rtc_audio_source.h",
"include/rtc_audio_track.h",
"include/rtc_data_channel.h",
Expand Down Expand Up @@ -105,6 +106,8 @@ rtc_shared_library("libwebrtc") {
"src/libwebrtc.cc",
"src/rtc_audio_device_impl.cc",
"src/rtc_audio_device_impl.h",
"src/rtc_audio_frame_impl.cc",
"src/rtc_audio_frame_impl.h",
"src/rtc_audio_source_impl.cc",
"src/rtc_audio_source_impl.h",
"src/rtc_audio_track_impl.cc",
Expand Down
94 changes: 13 additions & 81 deletions include/rtc_audio_frame.h
Original file line number Diff line number Diff line change
@@ -1,108 +1,40 @@
#ifndef AUDIO_FRAME_HXX
#define AUDIO_FRAME_HXX
#ifndef LIB_WEBRTC_RTC_AUDIO_FRAME_HXX
#define LIB_WEBRTC_RTC_AUDIO_FRAME_HXX

#include "media_manager_types.h"
#include "rtc_types.h"

namespace b2bua {
namespace libwebrtc {

class AudioFrame {
class RTCAudioFrame : public RefCountInterface {
public:
/**
* @brief Creates a new instance of AudioFrame.
* @return AudioFrame*: a pointer to the newly created AudioFrame.
*/
MEDIA_MANAGER_API static AudioFrame* Create();
LIB_WEBRTC_API static scoped_refptr<RTCAudioFrame> Create();

/**
* @brief Creates a new instance of AudioFrame with specified parameters.
* @param id: the unique identifier of the frame.
* @param timestamp: the timestamp of the frame.
* @param data: a pointer to the audio data buffer.
* @param samples_per_channel: the number of samples per channel.
* @param sample_rate_hz: the sample rate in Hz.
* @param num_channels: the number of audio channels.
* @return AudioFrame*: a pointer to the newly created AudioFrame.
*/
MEDIA_MANAGER_API static AudioFrame* Create(int id, uint32_t timestamp,
const int16_t* data,
size_t samples_per_channel,
int sample_rate_hz,
size_t num_channels = 1);

/**
* @brief Releases the memory of this AudioFrame.
*/
virtual void Release() = 0;
LIB_WEBRTC_API static scoped_refptr<RTCAudioFrame> Create(
uint32_t timestamp, const int16_t* data, size_t samples_per_channel,
int sample_rate_hz, size_t num_channels = 1);

public:
/**
* @brief Updates the audio frame with specified parameters.
* @param id: the unique identifier of the frame.
* @param timestamp: the timestamp of the frame.
* @param data: a pointer to the audio data buffer.
* @param samples_per_channel: the number of samples per channel.
* @param sample_rate_hz: the sample rate in Hz.
* @param num_channels: the number of audio channels.
*/
virtual void UpdateFrame(int id, uint32_t timestamp, const int16_t* data,
virtual void UpdateFrame(uint32_t timestamp, const int16_t* data,
size_t samples_per_channel, int sample_rate_hz,
size_t num_channels = 1) = 0;

/**
* @brief Copies the contents of another AudioFrame.
* @param src: the source AudioFrame to copy from.
*/
virtual void CopyFrom(const AudioFrame& src) = 0;
virtual void CopyFrom(const scoped_refptr<RTCAudioFrame> src) = 0;

/**
* @brief Adds another AudioFrame to this one.
* @param frame_to_add: the AudioFrame to add.
*/
virtual void Add(const AudioFrame& frame_to_add) = 0;
virtual void Add(const scoped_refptr<RTCAudioFrame> frame_to_add) = 0;

/**
* @brief Mutes the audio data in this AudioFrame.
*/
virtual void Mute() = 0;

/**
* @brief Returns a pointer to the audio data buffer.
* @return const int16_t*: a pointer to the audio data buffer.
*/
virtual const int16_t* data() = 0;

/**
* @brief Returns the number of samples per channel.
* @return size_t: the number of samples per channel.
*/
virtual size_t samples_per_channel() = 0;

/**
* @brief Returns the sample rate in Hz.
* @return int: the sample rate in Hz.
*/
virtual int sample_rate_hz() = 0;

/**
* @brief Returns the number of audio channels.
* @return size_t: the number of audio channels.
*/
virtual size_t num_channels() = 0;

/**
* @brief Returns the timestamp of the AudioFrame.
* @return uint32_t: the timestamp of the AudioFrame.
*/
virtual uint32_t timestamp() = 0;

/**
* @brief Returns the unique identifier of the AudioFrame.
* @return int: the unique identifier of the AudioFrame.
*/

virtual int id() = 0;
};

}; // namespace b2bua
} // namespace libwebrtc

#endif
62 changes: 62 additions & 0 deletions src/rtc_audio_frame_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "rtc_audio_frame_impl.h"

#include "audio/utility/audio_frame_operations.h"

namespace libwebrtc {

scoped_refptr<RTCAudioFrame> RTCAudioFrame::Create() {
return scoped_refptr<RTCAudioFrame>(
new RefCountedObject<RTCAudioFrameImpl>());
}

scoped_refptr<RTCAudioFrame> RTCAudioFrame::Create(
uint32_t timestamp, const int16_t* data, size_t samples_per_channel,
int sample_rate_hz, size_t num_channels /*= 1*/) {
auto audio_frame =
scoped_refptr<RTCAudioFrame>(new RefCountedObject<RTCAudioFrameImpl>());
audio_frame->UpdateFrame(timestamp, data, samples_per_channel, sample_rate_hz,
num_channels);
return audio_frame;
}

RTCAudioFrameImpl::RTCAudioFrameImpl(webrtc::AudioFrame& buffer) {
buffer_.CopyFrom(buffer);
}

RTCAudioFrameImpl::~RTCAudioFrameImpl() {}

void RTCAudioFrameImpl::UpdateFrame(uint32_t timestamp, const int16_t* data,
size_t samples_per_channel,
int sample_rate_hz,
size_t num_channels /*= 1*/) {
buffer_.UpdateFrame(timestamp, data, samples_per_channel, sample_rate_hz,
webrtc::AudioFrame::kNormalSpeech,
webrtc::AudioFrame::kVadUnknown, num_channels);
}

void RTCAudioFrameImpl::CopyFrom(const scoped_refptr<RTCAudioFrame> src) {
RTCAudioFrameImpl* frame = static_cast<RTCAudioFrameImpl*>(src.get());
buffer_.CopyFrom(frame->buffer_);
}

void RTCAudioFrameImpl::Add(const scoped_refptr<RTCAudioFrame> frame_to_add) {
RTCAudioFrameImpl* frame =
static_cast<RTCAudioFrameImpl*>(frame_to_add.get());
webrtc::AudioFrameOperations::Add(frame->buffer_, &buffer_);
}

void RTCAudioFrameImpl::Mute() { buffer_.Mute(); }

const int16_t* RTCAudioFrameImpl::data() { return buffer_.data(); }

size_t RTCAudioFrameImpl::samples_per_channel() {
return buffer_.samples_per_channel_;
}

int RTCAudioFrameImpl::sample_rate_hz() { return buffer_.sample_rate_hz_; }

size_t RTCAudioFrameImpl::num_channels() { return buffer_.num_channels_; }

uint32_t RTCAudioFrameImpl::timestamp() { return buffer_.timestamp_; }

} // namespace libwebrtc
37 changes: 37 additions & 0 deletions src/rtc_audio_frame_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "api/audio/audio_frame.h"
#include "include/rtc_audio_frame.h"

namespace libwebrtc {

class RTCAudioFrameImpl : public RTCAudioFrame {
public:
RTCAudioFrameImpl() {}
RTCAudioFrameImpl(webrtc::AudioFrame& buffer);
~RTCAudioFrameImpl();

public:
virtual void UpdateFrame(uint32_t timestamp, const int16_t* data,
size_t samples_per_channel, int sample_rate_hz,
size_t num_channels = 1) override;

virtual void CopyFrom(const scoped_refptr<RTCAudioFrame> src) override;

virtual void Add(const scoped_refptr<RTCAudioFrame> frame_to_add) override;

virtual void Mute() override;

virtual const int16_t* data() override;

virtual size_t samples_per_channel() override;

virtual int sample_rate_hz() override;

virtual size_t num_channels() override;

virtual uint32_t timestamp() override;

private:
webrtc::AudioFrame buffer_;
};

} // namespace libwebrtc

0 comments on commit 4a7b1ac

Please sign in to comment.