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 explicit VideoTrackAdapter(
31 const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
33 // Register |track| to receive video frames in |frame_callback| with
34 // a resolution within the boundaries of the arguments.
35 // Must be called on the main render thread. |frame_callback| is guaranteed to
36 // be released on the main render thread.
37 void AddTrack(const MediaStreamVideoTrack* track,
38 VideoCaptureDeliverFrameCB frame_callback,
39 int max_width, int max_height,
40 double min_aspect_ratio,
41 double max_aspect_ratio);
42 void RemoveTrack(const MediaStreamVideoTrack* track);
44 // Delivers |frame| to all tracks that have registered a callback.
45 // Must be called on the IO-thread.
46 void DeliverFrameOnIO(
47 const scoped_refptr<media::VideoFrame>& frame,
48 const media::VideoCaptureFormat& format,
49 const base::TimeTicks& estimated_capture_time);
51 const scoped_refptr<base::MessageLoopProxy>& io_message_loop() {
52 DCHECK(thread_checker_.CalledOnValidThread());
53 return io_message_loop_;
57 virtual ~VideoTrackAdapter();
58 friend class base::RefCountedThreadSafe<VideoTrackAdapter>;
61 const MediaStreamVideoTrack* track,
62 VideoCaptureDeliverFrameCB frame_callback,
63 int max_width, int max_height,
64 double min_aspect_ratio,
65 double max_aspect_ratio);
66 void RemoveTrackOnIO(const MediaStreamVideoTrack* track);
68 // |thread_checker_| is bound to the main render thread.
69 base::ThreadChecker thread_checker_;
71 scoped_refptr<base::MessageLoopProxy> io_message_loop_;
73 // |renderer_task_runner_| is used to ensure that
74 // VideoCaptureDeliverFrameCB is released on the main render thread.
75 scoped_refptr<base::SingleThreadTaskRunner> renderer_task_runner_;
77 // VideoFrameResolutionAdapter is an inner class that is created on the main
78 // render thread but operates on the IO-thread. It does the resolution
79 // adaptation and delivers frames to all registered tracks on the IO-thread.
80 class VideoFrameResolutionAdapter;
81 typedef std::vector<scoped_refptr<VideoFrameResolutionAdapter> >
83 FrameAdapters adapters_;
85 DISALLOW_COPY_AND_ASSIGN(VideoTrackAdapter);
88 } // namespace content
90 #endif // CONTENT_RENDERER_MEDIA_VIDEO_TRACK_ADAPTER_H_