• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_H_
6 #define CHROME_RENDERER_MEDIA_CAST_SESSION_H_
7 
8 #include <vector>
9 
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/ip_endpoint.h"
15 
16 namespace base {
17 class BinaryValue;
18 class DictionaryValue;
19 class MessageLoopProxy;
20 }  // namespace base
21 
22 namespace media {
23 class VideoFrame;
24 namespace cast {
25 class AudioFrameInput;
26 class VideoFrameInput;
27 struct AudioSenderConfig;
28 struct VideoSenderConfig;
29 }  // namespace cast
30 }  // namespace media
31 
32 namespace content {
33 class P2PSocketClient;
34 }  // namespace content
35 
36 class CastSessionDelegate;
37 
38 // This class represents a Cast session and allows the session to be
39 // configured on the main thread. Actual work is forwarded to
40 // CastSessionDelegate on the IO thread.
41 class CastSession : public base::RefCounted<CastSession> {
42  public:
43   typedef base::Callback<void(const scoped_refptr<
44       media::cast::AudioFrameInput>&)> AudioFrameInputAvailableCallback;
45   typedef base::Callback<void(const scoped_refptr<
46       media::cast::VideoFrameInput>&)> VideoFrameInputAvailableCallback;
47   typedef base::Callback<void(const std::vector<char>&)> SendPacketCallback;
48   typedef base::Callback<void(scoped_ptr<base::BinaryValue>)> EventLogsCallback;
49   typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)> StatsCallback;
50   typedef base::Callback<void(const std::string&)> ErrorCallback;
51 
52   CastSession();
53 
54   // Start encoding of audio and video using the provided configuration.
55   //
56   // When Cast sender is started and ready to be used
57   // media::cast::FrameInput will be given through |callback|.
58   // If it encounters an error, |error_callback| will be invoked with the
59   // error message. Both |callback| and |error_callback| will be made on
60   // the main thread.
61   // |StartUDP()| must be called before these methods.
62   void StartAudio(const media::cast::AudioSenderConfig& config,
63                   const AudioFrameInputAvailableCallback& callback,
64                   const ErrorCallback& error_callback);
65   void StartVideo(const media::cast::VideoSenderConfig& config,
66                   const VideoFrameInputAvailableCallback& callback,
67                   const ErrorCallback& error_callback);
68 
69   // This will create the Cast transport and connect to |remote_endpoint|.
70   // |options| is a dictionary which contain optional configuration for the
71   // udp transport.
72   // Must be called before initialization of audio or video.
73   void StartUDP(const net::IPEndPoint& remote_endpoint,
74                 scoped_ptr<base::DictionaryValue> options);
75 
76   // Creates or destroys event subscriber for the audio or video stream.
77   // |is_audio|: true if the event subscriber is for audio. Video otherwise.
78   // |enable|: If true, creates an event subscriber. Otherwise destroys
79   // existing subscriber and discards logs.
80   void ToggleLogging(bool is_audio, bool enable);
81 
82   // Returns raw event logs in serialized format for either the audio or video
83   // stream since last call and returns result in |callback|. Also attaches
84   // |extra_data| to the log.
85   void GetEventLogsAndReset(bool is_audio,
86       const std::string& extra_data, const EventLogsCallback& callback);
87 
88   // Returns stats in a DictionaryValue format for either the audio or video
89   // stream since last call and returns result in |callback|.
90   void GetStatsAndReset(bool is_audio, const StatsCallback& callback);
91 
92  private:
93   friend class base::RefCounted<CastSession>;
94   virtual ~CastSession();
95 
96   // This member should never be dereferenced on the main thread.
97   // CastSessionDelegate lives only on the IO thread. It is always
98   // safe to post task on the IO thread to access CastSessionDelegate
99   // because it is owned by this object.
100   scoped_ptr<CastSessionDelegate> delegate_;
101 
102   // Proxy to the IO message loop.
103   const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
104 
105   DISALLOW_COPY_AND_ASSIGN(CastSession);
106 };
107 
108 #endif  // CHROME_RENDERER_MEDIA_CAST_SESSION_H_
109