Chromium Code Reviews| 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..c6de66deeda58239fbb7c453b336c4c2f076ab65 |
| --- /dev/null |
| +++ b/chrome/renderer/media/cast_receiver_session.cc |
| @@ -0,0 +1,210 @@ |
| +// 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 "content/public/renderer/media_stream_source_api.h" |
| +#include "content/public/renderer/render_thread.h" |
| +#include "media/base/audio_capturer_source.h" |
| +#include "media/video/capture/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" |
| + |
| +class CastStreamingAudioCaptureSource : public media::AudioCapturerSource { |
|
Alpha Left Google
2015/02/04 03:07:52
Should be CastSreamingAudioCapturerSource.
hubbe
2015/02/05 20:22:59
Done.
|
| + public: |
| + CastStreamingAudioCaptureSource( |
| + 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: |
| + ~CastStreamingAudioCaptureSource() override; |
| + scoped_refptr<CastReceiverSession> cast_receiver_session_; |
| + CaptureCallback* capture_callback_; // Ownership?? |
| +}; |
| + |
| +class CastVideoCapturerSource : public media::VideoCapturerSource { |
|
Alpha Left Google
2015/02/04 03:07:52
Should be CastStreamingvideoCapturerSource to be c
hubbe
2015/02/05 20:22:59
Done.
|
| + public: |
| + explicit CastVideoCapturerSource( |
| + 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, |
| + const RunningCallback& running_callback) override; |
| + void StopCapture() override; |
| + private: |
| + ~CastVideoCapturerSource() override; |
| + scoped_refptr<CastReceiverSession> cast_receiver_session_; |
| +}; |
| + |
| +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::InitializeVideoWebMediaSource(new CastVideoCapturerSource(this), |
| + &video_source); |
| + blink::WebVector<blink::WebMediaStreamTrack> video_tracks( |
| + static_cast<size_t>(1)); |
| + video_tracks[0].initialize(video_source); |
| + |
| + blink::WebMediaStreamSource audio_source; |
| + content::InitializeAudioWebMediaSource( |
| + new CastStreamingAudioCaptureSource(this), |
| + &audio_source); |
| + 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( |
| + CastReceiverSessionDelegate::AudioCB audio_callback) { |
| + io_message_loop_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CastReceiverSessionDelegate::StartAudio, |
| + base::Unretained(delegate_.get()), |
| + audio_callback)); |
| +} |
| + |
| +void CastReceiverSession::StopAudio(base::WaitableEvent* event) { |
| + io_message_loop_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CastReceiverSessionDelegate::StopAudio, |
| + base::Unretained(delegate_.get()), |
| + event)); |
| +} |
| + |
| +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(base::WaitableEvent* event) { |
| + io_message_loop_proxy_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CastReceiverSessionDelegate::StopVideo, |
| + base::Unretained(delegate_.get()), |
| + event)); |
| +} |
| + |
| + |
| +CastVideoCapturerSource::CastVideoCapturerSource( |
| + scoped_refptr<CastReceiverSession> cast_receiver_session) |
| + : cast_receiver_session_(cast_receiver_session) { |
| +} |
| + |
| +CastVideoCapturerSource::~CastVideoCapturerSource() {} |
| + |
| +void CastVideoCapturerSource::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 CastVideoCapturerSource::StartCapture( |
| + const media::VideoCaptureParams& params, |
| + const content::VideoCaptureDeliverFrameCB& frame_callback, |
| + const RunningCallback& running_callback) { |
| + cast_receiver_session_->StartVideo(frame_callback); |
| +} |
| + |
| +void CastVideoCapturerSource::StopCapture() { |
| + base::WaitableEvent event(true, false); |
|
Alpha Left Google
2015/02/04 03:07:52
There is no need to stop and wait. |frame_callback
hubbe
2015/02/05 20:22:59
Done.
|
| + cast_receiver_session_->StopVideo(&event); |
| + event.Wait(); |
| +} |
| + |
| +CastStreamingAudioCaptureSource::CastStreamingAudioCaptureSource( |
| + scoped_refptr<CastReceiverSession> cast_receiver_session) |
| + : cast_receiver_session_(cast_receiver_session) { |
| +} |
| + |
| +CastStreamingAudioCaptureSource::~CastStreamingAudioCaptureSource() {} |
| + |
| +void CastStreamingAudioCaptureSource::Initialize( |
| + const media::AudioParameters& params, |
| + CaptureCallback* callback, |
| + int session_id) { |
| + if (params.sample_rate() != 48000 || |
|
Alpha Left Google
2015/02/04 03:07:52
Need a comment or TODO to fix this limitation.
hubbe
2015/02/05 20:22:59
Improved to not hardcode values and added TODO to
|
| + params.bits_per_sample() != 16 || |
| + params.channels() != 2) { |
| + callback->OnCaptureError(); |
| + return; |
| + } |
| + capture_callback_ = callback; |
| +} |
| + |
| +void CastStreamingAudioCaptureSource::Start() { |
| + cast_receiver_session_->StartAudio( |
| + base::Bind(&media::AudioCapturerSource::CaptureCallback::Capture, |
| + base::Unretained(capture_callback_))); |
| +} |
| + |
| +void CastStreamingAudioCaptureSource::Stop() { |
| + base::WaitableEvent event(true, false); |
|
Alpha Left Google
2015/02/04 03:07:52
Instead of blocking here what about having a third
hubbe
2015/02/05 20:22:59
Done.
|
| + cast_receiver_session_->StopAudio(&event); |
| + event.Wait(); |
| +} |
| + |
| +void CastStreamingAudioCaptureSource::SetVolume(double volume) { |
| + // not supported |
| +} |
| + |
| +void CastStreamingAudioCaptureSource::SetAutomaticGainControl(bool enable) { |
| + // not supported |
| +} |