Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / renderer / media / cast_session.cc
blob9d92f7b8d97d9de7dbeeffc5e04e5dcf9325e777
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/message_loop/message_loop_proxy.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_message_loop_proxy_(
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_message_loop_proxy_->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()
57 ->GetTaskRunner()->BelongsToCurrentThread());
59 io_message_loop_proxy_->PostTask(
60 FROM_HERE,
61 base::Bind(&CastSessionDelegate::StartAudio,
62 base::Unretained(delegate_.get()),
63 config,
64 media::BindToCurrentLoop(callback),
65 media::BindToCurrentLoop(error_callback)));
68 void CastSession::StartVideo(const media::cast::VideoSenderConfig& config,
69 const VideoFrameInputAvailableCallback& callback,
70 const ErrorCallback& error_callback) {
71 DCHECK(content::RenderThread::Get()
72 ->GetTaskRunner()->BelongsToCurrentThread());
74 io_message_loop_proxy_->PostTask(
75 FROM_HERE,
76 base::Bind(&CastSessionDelegate::StartVideo,
77 base::Unretained(delegate_.get()),
78 config,
79 media::BindToCurrentLoop(callback),
80 media::BindToCurrentLoop(error_callback),
81 media::BindToCurrentLoop(
82 base::Bind(&CreateVideoEncodeAccelerator)),
83 media::BindToCurrentLoop(
84 base::Bind(&CreateVideoEncodeMemory))));
87 void CastSession::StartUDP(const net::IPEndPoint& remote_endpoint,
88 scoped_ptr<base::DictionaryValue> options) {
89 io_message_loop_proxy_->PostTask(
90 FROM_HERE,
91 base::Bind(
92 &CastSessionDelegate::StartUDP,
93 base::Unretained(delegate_.get()),
94 remote_endpoint,
95 base::Passed(&options)));
98 void CastSession::ToggleLogging(bool is_audio, bool enable) {
99 io_message_loop_proxy_->PostTask(
100 FROM_HERE,
101 base::Bind(&CastSessionDelegate::ToggleLogging,
102 base::Unretained(delegate_.get()),
103 is_audio,
104 enable));
107 void CastSession::GetEventLogsAndReset(
108 bool is_audio, const std::string& extra_data,
109 const EventLogsCallback& callback) {
110 io_message_loop_proxy_->PostTask(
111 FROM_HERE,
112 base::Bind(&CastSessionDelegate::GetEventLogsAndReset,
113 base::Unretained(delegate_.get()),
114 is_audio,
115 extra_data,
116 media::BindToCurrentLoop(callback)));
119 void CastSession::GetStatsAndReset(bool is_audio,
120 const StatsCallback& callback) {
121 io_message_loop_proxy_->PostTask(
122 FROM_HERE,
123 base::Bind(&CastSessionDelegate::GetStatsAndReset,
124 base::Unretained(delegate_.get()),
125 is_audio,
126 media::BindToCurrentLoop(callback)));