Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / video_track_adapter.h
blob56aaa6a839711bea9032bf6f3bb5b00eb09de5c3
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_
8 #include <vector>
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"
16 namespace content {
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
22 // aspect ratio.
23 // Video frames are delivered to a track using a VideoCaptureDeliverFrameCB on
24 // the IO-thread.
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> {
29 public:
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 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(
52 const scoped_refptr<media::VideoFrame>& frame,
53 const base::TimeTicks& estimated_capture_time);
55 const scoped_refptr<base::MessageLoopProxy>& io_message_loop() {
56 DCHECK(thread_checker_.CalledOnValidThread());
57 return io_message_loop_;
60 // Start monitor that frames are delivered to this object. I.E, that
61 // |DeliverFrameOnIO| is called with a frame rate of |source_frame_rate|.
62 // |on_muted_callback| is triggered on the main render thread.
63 void StartFrameMonitoring(double source_frame_rate,
64 const OnMutedCallback& on_muted_callback);
65 void StopFrameMonitoring();
67 private:
68 virtual ~VideoTrackAdapter();
69 friend class base::RefCountedThreadSafe<VideoTrackAdapter>;
71 void AddTrackOnIO(
72 const MediaStreamVideoTrack* track,
73 VideoCaptureDeliverFrameCB frame_callback,
74 const gfx::Size& max_frame_size,
75 double min_aspect_ratio,
76 double max_aspect_ratio,
77 double max_frame_rate);
78 void RemoveTrackOnIO(const MediaStreamVideoTrack* track);
80 void StartFrameMonitoringOnIO(
81 const OnMutedCallback& on_muted_state_callback,
82 double source_frame_rate);
83 void StopFrameMonitoringOnIO();
85 // Compare |frame_counter_snapshot| with the current |frame_counter_|, and
86 // inform of the situation (muted, not muted) via |set_muted_state_callback|.
87 void CheckFramesReceivedOnIO(const OnMutedCallback& set_muted_state_callback,
88 uint64 old_frame_counter_snapshot);
90 // |thread_checker_| is bound to the main render thread.
91 base::ThreadChecker thread_checker_;
93 const scoped_refptr<base::MessageLoopProxy> io_message_loop_;
95 // |renderer_task_runner_| is used to ensure that
96 // VideoCaptureDeliverFrameCB is released on the main render thread.
97 const scoped_refptr<base::SingleThreadTaskRunner> renderer_task_runner_;
99 // VideoFrameResolutionAdapter is an inner class that is created on the main
100 // render thread but operates on the IO-thread. It does the resolution
101 // adaptation and delivers frames to all registered tracks on the IO-thread.
102 class VideoFrameResolutionAdapter;
103 typedef std::vector<scoped_refptr<VideoFrameResolutionAdapter> >
104 FrameAdapters;
105 FrameAdapters adapters_;
107 // Set to true if frame monitoring has been started. It is only accessed on
108 // the IO-thread.
109 bool monitoring_frame_rate_;
111 // Keeps track of it frames have been received. It is only accessed on the
112 // IO-thread.
113 bool muted_state_;
115 // Running frame counter, accessed on the IO-thread.
116 uint64 frame_counter_;
118 // Frame rate configured on the video source, accessed on the IO-thread.
119 float source_frame_rate_;
121 DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter);
124 } // namespace content
126 #endif // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_