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 #ifndef CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_
6 #define CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_
11 #include "base/basictypes.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/default_tick_clock.h"
18 #include "media/cast/cast_config.h"
19 #include "media/cast/cast_sender.h"
20 #include "media/cast/logging/logging_defines.h"
24 class DictionaryValue
;
25 class SingleThreadTaskRunner
;
32 class CastEnvironment
;
34 class RawEventSubscriberBundle
;
37 class CastTransportSender
;
38 } // namespace transport
42 // Breaks out functionality that is common between CastSessionDelegate and
43 // CastReceiverSessionDelegate.
44 class CastSessionDelegateBase
{
46 typedef base::Callback
<void(const std::string
&)> ErrorCallback
;
48 CastSessionDelegateBase();
49 virtual ~CastSessionDelegateBase();
51 // This will start the session by configuring and creating the Cast transport
52 // and the Cast sender.
53 // Must be called before initialization of audio or video.
54 void StartUDP(const net::IPEndPoint
& local_endpoint
,
55 const net::IPEndPoint
& remote_endpoint
,
56 scoped_ptr
<base::DictionaryValue
> options
,
57 const ErrorCallback
& error_callback
);
60 void StatusNotificationCB(
61 const ErrorCallback
& error_callback
,
62 media::cast::CastTransportStatus status
);
64 virtual void ReceivePacket(scoped_ptr
<media::cast::Packet
> packet
) = 0;
65 virtual void LogRawEvents(
66 const std::vector
<media::cast::PacketEvent
>& packet_events
,
67 const std::vector
<media::cast::FrameEvent
>& frame_events
) = 0;
69 base::ThreadChecker thread_checker_
;
70 scoped_refptr
<media::cast::CastEnvironment
> cast_environment_
;
71 scoped_ptr
<media::cast::CastTransportSender
> cast_transport_
;
73 // Proxy to the IO message loop.
74 const scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
75 base::WeakPtrFactory
<CastSessionDelegateBase
> weak_factory_
;
77 DISALLOW_COPY_AND_ASSIGN(CastSessionDelegateBase
);
80 // This class hosts CastSender and connects it to audio/video frame input
81 // and network socket.
82 // This class is created on the render thread and destroyed on the IO
83 // thread. All methods are accessible only on the IO thread.
84 class CastSessionDelegate
: public CastSessionDelegateBase
{
86 typedef base::Callback
<void(const scoped_refptr
<
87 media::cast::AudioFrameInput
>&)> AudioFrameInputAvailableCallback
;
88 typedef base::Callback
<void(const scoped_refptr
<
89 media::cast::VideoFrameInput
>&)> VideoFrameInputAvailableCallback
;
90 typedef base::Callback
<void(scoped_ptr
<base::BinaryValue
>)> EventLogsCallback
;
91 typedef base::Callback
<void(scoped_ptr
<base::DictionaryValue
>)> StatsCallback
;
93 CastSessionDelegate();
94 ~CastSessionDelegate() override
;
96 void StartUDP(const net::IPEndPoint
& local_endpoint
,
97 const net::IPEndPoint
& remote_endpoint
,
98 scoped_ptr
<base::DictionaryValue
> options
,
99 const ErrorCallback
& error_callback
);
101 // After calling StartAudio() or StartVideo() encoding of that media will
102 // begin as soon as data is delivered to its sink, if the second method is
103 // called the first media will be restarted. It is strongly recommended not to
104 // deliver any data between calling the two methods.
105 // It's OK to call only one of the two methods.
106 // StartUDP must be called before these methods.
107 void StartAudio(const media::cast::AudioSenderConfig
& config
,
108 const AudioFrameInputAvailableCallback
& callback
,
109 const ErrorCallback
& error_callback
);
111 void StartVideo(const media::cast::VideoSenderConfig
& config
,
112 const VideoFrameInputAvailableCallback
& callback
,
113 const ErrorCallback
& error_callback
,
114 const media::cast::CreateVideoEncodeAcceleratorCallback
&
116 const media::cast::CreateVideoEncodeMemoryCallback
&
117 create_video_encode_mem_cb
);
119 void ToggleLogging(bool is_audio
, bool enable
);
120 void GetEventLogsAndReset(bool is_audio
,
121 const std::string
& extra_data
, const EventLogsCallback
& callback
);
122 void GetStatsAndReset(bool is_audio
, const StatsCallback
& callback
);
125 // Called to report back operational status changes. The first time this is
126 // called with STATUS_INITIALIZED will result in running the "frame input
127 // available" callback, to indicate the session is ready to accept incoming
128 // audio/video frames. If this is called with an error that has halted the
129 // session, the |error_callback| provided to StartXXX() will be run. This
130 // method may be called multiple times during the session to indicate codec
131 // re-initializations are taking place and/or runtime errors have occurred.
132 void OnOperationalStatusChange(
134 const ErrorCallback
& error_callback
,
135 media::cast::OperationalStatus result
);
138 void ReceivePacket(scoped_ptr
<media::cast::Packet
> packet
) override
;
139 // Adds logs collected from transport on browser side.
140 void LogRawEvents(const std::vector
<media::cast::PacketEvent
>& packet_events
,
141 const std::vector
<media::cast::FrameEvent
>& frame_events
)
144 scoped_ptr
<media::cast::CastSender
> cast_sender_
;
146 AudioFrameInputAvailableCallback audio_frame_input_available_callback_
;
147 VideoFrameInputAvailableCallback video_frame_input_available_callback_
;
149 scoped_ptr
<media::cast::RawEventSubscriberBundle
> event_subscribers_
;
151 base::WeakPtrFactory
<CastSessionDelegate
> weak_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(CastSessionDelegate
);
156 #endif // CHROME_RENDERER_MEDIA_CAST_SESSION_DELEGATE_H_