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 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
);
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 double source_frame_rate
,
48 const OnMutedCallback
& on_muted_state_callback
);
49 void RemoveTrack(const MediaStreamVideoTrack
* track
);
51 // Delivers |frame| to all tracks that have registered a callback.
52 // Must be called on the IO-thread.
53 void DeliverFrameOnIO(
54 const scoped_refptr
<media::VideoFrame
>& frame
,
55 const media::VideoCaptureFormat
& format
,
56 const base::TimeTicks
& estimated_capture_time
);
58 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop() {
59 DCHECK(thread_checker_
.CalledOnValidThread());
60 return io_message_loop_
;
64 virtual ~VideoTrackAdapter();
65 friend class base::RefCountedThreadSafe
<VideoTrackAdapter
>;
68 const MediaStreamVideoTrack
* track
,
69 VideoCaptureDeliverFrameCB frame_callback
,
70 const gfx::Size
& max_frame_size
,
71 double min_aspect_ratio
,
72 double max_aspect_ratio
,
73 double max_frame_rate
);
74 void RemoveTrackOnIO(const MediaStreamVideoTrack
* track
);
76 void StartTrackMonitoringOnIO(
77 const OnMutedCallback
& on_muted_state_callback
,
78 double source_frame_rate
);
80 // Compare |frame_counter_snapshot| with the current |frame_counter_|, and
81 // inform of the situation (muted, not muted) via |set_muted_state_callback|.
82 void CheckFramesReceivedOnIO(const OnMutedCallback
& set_muted_state_callback
,
83 uint64 old_frame_counter_snapshot
);
85 // |thread_checker_| is bound to the main render thread.
86 base::ThreadChecker thread_checker_
;
88 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
90 // |renderer_task_runner_| is used to ensure that
91 // VideoCaptureDeliverFrameCB is released on the main render thread.
92 scoped_refptr
<base::SingleThreadTaskRunner
> renderer_task_runner_
;
94 // VideoFrameResolutionAdapter is an inner class that is created on the main
95 // render thread but operates on the IO-thread. It does the resolution
96 // adaptation and delivers frames to all registered tracks on the IO-thread.
97 class VideoFrameResolutionAdapter
;
98 typedef std::vector
<scoped_refptr
<VideoFrameResolutionAdapter
> >
100 FrameAdapters adapters_
;
102 // Running frame counter, accessed on the IO-thread.
103 uint64 frame_counter_
;
105 // Frame rate configured on the video source, accessed on the IO-thread.
106 float source_frame_rate_
;
108 DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter
);
111 } // namespace content
113 #endif // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_