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_EXTENSIONS_CAST_STREAMING_NATIVE_HANDLER_H_
6 #define CHROME_RENDERER_EXTENSIONS_CAST_STREAMING_NATIVE_HANDLER_H_
10 #include "base/memory/linked_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "extensions/renderer/object_backed_native_handler.h"
13 #include "v8/include/v8.h"
16 class CastUdpTransport
;
20 class DictionaryValue
;
32 class AudioCapturerSource
;
33 class AudioParameters
;
34 class VideoCapturerSource
;
36 struct FrameReceiverConfig
;
40 namespace extensions
{
42 // Native code that handle chrome.webrtc custom bindings.
43 class CastStreamingNativeHandler
: public ObjectBackedNativeHandler
{
45 explicit CastStreamingNativeHandler(ScriptContext
* context
);
46 ~CastStreamingNativeHandler() override
;
49 // Shut down all sessions and cancel any in-progress operations because the
50 // ScriptContext is about to become invalid.
51 void Invalidate() override
;
54 void CreateCastSession(
55 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
57 void DestroyCastRtpStream(
58 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
59 void CreateParamsCastRtpStream(
60 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
61 void GetSupportedParamsCastRtpStream(
62 const v8::FunctionCallbackInfo
<v8::Value
>& args
) const;
63 void StartCastRtpStream(
64 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
65 void StopCastRtpStream(
66 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
68 void DestroyCastUdpTransport(
69 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
70 void SetDestinationCastUdpTransport(
71 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
72 void SetOptionsCastUdpTransport(
73 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
74 void StopCastUdpTransport(
75 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
77 void StartCastRtpReceiver(
78 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
80 void ToggleLogging(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
81 void GetRawEvents(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
82 void GetStats(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
84 // Helper method to call the v8 callback function after a session is
86 void CallCreateCallback(scoped_ptr
<CastRtpStream
> stream1
,
87 scoped_ptr
<CastRtpStream
> stream2
,
88 scoped_ptr
<CastUdpTransport
> udp_transport
);
90 void CallStartCallback(int stream_id
) const;
91 void CallStopCallback(int stream_id
) const;
92 void CallErrorCallback(int stream_id
, const std::string
& message
) const;
94 // Callback called after a cast receiver has been started. Adds the
95 // output audio/video streams to the MediaStream specified by |url|.
96 void AddTracksToMediaStream(
97 const std::string
& url
,
98 const media::AudioParameters
& params
,
99 scoped_refptr
<media::AudioCapturerSource
> audio
,
100 scoped_ptr
<media::VideoCapturerSource
> video
);
102 // |function| is a javascript function that will take |error_message| as
103 // an argument. Called when something goes wrong in a cast receiver.
104 void CallReceiverErrorCallback(
105 v8::CopyablePersistentTraits
<v8::Function
>::CopyablePersistent function
,
106 const std::string
& error_message
);
108 void CallGetRawEventsCallback(int transport_id
,
109 scoped_ptr
<base::BinaryValue
> raw_events
);
110 void CallGetStatsCallback(int transport_id
,
111 scoped_ptr
<base::DictionaryValue
> stats
);
113 // Gets the RTP stream or UDP transport indexed by an ID.
114 // If not found, returns NULL and throws a V8 exception.
115 CastRtpStream
* GetRtpStreamOrThrow(int stream_id
) const;
116 CastUdpTransport
* GetUdpTransportOrThrow(int transport_id
) const;
118 // Fills out a media::cast::FrameReceiverConfig from the v8
119 // equivialent. (cast.streaming.receiverSession.RtpReceiverParams)
120 // Returns true if everything was ok, raises a v8 exception and
121 // returns false if anything went wrong.
122 bool FrameReceiverConfigFromArg(
123 v8::Isolate
* isolate
,
124 const v8::Local
<v8::Value
>& arg
,
125 media::cast::FrameReceiverConfig
* config
) const;
127 bool IPEndPointFromArg(v8::Isolate
* isolate
,
128 const v8::Local
<v8::Value
>& arg
,
129 net::IPEndPoint
* ip_endpoint
) const;
131 int last_transport_id_
;
133 typedef std::map
<int, linked_ptr
<CastRtpStream
> > RtpStreamMap
;
134 RtpStreamMap rtp_stream_map_
;
136 typedef std::map
<int, linked_ptr
<CastUdpTransport
> > UdpTransportMap
;
137 UdpTransportMap udp_transport_map_
;
139 v8::Global
<v8::Function
> create_callback_
;
141 typedef std::map
<int, linked_ptr
<v8::Global
<v8::Function
>>>
142 RtpStreamCallbackMap
;
143 RtpStreamCallbackMap get_raw_events_callbacks_
;
144 RtpStreamCallbackMap get_stats_callbacks_
;
146 base::WeakPtrFactory
<CastStreamingNativeHandler
> weak_factory_
;
148 DISALLOW_COPY_AND_ASSIGN(CastStreamingNativeHandler
);
151 } // namespace extensions
153 #endif // CHROME_RENDERER_EXTENSIONS_CAST_STREAMING_NATIVE_HANDLER_H_