Enforcing return type handling on VoIP API.

- This CL also affects some return type handling in Android Voip demo
app due to changes in return type handling.

Bug: webrtc:12193
Change-Id: Id76faf7c871476ed1f2d08fb587211ae234ae8d3
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/196625
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Per Ã…hgren <peah@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Tim Na <natim@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32821}
diff --git a/api/voip/BUILD.gn b/api/voip/BUILD.gn
index c099bfb..4db59fd 100644
--- a/api/voip/BUILD.gn
+++ b/api/voip/BUILD.gn
@@ -21,6 +21,7 @@
   ]
   deps = [
     "..:array_view",
+    "../../rtc_base/system:unused",
     "../audio_codecs:audio_codecs_api",
     "../neteq:neteq_api",
   ]
diff --git a/api/voip/DEPS b/api/voip/DEPS
index 3845dff..837b9a6 100644
--- a/api/voip/DEPS
+++ b/api/voip/DEPS
@@ -3,6 +3,10 @@
     "+third_party/absl/types/optional.h",
   ],
 
+  "voip_base.h": [
+    "+rtc_base/system/unused.h",
+  ],
+
   "voip_engine_factory.h": [
     "+modules/audio_device/include/audio_device.h",
     "+modules/audio_processing/include/audio_processing.h",
diff --git a/api/voip/voip_base.h b/api/voip/voip_base.h
index c5f54aa..6a411f8 100644
--- a/api/voip/voip_base.h
+++ b/api/voip/voip_base.h
@@ -12,6 +12,7 @@
 #define API_VOIP_VOIP_BASE_H_
 
 #include "absl/types/optional.h"
+#include "rtc_base/system/unused.h"
 
 namespace webrtc {
 
@@ -35,7 +36,7 @@
 
 enum class ChannelId : int {};
 
-enum class VoipResult {
+enum class RTC_WARN_UNUSED_RESULT VoipResult {
   // kOk indicates the function was successfully invoked with no error.
   kOk,
   // kInvalidArgument indicates the caller specified an invalid argument, such
diff --git a/audio/voip/test/voip_core_unittest.cc b/audio/voip/test/voip_core_unittest.cc
index f7a82f9..d290bd6 100644
--- a/audio/voip/test/voip_core_unittest.cc
+++ b/audio/voip/test/voip_core_unittest.cc
@@ -70,14 +70,18 @@
 
   auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
 
-  voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat);
-  voip_core_->SetReceiveCodecs(channel, {{kPcmuPayload, kPcmuFormat}});
+  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
+            VoipResult::kOk);
+  EXPECT_EQ(
+      voip_core_->SetReceiveCodecs(channel, {{kPcmuPayload, kPcmuFormat}}),
+      VoipResult::kOk);
 
   EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kOk);
   EXPECT_EQ(voip_core_->StartPlayout(channel), VoipResult::kOk);
 
-  voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
-                                         kPcmuSampleRateHz);
+  EXPECT_EQ(voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
+                                                   kPcmuSampleRateHz),
+            VoipResult::kOk);
 
   EXPECT_EQ(
       voip_core_->SendDtmfEvent(channel, kDtmfEventCode, kDtmfEventDurationMs),
@@ -125,7 +129,8 @@
 
   auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
 
-  voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat);
+  EXPECT_EQ(voip_core_->SetSendCodec(channel, kPcmuPayload, kPcmuFormat),
+            VoipResult::kOk);
 
   EXPECT_EQ(voip_core_->StartSend(channel), VoipResult::kOk);
   // Send Dtmf event without registering beforehand, thus payload
@@ -145,8 +150,10 @@
 TEST_F(VoipCoreTest, SendDtmfEventWithoutStartSend) {
   auto channel = voip_core_->CreateChannel(&transport_, 0xdeadc0de);
 
-  voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
-                                         kPcmuSampleRateHz);
+  EXPECT_EQ(voip_core_->RegisterTelephoneEventType(channel, kPcmuPayload,
+                                                   kPcmuSampleRateHz),
+            VoipResult::kOk);
+
   // Send Dtmf event without calling StartSend beforehand, thus
   // Dtmf events cannot be sent and kFailedPrecondition is expected.
   EXPECT_EQ(
diff --git a/examples/androidvoip/jni/android_voip_client.cc b/examples/androidvoip/jni/android_voip_client.cc
index d0763cd..95d3ed4 100644
--- a/examples/androidvoip/jni/android_voip_client.cc
+++ b/examples/androidvoip/jni/android_voip_client.cc
@@ -120,7 +120,7 @@
 
 namespace webrtc_examples {
 
-bool AndroidVoipClient::Init(
+void AndroidVoipClient::Init(
     JNIEnv* env,
     const webrtc::JavaParamRef<jobject>& application_context) {
   webrtc::VoipEngineConfig config;
@@ -132,20 +132,16 @@
   config.audio_processing = webrtc::AudioProcessingBuilder().Create();
 
   voip_thread_->Start();
+
   // Due to consistent thread requirement on
   // modules/audio_device/android/audio_device_template.h,
   // code is invoked in the context of voip_thread_.
-  return voip_thread_->Invoke<bool>(RTC_FROM_HERE, [this, &config] {
+  voip_thread_->Invoke<void>(RTC_FROM_HERE, [this, &config] {
     RTC_DCHECK_RUN_ON(voip_thread_.get());
 
     supported_codecs_ = config.encoder_factory->GetSupportedEncoders();
     env_ = webrtc::AttachCurrentThreadIfNeeded();
     voip_engine_ = webrtc::CreateVoipEngine(std::move(config));
-    if (!voip_engine_) {
-      RTC_LOG(LS_ERROR) << "VoipEngine creation failed";
-      return false;
-    }
-    return true;
   });
 }
 
@@ -175,9 +171,7 @@
   // Using `new` to access a non-public constructor.
   auto voip_client =
       absl::WrapUnique(new AndroidVoipClient(env, j_voip_client));
-  if (!voip_client->Init(env, application_context)) {
-    return nullptr;
-  }
+  voip_client->Init(env, application_context);
   return voip_client.release();
 }
 
@@ -220,8 +214,9 @@
   }
   for (const webrtc::AudioCodecSpec& codec : supported_codecs_) {
     if (codec.format.name == encoder) {
-      voip_engine_->Codec().SetSendCodec(
+      webrtc::VoipResult result = voip_engine_->Codec().SetSendCodec(
           *channel_, GetPayloadType(codec.format.name), codec.format);
+      RTC_CHECK(result == webrtc::VoipResult::kOk);
       return;
     }
   }
@@ -251,7 +246,9 @@
     }
   }
 
-  voip_engine_->Codec().SetReceiveCodecs(*channel_, decoder_specs);
+  webrtc::VoipResult result =
+      voip_engine_->Codec().SetReceiveCodecs(*channel_, decoder_specs);
+  RTC_CHECK(result == webrtc::VoipResult::kOk);
 }
 
 void AndroidVoipClient::SetDecoders(
@@ -305,13 +302,8 @@
 void AndroidVoipClient::StartSession(JNIEnv* env) {
   RUN_ON_VOIP_THREAD(StartSession, env);
 
+  // CreateChannel guarantees to return valid channel id.
   channel_ = voip_engine_->Base().CreateChannel(this, absl::nullopt);
-  if (!channel_) {
-    RTC_LOG(LS_ERROR) << "Channel creation failed";
-    Java_VoipClient_onStartSessionCompleted(env_, j_voip_client_,
-                                            /*isSuccessful=*/false);
-    return;
-  }
 
   rtp_socket_.reset(rtc::AsyncUDPSocket::Create(voip_thread_->socketserver(),
                                                 rtp_local_address_));
@@ -357,7 +349,9 @@
   rtp_socket_->Close();
   rtcp_socket_->Close();
 
-  voip_engine_->Base().ReleaseChannel(*channel_);
+  webrtc::VoipResult result = voip_engine_->Base().ReleaseChannel(*channel_);
+  RTC_CHECK(result == webrtc::VoipResult::kOk);
+
   channel_ = absl::nullopt;
   Java_VoipClient_onStopSessionCompleted(env_, j_voip_client_,
                                          /*isSuccessful=*/true);
@@ -470,9 +464,10 @@
     RTC_LOG(LS_ERROR) << "Channel has not been created";
     return;
   }
-  voip_engine_->Network().ReceivedRTPPacket(
+  webrtc::VoipResult result = voip_engine_->Network().ReceivedRTPPacket(
       *channel_,
       rtc::ArrayView<const uint8_t>(packet_copy.data(), packet_copy.size()));
+  RTC_CHECK(result == webrtc::VoipResult::kOk);
 }
 
 void AndroidVoipClient::OnSignalReadRTPPacket(rtc::AsyncPacketSocket* socket,
@@ -495,9 +490,10 @@
     RTC_LOG(LS_ERROR) << "Channel has not been created";
     return;
   }
-  voip_engine_->Network().ReceivedRTCPPacket(
+  webrtc::VoipResult result = voip_engine_->Network().ReceivedRTCPPacket(
       *channel_,
       rtc::ArrayView<const uint8_t>(packet_copy.data(), packet_copy.size()));
+  RTC_CHECK(result == webrtc::VoipResult::kOk);
 }
 
 void AndroidVoipClient::OnSignalReadRTCPPacket(rtc::AsyncPacketSocket* socket,
diff --git a/examples/androidvoip/jni/android_voip_client.h b/examples/androidvoip/jni/android_voip_client.h
index 4dd0b0a..bfca7e8 100644
--- a/examples/androidvoip/jni/android_voip_client.h
+++ b/examples/androidvoip/jni/android_voip_client.h
@@ -141,7 +141,7 @@
       : voip_thread_(rtc::Thread::CreateWithSocketServer()),
         j_voip_client_(env, j_voip_client) {}
 
-  bool Init(JNIEnv* env,
+  void Init(JNIEnv* env,
             const webrtc::JavaParamRef<jobject>& application_context);
 
   // Overloaded methods having native C++ variables as arguments.