Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / cast_transport_host_filter.cc
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.
4
5 #include "chrome/browser/media/cast_transport_host_filter.h"
6 #include "media/cast/transport/cast_transport_sender.h"
7
8 namespace cast {
9
10 CastTransportHostFilter::CastTransportHostFilter() {
11 }
12
13 CastTransportHostFilter::~CastTransportHostFilter() {
14 }
15
16 bool CastTransportHostFilter::OnMessageReceived(const IPC::Message& message,
17                                                 bool* message_was_ok) {
18   bool handled = true;
19   IPC_BEGIN_MESSAGE_MAP_EX(CastTransportHostFilter, message, *message_was_ok)
20     IPC_MESSAGE_HANDLER(CastHostMsg_New, OnNew)
21     IPC_MESSAGE_HANDLER(CastHostMsg_Delete, OnDelete)
22     IPC_MESSAGE_HANDLER(CastHostMsg_InsertCodedAudioFrame,
23                         OnInsertCodedAudioFrame)
24     IPC_MESSAGE_HANDLER(CastHostMsg_InsertCodedVideoFrame,
25                         OnInsertCodedVideoFrame)
26     IPC_MESSAGE_HANDLER(CastHostMsg_SendRtcpFromRtpSender,
27                         OnSendRtcpFromRtpSender);
28     IPC_MESSAGE_HANDLER(CastHostMsg_ResendPackets,
29                         OnResendPackets)
30     IPC_MESSAGE_UNHANDLED(handled = false);
31   IPC_END_MESSAGE_MAP_EX();
32   return handled;
33 }
34
35 void CastTransportHostFilter::NotifyStatusChange(
36     int32 channel_id,
37     media::cast::transport::CastTransportStatus status) {
38   Send(new CastMsg_NotifyStatusChange(channel_id, status));
39 }
40
41 void CastTransportHostFilter::ReceivedPacket(
42     int32 channel_id,
43     scoped_ptr<media::cast::transport::Packet> packet) {
44   Send(new CastMsg_ReceivedPacket(channel_id, *packet));
45 }
46
47 void CastTransportHostFilter::ReceivedRtpStatistics(
48     int32 channel_id,
49     bool audio,
50     const media::cast::transport::RtcpSenderInfo& sender_info,
51     base::TimeTicks time_sent,
52     uint32 rtp_timestamp) {
53   Send(new CastMsg_RtpStatistics(channel_id,
54                                  audio,
55                                  sender_info,
56                                  time_sent,
57                                  rtp_timestamp));
58 }
59
60 void CastTransportHostFilter::OnNew(
61     int32 channel_id,
62     const media::cast::transport::CastTransportConfig& config) {
63   media::cast::transport::CastTransportSender* sender =
64       id_map_.Lookup(channel_id);
65   if (sender) {
66     id_map_.Remove(channel_id);
67   }
68
69   sender = media::cast::transport::CastTransportSender::
70       CreateCastTransportSender(
71           &clock_,
72           config,
73           base::Bind(&CastTransportHostFilter::NotifyStatusChange,
74                      base::Unretained(this),
75                      channel_id),
76           base::MessageLoopProxy::current());
77   sender->SetPacketReceiver(
78           base::Bind(&CastTransportHostFilter::ReceivedPacket,
79                      base::Unretained(this),
80                      channel_id));
81   sender->SubscribeAudioRtpStatsCallback(
82           base::Bind(&CastTransportHostFilter::ReceivedRtpStatistics,
83                      base::Unretained(this),
84                      channel_id,
85                      true  /* audio */));
86   sender->SubscribeVideoRtpStatsCallback(
87           base::Bind(&CastTransportHostFilter::ReceivedRtpStatistics,
88                      base::Unretained(this),
89                      channel_id,
90                      false /* not audio */));
91
92   id_map_.AddWithID(sender, channel_id);
93 }
94
95 void CastTransportHostFilter::OnDelete(int32 channel_id) {
96   media::cast::transport::CastTransportSender* sender =
97       id_map_.Lookup(channel_id);
98   if (sender) {
99     id_map_.Remove(channel_id);
100   } else {
101     DVLOG(1) << "CastTransportHostFilter::Delete called "
102              << "on non-existing channel";
103   }
104 }
105
106 void CastTransportHostFilter::OnInsertCodedAudioFrame(
107     int32 channel_id,
108     const media::cast::transport::EncodedAudioFrame& audio_frame,
109     base::TimeTicks recorded_time) {
110   media::cast::transport::CastTransportSender* sender =
111       id_map_.Lookup(channel_id);
112   if (sender) {
113     sender->InsertCodedAudioFrame(&audio_frame, recorded_time);
114   } else {
115     DVLOG(1)
116         << "CastTransportHostFilter::OnInsertCodedAudioFrame "
117         << "on non-existing channel";
118   }
119 }
120
121 void CastTransportHostFilter::OnInsertCodedVideoFrame(
122     int32 channel_id,
123     const media::cast::transport::EncodedVideoFrame& video_frame,
124     base::TimeTicks capture_time) {
125   media::cast::transport::CastTransportSender* sender =
126       id_map_.Lookup(channel_id);
127   if (sender) {
128     sender->InsertCodedVideoFrame(&video_frame, capture_time);
129   } else {
130     DVLOG(1)
131         << "CastTransportHostFilter::OnInsertCodedVideoFrame "
132         << "on non-existing channel";
133   }
134 }
135
136 void CastTransportHostFilter::OnSendRtcpFromRtpSender(
137     int32 channel_id,
138     const media::cast::transport::SendRtcpFromRtpSenderData& data,
139     const media::cast::transport::RtcpSenderInfo& sender_info,
140     const media::cast::transport::RtcpDlrrReportBlock& dlrr,
141     const media::cast::transport::RtcpSenderLogMessage& sender_log) {
142   media::cast::transport::CastTransportSender* sender =
143       id_map_.Lookup(channel_id);
144   if (sender) {
145     sender->SendRtcpFromRtpSender(data.packet_type_flags,
146                                   sender_info,
147                                   dlrr,
148                                   sender_log,
149                                   data.sending_ssrc,
150                                   data.c_name);
151   } else {
152     DVLOG(1)
153         << "CastTransportHostFilter::OnSendRtcpFromRtpSender "
154         << "on non-existing channel";
155   }
156 }
157
158 void CastTransportHostFilter::OnResendPackets(
159     int32 channel_id,
160     bool is_audio,
161     const media::cast::MissingFramesAndPacketsMap& missing_packets) {
162   media::cast::transport::CastTransportSender* sender =
163       id_map_.Lookup(channel_id);
164   if (sender) {
165     sender->ResendPackets(is_audio, missing_packets);
166   } else {
167     DVLOG(1)
168         << "CastTransportHostFilter::OnResendPackets on non-existing channel";
169   }
170 }
171
172 }  // namespace cast