Chromium Code Reviews| 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/cast_receiver_impl.h" | 5 #include "media/cast/receiver/cast_receiver_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | |
| 8 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/debug/trace_event.h" | |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "media/cast/receiver/audio_decoder.h" | |
| 14 #include "media/cast/receiver/video_decoder.h" | |
| 11 | 15 |
| 12 namespace media { | 16 namespace media { |
| 13 namespace cast { | 17 namespace cast { |
| 14 | 18 |
| 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( | 19 scoped_ptr<CastReceiver> CastReceiver::Create( |
| 75 scoped_refptr<CastEnvironment> cast_environment, | 20 scoped_refptr<CastEnvironment> cast_environment, |
| 76 const FrameReceiverConfig& audio_config, | 21 const FrameReceiverConfig& audio_config, |
| 77 const FrameReceiverConfig& video_config, | 22 const FrameReceiverConfig& video_config, |
| 78 transport::PacketSender* const packet_sender) { | 23 transport::PacketSender* const packet_sender) { |
| 79 return scoped_ptr<CastReceiver>(new CastReceiverImpl( | 24 return scoped_ptr<CastReceiver>(new CastReceiverImpl( |
| 80 cast_environment, audio_config, video_config, packet_sender)); | 25 cast_environment, audio_config, video_config, packet_sender)); |
| 81 } | 26 } |
| 82 | 27 |
| 83 CastReceiverImpl::CastReceiverImpl( | 28 CastReceiverImpl::CastReceiverImpl( |
| 84 scoped_refptr<CastEnvironment> cast_environment, | 29 scoped_refptr<CastEnvironment> cast_environment, |
| 85 const FrameReceiverConfig& audio_config, | 30 const FrameReceiverConfig& audio_config, |
| 86 const FrameReceiverConfig& video_config, | 31 const FrameReceiverConfig& video_config, |
| 87 transport::PacketSender* const packet_sender) | 32 transport::PacketSender* const packet_sender) |
| 88 : pacer_(cast_environment->Clock(), | 33 : cast_environment_(cast_environment), |
| 34 pacer_(cast_environment->Clock(), | |
| 89 cast_environment->Logging(), | 35 cast_environment->Logging(), |
| 90 packet_sender, | 36 packet_sender, |
| 91 cast_environment->GetTaskRunner(CastEnvironment::MAIN)), | 37 cast_environment->GetTaskRunner(CastEnvironment::MAIN)), |
| 92 audio_receiver_(cast_environment, audio_config, &pacer_), | 38 audio_receiver_(cast_environment, audio_config, AUDIO_EVENT, &pacer_), |
| 93 video_receiver_(cast_environment, | 39 video_receiver_(cast_environment, video_config, VIDEO_EVENT, &pacer_), |
| 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), | 40 ssrc_of_audio_sender_(audio_config.incoming_ssrc), |
| 101 ssrc_of_video_sender_(video_config.incoming_ssrc) {} | 41 ssrc_of_video_sender_(video_config.incoming_ssrc), |
| 42 num_audio_channels_(audio_config.channels), | |
| 43 audio_sampling_rate_(audio_config.frequency), | |
| 44 audio_codec_(audio_config.codec.audio), | |
| 45 video_codec_(video_config.codec.video) {} | |
| 102 | 46 |
| 103 CastReceiverImpl::~CastReceiverImpl() {} | 47 CastReceiverImpl::~CastReceiverImpl() {} |
| 104 | 48 |
| 105 // The video and audio receivers should only be called from the main thread. | 49 void CastReceiverImpl::DispatchReceivedPacket(scoped_ptr<Packet> packet) { |
| 106 void CastReceiverImpl::ReceivedPacket(scoped_ptr<Packet> packet) { | 50 const uint8_t* const data = &packet->front(); |
| 107 const uint8_t* data = &packet->front(); | 51 const size_t length = packet->size(); |
| 108 size_t length = packet->size(); | 52 |
| 109 if (length < kMinLengthOfRtcp) { | 53 uint32 ssrc_of_sender; |
| 110 VLOG(1) << "Received a packet which is too short " << length; | 54 if (Rtcp::IsRtcpPacket(data, length)) { |
| 55 ssrc_of_sender = Rtcp::GetSsrcOfSender(data, length); | |
| 56 } else if (!FrameReceiver::ParseSenderSsrc(data, length, &ssrc_of_sender)) { | |
| 57 VLOG(1) << "Invalid RTP packet."; | |
| 111 return; | 58 return; |
| 112 } | 59 } |
| 113 uint32 ssrc_of_sender; | 60 |
| 114 if (!Rtcp::IsRtcpPacket(data, length)) { | 61 base::WeakPtr<FrameReceiver> target; |
| 115 if (length < kMinLengthOfRtp) { | 62 if (ssrc_of_sender == ssrc_of_video_sender_) { |
| 116 VLOG(1) << "Received a RTP packet which is too short " << length; | 63 target = video_receiver_.AsWeakPtr(); |
| 117 return; | 64 } else if (ssrc_of_sender == ssrc_of_audio_sender_) { |
| 118 } | 65 target = audio_receiver_.AsWeakPtr(); |
| 119 ssrc_of_sender = RtpReceiver::GetSsrcOfSender(data, length); | |
| 120 } else { | 66 } else { |
| 121 ssrc_of_sender = Rtcp::GetSsrcOfSender(data, length); | 67 VLOG(1) << "Dropping packet with a non matching sender SSRC: " |
| 68 << ssrc_of_sender; | |
| 69 return; | |
| 122 } | 70 } |
| 123 if (ssrc_of_sender == ssrc_of_audio_sender_) { | 71 cast_environment_->PostTask( |
| 124 cast_environment_->PostTask(CastEnvironment::MAIN, | 72 CastEnvironment::MAIN, |
| 125 FROM_HERE, | 73 FROM_HERE, |
| 126 base::Bind(&AudioReceiver::IncomingPacket, | 74 base::Bind(base::IgnoreResult(&FrameReceiver::ProcessPacket), |
| 127 audio_receiver_.AsWeakPtr(), | 75 target, |
| 128 base::Passed(&packet))); | 76 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 } | 77 } |
| 140 | 78 |
| 141 transport::PacketReceiverCallback CastReceiverImpl::packet_receiver() { | 79 transport::PacketReceiverCallback CastReceiverImpl::packet_receiver() { |
| 142 return base::Bind(&CastReceiverImpl::ReceivedPacket, base::Unretained(this)); | 80 return base::Bind(&CastReceiverImpl::DispatchReceivedPacket, |
|
Alpha Left Google
2014/06/02 20:16:59
I think we need a TODO here.
miu
2014/06/02 22:36:08
Done.
| |
| 81 base::Unretained(this)); | |
| 143 } | 82 } |
| 144 | 83 |
| 145 scoped_refptr<FrameReceiver> CastReceiverImpl::frame_receiver() { | 84 void CastReceiverImpl::RequestDecodedAudioFrame( |
| 146 return frame_receiver_; | 85 const AudioFrameDecodedCallback& callback) { |
| 86 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 87 DCHECK(!callback.is_null()); | |
| 88 audio_receiver_.RequestEncodedFrame(base::Bind( | |
| 89 &CastReceiverImpl::DecodeEncodedAudioFrame, | |
| 90 // Note: Use of Unretained is safe since this Closure is guaranteed to be | |
| 91 // invoked or discarded before destruction of |this|. | |
| 92 base::Unretained(this), | |
| 93 callback)); | |
| 94 } | |
| 95 | |
| 96 void CastReceiverImpl::RequestEncodedAudioFrame( | |
| 97 const FrameEncodedCallback& callback) { | |
| 98 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 99 audio_receiver_.RequestEncodedFrame(callback); | |
| 100 } | |
| 101 | |
| 102 void CastReceiverImpl::RequestDecodedVideoFrame( | |
| 103 const VideoFrameDecodedCallback& callback) { | |
| 104 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 105 DCHECK(!callback.is_null()); | |
| 106 video_receiver_.RequestEncodedFrame(base::Bind( | |
| 107 &CastReceiverImpl::DecodeEncodedVideoFrame, | |
| 108 // Note: Use of Unretained is safe since this Closure is guaranteed to be | |
| 109 // invoked or discarded before destruction of |this|. | |
| 110 base::Unretained(this), | |
| 111 callback)); | |
| 112 } | |
| 113 | |
| 114 void CastReceiverImpl::RequestEncodedVideoFrame( | |
| 115 const FrameEncodedCallback& callback) { | |
| 116 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 117 video_receiver_.RequestEncodedFrame(callback); | |
| 118 } | |
| 119 | |
| 120 void CastReceiverImpl::DecodeEncodedAudioFrame( | |
| 121 const AudioFrameDecodedCallback& callback, | |
| 122 scoped_ptr<transport::EncodedFrame> encoded_frame) { | |
| 123 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 124 if (!encoded_frame) { | |
| 125 callback.Run(make_scoped_ptr<AudioBus>(NULL), base::TimeTicks(), false); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 if (!audio_decoder_) { | |
| 130 audio_decoder_.reset(new AudioDecoder(cast_environment_, | |
| 131 num_audio_channels_, | |
| 132 audio_sampling_rate_, | |
| 133 audio_codec_)); | |
| 134 } | |
| 135 const uint32 frame_id = encoded_frame->frame_id; | |
| 136 const uint32 rtp_timestamp = encoded_frame->rtp_timestamp; | |
| 137 const base::TimeTicks playout_time = encoded_frame->reference_time; | |
| 138 audio_decoder_->DecodeFrame( | |
| 139 encoded_frame.Pass(), | |
| 140 base::Bind(&CastReceiverImpl::EmitDecodedAudioFrame, | |
| 141 cast_environment_, | |
| 142 callback, | |
| 143 frame_id, | |
| 144 rtp_timestamp, | |
| 145 playout_time)); | |
| 146 } | |
| 147 | |
| 148 void CastReceiverImpl::DecodeEncodedVideoFrame( | |
| 149 const VideoFrameDecodedCallback& callback, | |
| 150 scoped_ptr<transport::EncodedFrame> encoded_frame) { | |
| 151 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
| 152 if (!encoded_frame) { | |
| 153 callback.Run( | |
| 154 make_scoped_refptr<VideoFrame>(NULL), base::TimeTicks(), false); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc | |
| 159 TRACE_EVENT_INSTANT2( | |
| 160 "cast_perf_test", "PullEncodedVideoFrame", | |
| 161 TRACE_EVENT_SCOPE_THREAD, | |
| 162 "rtp_timestamp", encoded_frame->rtp_timestamp, | |
| 163 "render_time", encoded_frame->reference_time.ToInternalValue()); | |
| 164 | |
| 165 if (!video_decoder_) | |
| 166 video_decoder_.reset(new VideoDecoder(cast_environment_, video_codec_)); | |
| 167 const uint32 frame_id = encoded_frame->frame_id; | |
| 168 const uint32 rtp_timestamp = encoded_frame->rtp_timestamp; | |
| 169 const base::TimeTicks playout_time = encoded_frame->reference_time; | |
| 170 video_decoder_->DecodeFrame( | |
| 171 encoded_frame.Pass(), | |
| 172 base::Bind(&CastReceiverImpl::EmitDecodedVideoFrame, | |
| 173 cast_environment_, | |
| 174 callback, | |
| 175 frame_id, | |
| 176 rtp_timestamp, | |
| 177 playout_time)); | |
| 178 } | |
| 179 | |
| 180 // static | |
| 181 void CastReceiverImpl::EmitDecodedAudioFrame( | |
| 182 const scoped_refptr<CastEnvironment>& cast_environment, | |
| 183 const AudioFrameDecodedCallback& callback, | |
| 184 uint32 frame_id, | |
| 185 uint32 rtp_timestamp, | |
| 186 const base::TimeTicks& playout_time, | |
| 187 scoped_ptr<AudioBus> audio_bus, | |
| 188 bool is_continuous) { | |
| 189 DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN)); | |
| 190 if (audio_bus.get()) { | |
| 191 const base::TimeTicks now = cast_environment->Clock()->NowTicks(); | |
| 192 cast_environment->Logging()->InsertFrameEvent( | |
| 193 now, FRAME_DECODED, AUDIO_EVENT, rtp_timestamp, frame_id); | |
| 194 cast_environment->Logging()->InsertFrameEventWithDelay( | |
| 195 now, FRAME_PLAYOUT, AUDIO_EVENT, rtp_timestamp, frame_id, | |
| 196 playout_time - now); | |
| 197 } | |
| 198 callback.Run(audio_bus.Pass(), playout_time, is_continuous); | |
| 199 } | |
| 200 | |
| 201 // static | |
| 202 void CastReceiverImpl::EmitDecodedVideoFrame( | |
| 203 const scoped_refptr<CastEnvironment>& cast_environment, | |
| 204 const VideoFrameDecodedCallback& callback, | |
| 205 uint32 frame_id, | |
| 206 uint32 rtp_timestamp, | |
| 207 const base::TimeTicks& playout_time, | |
| 208 const scoped_refptr<VideoFrame>& video_frame, | |
| 209 bool is_continuous) { | |
| 210 DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN)); | |
| 211 if (video_frame) { | |
| 212 const base::TimeTicks now = cast_environment->Clock()->NowTicks(); | |
| 213 cast_environment->Logging()->InsertFrameEvent( | |
| 214 now, FRAME_DECODED, VIDEO_EVENT, rtp_timestamp, frame_id); | |
| 215 cast_environment->Logging()->InsertFrameEventWithDelay( | |
| 216 now, FRAME_PLAYOUT, VIDEO_EVENT, rtp_timestamp, frame_id, | |
| 217 playout_time - now); | |
| 218 | |
| 219 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc | |
| 220 TRACE_EVENT_INSTANT1( | |
| 221 "cast_perf_test", "FrameDecoded", | |
| 222 TRACE_EVENT_SCOPE_THREAD, | |
| 223 "rtp_timestamp", rtp_timestamp); | |
| 224 } | |
| 225 callback.Run(video_frame, playout_time, is_continuous); | |
| 147 } | 226 } |
| 148 | 227 |
| 149 } // namespace cast | 228 } // namespace cast |
| 150 } // namespace media | 229 } // namespace media |
| OLD | NEW |