OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/media/media_internals.h" | 5 #include "chrome/browser/media/media_internals.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "chrome/browser/media/media_internals_observer.h" | 10 #include "chrome/browser/media/media_internals_observer.h" |
11 #include "content/browser/browser_thread.h" | 11 #include "content/browser/browser_thread.h" |
12 #include "content/browser/webui/web_ui.h" | 12 #include "content/browser/webui/web_ui.h" |
13 #include "media/base/media_log.h" | |
13 #include "media/base/media_log_event.h" | 14 #include "media/base/media_log_event.h" |
14 | 15 |
15 // The names of the javascript functions to call with updates. | 16 // The names of the javascript functions to call with updates. |
17 static const char kAudioUpdateFunction[] = "media.addAudioStream"; | |
16 static const char kDeleteItemFunction[] = "media.onItemDeleted"; | 18 static const char kDeleteItemFunction[] = "media.onItemDeleted"; |
17 static const char kAudioUpdateFunction[] = "media.addAudioStream"; | 19 static const char kMediaEventFunction[] = "media.onMediaEvent"; |
20 static const char kMediaPlayerUpdateFunction[] = "media.addMediaPlayer"; | |
18 static const char kSendEverythingFunction[] = "media.onReceiveEverything"; | 21 static const char kSendEverythingFunction[] = "media.onReceiveEverything"; |
19 | 22 |
20 MediaInternals::~MediaInternals() {} | 23 MediaInternals::~MediaInternals() {} |
21 | 24 |
22 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { | 25 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { |
23 DCHECK(CalledOnValidThread()); | 26 DCHECK(CalledOnValidThread()); |
24 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 27 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
25 host, stream_id); | 28 host, stream_id); |
26 DeleteItem(stream); | 29 DeleteItem(stream); |
27 } | 30 } |
(...skipping 15 matching lines...) Expand all Loading... | |
43 void MediaInternals::OnSetAudioStreamVolume( | 46 void MediaInternals::OnSetAudioStreamVolume( |
44 void* host, int stream_id, double volume) { | 47 void* host, int stream_id, double volume) { |
45 DCHECK(CalledOnValidThread()); | 48 DCHECK(CalledOnValidThread()); |
46 UpdateAudioStream(host, stream_id, | 49 UpdateAudioStream(host, stream_id, |
47 "volume", Value::CreateDoubleValue(volume)); | 50 "volume", Value::CreateDoubleValue(volume)); |
48 } | 51 } |
49 | 52 |
50 void MediaInternals::OnMediaEvent( | 53 void MediaInternals::OnMediaEvent( |
51 int render_process_id, const media::MediaLogEvent& event) { | 54 int render_process_id, const media::MediaLogEvent& event) { |
52 DCHECK(CalledOnValidThread()); | 55 DCHECK(CalledOnValidThread()); |
53 // TODO(scottfr): Handle |event|. Record status information in data_ and pass | 56 std::string source = base::StringPrintf("media_players.%d.%d", |
54 // |event| along to observers. | 57 render_process_id, event.id); |
58 | |
59 // Notify observers that |event| has occured. | |
60 DictionaryValue dict; | |
61 dict.SetString("source", source); | |
62 dict.SetString("type", media::MediaLog::EventTypeToString(event.type)); | |
63 dict.SetDouble("time", event.time.ToDoubleT()); | |
64 dict.Set("params", event.params.DeepCopy()); | |
65 SendUpdate(kMediaEventFunction, &dict); | |
66 | |
67 // Handle parameterless events specially, but otherwise just add parameters | |
68 // to this.details_ and send an updated property list to any obsevers. | |
scherkus (not reviewing)
2011/08/10 16:00:37
ack should have clarified my comment
I don't see
Scott Franklin
2011/08/10 22:26:51
oh wow, s/details_/data_/. I imagine it would be h
| |
69 switch (event.type) { | |
70 case media::MediaLogEvent::WEBMEDIAPLAYER_CREATED: | |
71 SetItemProperty(kMediaPlayerUpdateFunction, source, "player_state", | |
72 base::Value::CreateStringValue("created")); | |
73 break; | |
74 case media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED: | |
75 SetItemProperty(kMediaPlayerUpdateFunction, source, "player_state", | |
76 base::Value::CreateStringValue("destroyed")); | |
77 break; | |
78 case media::MediaLogEvent::PLAY: | |
79 SetItemProperty(kMediaPlayerUpdateFunction, source, "player_state", | |
80 base::Value::CreateStringValue("playing")); | |
81 break; | |
82 case media::MediaLogEvent::PAUSE: | |
83 SetItemProperty(kMediaPlayerUpdateFunction, source, "player_state", | |
84 base::Value::CreateStringValue("paused")); | |
85 break; | |
86 default: | |
87 SetItemProperties(kMediaPlayerUpdateFunction, source, | |
88 event.params.DeepCopy()); | |
89 break; | |
90 } | |
55 } | 91 } |
56 | 92 |
57 void MediaInternals::AddObserver(MediaInternalsObserver* observer) { | 93 void MediaInternals::AddObserver(MediaInternalsObserver* observer) { |
58 DCHECK(CalledOnValidThread()); | 94 DCHECK(CalledOnValidThread()); |
59 observers_.AddObserver(observer); | 95 observers_.AddObserver(observer); |
60 } | 96 } |
61 | 97 |
62 void MediaInternals::RemoveObserver(MediaInternalsObserver* observer) { | 98 void MediaInternals::RemoveObserver(MediaInternalsObserver* observer) { |
63 DCHECK(CalledOnValidThread()); | 99 DCHECK(CalledOnValidThread()); |
64 observers_.RemoveObserver(observer); | 100 observers_.RemoveObserver(observer); |
65 } | 101 } |
66 | 102 |
67 void MediaInternals::SendEverything() { | 103 void MediaInternals::SendEverything() { |
68 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
69 SendUpdate(kSendEverythingFunction, &data_); | 105 SendUpdate(kSendEverythingFunction, &data_); |
70 } | 106 } |
71 | 107 |
72 MediaInternals::MediaInternals() {} | 108 MediaInternals::MediaInternals() {} |
73 | 109 |
74 void MediaInternals::UpdateAudioStream( | 110 void MediaInternals::UpdateAudioStream( |
75 void* host, int stream_id, const std::string& property, Value* value) { | 111 void* host, int stream_id, const std::string& property, Value* value) { |
76 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 112 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
77 host, stream_id); | 113 host, stream_id); |
78 UpdateItem(kAudioUpdateFunction, stream, property, value); | 114 SetItemProperty(kAudioUpdateFunction, stream, property, value); |
79 } | 115 } |
80 | 116 |
81 void MediaInternals::DeleteItem(const std::string& item) { | 117 void MediaInternals::DeleteItem(const std::string& item) { |
82 data_.Remove(item, NULL); | 118 data_.Remove(item, NULL); |
83 scoped_ptr<Value> value(Value::CreateStringValue(item)); | 119 scoped_ptr<Value> value(Value::CreateStringValue(item)); |
84 SendUpdate(kDeleteItemFunction, value.get()); | 120 SendUpdate(kDeleteItemFunction, value.get()); |
85 } | 121 } |
86 | 122 |
87 void MediaInternals::UpdateItem( | 123 void MediaInternals::SetItemProperty( |
88 const std::string& update_fn, const std::string& id, | 124 const std::string& update_fn, const std::string& id, |
89 const std::string& property, Value* value) { | 125 const std::string& property, Value* value) { |
126 DictionaryValue* properties = new DictionaryValue(); | |
127 properties->Set(property, value); | |
128 SetItemProperties(update_fn, id, properties); | |
129 } | |
130 | |
131 void MediaInternals::SetItemProperties( | |
132 const std::string& update_fn, const std::string& id, | |
133 DictionaryValue* properties) { | |
90 DictionaryValue* item_properties; | 134 DictionaryValue* item_properties; |
91 if (!data_.GetDictionary(id, &item_properties)) { | 135 if (!data_.GetDictionary(id, &item_properties)) { |
92 item_properties = new DictionaryValue(); | 136 item_properties = new DictionaryValue(); |
93 data_.Set(id, item_properties); | 137 data_.Set(id, item_properties); |
94 item_properties->SetString("id", id); | 138 item_properties->SetString("id", id); |
95 } | 139 } |
96 item_properties->Set(property, value); | 140 item_properties->MergeDictionary(properties); |
97 SendUpdate(update_fn, item_properties); | 141 SendUpdate(update_fn, item_properties); |
98 } | 142 } |
99 | 143 |
100 void MediaInternals::SendUpdate(const std::string& function, Value* value) { | 144 void MediaInternals::SendUpdate(const std::string& function, Value* value) { |
101 // Only bother serializing the update to JSON if someone is watching. | 145 // Only bother serializing the update to JSON if someone is watching. |
102 if (observers_.size()) { | 146 if (observers_.size()) { |
103 std::vector<const Value*> args; | 147 std::vector<const Value*> args; |
104 args.push_back(value); | 148 args.push_back(value); |
105 string16 update = WebUI::GetJavascriptCall(function, args); | 149 string16 update = WebUI::GetJavascriptCall(function, args); |
106 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); | 150 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); |
107 } | 151 } |
108 } | 152 } |
OLD | NEW |