• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
6 #define MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "media/base/audio_bus.h"
12 #include "media/cast/cast_config.h"
13 #include "media/cast/net/cast_transport_config.h"
14 #include "net/base/ip_endpoint.h"
15 
16 namespace base {
17 class TimeTicks;
18 class WaitableEvent;
19 }  // namespace base
20 
21 namespace net {
22 class IPEndPoint;
23 }  // namespace net
24 
25 namespace media {
26 
27 class VideoFrame;
28 
29 namespace cast {
30 
31 class CastEnvironment;
32 class CastReceiver;
33 class UdpTransport;
34 
35 // Common base functionality for an in-process Cast receiver.  This is meant to
36 // be subclassed with the OnAudioFrame() and OnVideoFrame() methods implemented,
37 // so that the implementor can focus on what is to be done with the frames,
38 // rather than on the boilerplate "glue" code.
39 class InProcessReceiver {
40  public:
41   // Construct a receiver with the given configuration.  |remote_end_point| can
42   // be left empty, if the transport should automatically mate with the first
43   // remote sender it encounters.
44   InProcessReceiver(const scoped_refptr<CastEnvironment>& cast_environment,
45                     const net::IPEndPoint& local_end_point,
46                     const net::IPEndPoint& remote_end_point,
47                     const FrameReceiverConfig& audio_config,
48                     const FrameReceiverConfig& video_config);
49 
50   virtual ~InProcessReceiver();
51 
52   // Convenience accessors.
cast_env()53   scoped_refptr<CastEnvironment> cast_env() const { return cast_environment_; }
audio_config()54   const FrameReceiverConfig& audio_config() const { return audio_config_; }
video_config()55   const FrameReceiverConfig& video_config() const { return video_config_; }
56 
57   // Begin delivering any received audio/video frames to the OnXXXFrame()
58   // methods.
59   virtual void Start();
60 
61   // Destroy the sub-compontents of this class.
62   // After this call, it is safe to destroy this object on any thread.
63   virtual void Stop();
64 
65  protected:
66   // To be implemented by subclasses.  These are called on the Cast MAIN thread
67   // as each frame is received.
68   virtual void OnAudioFrame(scoped_ptr<AudioBus> audio_frame,
69                             const base::TimeTicks& playout_time,
70                             bool is_continuous) = 0;
71   virtual void OnVideoFrame(const scoped_refptr<VideoFrame>& video_frame,
72                             const base::TimeTicks& playout_time,
73                             bool is_continuous) = 0;
74 
75   // Helper method that creates |transport_| and |cast_receiver_|, starts
76   // |transport_| receiving, and requests the first audio/video frame.
77   // Subclasses may override to provide additional start-up functionality.
78   virtual void StartOnMainThread();
79 
80   // Helper method that destroys |transport_| and |cast_receiver_|.
81   // Subclasses may override to provide additional start-up functionality.
82   virtual void StopOnMainThread(base::WaitableEvent* event);
83 
84   // Callback for the transport to notify of status changes.  A default
85   // implementation is provided here that simply logs socket errors.
86   virtual void UpdateCastTransportStatus(CastTransportStatus status);
87 
88  private:
89   friend class base::RefCountedThreadSafe<InProcessReceiver>;
90 
91   // CastReceiver callbacks that receive a frame and then request another.  See
92   // comments for the callbacks defined in src/media/cast/cast_receiver.h for
93   // argument description and semantics.
94   void GotAudioFrame(scoped_ptr<AudioBus> audio_frame,
95                      const base::TimeTicks& playout_time,
96                      bool is_continuous);
97   void GotVideoFrame(const scoped_refptr<VideoFrame>& video_frame,
98                      const base::TimeTicks& playout_time,
99                      bool is_continuous);
100   void PullNextAudioFrame();
101   void PullNextVideoFrame();
102 
103   const scoped_refptr<CastEnvironment> cast_environment_;
104   const net::IPEndPoint local_end_point_;
105   const net::IPEndPoint remote_end_point_;
106   const FrameReceiverConfig audio_config_;
107   const FrameReceiverConfig video_config_;
108 
109   scoped_ptr<UdpTransport> transport_;
110   scoped_ptr<CastReceiver> cast_receiver_;
111 
112   // NOTE: Weak pointers must be invalidated before all other member variables.
113   base::WeakPtrFactory<InProcessReceiver> weak_factory_;
114 
115   DISALLOW_COPY_AND_ASSIGN(InProcessReceiver);
116 };
117 
118 }  // namespace cast
119 }  // namespace media
120 
121 #endif  // MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
122