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.
5 #ifndef MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
6 #define MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
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"
31 class CastEnvironment;
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 {
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);
50 virtual ~InProcessReceiver();
52 // Convenience accessors.
53 scoped_refptr<CastEnvironment> cast_env() const { return cast_environment_; }
54 const FrameReceiverConfig& audio_config() const { return audio_config_; }
55 const FrameReceiverConfig& video_config() const { return video_config_; }
57 // Begin delivering any received audio/video frames to the OnXXXFrame()
61 // Destroy the sub-compontents of this class.
62 // After this call, it is safe to destroy this object on any thread.
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;
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();
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);
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);
89 friend class base::RefCountedThreadSafe<InProcessReceiver>;
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,
97 void GotVideoFrame(const scoped_refptr<VideoFrame>& video_frame,
98 const base::TimeTicks& playout_time,
100 void PullNextAudioFrame();
101 void PullNextVideoFrame();
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_;
109 scoped_ptr<UdpTransport> transport_;
110 scoped_ptr<CastReceiver> cast_receiver_;
112 // NOTE: Weak pointers must be invalidated before all other member variables.
113 base::WeakPtrFactory<InProcessReceiver> weak_factory_;
115 DISALLOW_COPY_AND_ASSIGN(InProcessReceiver);
121 #endif // MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_