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 CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
6 #define CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "content/renderer/media/media_stream_video_track.h"
14 #include "media/base/video_frame.h"
18 // VideoTrackAdapter is a helper class used by MediaStreamVideoSource used for
19 // adapting the video resolution from a source implementation to the resolution
20 // a track requires. Different tracks can have different resolution constraints.
21 // The constraints can be set as max width and height as well as max and min
23 // Video frames are delivered to a track using a VideoCaptureDeliverFrameCB on
25 // Adaptations is done by wrapping the original media::VideoFrame in a new
26 // media::VideoFrame with a new visible_rect and natural_size.
27 class VideoTrackAdapter
28 : public base::RefCountedThreadSafe
<VideoTrackAdapter
> {
30 typedef base::Callback
<void(bool mute_state
)> OnMutedCallback
;
32 explicit VideoTrackAdapter(
33 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
);
35 // Register |track| to receive video frames in |frame_callback| with
36 // a resolution within the boundaries of the arguments.
37 // Must be called on the main render thread. |frame_callback| is guaranteed to
38 // be released on the main render thread.
39 // |source_frame_rate| is used to calculate a prudent interval to check for
40 // passing frames and inform of the result via |on_muted_state_callback|.
41 void AddTrack(const MediaStreamVideoTrack
* track
,
42 VideoCaptureDeliverFrameCB frame_callback
,
43 int max_width
, int max_height
,
44 double min_aspect_ratio
,
45 double max_aspect_ratio
,
46 double max_frame_rate
);
47 void RemoveTrack(const MediaStreamVideoTrack
* track
);
49 // Delivers |frame| to all tracks that have registered a callback.
50 // Must be called on the IO-thread.
51 void DeliverFrameOnIO(const scoped_refptr
<media::VideoFrame
>& frame
,
52 base::TimeTicks estimated_capture_time
);
54 base::SingleThreadTaskRunner
* io_task_runner() const {
55 DCHECK(thread_checker_
.CalledOnValidThread());
56 return io_task_runner_
.get();
59 // Start monitor that frames are delivered to this object. I.E, that
60 // |DeliverFrameOnIO| is called with a frame rate of |source_frame_rate|.
61 // |on_muted_callback| is triggered on the main render thread.
62 void StartFrameMonitoring(double source_frame_rate
,
63 const OnMutedCallback
& on_muted_callback
);
64 void StopFrameMonitoring();
67 virtual ~VideoTrackAdapter();
68 friend class base::RefCountedThreadSafe
<VideoTrackAdapter
>;
71 const MediaStreamVideoTrack
* track
,
72 VideoCaptureDeliverFrameCB frame_callback
,
73 const gfx::Size
& max_frame_size
,
74 double min_aspect_ratio
,
75 double max_aspect_ratio
,
76 double max_frame_rate
);
77 void RemoveTrackOnIO(const MediaStreamVideoTrack
* track
);
79 void StartFrameMonitoringOnIO(
80 const OnMutedCallback
& on_muted_state_callback
,
81 double source_frame_rate
);
82 void StopFrameMonitoringOnIO();
84 // Compare |frame_counter_snapshot| with the current |frame_counter_|, and
85 // inform of the situation (muted, not muted) via |set_muted_state_callback|.
86 void CheckFramesReceivedOnIO(const OnMutedCallback
& set_muted_state_callback
,
87 uint64 old_frame_counter_snapshot
);
89 // |thread_checker_| is bound to the main render thread.
90 base::ThreadChecker thread_checker_
;
92 const scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
94 // |renderer_task_runner_| is used to ensure that
95 // VideoCaptureDeliverFrameCB is released on the main render thread.
96 const scoped_refptr
<base::SingleThreadTaskRunner
> renderer_task_runner_
;
98 // VideoFrameResolutionAdapter is an inner class that is created on the main
99 // render thread but operates on the IO-thread. It does the resolution
100 // adaptation and delivers frames to all registered tracks on the IO-thread.
101 class VideoFrameResolutionAdapter
;
102 typedef std::vector
<scoped_refptr
<VideoFrameResolutionAdapter
> >
104 FrameAdapters adapters_
;
106 // Set to true if frame monitoring has been started. It is only accessed on
108 bool monitoring_frame_rate_
;
110 // Keeps track of it frames have been received. It is only accessed on the
114 // Running frame counter, accessed on the IO-thread.
115 uint64 frame_counter_
;
117 // Frame rate configured on the video source, accessed on the IO-thread.
118 float source_frame_rate_
;
120 DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter
);
123 } // namespace content
125 #endif // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_