1 /*
2  * Copyright 2009-2017 Alibaba Cloud All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <alibabacloud/oss/model/PutLiveChannelRequest.h>
19 #include <sstream>
20 #include "utils/Utils.h"
21 #include "ModelError.h"
22 #include "Const.h"
23 
24 
25 using namespace AlibabaCloud::OSS;
26 
PutLiveChannelRequest(const std::string & bucket,const std::string & channelName,const std::string & type)27 PutLiveChannelRequest::PutLiveChannelRequest(const std::string& bucket,
28     const std::string& channelName, const std::string& type)
29     :LiveChannelRequest(bucket, channelName),
30     channelType_(type),
31     playListName_("playlist.m3u8"),
32     status_(LiveChannelStatus::EnabledStatus),
33     fragDuration_(5),
34     fragCount_(3),
35     snapshot_(false)
36 {
37 }
38 
setChannelType(const std::string & channelType)39 void PutLiveChannelRequest::setChannelType(const std::string& channelType)
40 {
41     channelType_ = channelType;
42 }
43 
setDescripition(const std::string & description)44 void PutLiveChannelRequest::setDescripition(const std::string& description)
45 {
46     description_ = description;
47 }
48 
setDestBucket(const std::string & destBucket)49 void PutLiveChannelRequest::setDestBucket(const std::string& destBucket)
50 {
51     destBucket_ = destBucket;
52     snapshot_ = true;
53 }
54 
setFragCount(uint64_t fragCount)55 void PutLiveChannelRequest::setFragCount(uint64_t fragCount)
56 {
57     fragCount_ = fragCount;
58 }
59 
setFragDuration(uint64_t fragDuration)60 void PutLiveChannelRequest::setFragDuration(uint64_t fragDuration)
61 {
62     fragDuration_ = fragDuration;
63 }
64 
setStatus(LiveChannelStatus status)65 void PutLiveChannelRequest::setStatus(LiveChannelStatus status)
66 {
67     status_ = status;
68 }
69 
setInterval(uint64_t interval)70 void PutLiveChannelRequest::setInterval(uint64_t interval)
71 {
72     interval_ = interval;
73     snapshot_ = true;
74 }
75 
setNotifyTopic(const std::string & notifyTopic)76 void PutLiveChannelRequest::setNotifyTopic(const std::string& notifyTopic)
77 {
78     notifyTopic_ = notifyTopic;
79     snapshot_ = true;
80 }
81 
setRoleName(const std::string & roleName)82 void PutLiveChannelRequest::setRoleName(const std::string& roleName)
83 {
84     roleName_ = roleName;
85     snapshot_ = true;
86 }
87 
setPlayListName(const std::string & playListName)88 void PutLiveChannelRequest::setPlayListName(const std::string& playListName)
89 {
90     playListName_ = playListName;
91     std::size_t pos = playListName_.find_last_of('.');
92     if(pos != std::string::npos)
93     {
94         std::string suffixStr = playListName_.substr(pos);
95         if(ToLower(suffixStr.c_str()) == ".m3u8")
96         {
97             std::string prefixStr;
98             if(pos > 0)
99             {
100                 prefixStr = playListName_.substr(0, pos);
101                 playListName_ = prefixStr + ".m3u8";
102             }
103         }
104     }
105 }
106 
validate() const107 int PutLiveChannelRequest::validate() const
108 {
109     int ret = LiveChannelRequest::validate();
110 
111     if(ret)
112     {
113         return ret;
114     }
115 
116     if(!description_.empty() &&
117         description_.size() > MaxLiveChannelDescriptionLength)
118     {
119         return ARG_ERROR_LIVECHANNEL_BAD_DESCRIPTION_PARAM;
120     }
121 
122     if(status_ != LiveChannelStatus::EnabledStatus &&
123         status_ != LiveChannelStatus::DisabledStatus)
124     {
125         return ARG_ERROR_LIVECHANNEL_BAD_STATUS_PARAM;
126     }
127 
128     if(channelType_ != "HLS")
129     {
130         return ARG_ERROR_LIVECHANNEL_BAD_CHANNEL_TYPE_PARAM;
131     }
132 
133     if(fragDuration_ < MinLiveChannelFragDuration ||
134         fragDuration_ > MaxLiveChannelFragDuration)
135     {
136         return ARG_ERROR_LIVECHANNEL_BAD_FRAGDURATION_PARAM;
137     }
138 
139     if(fragCount_ < MinLiveChannelFragCount ||
140         fragCount_ > MaxLiveChannelFragCount)
141     {
142         return ARG_ERROR_LIVECHANNEL_BAD_FRAGCOUNT_PARAM;
143     }
144 
145     if(!IsValidPlayListName(playListName_))
146     {
147         return ARG_ERROR_LIVECHANNEL_BAD_PALYLIST_PARAM;
148     }
149 
150     if(snapshot_)
151     {
152         if(roleName_.empty() || notifyTopic_.empty() ||
153             destBucket_.empty() || !IsValidBucketName(bucket_) ||
154             interval_ < MinLiveChannelInterval ||
155             interval_ > MaxLiveChannelInterval)
156         {
157             return ARG_ERROR_LIVECHANNEL_BAD_SNAPSHOT_PARAM;
158         }
159     }
160     return 0;
161 }
162 
payload() const163 std::string PutLiveChannelRequest::payload() const
164 {
165     std::stringstream ss;
166     ss << "<LiveChannelConfiguration>" << std::endl;
167     ss << "  <Description>" << description_ << "</Description>" << std::endl;
168     ss << "  <Status>" << ToLiveChannelStatusName(status_) << "</Status>" << std::endl;
169     ss << "  <Target>" << std::endl;
170     ss << "    <Type>" << channelType_ << "</Type>" << std::endl;
171     ss << "    <FragDuration>" << std::to_string(fragDuration_) << "</FragDuration>" << std::endl;
172     ss << "    <FragCount>" << std::to_string(fragCount_) << "</FragCount>" << std::endl;
173     ss << "    <PlaylistName>" << playListName_ << "</PlaylistName>" << std::endl;
174     ss << "  </Target>" << std::endl;
175     if(snapshot_)
176     {
177         ss << "  <Snapshot>" << std::endl;
178         ss << "    <RoleName>" << roleName_ << "</RoleName>" << std::endl;
179         ss << "    <DestBucket>" << destBucket_ << "</DestBucket>" << std::endl;
180         ss << "    <NotifyTopic>" << notifyTopic_ << "</NotifyTopic>" << std::endl;
181         ss << "    <Interval>" << std::to_string(interval_) << "</Interval>" << std::endl;
182         ss << "  </Snapshot>" << std::endl;
183     }
184     ss << "</LiveChannelConfiguration>" << std::endl;
185     return ss.str();
186 }
187 
specialParameters() const188 ParameterCollection PutLiveChannelRequest::specialParameters() const
189 {
190     ParameterCollection collection;
191     collection["live"] = "";
192     return collection;
193 }
194