Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / common / media / media_stream_options.cc
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
3 // found in the LICENSE file.
4
5 #include "content/common/media/media_stream_options.h"
6
7 #include "base/logging.h"
8
9 namespace content {
10
11 const char kMediaStreamSource[] = "chromeMediaSource";
12 const char kMediaStreamSourceId[] = "chromeMediaSourceId";
13 const char kMediaStreamSourceInfoId[] = "sourceId";
14 const char kMediaStreamSourceTab[] = "tab";
15 const char kMediaStreamSourceScreen[] = "screen";
16 const char kMediaStreamSourceDesktop[] = "desktop";
17 const char kMediaStreamSourceSystem[] = "system";
18 const char kMediaStreamRenderToAssociatedSink[] =
19     "chromeRenderToAssociatedSink";
20 // The prefix of this constant is 'goog' to match with other getUserMedia
21 // constraints for audio.
22 const char kMediaStreamAudioDucking[] = "googDucking";
23
24 namespace {
25
26 bool GetFirstConstraintByName(const StreamOptions::Constraints& constraints,
27                               const std::string& name,
28                               std::string* value) {
29   for (StreamOptions::Constraints::const_iterator it = constraints.begin();
30       it != constraints.end(); ++it ) {
31     if (it->name == name) {
32       *value = it->value;
33       return true;
34     }
35   }
36   return false;
37 }
38
39 bool GetFirstConstraintByName(const StreamOptions::Constraints& mandatory,
40                               const StreamOptions::Constraints& optional,
41                               const std::string& name,
42                               std::string* value,
43                               bool* is_mandatory) {
44   if (GetFirstConstraintByName(mandatory, name, value)) {
45     if (is_mandatory)
46       *is_mandatory = true;
47     return true;
48   }
49   if (is_mandatory)
50     *is_mandatory = false;
51   return GetFirstConstraintByName(optional, name, value);
52 }
53
54 } // namespace
55
56 StreamOptions::StreamOptions()
57     : audio_requested(false),
58       video_requested(false) {}
59
60 StreamOptions::StreamOptions(bool request_audio, bool request_video)
61     :  audio_requested(request_audio), video_requested(request_video) {
62 }
63
64 StreamOptions::~StreamOptions() {}
65
66 StreamOptions::Constraint::Constraint() {}
67
68 StreamOptions::Constraint::Constraint(const std::string& name,
69                                       const std::string& value)
70     : name(name), value(value) {
71 }
72
73 bool StreamOptions::GetFirstAudioConstraintByName(const std::string& name,
74                                                   std::string* value,
75                                                   bool* is_mandatory) const {
76   return GetFirstConstraintByName(mandatory_audio, optional_audio, name, value,
77                                   is_mandatory);
78 }
79
80 bool StreamOptions::GetFirstVideoConstraintByName(const std::string& name,
81                                                   std::string* value,
82                                                   bool* is_mandatory) const {
83   return GetFirstConstraintByName(mandatory_video, optional_video, name, value,
84                                   is_mandatory);
85 }
86
87 // static
88 void StreamOptions::GetConstraintsByName(
89     const StreamOptions::Constraints& constraints,
90     const std::string& name,
91     std::vector<std::string>* values) {
92   for (StreamOptions::Constraints::const_iterator it = constraints.begin();
93       it != constraints.end(); ++it ) {
94     if (it->name == name)
95       values->push_back(it->value);
96   }
97 }
98
99 // static
100 const int StreamDeviceInfo::kNoId = -1;
101
102 StreamDeviceInfo::StreamDeviceInfo()
103     : session_id(kNoId) {}
104
105 StreamDeviceInfo::StreamDeviceInfo(MediaStreamType service_param,
106                                    const std::string& name_param,
107                                    const std::string& device_param)
108     : device(service_param, device_param, name_param),
109       session_id(kNoId) {
110 }
111
112 StreamDeviceInfo::StreamDeviceInfo(MediaStreamType service_param,
113                                    const std::string& name_param,
114                                    const std::string& device_param,
115                                    int sample_rate,
116                                    int channel_layout,
117                                    int frames_per_buffer)
118     : device(service_param, device_param, name_param, sample_rate,
119              channel_layout, frames_per_buffer),
120       session_id(kNoId) {
121 }
122
123 // static
124 bool StreamDeviceInfo::IsEqual(const StreamDeviceInfo& first,
125                                const StreamDeviceInfo& second) {
126   return first.device.IsEqual(second.device) &&
127       first.session_id == second.session_id;
128 }
129
130 }  // namespace content