Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/video_track_recorder.h" | 5 #include "content/renderer/media/video_track_recorder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 911 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 922 } | 922 } |
| 923 #endif //#if BUILDFLAG(RTC_USE_H264) | 923 #endif //#if BUILDFLAG(RTC_USE_H264) |
| 924 | 924 |
| 925 } // anonymous namespace | 925 } // anonymous namespace |
| 926 | 926 |
| 927 VideoTrackRecorder::VideoTrackRecorder( | 927 VideoTrackRecorder::VideoTrackRecorder( |
| 928 CodecId codec, | 928 CodecId codec, |
| 929 const blink::WebMediaStreamTrack& track, | 929 const blink::WebMediaStreamTrack& track, |
| 930 const OnEncodedVideoCB& on_encoded_video_callback, | 930 const OnEncodedVideoCB& on_encoded_video_callback, |
| 931 int32_t bits_per_second) | 931 int32_t bits_per_second) |
| 932 : track_(track) { | 932 : main_task_runner_(base::MessageLoop::current()->task_runner()), |
| 933 status_(NOT_INITIALIZED), | |
| 934 track_(track), | |
| 935 codec_(codec), | |
| 936 on_encoded_video_callback_(on_encoded_video_callback), | |
| 937 bits_per_second_(bits_per_second), | |
| 938 paused_(false), | |
| 939 weak_ptr_factory_(this) { | |
| 933 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 940 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 934 DCHECK(!track_.isNull()); | 941 DCHECK(!track_.isNull()); |
| 935 DCHECK(track_.getExtraData()); | 942 DCHECK(track_.getExtraData()); |
| 936 | 943 |
| 937 const auto& vea_supported_profile = CodecIdToVEAProfile(codec); | 944 // It is safe to use Unretained, because the class gets disconnected from |
| 938 // TODO(emircan): Prioritize software based encoders in lower resolutions. | 945 // track in dtor. |
| 939 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN) { | |
| 940 encoder_ = new VEAEncoder(on_encoded_video_callback, bits_per_second, | |
| 941 vea_supported_profile); | |
| 942 } else { | |
| 943 switch (codec) { | |
| 944 #if BUILDFLAG(RTC_USE_H264) | |
| 945 case CodecId::H264: | |
| 946 encoder_ = new H264Encoder(on_encoded_video_callback, bits_per_second); | |
| 947 break; | |
| 948 #endif | |
| 949 case CodecId::VP8: | |
| 950 case CodecId::VP9: | |
| 951 encoder_ = new VpxEncoder(codec == CodecId::VP9, | |
| 952 on_encoded_video_callback, bits_per_second); | |
| 953 break; | |
| 954 default: | |
| 955 NOTREACHED() << "Unsupported codec"; | |
| 956 } | |
| 957 } | |
| 958 | |
| 959 // StartFrameEncode() will be called on Render IO thread. | |
| 960 MediaStreamVideoSink::ConnectToTrack( | 946 MediaStreamVideoSink::ConnectToTrack( |
| 961 track_, | 947 track_, |
| 962 base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, encoder_), | 948 base::Bind(&VideoTrackRecorder::StartFrameEncode, base::Unretained(this)), |
|
mcasas
2016/05/25 16:42:47
Why not use weak_ptr_factory_.GetWeakPtr() here?
emircan
2016/05/27 04:27:20
Done.
| |
| 963 false); | 949 false); |
| 964 } | 950 } |
| 965 | 951 |
| 966 VideoTrackRecorder::~VideoTrackRecorder() { | 952 VideoTrackRecorder::~VideoTrackRecorder() { |
| 967 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 953 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 968 MediaStreamVideoSink::DisconnectFromTrack(); | 954 MediaStreamVideoSink::DisconnectFromTrack(); |
| 969 track_.reset(); | 955 track_.reset(); |
| 970 } | 956 } |
| 971 | 957 |
| 958 void VideoTrackRecorder::StartFrameEncode( | |
| 959 const scoped_refptr<VideoFrame>& frame, | |
| 960 base::TimeTicks capture_timestamp) { | |
| 961 if (!origin_task_runner_.get()) | |
| 962 origin_task_runner_ = base::MessageLoop::current()->task_runner(); | |
| 963 DCHECK(origin_task_runner_->BelongsToCurrentThread()); | |
| 964 | |
| 965 if (status_ == INITIALIZED) { | |
| 966 encoder_->StartFrameEncode(frame, capture_timestamp); | |
| 967 return; | |
| 968 } | |
| 969 | |
| 970 if (status_ == NOT_INITIALIZED) { | |
| 971 main_task_runner_->PostTask( | |
| 972 FROM_HERE, | |
| 973 base::Bind(&VideoTrackRecorder::InitializeEncoder, | |
| 974 weak_ptr_factory_.GetWeakPtr(), frame, capture_timestamp)); | |
|
mcasas
2016/05/25 16:42:47
Actually, it might be easier than all this thread
emircan
2016/05/27 04:27:20
VEAEncoder needs to access RenderThreadImpl method
| |
| 975 status_ = INITIALIZING; | |
| 976 } | |
| 977 } | |
| 978 | |
| 972 void VideoTrackRecorder::Pause() { | 979 void VideoTrackRecorder::Pause() { |
| 973 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 980 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 974 DCHECK(encoder_); | 981 if (status_ == INITIALIZED) |
| 975 encoder_->SetPaused(true); | 982 encoder_->SetPaused(true); |
| 983 else | |
| 984 paused_ = true; | |
| 976 } | 985 } |
| 977 | 986 |
| 978 void VideoTrackRecorder::Resume() { | 987 void VideoTrackRecorder::Resume() { |
| 979 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | 988 DCHECK(main_render_thread_checker_.CalledOnValidThread()); |
| 980 DCHECK(encoder_); | 989 if (status_ == INITIALIZED) |
| 981 encoder_->SetPaused(false); | 990 encoder_->SetPaused(false); |
| 991 else | |
| 992 paused_ = false; | |
| 982 } | 993 } |
| 983 | 994 |
| 984 void VideoTrackRecorder::OnVideoFrameForTesting( | 995 void VideoTrackRecorder::OnVideoFrameForTesting( |
| 985 const scoped_refptr<media::VideoFrame>& frame, | 996 const scoped_refptr<media::VideoFrame>& frame, |
| 986 base::TimeTicks timestamp) { | 997 base::TimeTicks timestamp) { |
| 987 encoder_->StartFrameEncode(frame, timestamp); | 998 if (!origin_task_runner_.get()) |
| 999 origin_task_runner_ = base::MessageLoop::current()->task_runner(); | |
| 1000 DCHECK(origin_task_runner_->BelongsToCurrentThread()); | |
| 1001 | |
| 1002 if (status_ == INITIALIZED) { | |
| 1003 encoder_->StartFrameEncode(frame, timestamp); | |
| 1004 } | |
| 1005 | |
| 1006 if (status_ == NOT_INITIALIZED) { | |
| 1007 main_task_runner_->PostTask( | |
| 1008 FROM_HERE, | |
| 1009 base::Bind(&VideoTrackRecorder::InitializeEncoder, | |
| 1010 weak_ptr_factory_.GetWeakPtr(), frame, timestamp)); | |
| 1011 status_ = INITIALIZING; | |
| 1012 } | |
| 1013 } | |
| 1014 | |
| 1015 void VideoTrackRecorder::InitializeEncoder( | |
| 1016 const scoped_refptr<VideoFrame>& frame, | |
| 1017 base::TimeTicks capture_timestamp) { | |
| 1018 DVLOG(3) << __FUNCTION__; | |
| 1019 DCHECK(main_render_thread_checker_.CalledOnValidThread()); | |
| 1020 | |
| 1021 const gfx::Size& input_size = frame->visible_rect().size(); | |
| 1022 const auto& vea_supported_profile = CodecIdToVEAProfile(codec_); | |
| 1023 // Prioritizing software based encoders in lower resolutions. | |
| 1024 if (vea_supported_profile != media::VIDEO_CODEC_PROFILE_UNKNOWN && | |
| 1025 input_size.width() >= kVEAEncoderMinResolutionWidth && | |
| 1026 input_size.height() >= kVEAEncoderMinResolutionHeight) { | |
| 1027 encoder_ = new VEAEncoder(on_encoded_video_callback_, bits_per_second_, | |
| 1028 vea_supported_profile); | |
| 1029 } | |
| 1030 else { | |
| 1031 switch (codec_) { | |
| 1032 #if BUILDFLAG(RTC_USE_H264) | |
| 1033 case CodecId::H264: | |
| 1034 encoder_ = | |
| 1035 new H264Encoder(on_encoded_video_callback_, bits_per_second_); | |
| 1036 break; | |
| 1037 #endif | |
| 1038 case CodecId::VP8: | |
| 1039 case CodecId::VP9: | |
| 1040 encoder_ = new VpxEncoder(codec_ == CodecId::VP9, | |
| 1041 on_encoded_video_callback_, bits_per_second_); | |
| 1042 break; | |
| 1043 default: | |
| 1044 NOTREACHED() << "Unsupported codec"; | |
| 1045 } | |
| 1046 } | |
| 1047 encoder_->SetPaused(paused_); | |
| 1048 origin_task_runner_->PostTask( | |
| 1049 FROM_HERE, base::Bind(&VideoTrackRecorder::Encoder::StartFrameEncode, | |
| 1050 encoder_, frame, capture_timestamp)); | |
| 1051 status_ = INITIALIZED; | |
| 988 } | 1052 } |
| 989 | 1053 |
| 990 } // namespace content | 1054 } // namespace content |
| OLD | NEW |