root/trunk/whisperer/stream_poster.h

Revision 7, 5.3 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Mihai Ianculescu
31
32 #ifndef __WHISPERER_STREAM_POSTER_H__
33 #define __WHISPERER_STREAM_POSTER_H__
34
35 #include <whisperlib/net/http/http_client_protocol.h>
36 #include "stream_request.h"
37
38 class StreamManager;
39
40 class StreamPoster {
41  public:
42   StreamPoster(StreamManager* manager,
43       const http::ClientParams* http_client_params);
44   virtual ~StreamPoster();
45
46   bool Start(const URL& url, int retry_timeout,
47              const string& user, const string& password,
48              Closure* done_callback);
49   void Stop();
50
51  public:
52   void Run();
53   void Done();
54
55   const URL& url() const {
56     return url_;
57   }
58   const string& user() const {
59     return user_;
60   }
61   const string& pass() const {
62     return pass_;
63   }
64   int retry_timeout() const {
65     return retry_timeout_;
66   }
67
68   // The poster stream request.
69   class PosterRequest : public StreamRequest {
70    public:
71     PosterRequest(const URL& url,
72                   const net::HostPort& server,
73                   const string& user,
74                   const string& pass,
75                   const http::ClientParams* http_client_params,
76                   Closure* done_callback)
77         : url_(url),
78           server_(server),
79           user_(user),
80           pass_(pass),
81           http_client_params_(http_client_params),
82           done_callback_(done_callback),
83           request_(NULL),
84           protocol_(NULL) {
85     }
86     virtual ~PosterRequest() {
87       if (done_callback_) {
88         done_callback_->Run();
89       }
90
91       delete protocol_;
92       delete request_;
93     }
94
95     void Stop() {
96       if (reader_) {
97         reader_->Close();
98       }
99       if (protocol_) {
100         if (protocol_->connection()) {
101           protocol_->connection()->ForceClose();
102         }
103       }
104     }
105
106    protected:
107     // called to boot the request after everything was set up
108     virtual void Start();
109     // called if the set up of the request fails
110     virtual void Fail(http::HttpReturnCode result);
111     // nothing to authenticate here..
112     virtual bool Authenticate(net::UserAuthenticator* auth) {
113       return true;
114     }
115
116     // called from the file reader
117     virtual void ReaderProcess(io::MemoryStream* inbuf);
118     virtual void ReaderClose();
119
120     // called from the client request
121     bool RequestProcess(int32 available_out);
122
123    private:
124     // The request parameters.
125     URL url_;
126     net::HostPort server_;
127     const string user_;
128     const string pass_;
129     const http::ClientParams* http_client_params_;
130
131     // The request done callback.
132     Closure* done_callback_;
133
134    protected:
135     // The actual request.
136     http::ClientRequest* request_;
137     http::ClientStreamingProtocol* protocol_;
138
139     friend class StreamPoster;
140   };
141
142  private:
143   // The associated stream manager.
144   StreamManager* manager_;
145   // The HTTP client params to be used when posting.
146   const http::ClientParams* http_client_params_;
147
148   // The URL to post to.
149   URL url_;
150   // The timeout, in miliseconds,  at which a failed POST should be retried.
151   int retry_timeout_;
152   // If we need tot connect w/ a user/pass we use these :
153   string user_;
154   string pass_;
155   // True if relayed (ie. no encoding in StreamProvider).
156   bool relay_;
157   // True if the outgoing frames should be wrapped into our internal protocol.
158   bool wrapped_;
159   // The encoder type.
160   media::Stream::Type encoder_type_;
161   // Params for the the audio encoder.
162   media::Encoder::Audio::Params audio_params_;
163   // Params for the video encoder.
164   media::Encoder::Video::Params video_params_;
165   // The audio port.
166   int audio_port_;
167   // The video port.
168   int video_port_;
169
170   // The request done callback.
171   Closure* done_callback_;
172
173   // The current request.
174   PosterRequest* request_;
175   // The started/stopped flags.
176   bool started_;
177   bool stopped_;
178  private:
179   DISALLOW_EVIL_CONSTRUCTORS(StreamPoster);
180 };
181
182 #endif   // __WHISPERER_STREAM_POSTER_H__
Note: See TracBrowser for help on using the browser.