| 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 "chrome/renderer/media/cast_session_delegate.h" | 5 #include "chrome/renderer/media/cast_session_delegate.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
| 9 #include "chrome/renderer/media/cast_transport_sender_ipc.h" |
| 9 #include "content/public/renderer/p2p_socket_client.h" | 10 #include "content/public/renderer/p2p_socket_client.h" |
| 10 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
| 11 #include "media/cast/cast_config.h" | 12 #include "media/cast/cast_config.h" |
| 12 #include "media/cast/cast_environment.h" | 13 #include "media/cast/cast_environment.h" |
| 13 #include "media/cast/cast_sender.h" | 14 #include "media/cast/cast_sender.h" |
| 14 #include "media/cast/logging/logging_defines.h" | 15 #include "media/cast/logging/logging_defines.h" |
| 15 #include "media/cast/transport/cast_transport_config.h" | 16 #include "media/cast/transport/cast_transport_config.h" |
| 16 #include "media/cast/transport/cast_transport_sender.h" | 17 #include "media/cast/transport/cast_transport_sender.h" |
| 17 | 18 |
| 18 using media::cast::AudioSenderConfig; | 19 using media::cast::AudioSenderConfig; |
| 19 using media::cast::CastEnvironment; | 20 using media::cast::CastEnvironment; |
| 20 using media::cast::CastSender; | 21 using media::cast::CastSender; |
| 21 using media::cast::VideoSenderConfig; | 22 using media::cast::VideoSenderConfig; |
| 22 | 23 |
| 23 namespace { | |
| 24 | |
| 25 // This is a dummy class that does nothing. This is needed temporarily | |
| 26 // to enable tests for cast.streaming extension APIs. | |
| 27 // The real implementation of CastTransportSender is to use IPC to send | |
| 28 // data to the browser process. | |
| 29 // See crbug.com/327482 for more details. | |
| 30 class DummyTransport : public media::cast::transport::CastTransportSender { | |
| 31 public: | |
| 32 DummyTransport() {} | |
| 33 virtual ~DummyTransport() {} | |
| 34 | |
| 35 // CastTransportSender implementations. | |
| 36 virtual void SetPacketReceiver( | |
| 37 const media::cast::transport::PacketReceiverCallback& packet_receiver) | |
| 38 OVERRIDE {} | |
| 39 virtual void InsertCodedAudioFrame( | |
| 40 const media::cast::transport::EncodedAudioFrame* audio_frame, | |
| 41 const base::TimeTicks& recorded_time) OVERRIDE {} | |
| 42 virtual void InsertCodedVideoFrame( | |
| 43 const media::cast::transport::EncodedVideoFrame* video_frame, | |
| 44 const base::TimeTicks& capture_time) OVERRIDE {} | |
| 45 virtual void SendRtcpFromRtpSender( | |
| 46 uint32 packet_type_flags, | |
| 47 const media::cast::transport::RtcpSenderInfo& sender_info, | |
| 48 const media::cast::transport::RtcpDlrrReportBlock& dlrr, | |
| 49 const media::cast::transport::RtcpSenderLogMessage& sender_log, | |
| 50 uint32 sending_ssrc, | |
| 51 const std::string& c_name) OVERRIDE {} | |
| 52 virtual void ResendPackets( | |
| 53 bool is_audio, | |
| 54 const media::cast::transport::MissingFramesAndPacketsMap& missing_packets) | |
| 55 OVERRIDE {} | |
| 56 virtual void SubscribeAudioRtpStatsCallback( | |
| 57 const media::cast::transport::CastTransportRtpStatistics& callback) | |
| 58 OVERRIDE {} | |
| 59 virtual void SubscribeVideoRtpStatsCallback( | |
| 60 const media::cast::transport::CastTransportRtpStatistics& callback) | |
| 61 OVERRIDE {} | |
| 62 | |
| 63 private: | |
| 64 DISALLOW_COPY_AND_ASSIGN(DummyTransport); | |
| 65 }; | |
| 66 } // namespace | |
| 67 | |
| 68 CastSessionDelegate::CastSessionDelegate() | 24 CastSessionDelegate::CastSessionDelegate() |
| 69 : audio_encode_thread_("CastAudioEncodeThread"), | 25 : audio_encode_thread_("CastAudioEncodeThread"), |
| 70 video_encode_thread_("CastVideoEncodeThread"), | 26 video_encode_thread_("CastVideoEncodeThread"), |
| 27 transport_configured_(false), |
| 71 io_message_loop_proxy_( | 28 io_message_loop_proxy_( |
| 72 content::RenderThread::Get()->GetIOMessageLoopProxy()) { | 29 content::RenderThread::Get()->GetIOMessageLoopProxy()) { |
| 30 DCHECK(io_message_loop_proxy_); |
| 73 } | 31 } |
| 74 | 32 |
| 75 CastSessionDelegate::~CastSessionDelegate() { | 33 CastSessionDelegate::~CastSessionDelegate() { |
| 76 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 34 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 77 } | 35 } |
| 78 | 36 |
| 79 void CastSessionDelegate::Initialize() { | 37 void CastSessionDelegate::Initialize() { |
| 80 if (cast_environment_) | 38 if (cast_environment_) |
| 81 return; // Already initialized. | 39 return; // Already initialized. |
| 82 | 40 |
| 83 cast_transport_.reset(new DummyTransport()); | |
| 84 audio_encode_thread_.Start(); | 41 audio_encode_thread_.Start(); |
| 85 video_encode_thread_.Start(); | 42 video_encode_thread_.Start(); |
| 86 | 43 |
| 87 // CastSender uses the renderer's IO thread as the main thread. This reduces | 44 // CastSender uses the renderer's IO thread as the main thread. This reduces |
| 88 // thread hopping for incoming video frames and outgoing network packets. | 45 // thread hopping for incoming video frames and outgoing network packets. |
| 89 // There's no need to decode so no thread assigned for decoding. | 46 // There's no need to decode so no thread assigned for decoding. |
| 90 // Get default logging: All disabled. | 47 // Get default logging: All disabled. |
| 91 cast_environment_ = new CastEnvironment( | 48 cast_environment_ = new CastEnvironment( |
| 92 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), | 49 scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(), |
| 93 base::MessageLoopProxy::current(), | 50 base::MessageLoopProxy::current(), |
| 94 audio_encode_thread_.message_loop_proxy(), | 51 audio_encode_thread_.message_loop_proxy(), |
| 95 NULL, | 52 NULL, |
| 96 video_encode_thread_.message_loop_proxy(), | 53 video_encode_thread_.message_loop_proxy(), |
| 97 NULL, | 54 NULL, |
| 98 base::MessageLoopProxy::current(), | 55 base::MessageLoopProxy::current(), |
| 99 media::cast::GetDefaultCastSenderLoggingConfig()); | 56 media::cast::GetDefaultCastSenderLoggingConfig()); |
| 100 } | 57 } |
| 101 | 58 |
| 102 void CastSessionDelegate::StartAudio( | 59 void CastSessionDelegate::StartAudio( |
| 103 const AudioSenderConfig& config, | 60 const AudioSenderConfig& config, |
| 104 const FrameInputAvailableCallback& callback) { | 61 const FrameInputAvailableCallback& callback) { |
| 105 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 62 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 106 | 63 |
| 107 audio_config_.reset(new AudioSenderConfig(config)); | 64 audio_config_.reset(new AudioSenderConfig(config)); |
| 108 StartSendingInternal(callback, true); | 65 video_frame_input_available_callback_ = callback; |
| 66 StartSendingInternal(); |
| 109 } | 67 } |
| 110 | 68 |
| 111 void CastSessionDelegate::StartVideo( | 69 void CastSessionDelegate::StartVideo( |
| 112 const VideoSenderConfig& config, | 70 const VideoSenderConfig& config, |
| 113 const FrameInputAvailableCallback& callback) { | 71 const FrameInputAvailableCallback& callback) { |
| 114 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 72 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 73 audio_frame_input_available_callback_ = callback; |
| 115 | 74 |
| 116 video_config_.reset(new VideoSenderConfig(config)); | 75 video_config_.reset(new VideoSenderConfig(config)); |
| 117 StartSendingInternal(callback, false); | 76 StartSendingInternal(); |
| 118 } | 77 } |
| 119 | 78 |
| 120 void CastSessionDelegate::StartSendingInternal( | 79 void CastSessionDelegate::StartUDP( |
| 121 const FrameInputAvailableCallback& callback, | 80 const net::IPEndPoint& local_endpoint, |
| 122 bool is_audio) { | 81 const net::IPEndPoint& remote_endpoint) { |
| 123 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); | 82 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 83 transport_configured_ = true; |
| 84 local_endpoint_ = local_endpoint; |
| 85 remote_endpoint_ = remote_endpoint; |
| 86 StartSendingInternal(); |
| 87 } |
| 88 |
| 89 void CastSessionDelegate::StatusNotificationCB( |
| 90 media::cast::transport::CastTransportStatus unused_status) { |
| 91 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 92 // TODO(hubbe): Call javascript UDPTransport error function. |
| 93 } |
| 94 |
| 95 void CastSessionDelegate::StartSendingInternal() { |
| 96 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 97 |
| 98 // No transport, wait. |
| 99 if (!transport_configured_) |
| 100 return; |
| 101 |
| 102 // No audio or video, wait. |
| 103 if (!audio_config_ && !video_config_) |
| 104 return; |
| 124 | 105 |
| 125 Initialize(); | 106 Initialize(); |
| 126 | 107 |
| 127 if (is_audio) { | 108 media::cast::transport::CastTransportConfig config; |
| 128 audio_frame_input_available_callback_.reset( | 109 |
| 129 new FrameInputAvailableCallback(callback)); | 110 // TODO(hubbe): set config.aes_key and config.aes_iv_mask. |
| 130 } else { | 111 config.local_endpoint = local_endpoint_; |
| 131 video_frame_input_available_callback_.reset( | 112 config.receiver_endpoint = remote_endpoint_; |
| 132 new FrameInputAvailableCallback(callback)); | 113 if (audio_config_) { |
| 114 config.audio_ssrc = audio_config_->sender_ssrc; |
| 115 config.audio_codec = audio_config_->codec; |
| 116 config.audio_rtp_config = audio_config_->rtp_config; |
| 117 config.audio_frequency = audio_config_->frequency; |
| 118 config.audio_channels = audio_config_->channels; |
| 133 } | 119 } |
| 120 if (video_config_) { |
| 121 config.video_ssrc = video_config_->sender_ssrc; |
| 122 config.video_codec = video_config_->codec; |
| 123 config.video_rtp_config = video_config_->rtp_config; |
| 124 } |
| 125 |
| 126 cast_transport_.reset(new CastTransportSenderIPC( |
| 127 config, |
| 128 base::Bind(&CastSessionDelegate::StatusNotificationCB, |
| 129 base::Unretained(this)))); |
| 134 | 130 |
| 135 cast_sender_.reset(CastSender::CreateCastSender( | 131 cast_sender_.reset(CastSender::CreateCastSender( |
| 136 cast_environment_, | 132 cast_environment_, |
| 137 audio_config_.get(), | 133 audio_config_.get(), |
| 138 video_config_.get(), | 134 video_config_.get(), |
| 139 NULL, // GPU. | 135 NULL, // GPU. |
| 140 base::Bind(&CastSessionDelegate::InitializationResult, | 136 base::Bind(&CastSessionDelegate::InitializationResult, |
| 141 base::Unretained(this)), | 137 base::Unretained(this)), |
| 142 cast_transport_.get())); | 138 cast_transport_.get())); |
| 139 cast_transport_->SetPacketReceiver(cast_sender_->packet_receiver()); |
| 143 } | 140 } |
| 144 | 141 |
| 145 void CastSessionDelegate::InitializationResult( | 142 void CastSessionDelegate::InitializationResult( |
| 146 media::cast::CastInitializationStatus result) const { | 143 media::cast::CastInitializationStatus result) const { |
| 147 DCHECK(cast_sender_); | 144 DCHECK(cast_sender_); |
| 148 | 145 |
| 149 // TODO(pwestin): handle the error codes. | 146 // TODO(pwestin): handle the error codes. |
| 150 if (result == media::cast::STATUS_INITIALIZED) { | 147 if (result == media::cast::STATUS_INITIALIZED) { |
| 151 if (audio_frame_input_available_callback_) { | 148 if (!audio_frame_input_available_callback_.is_null()) { |
| 152 audio_frame_input_available_callback_->Run(cast_sender_->frame_input()); | 149 audio_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
| 153 } | 150 } |
| 154 if (video_frame_input_available_callback_) { | 151 if (!video_frame_input_available_callback_.is_null()) { |
| 155 video_frame_input_available_callback_->Run(cast_sender_->frame_input()); | 152 video_frame_input_available_callback_.Run(cast_sender_->frame_input()); |
| 156 } | 153 } |
| 157 } | 154 } |
| 158 } | 155 } |
| OLD | NEW |