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 #ifndef CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_ 6 #define CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/threading/thread.h" 13 #include "base/threading/thread_checker.h" 14 #include "base/time/default_tick_clock.h" 15 #include "chrome/renderer/media/cast_session.h" 16 #include "content/public/renderer/p2p_socket_client.h" 17 #include "content/public/renderer/p2p_socket_client_delegate.h" 18 #include "media/cast/cast_config.h" 19 #include "media/cast/cast_sender.h" 20 21 namespace base { 22 class MessageLoopProxy; 23 } // namespace base 24 25 namespace media { 26 class VideoFrame; 27 28 namespace cast { 29 class CastEnvironment; 30 class FrameInput; 31 } // namespace cast 32 } // namespace media 33 34 // This class hosts CastSender and connects it to audio/video frame input 35 // and network socket. 36 // This class is created on the render thread and destroyed on the IO 37 // thread. All methods are accessible only on the IO thread. 38 class CastSessionDelegate 39 : public media::cast::PacketSender, 40 public content::P2PSocketClientDelegate { 41 public: 42 typedef 43 base::Callback<void(const scoped_refptr<media::cast::FrameInput>&)> 44 FrameInputAvailableCallback; 45 46 CastSessionDelegate(); 47 virtual ~CastSessionDelegate(); 48 49 void SetSocketFactory( 50 scoped_ptr<CastSession::P2PSocketFactory> socket_factory, 51 const net::IPEndPoint& remote_address); 52 53 // After calling StartAudio() and StartVideo() with configuration encoding 54 // will begin. 55 void StartAudio(const media::cast::AudioSenderConfig& config, 56 const FrameInputAvailableCallback& callback); 57 void StartVideo(const media::cast::VideoSenderConfig& config, 58 const FrameInputAvailableCallback& callback); 59 60 private: 61 // Start encoding threads and configure CastSender. It is ready to accept 62 // audio/video frames after this call. 63 void StartSending(); 64 65 // If both audio and video are configured properly then start CastSender. 66 void MaybeStartSending(); 67 68 // media::cast::PacketSender Implementation 69 virtual bool SendPacket(const media::cast::Packet& packet) OVERRIDE; 70 virtual bool SendPackets(const media::cast::PacketList& packets) OVERRIDE; 71 72 // content::P2PSocketClient::Delegate Implementation 73 virtual void OnOpen(const net::IPEndPoint& address) OVERRIDE; 74 virtual void OnIncomingTcpConnection( 75 const net::IPEndPoint& address, 76 content::P2PSocketClient* client) OVERRIDE; 77 virtual void OnSendComplete() OVERRIDE; 78 virtual void OnError() OVERRIDE; 79 virtual void OnDataReceived(const net::IPEndPoint& address, 80 const std::vector<char>& data, 81 const base::TimeTicks& timestamp) OVERRIDE; 82 private: 83 base::ThreadChecker thread_checker_; 84 scoped_refptr<media::cast::CastEnvironment> cast_environment_; 85 scoped_ptr<media::cast::CastSender> cast_sender_; 86 scoped_ptr<CastSession::P2PSocketFactory> socket_factory_; 87 net::IPEndPoint remote_address_; 88 scoped_refptr<content::P2PSocketClient> socket_; 89 90 // Utilities threads owned by this class. They are used by CastSender for 91 // encoding. 92 // TODO(hclam): See crbug.com/317006 for more details. 93 // This class shouldn't create and own threads. 94 base::Thread audio_encode_thread_; 95 base::Thread video_encode_thread_; 96 97 // Clock used by CastSender. 98 base::DefaultTickClock clock_; 99 100 // Configuration for audio and video. 101 media::cast::AudioSenderConfig audio_config_; 102 media::cast::VideoSenderConfig video_config_; 103 bool audio_configured_; 104 bool video_configured_; 105 std::vector<FrameInputAvailableCallback> frame_input_available_callbacks_; 106 107 // Proxy to the IO message loop. 108 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 109 110 DISALLOW_COPY_AND_ASSIGN(CastSessionDelegate); 111 }; 112 113 #endif // CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_ 114