Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / renderer / media / cast_session.cc
blob06c29823c813857e49428dd6b9e7271efb728980
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.
5 #include "chrome/renderer/media/cast_session.h"
7 #include "base/single_thread_task_runner.h"
8 #include "chrome/renderer/media/cast_session_delegate.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "content/public/renderer/video_encode_accelerator.h"
11 #include "media/base/bind_to_current_loop.h"
12 #include "media/base/video_frame.h"
13 #include "media/cast/cast_config.h"
14 #include "media/cast/cast_sender.h"
15 #include "media/cast/logging/logging_defines.h"
17 namespace {
19 void CreateVideoEncodeAccelerator(
20 const media::cast::ReceiveVideoEncodeAcceleratorCallback& callback) {
21 DCHECK(content::RenderThread::Get());
23 // Delegate the call to content API on the render thread.
24 content::CreateVideoEncodeAccelerator(callback);
27 void CreateVideoEncodeMemory(
28 size_t size,
29 const media::cast::ReceiveVideoEncodeMemoryCallback& callback) {
30 DCHECK(content::RenderThread::Get());
32 scoped_ptr<base::SharedMemory> shm =
33 content::RenderThread::Get()->HostAllocateSharedMemoryBuffer(size);
34 DCHECK(shm) << "Failed to allocate shared memory";
35 if (!shm->Map(size)) {
36 NOTREACHED() << "Map failed";
38 callback.Run(shm.Pass());
41 } // namespace
43 CastSession::CastSession()
44 : delegate_(new CastSessionDelegate()),
45 io_task_runner_(
46 content::RenderThread::Get()->GetIOMessageLoopProxy()) {}
48 CastSession::~CastSession() {
49 // We should always be able to delete the object on the IO thread.
50 CHECK(io_task_runner_->DeleteSoon(FROM_HERE, delegate_.release()));
53 void CastSession::StartAudio(const media::cast::AudioSenderConfig& config,
54 const AudioFrameInputAvailableCallback& callback,
55 const ErrorCallback& error_callback) {
56 DCHECK(content::RenderThread::Get());
58 io_task_runner_->PostTask(
59 FROM_HERE,
60 base::Bind(&CastSessionDelegate::StartAudio,
61 base::Unretained(delegate_.get()),
62 config,
63 media::BindToCurrentLoop(callback),
64 media::BindToCurrentLoop(error_callback)));
67 void CastSession::StartVideo(const media::cast::VideoSenderConfig& config,
68 const VideoFrameInputAvailableCallback& callback,
69 const ErrorCallback& error_callback) {
70 DCHECK(content::RenderThread::Get());
72 io_task_runner_->PostTask(
73 FROM_HERE,
74 base::Bind(&CastSessionDelegate::StartVideo,
75 base::Unretained(delegate_.get()),
76 config,
77 media::BindToCurrentLoop(callback),
78 media::BindToCurrentLoop(error_callback),
79 media::BindToCurrentLoop(
80 base::Bind(&CreateVideoEncodeAccelerator)),
81 media::BindToCurrentLoop(
82 base::Bind(&CreateVideoEncodeMemory))));
85 void CastSession::StartUDP(const net::IPEndPoint& remote_endpoint,
86 scoped_ptr<base::DictionaryValue> options,
87 const ErrorCallback& error_callback) {
88 io_task_runner_->PostTask(
89 FROM_HERE,
90 base::Bind(
91 &CastSessionDelegate::StartUDP,
92 base::Unretained(delegate_.get()),
93 net::IPEndPoint(),
94 remote_endpoint,
95 base::Passed(&options),
96 media::BindToCurrentLoop(error_callback)));
99 void CastSession::ToggleLogging(bool is_audio, bool enable) {
100 io_task_runner_->PostTask(
101 FROM_HERE,
102 base::Bind(&CastSessionDelegate::ToggleLogging,
103 base::Unretained(delegate_.get()),
104 is_audio,
105 enable));
108 void CastSession::GetEventLogsAndReset(
109 bool is_audio, const std::string& extra_data,
110 const EventLogsCallback& callback) {
111 io_task_runner_->PostTask(
112 FROM_HERE,
113 base::Bind(&CastSessionDelegate::GetEventLogsAndReset,
114 base::Unretained(delegate_.get()),
115 is_audio,
116 extra_data,
117 media::BindToCurrentLoop(callback)));
120 void CastSession::GetStatsAndReset(bool is_audio,
121 const StatsCallback& callback) {
122 io_task_runner_->PostTask(
123 FROM_HERE,
124 base::Bind(&CastSessionDelegate::GetStatsAndReset,
125 base::Unretained(delegate_.get()),
126 is_audio,
127 media::BindToCurrentLoop(callback)));