Index: chrome/renderer/media/cast_receiver_session.cc |
diff --git a/chrome/renderer/media/cast_receiver_session.cc b/chrome/renderer/media/cast_receiver_session.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0e1075c502dc5cdbe6807ebf6a761d8131240be0 |
--- /dev/null |
+++ b/chrome/renderer/media/cast_receiver_session.cc |
@@ -0,0 +1,209 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/renderer/media/cast_receiver_session.h" |
+ |
+#include "base/synchronization/waitable_event.h" |
+#include "chrome/renderer/media/cast_receiver_audio_valve.h" |
+#include "content/public/renderer/media_stream_source_api.h" |
+#include "content/public/renderer/render_thread.h" |
+#include "media/base/audio_capturer_source.h" |
+#include "media/base/video_capturer_source.h" |
+#include "third_party/WebKit/public/platform/WebMediaStream.h" |
+#include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
+#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
+ |
+// This is a render thread object. |
+class CastStreamingAudioCapturerSource : public media::AudioCapturerSource { |
+ public: |
+ CastStreamingAudioCapturerSource( |
+ scoped_refptr<CastReceiverSession> cast_receiver_session); |
+ void Initialize(const media::AudioParameters& params, |
+ CaptureCallback* callback, |
+ int session_id) override; |
+ void Start() override; |
+ void Stop() override; |
+ void SetVolume(double volume) override; |
+ void SetAutomaticGainControl(bool enable) override; |
+ private: |
+ ~CastStreamingAudioCapturerSource() override; |
+ scoped_refptr<CastReceiverSession> cast_receiver_session_; |
miu
2015/02/11 02:52:49
nit: const scoped_refptr<...>
hubbe
2015/02/11 22:38:17
Done.
|
+ scoped_refptr<CastReceiverAudioValve> audio_valve_; |
+}; |
+ |
+// This is a render thread object. |
+class CastStreamingVideoCapturerSource : public media::VideoCapturerSource { |
+ public: |
+ explicit CastStreamingVideoCapturerSource( |
+ scoped_refptr<CastReceiverSession> cast_receiver_session); |
+ protected: |
+ void GetCurrentSupportedFormats( |
+ int max_requested_width, |
+ int max_requested_height, |
+ double max_requested_frame_rate, |
+ const VideoCaptureDeviceFormatsCB& callback) override; |
+ void StartCapture( |
+ const media::VideoCaptureParams& params, |
+ const content::VideoCaptureDeliverFrameCB& frame_callback, |
+ scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
+ const RunningCallback& running_callback) override; |
+ void StopCapture() override; |
+ private: |
+ scoped_refptr<CastReceiverSession> cast_receiver_session_; |
miu
2015/02/11 02:52:49
ditto on const scoped_refptr<...>
hubbe
2015/02/11 22:38:16
Done.
|
+}; |
+ |
+CastReceiverSession::CastReceiverSession() |
+ : delegate_(new CastReceiverSessionDelegate()), |
+ io_message_loop_proxy_( |
+ content::RenderThread::Get()->GetIOMessageLoopProxy()) {} |
+ |
+CastReceiverSession::~CastReceiverSession() { |
+ // We should always be able to delete the object on the IO thread. |
+ CHECK(io_message_loop_proxy_->DeleteSoon(FROM_HERE, delegate_.release())); |
+} |
+ |
+void CastReceiverSession::Start( |
+ const media::cast::FrameReceiverConfig& audio_config, |
+ const media::cast::FrameReceiverConfig& video_config, |
+ const net::IPEndPoint& local_endpoint, |
+ const net::IPEndPoint& remote_endpoint, |
+ scoped_ptr<base::DictionaryValue> options, |
+ const media::VideoCaptureFormat& capture_format, |
+ const StartCB& start_callback) { |
+ format_ = capture_format; |
+ io_message_loop_proxy_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&CastReceiverSessionDelegate::Start, |
+ base::Unretained(delegate_.get()), |
+ audio_config, |
+ video_config, |
+ local_endpoint, |
+ remote_endpoint, |
+ base::Passed(&options), |
+ format_)); |
+ blink::WebMediaStreamSource video_source; |
+ content::InitializeWebMediaStreamSourceForVideo( |
+ make_scoped_ptr(new CastStreamingVideoCapturerSource(this)), |
+ &video_source, |
+ true, |
+ true); |
+ blink::WebVector<blink::WebMediaStreamTrack> video_tracks( |
+ static_cast<size_t>(1)); |
+ video_tracks[0].initialize(video_source); |
+ |
+ blink::WebMediaStreamSource audio_source; |
+ content::InitializeWebMediaStreamSourceForAudio( |
+ new CastStreamingAudioCapturerSource(this), |
+ &audio_source, |
+ true, |
+ true); |
+ blink::WebVector<blink::WebMediaStreamTrack> audio_tracks( |
+ static_cast<size_t>(1)); |
+ audio_tracks[0].initialize(audio_source); |
+ |
+ blink::WebMediaStream stream; |
+ stream.initialize(audio_tracks, video_tracks); |
+ |
+ base::MessageLoop::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(start_callback, stream)); |
+} |
+ |
+void CastReceiverSession::StartAudio( |
+ scoped_refptr<CastReceiverAudioValve> audio_valve) { |
+ io_message_loop_proxy_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&CastReceiverSessionDelegate::StartAudio, |
+ base::Unretained(delegate_.get()), |
+ audio_valve)); |
+} |
+ |
+void CastReceiverSession::StartVideo( |
+ content::VideoCaptureDeliverFrameCB frame_callback) { |
+ io_message_loop_proxy_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&CastReceiverSessionDelegate::StartVideo, |
+ base::Unretained(delegate_.get()), |
+ frame_callback)); |
+} |
+ |
+void CastReceiverSession::StopVideo() { |
+ io_message_loop_proxy_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&CastReceiverSessionDelegate::StopVideo, |
+ base::Unretained(delegate_.get()))); |
+} |
+ |
+CastStreamingVideoCapturerSource::CastStreamingVideoCapturerSource( |
miu
2015/02/11 02:52:49
Should this class include a destructor with a DCHE
hubbe
2015/02/11 22:38:16
For video it is actually not relevant if they call
|
+ scoped_refptr<CastReceiverSession> cast_receiver_session) |
+ : cast_receiver_session_(cast_receiver_session) { |
+} |
+ |
+void CastStreamingVideoCapturerSource::GetCurrentSupportedFormats( |
+ int max_requested_width, |
+ int max_requested_height, |
+ double max_requested_frame_rate, |
+ const VideoCaptureDeviceFormatsCB& callback) { |
+ std::vector<media::VideoCaptureFormat> formats; |
+ if (cast_receiver_session_->format().IsValid()) { |
+ formats.push_back(cast_receiver_session_->format()); |
+ } |
+ callback.Run(formats); |
+} |
+ |
+void CastStreamingVideoCapturerSource::StartCapture( |
+ const media::VideoCaptureParams& params, |
+ const content::VideoCaptureDeliverFrameCB& frame_callback, |
+ scoped_refptr<base::SingleThreadTaskRunner> frame_callback_task_runner, |
+ const RunningCallback& running_callback) { |
+ if (frame_callback_task_runner != |
+ content::RenderThread::Get()->GetIOMessageLoopProxy()) { |
+ DCHECK(false) << "Only IO thread supported right now."; |
+ running_callback.Run(false); |
+ return; |
+ } |
+ cast_receiver_session_->StartVideo(frame_callback); |
+ running_callback.Run(true); |
+} |
+ |
+void CastStreamingVideoCapturerSource::StopCapture() { |
+ cast_receiver_session_->StopVideo(); |
+} |
+ |
+CastStreamingAudioCapturerSource::CastStreamingAudioCapturerSource( |
+ scoped_refptr<CastReceiverSession> cast_receiver_session) |
+ : cast_receiver_session_(cast_receiver_session) { |
+} |
+ |
+CastStreamingAudioCapturerSource::~CastStreamingAudioCapturerSource() {} |
miu
2015/02/11 02:52:49
Do we need to check that Stop() was called first?
hubbe
2015/02/11 22:38:16
Done.
|
+ |
+void CastStreamingAudioCapturerSource::Initialize( |
+ const media::AudioParameters& params, |
+ CaptureCallback* callback, |
+ int session_id) { |
+ // TODO(hubbe): Consider converting the audio to whatever the caller wants. |
+ if (params.sample_rate() != |
+ cast_receiver_session_->audio_config_.rtp_timebase || |
+ params.channels() != cast_receiver_session_->audio_config_.channels) { |
+ callback->OnCaptureError(); |
+ return; |
+ } |
+ audio_valve_ = new CastReceiverAudioValve(callback); |
+} |
+ |
+void CastStreamingAudioCapturerSource::Start() { |
+ cast_receiver_session_->StartAudio(audio_valve_); |
+} |
+ |
+void CastStreamingAudioCapturerSource::Stop() { |
+ audio_valve_->Stop(); |
+} |
+ |
+void CastStreamingAudioCapturerSource::SetVolume(double volume) { |
+ // not supported |
+} |
+ |
+void CastStreamingAudioCapturerSource::SetAutomaticGainControl(bool enable) { |
+ // not supported |
+} |