Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / cast_receiver_impl.cc
blobb38cd99667700cfe3fb93cf3b132250c854f0949
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
3 // found in the LICENSE file.
5 #include "media/cast/cast_receiver_impl.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
12 namespace media {
13 namespace cast {
15 // The video and audio receivers should only be called from the main thread.
16 // LocalFrameReciever posts tasks to the main thread, making the cast interface
17 // thread safe.
18 class LocalFrameReceiver : public FrameReceiver {
19 public:
20 LocalFrameReceiver(scoped_refptr<CastEnvironment> cast_environment,
21 AudioReceiver* audio_receiver,
22 VideoReceiver* video_receiver)
23 : cast_environment_(cast_environment),
24 audio_receiver_(audio_receiver),
25 video_receiver_(video_receiver) {}
27 virtual void GetRawVideoFrame(const VideoFrameDecodedCallback& callback)
28 OVERRIDE {
29 cast_environment_->PostTask(CastEnvironment::MAIN,
30 FROM_HERE,
31 base::Bind(&VideoReceiver::GetRawVideoFrame,
32 video_receiver_->AsWeakPtr(),
33 callback));
36 virtual void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback)
37 OVERRIDE {
38 cast_environment_->PostTask(CastEnvironment::MAIN,
39 FROM_HERE,
40 base::Bind(&VideoReceiver::GetEncodedVideoFrame,
41 video_receiver_->AsWeakPtr(),
42 callback));
45 virtual void GetRawAudioFrame(const AudioFrameDecodedCallback& callback)
46 OVERRIDE {
47 cast_environment_->PostTask(CastEnvironment::MAIN,
48 FROM_HERE,
49 base::Bind(&AudioReceiver::GetRawAudioFrame,
50 audio_receiver_->AsWeakPtr(),
51 callback));
54 virtual void GetCodedAudioFrame(const AudioFrameEncodedCallback& callback)
55 OVERRIDE {
56 cast_environment_->PostTask(CastEnvironment::MAIN,
57 FROM_HERE,
58 base::Bind(&AudioReceiver::GetEncodedAudioFrame,
59 audio_receiver_->AsWeakPtr(),
60 callback));
63 protected:
64 virtual ~LocalFrameReceiver() {}
66 private:
67 friend class base::RefCountedThreadSafe<LocalFrameReceiver>;
69 scoped_refptr<CastEnvironment> cast_environment_;
70 AudioReceiver* audio_receiver_;
71 VideoReceiver* video_receiver_;
74 scoped_ptr<CastReceiver> CastReceiver::Create(
75 scoped_refptr<CastEnvironment> cast_environment,
76 const AudioReceiverConfig& audio_config,
77 const VideoReceiverConfig& video_config,
78 transport::PacketSender* const packet_sender) {
79 return scoped_ptr<CastReceiver>(new CastReceiverImpl(
80 cast_environment, audio_config, video_config, packet_sender));
83 CastReceiverImpl::CastReceiverImpl(
84 scoped_refptr<CastEnvironment> cast_environment,
85 const AudioReceiverConfig& audio_config,
86 const VideoReceiverConfig& video_config,
87 transport::PacketSender* const packet_sender)
88 : pacer_(cast_environment->Clock(),
89 cast_environment->Logging(),
90 packet_sender,
91 cast_environment->GetTaskRunner(CastEnvironment::MAIN)),
92 audio_receiver_(cast_environment, audio_config, &pacer_),
93 video_receiver_(cast_environment,
94 video_config,
95 &pacer_),
96 frame_receiver_(new LocalFrameReceiver(cast_environment,
97 &audio_receiver_,
98 &video_receiver_)),
99 cast_environment_(cast_environment),
100 ssrc_of_audio_sender_(audio_config.incoming_ssrc),
101 ssrc_of_video_sender_(video_config.incoming_ssrc) {}
103 CastReceiverImpl::~CastReceiverImpl() {}
105 // The video and audio receivers should only be called from the main thread.
106 void CastReceiverImpl::ReceivedPacket(scoped_ptr<Packet> packet) {
107 const uint8_t* data = &packet->front();
108 size_t length = packet->size();
109 if (length < kMinLengthOfRtcp) {
110 VLOG(1) << "Received a packet which is too short " << length;
111 return;
113 uint32 ssrc_of_sender;
114 if (!Rtcp::IsRtcpPacket(data, length)) {
115 if (length < kMinLengthOfRtp) {
116 VLOG(1) << "Received a RTP packet which is too short " << length;
117 return;
119 ssrc_of_sender = RtpReceiver::GetSsrcOfSender(data, length);
120 } else {
121 ssrc_of_sender = Rtcp::GetSsrcOfSender(data, length);
123 if (ssrc_of_sender == ssrc_of_audio_sender_) {
124 cast_environment_->PostTask(CastEnvironment::MAIN,
125 FROM_HERE,
126 base::Bind(&AudioReceiver::IncomingPacket,
127 audio_receiver_.AsWeakPtr(),
128 base::Passed(&packet)));
129 } else if (ssrc_of_sender == ssrc_of_video_sender_) {
130 cast_environment_->PostTask(CastEnvironment::MAIN,
131 FROM_HERE,
132 base::Bind(&VideoReceiver::IncomingPacket,
133 video_receiver_.AsWeakPtr(),
134 base::Passed(&packet)));
135 } else {
136 VLOG(1) << "Received a packet with a non matching sender SSRC "
137 << ssrc_of_sender;
141 transport::PacketReceiverCallback CastReceiverImpl::packet_receiver() {
142 return base::Bind(&CastReceiverImpl::ReceivedPacket, base::Unretained(this));
145 scoped_refptr<FrameReceiver> CastReceiverImpl::frame_receiver() {
146 return frame_receiver_;
149 } // namespace cast
150 } // namespace media