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 void CreateCastSession(
50 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
52 void DestroyCastRtpStream(
53 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
54 void CreateParamsCastRtpStream(
55 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
56 void GetSupportedParamsCastRtpStream(
57 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
58 void StartCastRtpStream(
59 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
60 void StopCastRtpStream(
61 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
63 void DestroyCastUdpTransport(
64 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
65 void SetDestinationCastUdpTransport(
66 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
67 void SetOptionsCastUdpTransport(
68 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
69 void StopCastUdpTransport(
70 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
71 void StartCastRtpReceiver(
72 const v8::FunctionCallbackInfo
<v8::Value
>& args
);
74 void ToggleLogging(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
75 void GetRawEvents(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
76 void GetStats(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
78 // Helper method to call the v8 callback function after a session is
80 void CallCreateCallback(scoped_ptr
<CastRtpStream
> stream1
,
81 scoped_ptr
<CastRtpStream
> stream2
,
82 scoped_ptr
<CastUdpTransport
> udp_transport
);
84 void CallStartCallback(int stream_id
);
85 void CallStopCallback(int stream_id
);
86 void CallErrorCallback(int stream_id
, const std::string
& message
);
88 // Callback called after a cast receiver has been started. Adds the
89 // output audio/video streams to the MediaStream specified by |url|.
90 void AddTracksToMediaStream(
91 const std::string
& url
,
92 const media::AudioParameters
& params
,
93 scoped_refptr
<media::AudioCapturerSource
> audio
,
94 scoped_ptr
<media::VideoCapturerSource
> video
);
96 // |function| is a javascript function that will take |error_message| as
97 // an argument. Called when something goes wrong in a cast receiver.
98 void CallReceiverErrorCallback(
99 v8::CopyablePersistentTraits
<v8::Function
>::CopyablePersistent function
,
100 const std::string
& error_message
);
102 void CallGetRawEventsCallback(int transport_id
,
103 scoped_ptr
<base::BinaryValue
> raw_events
);
104 void CallGetStatsCallback(int transport_id
,
105 scoped_ptr
<base::DictionaryValue
> stats
);
107 // Gets the RTP stream or UDP transport indexed by an ID.
108 // If not found, returns NULL and throws a V8 exception.
109 CastRtpStream
* GetRtpStreamOrThrow(int stream_id
) const;
110 CastUdpTransport
* GetUdpTransportOrThrow(int transport_id
) const;
112 // Fills out a media::cast::FrameReceiverConfig from the v8
113 // equivialent. (cast.streaming.receiverSession.RtpReceiverParams)
114 // Returns true if everything was ok, raises a v8 exception and
115 // returns false if anything went wrong.
116 bool FrameReceiverConfigFromArg(
117 v8::Isolate
* isolate
,
118 const v8::Handle
<v8::Value
>& arg
,
119 media::cast::FrameReceiverConfig
* config
);
121 bool IPEndPointFromArg(v8::Isolate
* isolate
,
122 const v8::Handle
<v8::Value
>& arg
,
123 net::IPEndPoint
* ip_endpoint
);
125 int last_transport_id_
;
127 typedef std::map
<int, linked_ptr
<CastRtpStream
> > RtpStreamMap
;
128 RtpStreamMap rtp_stream_map_
;
130 typedef std::map
<int, linked_ptr
<CastUdpTransport
> > UdpTransportMap
;
131 UdpTransportMap udp_transport_map_
;
133 v8::Global
<v8::Function
> create_callback_
;
135 typedef std::map
<int, linked_ptr
<v8::Global
<v8::Function
>>>
136 RtpStreamCallbackMap
;
137 RtpStreamCallbackMap get_raw_events_callbacks_
;
138 RtpStreamCallbackMap get_stats_callbacks_
;
140 base::WeakPtrFactory
<CastStreamingNativeHandler
> weak_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(CastStreamingNativeHandler
);
145 } // namespace extensions
147 #endif // CHROME_RENDERER_EXTENSIONS_CAST_STREAMING_NATIVE_HANDLER_H_