OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/transport/rtp_sender/rtp_sender.h" | 5 #include "media/cast/transport/rtp_sender/rtp_sender.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
9 #include "media/cast/transport/cast_transport_defines.h" | 9 #include "media/cast/transport/cast_transport_defines.h" |
10 #include "media/cast/transport/pacing/paced_sender.h" | 10 #include "media/cast/transport/pacing/paced_sender.h" |
11 #include "net/base/big_endian.h" | 11 #include "net/base/big_endian.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 namespace cast { | 14 namespace cast { |
15 namespace transport { | 15 namespace transport { |
16 | 16 |
17 // Schedule the RTP statistics callback every 33mS. As this interval affects the | 17 // Schedule the RTP statistics callback every 33mS. As this interval affects the |
18 // time offset of the render and playout times, we want it in the same ball park | 18 // time offset of the render and playout times, we want it in the same ball park |
19 // as the frame rate. | 19 // as the frame rate. |
20 static const int kStatsCallbackIntervalMs = 33; | 20 static const int kStatsCallbackIntervalMs = 33; |
21 | 21 |
22 RtpSender::RtpSender( | 22 RtpSender::RtpSender( |
23 base::TickClock* clock, | 23 base::TickClock* clock, |
24 const CastTransportConfig& config, | 24 const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner, |
25 bool is_audio, | |
26 const scoped_refptr<base::TaskRunner>& transport_task_runner, | |
27 PacedSender* const transport) | 25 PacedSender* const transport) |
28 : config_(), | 26 : clock_(clock), |
29 transport_(transport), | 27 transport_(transport), |
30 stats_callback_(), | 28 stats_callback_(), |
31 transport_task_runner_(transport_task_runner) { | 29 transport_task_runner_(transport_task_runner), |
32 // Store generic cast config and create packetizer config. | 30 weak_factory_(this) { |
33 if (is_audio) { | 31 // Randomly set sequence number start value. |
34 storage_.reset( | |
35 new PacketStorage(clock, config.audio_rtp_config.history_ms)); | |
36 config_.audio = true; | |
37 config_.ssrc = config.audio_ssrc; | |
38 config_.payload_type = config.audio_rtp_config.payload_type; | |
39 config_.frequency = config.audio_frequency; | |
40 config_.audio_codec = config.audio_codec; | |
41 } else { | |
42 storage_.reset( | |
43 new PacketStorage(clock, config.audio_rtp_config.history_ms)); | |
44 config_.audio = false; | |
45 config_.ssrc = config.video_ssrc; | |
46 config_.payload_type = config.video_rtp_config.payload_type; | |
47 config_.frequency = kVideoFrequency; | |
48 config_.video_codec = config.video_codec; | |
49 } | |
50 // Randomly set start values. | |
51 config_.sequence_number = base::RandInt(0, 65535); | 32 config_.sequence_number = base::RandInt(0, 65535); |
52 packetizer_.reset( | |
53 new RtpPacketizer(transport, storage_.get(), config_)); | |
54 } | 33 } |
55 | 34 |
56 RtpSender::~RtpSender() {} | 35 RtpSender::~RtpSender() {} |
57 | 36 |
| 37 void RtpSender::InitializeAudio(const CastTransportAudioConfig& config) { |
| 38 storage_.reset(new PacketStorage(clock_, config.base.rtp_config.history_ms)); |
| 39 config_.audio = true; |
| 40 config_.ssrc = config.base.ssrc; |
| 41 config_.payload_type = config.base.rtp_config.payload_type; |
| 42 config_.frequency = config.frequency; |
| 43 config_.audio_codec = config.codec; |
| 44 packetizer_.reset(new RtpPacketizer(transport_, storage_.get(), config_)); |
| 45 } |
| 46 |
| 47 void RtpSender::InitializeVideo(const CastTransportVideoConfig& config) { |
| 48 storage_.reset(new PacketStorage(clock_, config.base.rtp_config.history_ms)); |
| 49 config_.audio = false; |
| 50 config_.ssrc = config.base.ssrc; |
| 51 config_.payload_type = config.base.rtp_config.payload_type; |
| 52 config_.frequency = kVideoFrequency; |
| 53 config_.video_codec = config.codec; |
| 54 packetizer_.reset(new RtpPacketizer(transport_, storage_.get(), config_)); |
| 55 } |
| 56 |
58 void RtpSender::IncomingEncodedVideoFrame(const EncodedVideoFrame* video_frame, | 57 void RtpSender::IncomingEncodedVideoFrame(const EncodedVideoFrame* video_frame, |
59 const base::TimeTicks& capture_time) { | 58 const base::TimeTicks& capture_time) { |
| 59 DCHECK(packetizer_); |
60 packetizer_->IncomingEncodedVideoFrame(video_frame, capture_time); | 60 packetizer_->IncomingEncodedVideoFrame(video_frame, capture_time); |
61 } | 61 } |
62 | 62 |
63 void RtpSender::IncomingEncodedAudioFrame( | 63 void RtpSender::IncomingEncodedAudioFrame( |
64 const EncodedAudioFrame* audio_frame, | 64 const EncodedAudioFrame* audio_frame, |
65 const base::TimeTicks& recorded_time) { | 65 const base::TimeTicks& recorded_time) { |
| 66 DCHECK(packetizer_); |
66 packetizer_->IncomingEncodedAudioFrame(audio_frame, recorded_time); | 67 packetizer_->IncomingEncodedAudioFrame(audio_frame, recorded_time); |
67 } | 68 } |
68 | 69 |
69 void RtpSender::ResendPackets( | 70 void RtpSender::ResendPackets( |
70 const MissingFramesAndPacketsMap& missing_frames_and_packets) { | 71 const MissingFramesAndPacketsMap& missing_frames_and_packets) { |
| 72 DCHECK(storage_); |
71 // Iterate over all frames in the list. | 73 // Iterate over all frames in the list. |
72 for (MissingFramesAndPacketsMap::const_iterator it = | 74 for (MissingFramesAndPacketsMap::const_iterator it = |
73 missing_frames_and_packets.begin(); | 75 missing_frames_and_packets.begin(); |
74 it != missing_frames_and_packets.end(); | 76 it != missing_frames_and_packets.end(); |
75 ++it) { | 77 ++it) { |
76 PacketList packets_to_resend; | 78 PacketList packets_to_resend; |
77 uint8 frame_id = it->first; | 79 uint8 frame_id = it->first; |
78 const PacketIdSet& packets_set = it->second; | 80 const PacketIdSet& packets_set = it->second; |
79 bool success = false; | 81 bool success = false; |
80 | 82 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 129 |
128 void RtpSender::SubscribeRtpStatsCallback( | 130 void RtpSender::SubscribeRtpStatsCallback( |
129 const CastTransportRtpStatistics& callback) { | 131 const CastTransportRtpStatistics& callback) { |
130 stats_callback_ = callback; | 132 stats_callback_ = callback; |
131 ScheduleNextStatsReport(); | 133 ScheduleNextStatsReport(); |
132 } | 134 } |
133 | 135 |
134 void RtpSender::ScheduleNextStatsReport() { | 136 void RtpSender::ScheduleNextStatsReport() { |
135 transport_task_runner_->PostDelayedTask( | 137 transport_task_runner_->PostDelayedTask( |
136 FROM_HERE, | 138 FROM_HERE, |
137 base::Bind(&RtpSender::RtpStatistics, base::AsWeakPtr(this)), | 139 base::Bind(&RtpSender::RtpStatistics, weak_factory_.GetWeakPtr()), |
138 base::TimeDelta::FromMilliseconds(kStatsCallbackIntervalMs)); | 140 base::TimeDelta::FromMilliseconds(kStatsCallbackIntervalMs)); |
139 } | 141 } |
140 | 142 |
141 void RtpSender::RtpStatistics() { | 143 void RtpSender::RtpStatistics() { |
142 RtcpSenderInfo sender_info; | 144 RtcpSenderInfo sender_info; |
143 base::TimeTicks time_sent; | 145 base::TimeTicks time_sent; |
144 uint32 rtp_timestamp = 0; | 146 uint32 rtp_timestamp = 0; |
145 packetizer_->LastSentTimestamp(&time_sent, &rtp_timestamp); | 147 packetizer_->LastSentTimestamp(&time_sent, &rtp_timestamp); |
146 sender_info.send_packet_count = packetizer_->send_packets_count(); | 148 sender_info.send_packet_count = packetizer_->send_packets_count(); |
147 sender_info.send_octet_count = packetizer_->send_octet_count(); | 149 sender_info.send_octet_count = packetizer_->send_octet_count(); |
148 stats_callback_.Run(sender_info, time_sent, rtp_timestamp); | 150 stats_callback_.Run(sender_info, time_sent, rtp_timestamp); |
149 ScheduleNextStatsReport(); | 151 ScheduleNextStatsReport(); |
150 } | 152 } |
151 | 153 |
152 } // namespace transport | 154 } // namespace transport |
153 } // namespace cast | 155 } // namespace cast |
154 } // namespace media | 156 } // namespace media |
OLD | NEW |