Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: media/filters/ffmpeg_demuxer.cc

Issue 2305923002: Ensure FFmpegDemuxer WeakPtrs are created on the right thread. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/filters/ffmpeg_demuxer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/filters/ffmpeg_demuxer.h" 5 #include "media/filters/ffmpeg_demuxer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <memory> 8 #include <memory>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 std::string FFmpegDemuxer::GetDisplayName() const { 853 std::string FFmpegDemuxer::GetDisplayName() const {
854 return "FFmpegDemuxer"; 854 return "FFmpegDemuxer";
855 } 855 }
856 856
857 void FFmpegDemuxer::Initialize(DemuxerHost* host, 857 void FFmpegDemuxer::Initialize(DemuxerHost* host,
858 const PipelineStatusCB& status_cb, 858 const PipelineStatusCB& status_cb,
859 bool enable_text_tracks) { 859 bool enable_text_tracks) {
860 DCHECK(task_runner_->BelongsToCurrentThread()); 860 DCHECK(task_runner_->BelongsToCurrentThread());
861 host_ = host; 861 host_ = host;
862 text_enabled_ = enable_text_tracks; 862 text_enabled_ = enable_text_tracks;
863 weak_this_ = weak_factory_.GetWeakPtr();
863 864
864 url_protocol_.reset(new BlockingUrlProtocol( 865 url_protocol_.reset(new BlockingUrlProtocol(
865 data_source_, 866 data_source_,
866 BindToCurrentLoop(base::Bind(&FFmpegDemuxer::OnDataSourceError, 867 BindToCurrentLoop(base::Bind(&FFmpegDemuxer::OnDataSourceError,
867 base::Unretained(this))))); 868 base::Unretained(this)))));
868 glue_.reset(new FFmpegGlue(url_protocol_.get())); 869 glue_.reset(new FFmpegGlue(url_protocol_.get()));
869 AVFormatContext* format_context = glue_->format_context(); 870 AVFormatContext* format_context = glue_->format_context();
870 871
871 // Disable ID3v1 tag reading to avoid costly seeks to end of file for data we 872 // Disable ID3v1 tag reading to avoid costly seeks to end of file for data we
872 // don't use. FFmpeg will only read ID3v1 tags if no other metadata is 873 // don't use. FFmpeg will only read ID3v1 tags if no other metadata is
(...skipping 27 matching lines...) Expand all
900 901
901 // Abort all outstanding reads. 902 // Abort all outstanding reads.
902 for (auto* stream : streams_) { 903 for (auto* stream : streams_) {
903 if (stream) 904 if (stream)
904 stream->Abort(); 905 stream->Abort();
905 } 906 }
906 907
907 // It's important to invalidate read/seek completion callbacks to avoid any 908 // It's important to invalidate read/seek completion callbacks to avoid any
908 // errors that occur because of the data source abort. 909 // errors that occur because of the data source abort.
909 weak_factory_.InvalidateWeakPtrs(); 910 weak_factory_.InvalidateWeakPtrs();
911 weak_this_ = weak_factory_.GetWeakPtr();
910 data_source_->Abort(); 912 data_source_->Abort();
911 913
912 // Aborting the read may cause EOF to be marked, undo this. 914 // Aborting the read may cause EOF to be marked, undo this.
913 blocking_thread_.task_runner()->PostTask( 915 blocking_thread_.task_runner()->PostTask(
914 FROM_HERE, base::Bind(&UnmarkEndOfStream, glue_->format_context())); 916 FROM_HERE, base::Bind(&UnmarkEndOfStream, glue_->format_context()));
915 pending_read_ = false; 917 pending_read_ = false;
916 918
917 // TODO(dalecurtis): We probably should report PIPELINE_ERROR_ABORT here 919 // TODO(dalecurtis): We probably should report PIPELINE_ERROR_ABORT here
918 // instead to avoid any preroll work that may be started upon return, but 920 // instead to avoid any preroll work that may be started upon return, but
919 // currently the PipelineImpl does not know how to handle this. 921 // currently the PipelineImpl does not know how to handle this.
(...skipping 25 matching lines...) Expand all
945 data_source_ = NULL; 947 data_source_ = NULL;
946 948
947 // Invalidate WeakPtrs on |task_runner_|, destruction may happen on another 949 // Invalidate WeakPtrs on |task_runner_|, destruction may happen on another
948 // thread. 950 // thread.
949 weak_factory_.InvalidateWeakPtrs(); 951 weak_factory_.InvalidateWeakPtrs();
950 } 952 }
951 953
952 void FFmpegDemuxer::StartWaitingForSeek(base::TimeDelta seek_time) {} 954 void FFmpegDemuxer::StartWaitingForSeek(base::TimeDelta seek_time) {}
953 955
954 void FFmpegDemuxer::CancelPendingSeek(base::TimeDelta seek_time) { 956 void FFmpegDemuxer::CancelPendingSeek(base::TimeDelta seek_time) {
957 if (!blocking_thread_.IsRunning())
watk 2016/09/02 00:21:20 Pretty sure this isn't thread safe?
DaleCurtis 2016/09/02 00:32:47 Why do you think not? IsRunning() is thread safe.
watk 2016/09/02 20:07:09 Ah, just because the docs in base::Thread say its
958 return;
959
955 if (task_runner_->BelongsToCurrentThread()) { 960 if (task_runner_->BelongsToCurrentThread()) {
956 AbortPendingReads(); 961 AbortPendingReads();
957 } else { 962 } else {
958 task_runner_->PostTask(FROM_HERE, 963 // Don't use GetWeakPtr() here since we are on the wrong thread.
959 base::Bind(&FFmpegDemuxer::AbortPendingReads, 964 task_runner_->PostTask(
960 weak_factory_.GetWeakPtr())); 965 FROM_HERE, base::Bind(&FFmpegDemuxer::AbortPendingReads, weak_this_));
watk 2016/09/02 00:21:20 Can you tell me why this pattern is safe? I see it
DaleCurtis 2016/09/02 00:32:47 This particular pattern is wrong since we invalida
watk 2016/09/02 20:07:10 Oh ok, I was confused, thanks :)
961 } 966 }
962 } 967 }
963 968
964 void FFmpegDemuxer::Seek(base::TimeDelta time, const PipelineStatusCB& cb) { 969 void FFmpegDemuxer::Seek(base::TimeDelta time, const PipelineStatusCB& cb) {
965 DCHECK(task_runner_->BelongsToCurrentThread()); 970 DCHECK(task_runner_->BelongsToCurrentThread());
966 CHECK(pending_seek_cb_.is_null()); 971 CHECK(pending_seek_cb_.is_null());
967 972
968 // FFmpeg requires seeks to be adjusted according to the lowest starting time. 973 // FFmpeg requires seeks to be adjusted according to the lowest starting time.
969 // Since EnqueuePacket() rebased negative timestamps by the start time, we 974 // Since EnqueuePacket() rebased negative timestamps by the start time, we
970 // must correct the shift here. 975 // must correct the shift here.
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
1752 1757
1753 void FFmpegDemuxer::SetLiveness(DemuxerStream::Liveness liveness) { 1758 void FFmpegDemuxer::SetLiveness(DemuxerStream::Liveness liveness) {
1754 DCHECK(task_runner_->BelongsToCurrentThread()); 1759 DCHECK(task_runner_->BelongsToCurrentThread());
1755 for (auto* stream : streams_) { 1760 for (auto* stream : streams_) {
1756 if (stream) 1761 if (stream)
1757 stream->SetLiveness(liveness); 1762 stream->SetLiveness(liveness);
1758 } 1763 }
1759 } 1764 }
1760 1765
1761 } // namespace media 1766 } // namespace media
OLDNEW
« no previous file with comments | « media/filters/ffmpeg_demuxer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698