OLD | NEW |
---|---|
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/base/message_loop_factory.h" | 5 #include "media/base/message_loop_factory.h" |
6 | 6 |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 | 8 |
9 namespace media { | 9 namespace media { |
10 | 10 |
11 struct MessageLoopTypeNamePair { | |
12 MessageLoopFactory::MessageLoopType type; | |
13 const char* name; | |
14 }; | |
15 | |
16 static const MessageLoopTypeNamePair message_loop_type_to_name_mapping[] = { | |
scherkus (not reviewing)
2012/08/09 17:13:24
this should be kMessageLoopXxx
xhwang
2012/08/10 01:04:28
Done.
| |
17 { MessageLoopFactory::AUDIO_DECODER, "AudioDecoderThread" }, | |
18 { MessageLoopFactory::VIDEO_DECODER, "VideoDecoderThread" }, | |
19 { MessageLoopFactory::PIPELINE, "PipelineThread" } | |
20 }; | |
21 | |
11 MessageLoopFactory::MessageLoopFactory() {} | 22 MessageLoopFactory::MessageLoopFactory() {} |
12 | 23 |
13 MessageLoopFactory::~MessageLoopFactory() { | 24 MessageLoopFactory::~MessageLoopFactory() { |
14 for (ThreadList::reverse_iterator it = threads_.rbegin(); | 25 for (ThreadList::reverse_iterator it = threads_.rbegin(); |
15 it != threads_.rend(); ++it) { | 26 it != threads_.rend(); ++it) { |
16 base::Thread* thread = it->second; | 27 base::Thread* thread = it->second; |
17 thread->Stop(); | 28 thread->Stop(); |
18 delete thread; | 29 delete thread; |
19 } | 30 } |
20 threads_.clear(); | 31 threads_.clear(); |
21 } | 32 } |
22 | 33 |
23 MessageLoop* MessageLoopFactory::GetMessageLoop(const std::string& name) { | 34 MessageLoop* MessageLoopFactory::GetMessageLoop(MessageLoopType type) { |
24 return GetThread(name)->message_loop(); | 35 return GetThread(type)->message_loop(); |
25 } | 36 } |
26 | 37 |
27 scoped_refptr<base::MessageLoopProxy> | 38 scoped_refptr<base::MessageLoopProxy> |
28 MessageLoopFactory::GetMessageLoopProxy(const std::string& name) { | 39 MessageLoopFactory::GetMessageLoopProxy(MessageLoopType type) { |
29 return GetThread(name)->message_loop_proxy(); | 40 return GetThread(type)->message_loop_proxy(); |
30 } | 41 } |
31 | 42 |
32 base::Thread* MessageLoopFactory::GetThread(const std::string& name) { | 43 base::Thread* MessageLoopFactory::GetThread(MessageLoopType type) { |
33 DCHECK(!name.empty()); | |
34 | |
35 base::AutoLock auto_lock(lock_); | 44 base::AutoLock auto_lock(lock_); |
36 for (ThreadList::iterator it = threads_.begin(); it != threads_.end(); ++it) { | 45 for (ThreadList::iterator it = threads_.begin(); it != threads_.end(); ++it) { |
37 if (it->first == name) | 46 if (it->first == type) |
38 return it->second; | 47 return it->second; |
39 } | 48 } |
40 | 49 |
50 std::string name; | |
51 for (size_t i = 0; i < arraysize(message_loop_type_to_name_mapping); ++i) { | |
52 if (message_loop_type_to_name_mapping[i].type == type) | |
53 name = message_loop_type_to_name_mapping[i].name; | |
54 } | |
55 DCHECK(!name.empty()); | |
56 | |
41 base::Thread* thread = new base::Thread(name.c_str()); | 57 base::Thread* thread = new base::Thread(name.c_str()); |
42 CHECK(thread->Start()) << "Failed to start thread: " << name; | 58 CHECK(thread->Start()) << "Failed to start thread: " << name; |
43 threads_.push_back(std::make_pair(name, thread)); | 59 threads_.push_back(std::make_pair(type, thread)); |
44 return thread; | 60 return thread; |
45 } | 61 } |
46 | 62 |
47 } // namespace media | 63 } // namespace media |
OLD | NEW |