Add 'AudioPacket' notification to media transport interface.

So far, base channel was only notifying about 'first audio packet' when
RTP was used, and it never notified about it when media_transport
interface was used. This change adds a sigslot to notify about a new
media packet to the media transport interface.

Bug: webrtc:9719
Change-Id: Ie9230c407f35b1aaa71ba71008ac34ba8869e2d4
Reviewed-on: https://webrtc-review.googlesource.com/c/117249
Reviewed-by: Anton Sukhanov <sukhanov@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Commit-Queue: Peter Slatala <psla@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26282}
diff --git a/api/media_transport_interface.cc b/api/media_transport_interface.cc
index ccdcb91..b5eaa89 100644
--- a/api/media_transport_interface.cc
+++ b/api/media_transport_interface.cc
@@ -157,6 +157,9 @@
   return std::unique_ptr<MediaTransportInterface>(nullptr);
 }
 
+MediaTransportInterface::MediaTransportInterface() = default;
+MediaTransportInterface::~MediaTransportInterface() = default;
+
 void MediaTransportInterface::SetKeyFrameRequestCallback(
     MediaTransportKeyFrameRequestCallback* callback) {}
 
@@ -168,6 +171,9 @@
 void MediaTransportInterface::SetNetworkChangeCallback(
     MediaTransportNetworkChangeCallback* callback) {}
 
+void MediaTransportInterface::SetFirstAudioPacketReceivedObserver(
+    AudioPacketReceivedObserver* observer) {}
+
 void MediaTransportInterface::AddTargetTransferRateObserver(
     TargetTransferRateObserver* observer) {}
 void MediaTransportInterface::RemoveTargetTransferRateObserver(
diff --git a/api/media_transport_interface.h b/api/media_transport_interface.h
index ee1b8e3..a440860 100644
--- a/api/media_transport_interface.h
+++ b/api/media_transport_interface.h
@@ -40,6 +40,15 @@
 
 class RtcEventLog;
 
+class AudioPacketReceivedObserver {
+ public:
+  virtual ~AudioPacketReceivedObserver() = default;
+
+  // Invoked for the first received audio packet on a given channel id.
+  // It will be invoked once for each channel id.
+  virtual void OnFirstAudioPacketReceived(int64_t channel_id) = 0;
+};
+
 // A collection of settings for creation of media transport.
 struct MediaTransportSettings final {
   MediaTransportSettings();
@@ -352,7 +361,8 @@
 // and receiving bandwidth estimate update from congestion control.
 class MediaTransportInterface {
  public:
-  virtual ~MediaTransportInterface() = default;
+  MediaTransportInterface();
+  virtual ~MediaTransportInterface();
 
   // Start asynchronous send of audio frame. The status returned by this method
   // only pertains to the synchronous operations (e.g.
@@ -399,6 +409,15 @@
   virtual void RemoveTargetTransferRateObserver(
       TargetTransferRateObserver* observer);
 
+  // Sets audio packets observer, which gets informed about incoming audio
+  // packets. Before destruction, the observer must be unregistered by setting
+  // nullptr.
+  //
+  // This method may be temporary, when the multiplexer is implemented (or
+  // multiplexer may use it to demultiplex channel ids).
+  virtual void SetFirstAudioPacketReceivedObserver(
+      AudioPacketReceivedObserver* observer);
+
   // Intended for receive side. AddRttObserver registers an observer to be
   // called for each RTT measurement, typically once per ACK. Before media
   // transport is destructed the observer must be unregistered.
diff --git a/pc/channel.cc b/pc/channel.cc
index 793f1ee..d26a511 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -744,6 +744,9 @@
                   crypto_options) {}
 
 VoiceChannel::~VoiceChannel() {
+  if (media_transport()) {
+    media_transport()->SetFirstAudioPacketReceivedObserver(nullptr);
+  }
   TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
   // this can't be done in the base class, since it calls a virtual
   DisableMedia_w();
@@ -762,6 +765,19 @@
   OnNetworkRouteChanged(absl::make_optional(network_route));
 }
 
+void VoiceChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport,
+                          webrtc::MediaTransportInterface* media_transport) {
+  BaseChannel::Init_w(rtp_transport, media_transport);
+  if (BaseChannel::media_transport()) {
+    this->media_transport()->SetFirstAudioPacketReceivedObserver(this);
+  }
+}
+
+void VoiceChannel::OnFirstAudioPacketReceived(int64_t channel_id) {
+  has_received_packet_ = true;
+  signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
+}
+
 void VoiceChannel::UpdateMediaSendRecvState_w() {
   // Render incoming data if we're the active call, and we have the local
   // content. We receive data on the default channel and multiplexed streams.
@@ -1034,7 +1050,8 @@
   Deinit();
 }
 
-void RtpDataChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport) {
+void RtpDataChannel::Init_w(webrtc::RtpTransportInternal* rtp_transport,
+                            webrtc::MediaTransportInterface* media_transport) {
   BaseChannel::Init_w(rtp_transport, /*media_transport=*/nullptr);
   media_channel()->SignalDataReceived.connect(this,
                                               &RtpDataChannel::OnDataReceived);
diff --git a/pc/channel.h b/pc/channel.h
index 09836b2..227c593 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -20,6 +20,7 @@
 
 #include "api/call/audio_sink.h"
 #include "api/jsep.h"
+#include "api/media_transport_interface.h"
 #include "api/rtp_receiver_interface.h"
 #include "api/video/video_sink_interface.h"
 #include "api/video/video_source_interface.h"
@@ -87,8 +88,8 @@
               bool srtp_required,
               webrtc::CryptoOptions crypto_options);
   virtual ~BaseChannel();
-  void Init_w(webrtc::RtpTransportInternal* rtp_transport,
-              webrtc::MediaTransportInterface* media_transport);
+  virtual void Init_w(webrtc::RtpTransportInternal* rtp_transport,
+                      webrtc::MediaTransportInterface* media_transport);
 
   // Deinit may be called multiple times and is simply ignored if it's already
   // done.
@@ -292,6 +293,8 @@
 
   bool RegisterRtpDemuxerSink();
 
+  bool has_received_packet_ = false;
+
  private:
   bool ConnectToRtpTransport();
   void DisconnectFromRtpTransport();
@@ -301,6 +304,7 @@
 
   // MediaTransportNetworkChangeCallback override.
   void OnNetworkRouteChanged(const rtc::NetworkRoute& network_route) override;
+
   rtc::Thread* const worker_thread_;
   rtc::Thread* const network_thread_;
   rtc::Thread* const signaling_thread_;
@@ -323,7 +327,6 @@
   std::vector<std::pair<rtc::Socket::Option, int> > rtcp_socket_options_;
   bool writable_ = false;
   bool was_ever_writable_ = false;
-  bool has_received_packet_ = false;
   const bool srtp_required_ = true;
   webrtc::CryptoOptions crypto_options_;
 
@@ -346,7 +349,8 @@
 
 // VoiceChannel is a specialization that adds support for early media, DTMF,
 // and input/output level monitoring.
-class VoiceChannel : public BaseChannel {
+class VoiceChannel : public BaseChannel,
+                     public webrtc::AudioPacketReceivedObserver {
  public:
   VoiceChannel(rtc::Thread* worker_thread,
                rtc::Thread* network_thread,
@@ -366,6 +370,8 @@
   cricket::MediaType media_type() const override {
     return cricket::MEDIA_TYPE_AUDIO;
   }
+  void Init_w(webrtc::RtpTransportInternal* rtp_transport,
+              webrtc::MediaTransportInterface* media_transport) override;
 
  private:
   // overrides from BaseChannel
@@ -377,6 +383,8 @@
                           webrtc::SdpType type,
                           std::string* error_desc) override;
 
+  void OnFirstAudioPacketReceived(int64_t channel_id) override;
+
   // Last AudioSendParameters sent down to the media_channel() via
   // SetSendParameters.
   AudioSendParameters last_send_params_;
@@ -443,7 +451,9 @@
               DtlsTransportInternal* rtcp_dtls_transport,
               rtc::PacketTransportInternal* rtp_packet_transport,
               rtc::PacketTransportInternal* rtcp_packet_transport);
-  void Init_w(webrtc::RtpTransportInternal* rtp_transport);
+  void Init_w(
+      webrtc::RtpTransportInternal* rtp_transport,
+      webrtc::MediaTransportInterface* media_transport = nullptr) override;
 
   virtual bool SendData(const SendDataParams& params,
                         const rtc::CopyOnWriteBuffer& payload,