Home | History | Annotate | Download | only in media
      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.
      4 
      5 #include "chrome/renderer/media/cast_session.h"
      6 
      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"
     16 
     17 namespace {
     18 
     19 void CreateVideoEncodeAccelerator(
     20     const media::cast::ReceiveVideoEncodeAcceleratorCallback& callback) {
     21   DCHECK(content::RenderThread::Get());
     22 
     23   // Delegate the call to content API on the render thread.
     24   content::CreateVideoEncodeAccelerator(callback);
     25 }
     26 
     27 void CreateVideoEncodeMemory(
     28     size_t size,
     29     const media::cast::ReceiveVideoEncodeMemoryCallback& callback) {
     30   DCHECK(content::RenderThread::Get());
     31 
     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";
     37   }
     38   callback.Run(shm.Pass());
     39 }
     40 
     41 }  // namespace
     42 
     43 CastSession::CastSession()
     44     : delegate_(new CastSessionDelegate()),
     45       io_message_loop_proxy_(
     46           content::RenderThread::Get()->GetIOMessageLoopProxy()) {}
     47 
     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()));
     51 }
     52 
     53 void CastSession::StartAudio(const media::cast::AudioSenderConfig& config,
     54                              const AudioFrameInputAvailableCallback& callback,
     55                              const ErrorCallback& error_callback) {
     56   DCHECK(content::RenderThread::Get()
     57              ->GetMessageLoop()
     58              ->message_loop_proxy()
     59              ->BelongsToCurrentThread());
     60 
     61   io_message_loop_proxy_->PostTask(
     62       FROM_HERE,
     63       base::Bind(&CastSessionDelegate::StartAudio,
     64                  base::Unretained(delegate_.get()),
     65                  config,
     66                  media::BindToCurrentLoop(callback),
     67                  media::BindToCurrentLoop(error_callback)));
     68 }
     69 
     70 void CastSession::StartVideo(const media::cast::VideoSenderConfig& config,
     71                              const VideoFrameInputAvailableCallback& callback,
     72                              const ErrorCallback& error_callback) {
     73   DCHECK(content::RenderThread::Get()
     74              ->GetMessageLoop()
     75              ->message_loop_proxy()
     76              ->BelongsToCurrentThread());
     77 
     78   io_message_loop_proxy_->PostTask(
     79       FROM_HERE,
     80       base::Bind(&CastSessionDelegate::StartVideo,
     81                  base::Unretained(delegate_.get()),
     82                  config,
     83                  media::BindToCurrentLoop(callback),
     84                  media::BindToCurrentLoop(error_callback),
     85                  media::BindToCurrentLoop(
     86                      base::Bind(&CreateVideoEncodeAccelerator)),
     87                  media::BindToCurrentLoop(
     88                      base::Bind(&CreateVideoEncodeMemory))));
     89 }
     90 
     91 void CastSession::StartUDP(const net::IPEndPoint& remote_endpoint) {
     92   io_message_loop_proxy_->PostTask(
     93       FROM_HERE,
     94       base::Bind(
     95           &CastSessionDelegate::StartUDP,
     96           base::Unretained(delegate_.get()),
     97           remote_endpoint));
     98 }
     99 
    100 void CastSession::ToggleLogging(bool is_audio, bool enable) {
    101   io_message_loop_proxy_->PostTask(
    102       FROM_HERE,
    103       base::Bind(&CastSessionDelegate::ToggleLogging,
    104                  base::Unretained(delegate_.get()),
    105                  is_audio,
    106                  enable));
    107 }
    108 
    109 void CastSession::GetEventLogsAndReset(
    110     bool is_audio, const std::string& extra_data,
    111     const EventLogsCallback& callback) {
    112   io_message_loop_proxy_->PostTask(
    113       FROM_HERE,
    114       base::Bind(&CastSessionDelegate::GetEventLogsAndReset,
    115                  base::Unretained(delegate_.get()),
    116                  is_audio,
    117                  extra_data,
    118                  media::BindToCurrentLoop(callback)));
    119 }
    120 
    121 void CastSession::GetStatsAndReset(bool is_audio,
    122                                    const StatsCallback& callback) {
    123   io_message_loop_proxy_->PostTask(
    124       FROM_HERE,
    125       base::Bind(&CastSessionDelegate::GetStatsAndReset,
    126                  base::Unretained(delegate_.get()),
    127                  is_audio,
    128                  media::BindToCurrentLoop(callback)));
    129 }
    130