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