| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "media/cast/cast_receiver_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 | |
| 12 namespace media { | |
| 13 namespace cast { | |
| 14 | |
| 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) {} | |
| 26 | |
| 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)); | |
| 34 } | |
| 35 | |
| 36 virtual void GetEncodedVideoFrame(const FrameEncodedCallback& callback) | |
| 37 OVERRIDE { | |
| 38 cast_environment_->PostTask(CastEnvironment::MAIN, | |
| 39 FROM_HERE, | |
| 40 base::Bind(&VideoReceiver::GetEncodedVideoFrame, | |
| 41 video_receiver_->AsWeakPtr(), | |
| 42 callback)); | |
| 43 } | |
| 44 | |
| 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)); | |
| 52 } | |
| 53 | |
| 54 virtual void GetCodedAudioFrame(const FrameEncodedCallback& callback) | |
| 55 OVERRIDE { | |
| 56 cast_environment_->PostTask(CastEnvironment::MAIN, | |
| 57 FROM_HERE, | |
| 58 base::Bind(&AudioReceiver::GetEncodedAudioFrame, | |
| 59 audio_receiver_->AsWeakPtr(), | |
| 60 callback)); | |
| 61 } | |
| 62 | |
| 63 protected: | |
| 64 virtual ~LocalFrameReceiver() {} | |
| 65 | |
| 66 private: | |
| 67 friend class base::RefCountedThreadSafe<LocalFrameReceiver>; | |
| 68 | |
| 69 scoped_refptr<CastEnvironment> cast_environment_; | |
| 70 AudioReceiver* audio_receiver_; | |
| 71 VideoReceiver* video_receiver_; | |
| 72 }; | |
| 73 | |
| 74 scoped_ptr<CastReceiver> CastReceiver::Create( | |
| 75 scoped_refptr<CastEnvironment> cast_environment, | |
| 76 const FrameReceiverConfig& audio_config, | |
| 77 const FrameReceiverConfig& video_config, | |
| 78 transport::PacketSender* const packet_sender) { | |
| 79 return scoped_ptr<CastReceiver>(new CastReceiverImpl( | |
| 80 cast_environment, audio_config, video_config, packet_sender)); | |
| 81 } | |
| 82 | |
| 83 CastReceiverImpl::CastReceiverImpl( | |
| 84 scoped_refptr<CastEnvironment> cast_environment, | |
| 85 const FrameReceiverConfig& audio_config, | |
| 86 const FrameReceiverConfig& 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) {} | |
| 102 | |
| 103 CastReceiverImpl::~CastReceiverImpl() {} | |
| 104 | |
| 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; | |
| 112 } | |
| 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; | |
| 118 } | |
| 119 ssrc_of_sender = RtpReceiver::GetSsrcOfSender(data, length); | |
| 120 } else { | |
| 121 ssrc_of_sender = Rtcp::GetSsrcOfSender(data, length); | |
| 122 } | |
| 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; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 transport::PacketReceiverCallback CastReceiverImpl::packet_receiver() { | |
| 142 return base::Bind(&CastReceiverImpl::ReceivedPacket, base::Unretained(this)); | |
| 143 } | |
| 144 | |
| 145 scoped_refptr<FrameReceiver> CastReceiverImpl::frame_receiver() { | |
| 146 return frame_receiver_; | |
| 147 } | |
| 148 | |
| 149 } // namespace cast | |
| 150 } // namespace media | |
| OLD | NEW |