DTMF Event Sub-API on VoIP API
Added VoipDtmf in VoipEngine as a sub-API to provide DTMF related interfaces; also added relevant unit tests. Bug: webrtc:11802 Change-Id: Ie9832aebe075a48ae1207be142361b73646673ca Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/180225 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Tim Na <natim@webrtc.org> Reviewed-by: Per Åhgren <peah@webrtc.org> Commit-Queue: Tim Na <natim@webrtc.org> Cr-Commit-Position: refs/heads/master@{#31974}
This commit is contained in:
@ -63,6 +63,12 @@ class AudioChannel : public rtc::RefCountInterface {
|
||||
absl::optional<SdpAudioFormat> GetEncoderFormat() const {
|
||||
return egress_->GetEncoderFormat();
|
||||
}
|
||||
void RegisterTelephoneEventType(int rtp_payload_type, int sample_rate_hz) {
|
||||
egress_->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz);
|
||||
}
|
||||
bool SendTelephoneEvent(int dtmf_event, int duration_ms) {
|
||||
return egress_->SendTelephoneEvent(dtmf_event, duration_ms);
|
||||
}
|
||||
|
||||
// APIs relayed to AudioIngress.
|
||||
bool IsPlaying() const { return ingress_->IsPlaying(); }
|
||||
|
||||
@ -24,6 +24,9 @@ using ::testing::NiceMock;
|
||||
using ::testing::Return;
|
||||
|
||||
constexpr int kPcmuPayload = 0;
|
||||
constexpr int kPcmuSampleRateHz = 8000;
|
||||
constexpr int kDtmfEventDurationMs = 1000;
|
||||
constexpr DtmfEvent kDtmfEventCode = DtmfEvent::kDigitZero;
|
||||
|
||||
class VoipCoreTest : public ::testing::Test {
|
||||
public:
|
||||
@ -68,6 +71,12 @@ TEST_F(VoipCoreTest, BasicVoipCoreOperation) {
|
||||
EXPECT_TRUE(voip_core_->StartSend(*channel));
|
||||
EXPECT_TRUE(voip_core_->StartPlayout(*channel));
|
||||
|
||||
voip_core_->RegisterTelephoneEventType(*channel, kPcmuPayload,
|
||||
kPcmuSampleRateHz);
|
||||
|
||||
EXPECT_TRUE(voip_core_->SendDtmfEvent(*channel, kDtmfEventCode,
|
||||
kDtmfEventDurationMs));
|
||||
|
||||
// Program mock as operational that is ready to be stopped.
|
||||
EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(true));
|
||||
EXPECT_CALL(*audio_device_, Playing()).WillOnce(Return(true));
|
||||
@ -91,9 +100,52 @@ TEST_F(VoipCoreTest, ExpectFailToUseReleasedChannelId) {
|
||||
// These should be no-op.
|
||||
voip_core_->SetSendCodec(*channel, kPcmuPayload, kPcmuFormat);
|
||||
voip_core_->SetReceiveCodecs(*channel, {{kPcmuPayload, kPcmuFormat}});
|
||||
voip_core_->RegisterTelephoneEventType(*channel, kPcmuPayload,
|
||||
kPcmuSampleRateHz);
|
||||
|
||||
EXPECT_FALSE(voip_core_->StartSend(*channel));
|
||||
EXPECT_FALSE(voip_core_->StartPlayout(*channel));
|
||||
EXPECT_FALSE(voip_core_->SendDtmfEvent(*channel, kDtmfEventCode,
|
||||
kDtmfEventDurationMs));
|
||||
}
|
||||
|
||||
TEST_F(VoipCoreTest, SendDtmfEventWithoutRegistering) {
|
||||
// Program mock as non-operational and ready to start send.
|
||||
EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(false));
|
||||
EXPECT_CALL(*audio_device_, InitRecording()).WillOnce(Return(0));
|
||||
EXPECT_CALL(*audio_device_, StartRecording()).WillOnce(Return(0));
|
||||
|
||||
auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
|
||||
EXPECT_TRUE(channel);
|
||||
|
||||
voip_core_->SetSendCodec(*channel, kPcmuPayload, kPcmuFormat);
|
||||
|
||||
EXPECT_TRUE(voip_core_->StartSend(*channel));
|
||||
// Send Dtmf event without registering beforehand, thus payload
|
||||
// type is not set and false is expected.
|
||||
EXPECT_FALSE(voip_core_->SendDtmfEvent(*channel, kDtmfEventCode,
|
||||
kDtmfEventDurationMs));
|
||||
|
||||
// Program mock as sending and is ready to be stopped.
|
||||
EXPECT_CALL(*audio_device_, Recording()).WillOnce(Return(true));
|
||||
EXPECT_CALL(*audio_device_, StopRecording()).WillOnce(Return(0));
|
||||
|
||||
EXPECT_TRUE(voip_core_->StopSend(*channel));
|
||||
voip_core_->ReleaseChannel(*channel);
|
||||
}
|
||||
|
||||
TEST_F(VoipCoreTest, SendDtmfEventWithoutStartSend) {
|
||||
auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
|
||||
EXPECT_TRUE(channel);
|
||||
|
||||
voip_core_->RegisterTelephoneEventType(*channel, kPcmuPayload,
|
||||
kPcmuSampleRateHz);
|
||||
// Send Dtmf event without calling StartSend beforehand, thus
|
||||
// Dtmf events cannot be sent and false is expected.
|
||||
EXPECT_FALSE(voip_core_->SendDtmfEvent(*channel, kDtmfEventCode,
|
||||
kDtmfEventDurationMs));
|
||||
|
||||
voip_core_->ReleaseChannel(*channel);
|
||||
}
|
||||
|
||||
TEST_F(VoipCoreTest, StartSendAndPlayoutWithoutSettingCodec) {
|
||||
|
||||
@ -340,4 +340,24 @@ void VoipCore::SetReceiveCodecs(
|
||||
}
|
||||
}
|
||||
|
||||
void VoipCore::RegisterTelephoneEventType(ChannelId channel,
|
||||
int rtp_payload_type,
|
||||
int sample_rate_hz) {
|
||||
// Failure to locate channel is logged internally in GetChannel.
|
||||
if (auto audio_channel = GetChannel(channel)) {
|
||||
audio_channel->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz);
|
||||
}
|
||||
}
|
||||
|
||||
bool VoipCore::SendDtmfEvent(ChannelId channel,
|
||||
DtmfEvent dtmf_event,
|
||||
int duration_ms) {
|
||||
// Failure to locate channel is logged internally in GetChannel.
|
||||
if (auto audio_channel = GetChannel(channel)) {
|
||||
return audio_channel->SendTelephoneEvent(static_cast<int>(dtmf_event),
|
||||
duration_ms);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include "api/task_queue/task_queue_factory.h"
|
||||
#include "api/voip/voip_base.h"
|
||||
#include "api/voip/voip_codec.h"
|
||||
#include "api/voip/voip_dtmf.h"
|
||||
#include "api/voip/voip_engine.h"
|
||||
#include "api/voip/voip_network.h"
|
||||
#include "audio/audio_transport_impl.h"
|
||||
@ -45,7 +46,8 @@ namespace webrtc {
|
||||
class VoipCore : public VoipEngine,
|
||||
public VoipBase,
|
||||
public VoipNetwork,
|
||||
public VoipCodec {
|
||||
public VoipCodec,
|
||||
public VoipDtmf {
|
||||
public:
|
||||
~VoipCore() override = default;
|
||||
|
||||
@ -63,6 +65,7 @@ class VoipCore : public VoipEngine,
|
||||
VoipBase& Base() override { return *this; }
|
||||
VoipNetwork& Network() override { return *this; }
|
||||
VoipCodec& Codec() override { return *this; }
|
||||
VoipDtmf& Dtmf() override { return *this; }
|
||||
|
||||
// Implements VoipBase interfaces.
|
||||
absl::optional<ChannelId> CreateChannel(
|
||||
@ -88,6 +91,14 @@ class VoipCore : public VoipEngine,
|
||||
ChannelId channel,
|
||||
const std::map<int, SdpAudioFormat>& decoder_specs) override;
|
||||
|
||||
// Implements VoipDtmf interfaces.
|
||||
void RegisterTelephoneEventType(ChannelId channel,
|
||||
int rtp_payload_type,
|
||||
int sample_rate_hz) override;
|
||||
bool SendDtmfEvent(ChannelId channel,
|
||||
DtmfEvent dtmf_event,
|
||||
int duration_ms) override;
|
||||
|
||||
private:
|
||||
// Fetches the corresponding AudioChannel assigned with given |channel|.
|
||||
// Returns nullptr if not found.
|
||||
|
||||
Reference in New Issue
Block a user