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/transport/cast_transport_config.h"
30 class CastEnvironment
;
35 } // namespace transport
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
{
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 AudioReceiverConfig
& audio_config
,
50 const VideoReceiverConfig
& video_config
);
52 virtual ~InProcessReceiver();
54 // Convenience accessors.
55 scoped_refptr
<CastEnvironment
> cast_env() const { return cast_environment_
; }
56 const AudioReceiverConfig
& audio_config() const { return audio_config_
; }
57 const VideoReceiverConfig
& video_config() const { return video_config_
; }
59 // Begin delivering any received audio/video frames to the OnXXXFrame()
63 // Destroy the sub-compontents of this class.
64 // After this call, it is safe to destroy this object on any thread.
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;
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();
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
);
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
);
91 friend class base::RefCountedThreadSafe
<InProcessReceiver
>;
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
,
99 void GotVideoFrame(const scoped_refptr
<VideoFrame
>& video_frame
,
100 const base::TimeTicks
& playout_time
,
102 void PullNextAudioFrame();
103 void PullNextVideoFrame();
105 const scoped_refptr
<CastEnvironment
> cast_environment_
;
106 const net::IPEndPoint local_end_point_
;
107 const net::IPEndPoint remote_end_point_
;
108 const AudioReceiverConfig audio_config_
;
109 const VideoReceiverConfig video_config_
;
111 scoped_ptr
<transport::UdpTransport
> transport_
;
112 scoped_ptr
<CastReceiver
> cast_receiver_
;
114 // NOTE: Weak pointers must be invalidated before all other member variables.
115 base::WeakPtrFactory
<InProcessReceiver
> weak_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(InProcessReceiver
);
123 #endif // MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_