1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/transport/cast_transport_sender_impl.h"
7 #include "base/single_thread_task_runner.h"
8 #include "media/cast/transport/cast_transport_config.h"
9 #include "media/cast/transport/cast_transport_defines.h"
15 scoped_ptr
<CastTransportSender
> CastTransportSender::Create(
17 base::TickClock
* clock
,
18 const net::IPEndPoint
& remote_end_point
,
19 const CastTransportStatusCallback
& status_callback
,
20 const BulkRawEventsCallback
& raw_events_callback
,
21 base::TimeDelta raw_events_callback_interval
,
22 const scoped_refptr
<base::SingleThreadTaskRunner
>& transport_task_runner
) {
23 return scoped_ptr
<CastTransportSender
>(
24 new CastTransportSenderImpl(net_log
,
29 raw_events_callback_interval
,
30 transport_task_runner
.get(),
34 CastTransportSenderImpl::CastTransportSenderImpl(
36 base::TickClock
* clock
,
37 const net::IPEndPoint
& remote_end_point
,
38 const CastTransportStatusCallback
& status_callback
,
39 const BulkRawEventsCallback
& raw_events_callback
,
40 base::TimeDelta raw_events_callback_interval
,
41 const scoped_refptr
<base::SingleThreadTaskRunner
>& transport_task_runner
,
42 PacketSender
* external_transport
)
44 status_callback_(status_callback
),
45 transport_task_runner_(transport_task_runner
),
46 transport_(external_transport
? NULL
47 : new UdpTransport(net_log
,
48 transport_task_runner
,
55 external_transport
? external_transport
: transport_
.get(),
56 transport_task_runner
),
57 rtcp_builder_(&pacer_
),
58 raw_events_callback_(raw_events_callback
) {
59 if (!raw_events_callback_
.is_null()) {
60 DCHECK(raw_events_callback_interval
> base::TimeDelta());
61 event_subscriber_
.reset(new SimpleEventSubscriber
);
62 logging_
.AddRawEventSubscriber(event_subscriber_
.get());
63 raw_events_timer_
.Start(FROM_HERE
,
64 raw_events_callback_interval
,
66 &CastTransportSenderImpl::SendRawEvents
);
70 CastTransportSenderImpl::~CastTransportSenderImpl() {
71 if (event_subscriber_
.get())
72 logging_
.RemoveRawEventSubscriber(event_subscriber_
.get());
75 void CastTransportSenderImpl::InitializeAudio(
76 const CastTransportAudioConfig
& config
) {
77 pacer_
.RegisterAudioSsrc(config
.rtp
.config
.ssrc
);
78 audio_sender_
.reset(new TransportAudioSender(
79 config
, clock_
, transport_task_runner_
, &pacer_
));
80 if (audio_sender_
->initialized())
81 status_callback_
.Run(TRANSPORT_AUDIO_INITIALIZED
);
83 status_callback_
.Run(TRANSPORT_AUDIO_UNINITIALIZED
);
86 void CastTransportSenderImpl::InitializeVideo(
87 const CastTransportVideoConfig
& config
) {
88 pacer_
.RegisterVideoSsrc(config
.rtp
.config
.ssrc
);
89 video_sender_
.reset(new TransportVideoSender(
90 config
, clock_
, transport_task_runner_
, &pacer_
));
91 if (video_sender_
->initialized())
92 status_callback_
.Run(TRANSPORT_VIDEO_INITIALIZED
);
94 status_callback_
.Run(TRANSPORT_VIDEO_UNINITIALIZED
);
97 void CastTransportSenderImpl::SetPacketReceiver(
98 const PacketReceiverCallback
& packet_receiver
) {
99 transport_
->StartReceiving(packet_receiver
);
102 void CastTransportSenderImpl::InsertCodedAudioFrame(
103 const EncodedAudioFrame
* audio_frame
,
104 const base::TimeTicks
& recorded_time
) {
105 DCHECK(audio_sender_
) << "Audio sender uninitialized";
106 audio_sender_
->InsertCodedAudioFrame(audio_frame
, recorded_time
);
109 void CastTransportSenderImpl::InsertCodedVideoFrame(
110 const EncodedVideoFrame
* video_frame
,
111 const base::TimeTicks
& capture_time
) {
112 DCHECK(video_sender_
) << "Video sender uninitialized";
113 video_sender_
->InsertCodedVideoFrame(video_frame
, capture_time
);
116 void CastTransportSenderImpl::SendRtcpFromRtpSender(
117 uint32 packet_type_flags
,
118 const RtcpSenderInfo
& sender_info
,
119 const RtcpDlrrReportBlock
& dlrr
,
120 const RtcpSenderLogMessage
& sender_log
,
122 const std::string
& c_name
) {
123 rtcp_builder_
.SendRtcpFromRtpSender(
124 packet_type_flags
, sender_info
, dlrr
, sender_log
, sending_ssrc
, c_name
);
127 void CastTransportSenderImpl::ResendPackets(
129 const MissingFramesAndPacketsMap
& missing_packets
) {
131 DCHECK(audio_sender_
) << "Audio sender uninitialized";
132 audio_sender_
->ResendPackets(missing_packets
);
134 DCHECK(video_sender_
) << "Video sender uninitialized";
135 video_sender_
->ResendPackets(missing_packets
);
139 void CastTransportSenderImpl::SubscribeAudioRtpStatsCallback(
140 const CastTransportRtpStatistics
& callback
) {
141 DCHECK(audio_sender_
) << "Audio sender uninitialized";
142 audio_sender_
->SubscribeAudioRtpStatsCallback(callback
);
145 void CastTransportSenderImpl::SubscribeVideoRtpStatsCallback(
146 const CastTransportRtpStatistics
& callback
) {
147 DCHECK(video_sender_
) << "Video sender uninitialized";
148 video_sender_
->SubscribeVideoRtpStatsCallback(callback
);
151 void CastTransportSenderImpl::SendRawEvents() {
152 DCHECK(event_subscriber_
.get());
153 DCHECK(!raw_events_callback_
.is_null());
154 std::vector
<PacketEvent
> packet_events
;
155 event_subscriber_
->GetPacketEventsAndReset(&packet_events
);
156 raw_events_callback_
.Run(packet_events
);
159 } // namespace transport