summaryrefslogtreecommitdiffstats
path: root/media/cast/test/utility/in_process_receiver.h
blob: cd957d4f778ec400300446aae4804382df0e71cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_
#define MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "media/base/audio_bus.h"
#include "media/cast/cast_config.h"
#include "media/cast/net/cast_transport_config.h"
#include "media/cast/net/cast_transport_sender.h"
#include "net/base/ip_endpoint.h"

namespace base {
class TimeTicks;
class WaitableEvent;
}  // namespace base

namespace net {
class IPEndPoint;
}  // namespace net

namespace media {

class VideoFrame;

namespace cast {

class CastEnvironment;
class CastReceiver;
class UdpTransport;

// Common base functionality for an in-process Cast receiver.  This is meant to
// be subclassed with the OnAudioFrame() and OnVideoFrame() methods implemented,
// so that the implementor can focus on what is to be done with the frames,
// rather than on the boilerplate "glue" code.
class InProcessReceiver {
 public:
  // Construct a receiver with the given configuration.  |remote_end_point| can
  // be left empty, if the transport should automatically mate with the first
  // remote sender it encounters.
  InProcessReceiver(const scoped_refptr<CastEnvironment>& cast_environment,
                    const net::IPEndPoint& local_end_point,
                    const net::IPEndPoint& remote_end_point,
                    const FrameReceiverConfig& audio_config,
                    const FrameReceiverConfig& video_config);

  virtual ~InProcessReceiver();

  // Convenience accessors.
  scoped_refptr<CastEnvironment> cast_env() const { return cast_environment_; }
  const FrameReceiverConfig& audio_config() const { return audio_config_; }
  const FrameReceiverConfig& video_config() const { return video_config_; }

  // Begin delivering any received audio/video frames to the OnXXXFrame()
  // methods.
  virtual void Start();

  // Destroy the sub-compontents of this class.
  // After this call, it is safe to destroy this object on any thread.
  virtual void Stop();

 protected:
  // To be implemented by subclasses.  These are called on the Cast MAIN thread
  // as each frame is received.
  virtual void OnAudioFrame(scoped_ptr<AudioBus> audio_frame,
                            const base::TimeTicks& playout_time,
                            bool is_continuous) = 0;
  virtual void OnVideoFrame(const scoped_refptr<VideoFrame>& video_frame,
                            const base::TimeTicks& playout_time,
                            bool is_continuous) = 0;

  // Helper method that creates |transport_| and |cast_receiver_|, starts
  // |transport_| receiving, and requests the first audio/video frame.
  // Subclasses may final to provide additional start-up functionality.
  virtual void StartOnMainThread();

  // Helper method that destroys |transport_| and |cast_receiver_|.
  // Subclasses may final to provide additional start-up functionality.
  virtual void StopOnMainThread(base::WaitableEvent* event);

  // Callback for the transport to notify of status changes.  A default
  // implementation is provided here that simply logs socket errors.
  virtual void UpdateCastTransportStatus(CastTransportStatus status);

 private:
  friend class base::RefCountedThreadSafe<InProcessReceiver>;

  // CastReceiver callbacks that receive a frame and then request another.  See
  // comments for the callbacks defined in src/media/cast/cast_receiver.h for
  // argument description and semantics.
  void GotAudioFrame(scoped_ptr<AudioBus> audio_frame,
                     const base::TimeTicks& playout_time,
                     bool is_continuous);
  void GotVideoFrame(const scoped_refptr<VideoFrame>& video_frame,
                     const base::TimeTicks& playout_time,
                     bool is_continuous);
  void PullNextAudioFrame();
  void PullNextVideoFrame();

  void ReceivePacket(scoped_ptr<Packet> packet);

  const scoped_refptr<CastEnvironment> cast_environment_;
  const net::IPEndPoint local_end_point_;
  const net::IPEndPoint remote_end_point_;
  const FrameReceiverConfig audio_config_;
  const FrameReceiverConfig video_config_;

  scoped_ptr<CastTransportSender> transport_;
  scoped_ptr<CastReceiver> cast_receiver_;

  // NOTE: Weak pointers must be invalidated before all other member variables.
  base::WeakPtrFactory<InProcessReceiver> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(InProcessReceiver);
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_TEST_IN_PROCESS_RECEIVER_H_