Commit bb517383 authored by miu@chromium.org's avatar miu@chromium.org

[Cast] Halt AudioSender transmission when too many frames are in-flight.

The approach here is to literally copy most of the logic in VideoSender
over into AudioSender, since VideoSender already solves a number of
transmission/re-transmission issues using heuristics that should work
well for audio.  (This also brings us much closer to being able to merge
AudioSender and VideoSender into one implementation.)

Testing: Confirmed correct halting and recovery behavior between
cast_sender_app and cast_receiver_app (SIGSTOPP'ed and SIGCONT'ed each
to simulate a temporary outage).  Also tested a Chromium sender with a
Chromecast receiver.

BUG=380023

Review URL: https://codereview.chromium.org/340903003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278323 0039d316-1c4b-4281-b951-d872f2087c98
parent 5f5249e6
......@@ -9,14 +9,12 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/sys_byteorder.h"
#include "base/time/time.h"
#include "media/base/audio_bus.h"
#include "media/cast/cast_defines.h"
#include "media/cast/cast_environment.h"
#include "media/cast/logging/logging_defines.h"
#include "third_party/opus/src/include/opus.h"
namespace media {
......@@ -33,28 +31,6 @@ const int kFrameDurationMillis = 1000 / kFramesPerSecond; // No remainder!
// coming in too slow with respect to the capture timestamps.
const int kUnderrunThresholdMillis = 3 * kFrameDurationMillis;
void LogAudioFrameEncodedEvent(
const scoped_refptr<media::cast::CastEnvironment>& cast_environment,
base::TimeTicks event_time,
media::cast::RtpTimestamp rtp_timestamp,
uint32 frame_id,
size_t frame_size) {
if (!cast_environment->CurrentlyOn(CastEnvironment::MAIN)) {
cast_environment->PostTask(
CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&LogAudioFrameEncodedEvent,
cast_environment, event_time,
rtp_timestamp, frame_id, frame_size));
return;
}
cast_environment->Logging()->InsertEncodedFrameEvent(
event_time, media::cast::FRAME_ENCODED, media::cast::AUDIO_EVENT,
rtp_timestamp, frame_id,
static_cast<int>(frame_size), /* key_frame - unused */ false,
/*target_bitrate - unused*/ 0);
}
} // namespace
......@@ -150,11 +126,6 @@ class AudioEncoder::ImplBase
audio_frame->reference_time = frame_capture_time_;
if (EncodeFromFilledBuffer(&audio_frame->data)) {
LogAudioFrameEncodedEvent(cast_environment_,
cast_environment_->Clock()->NowTicks(),
audio_frame->rtp_timestamp,
audio_frame->frame_id,
audio_frame->data.size());
cast_environment_->PostTask(
CastEnvironment::MAIN,
FROM_HERE,
......
This diff is collapsed.
......@@ -14,6 +14,8 @@
#include "base/time/time.h"
#include "media/base/audio_bus.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/rtcp/rtcp.h"
#include "media/cast/rtp_timestamp_helper.h"
......@@ -22,8 +24,12 @@ namespace cast {
class AudioEncoder;
// This class is not thread safe.
// It's only called from the main cast thread.
// Not thread safe. Only called from the main cast thread.
// This class owns all objects related to sending audio, objects that create RTP
// packets, congestion control, audio encoder, parsing and sending of
// RTCP packets.
// Additionally it posts a bunch of delayed tasks to the main thread for various
// timeouts.
class AudioSender : public RtcpSenderFeedback,
public base::NonThreadSafe,
public base::SupportsWeakPtr<AudioSender> {
......@@ -38,6 +44,10 @@ class AudioSender : public RtcpSenderFeedback,
return cast_initialization_status_;
}
// Note: It is not guaranteed that |audio_frame| will actually be encoded and
// sent, if AudioSender detects too many frames in flight. Therefore, clients
// should be careful about the rate at which this method is called.
//
// Note: It is invalid to call this method if InitializationResult() returns
// anything but STATUS_AUDIO_INITIALIZED.
void InsertAudio(scoped_ptr<AudioBus> audio_bus,
......@@ -46,31 +56,98 @@ class AudioSender : public RtcpSenderFeedback,
// Only called from the main cast thread.
void IncomingRtcpPacket(scoped_ptr<Packet> packet);
private:
void ResendPackets(
const MissingFramesAndPacketsMap& missing_frames_and_packets);
protected:
// Protected for testability.
virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback)
OVERRIDE;
private:
// Schedule and execute periodic sending of RTCP report.
void ScheduleNextRtcpReport();
void SendRtcpReport(bool schedule_future_reports);
// Schedule and execute periodic checks for re-sending packets. If no
// acknowledgements have been received for "too long," AudioSender will
// speculatively re-send certain packets of an unacked frame to kick-start
// re-transmission. This is a last resort tactic to prevent the session from
// getting stuck after a long outage.
void ScheduleNextResendCheck();
void ResendCheck();
void ResendForKickstart();
// Returns true if there are too many frames in flight, as defined by the
// configured target playout delay plus simple logic. When this is true,
// InsertAudio() will silenty drop frames instead of sending them to the audio
// encoder.
bool AreTooManyFramesInFlight() const;
// Called by the |audio_encoder_| with the next EncodedFrame to send.
void SendEncodedAudioFrame(scoped_ptr<transport::EncodedFrame> audio_frame);
virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback)
OVERRIDE;
scoped_refptr<CastEnvironment> cast_environment_;
const scoped_refptr<CastEnvironment> cast_environment_;
// The total amount of time between a frame's capture/recording on the sender
// and its playback on the receiver (i.e., shown to a user). This is fixed as
// a value large enough to give the system sufficient time to encode,
// transmit/retransmit, receive, decode, and render; given its run-time
// environment (sender/receiver hardware performance, network conditions,
// etc.).
const base::TimeDelta target_playout_delay_;
// Sends encoded frames over the configured transport (e.g., UDP). In
// Chromium, this could be a proxy that first sends the frames from a renderer
// process to the browser process over IPC, with the browser process being
// responsible for "packetizing" the frames and pushing packets into the
// network layer.
transport::CastTransportSender* const transport_sender_;
// Maximum number of outstanding frames before the encoding and sending of
// new frames shall halt.
const int max_unacked_frames_;
// Encodes AudioBuses into EncodedFrames.
scoped_ptr<AudioEncoder> audio_encoder_;
RtpTimestampHelper rtp_timestamp_helper_;
const int configured_encoder_bitrate_;
// Manages sending/receiving of RTCP packets, including sender/receiver
// reports.
Rtcp rtcp_;
// Records lip-sync (i.e., mapping of RTP <--> NTP timestamps), and
// extrapolates this mapping to any other point in time.
RtpTimestampHelper rtp_timestamp_helper_;
// Counts how many RTCP reports are being "aggressively" sent (i.e., one per
// frame) at the start of the session. Once a threshold is reached, RTCP
// reports are instead sent at the configured interval + random drift.
int num_aggressive_rtcp_reports_sent_;
// This is "null" until the first frame is sent. Thereafter, this tracks the
// last time any frame was sent or re-sent.
base::TimeTicks last_send_time_;
// The ID of the last frame sent. Logic throughout AudioSender assumes this
// can safely wrap-around. This member is invalid until
// |!last_send_time_.is_null()|.
uint32 last_sent_frame_id_;
// The ID of the latest (not necessarily the last) frame that has been
// acknowledged. Logic throughout AudioSender assumes this can safely
// wrap-around. This member is invalid until |!last_send_time_.is_null()|.
uint32 latest_acked_frame_id_;
// Counts the number of duplicate ACK that are being received. When this
// number reaches a threshold, the sender will take this as a sign that the
// receiver hasn't yet received the first packet of the next frame. In this
// case, AudioSender will trigger a re-send of the next frame.
int duplicate_ack_counter_;
// If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED.
CastInitializationStatus cast_initialization_status_;
// Used to map the lower 8 bits of the frame id to a RTP timestamp. This is
// good enough as we only use it for logging.
// This is a "good enough" mapping for finding the RTP timestamp associated
// with a video frame. The key is the lowest 8 bits of frame id (which is
// what is sent via RTCP). This map is used for logging purposes.
RtpTimestamp frame_id_to_rtp_timestamp_[256];
// NOTE: Weak pointers must be invalidated before all other member variables.
......
......@@ -78,7 +78,7 @@ VideoSender::VideoSender(
media::cast::transport::CastTransportVideoConfig transport_config;
transport_config.codec = video_config.codec;
transport_config.rtp.config = video_config.rtp_config;
transport_config.rtp.max_outstanding_frames = max_unacked_frames_ + 1;
transport_config.rtp.max_outstanding_frames = max_unacked_frames_;
transport_sender_->InitializeVideo(transport_config);
rtcp_.SetCastReceiverEventHistorySize(kReceiverRtcpEventHistorySize);
......
......@@ -76,15 +76,13 @@ class VideoSender : public RtcpSenderFeedback,
void ScheduleNextRtcpReport();
void SendRtcpReport(bool schedule_future_reports);
// Schedule and execute periodic checks for re-sending frames. If no
// Schedule and execute periodic checks for re-sending packets. If no
// acknowledgements have been received for "too long," VideoSender will
// speculatively re-send the frame just after |latest_acked_frame_id_| (the
// whole frame). This is a last resort tactic to prevent the session from
// speculatively re-send certain packets of an unacked frame to kick-start
// re-transmission. This is a last resort tactic to prevent the session from
// getting stuck after a long outage.
void ScheduleNextResendCheck();
void ResendCheck();
// Resend certain packets of an unacked frame to kick start re-transmission.
void ResendForKickstart();
// Returns true if there are too many frames in flight, as defined by the
......@@ -169,10 +167,7 @@ class VideoSender : public RtcpSenderFeedback,
// This is a "good enough" mapping for finding the RTP timestamp associated
// with a video frame. The key is the lowest 8 bits of frame id (which is
// what is sent via RTCP). This map is used for logging purposes. The only
// time when this mapping will be incorrect is when it receives an ACK for a
// old enough frame such that 8-bit wrap around has already occurred, which
// should be pretty rare.
// what is sent via RTCP). This map is used for logging purposes.
RtpTimestamp frame_id_to_rtp_timestamp_[256];
// NOTE: Weak pointers must be invalidated before all other member variables.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment