2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
#ifndef MEDIA_BASE_STATUS_H_
|
|
|
|
#define MEDIA_BASE_STATUS_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
namespace error {
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Error codes for the packager APIs.
|
2013-10-11 21:44:55 +00:00
|
|
|
enum Code {
|
|
|
|
// Not an error; returned on success
|
|
|
|
OK,
|
|
|
|
|
|
|
|
// Unknown error. An example of where this error may be returned is
|
|
|
|
// errors raised by APIs that do not return enough error information
|
|
|
|
// may be converted to this error.
|
|
|
|
UNKNOWN,
|
|
|
|
|
|
|
|
// The operation was cancelled (typically by the caller).
|
|
|
|
CANCELLED,
|
|
|
|
|
|
|
|
// Client specified an invalid argument. INVALID_ARGUMENT indicates
|
|
|
|
// arguments that are problematic regardless of the state of the system
|
|
|
|
// (e.g. a malformed file name).
|
|
|
|
INVALID_ARGUMENT,
|
|
|
|
|
|
|
|
// Operation is not implemented or not supported/enabled.
|
|
|
|
UNIMPLEMENTED,
|
|
|
|
|
|
|
|
// Cannot open file.
|
|
|
|
FILE_FAILURE,
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
// End of stream.
|
|
|
|
END_OF_STREAM,
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2013-12-16 17:13:29 +00:00
|
|
|
// Failure to get HTTP response successfully,
|
|
|
|
HTTP_FAILURE,
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
// Unable to parse the media file.
|
|
|
|
PARSER_FAILURE,
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
// Fail to mux the media file.
|
|
|
|
MUXER_FAILURE,
|
|
|
|
|
|
|
|
// This track fragment is finalized.
|
|
|
|
FRAGMENT_FINALIZED,
|
|
|
|
|
2014-01-14 04:52:05 +00:00
|
|
|
// Server errors. Receives malformed response from server.
|
|
|
|
SERVER_ERROR,
|
|
|
|
|
|
|
|
// Internal errors. Some invariants have been broken.
|
|
|
|
INTERNAL_ERROR,
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace error
|
|
|
|
|
|
|
|
class Status {
|
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Creates a "successful" status.
|
2013-10-11 21:44:55 +00:00
|
|
|
Status() : error_code_(error::OK) {}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Create a status with the specified code, and error message.
|
|
|
|
/// If "error_code == error::OK", error_message is ignored and a Status
|
|
|
|
/// object identical to Status::OK is constructed.
|
2013-11-12 20:32:44 +00:00
|
|
|
Status(error::Code error_code, const std::string& error_message)
|
|
|
|
: error_code_(error_code) {
|
2013-10-11 21:44:55 +00:00
|
|
|
if (!ok())
|
|
|
|
error_message_ = error_message;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Status() {}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @name Some pre-defined Status objects.
|
|
|
|
/// @{
|
2013-11-12 20:32:44 +00:00
|
|
|
static const Status& OK; // Identical to 0-arg constructor.
|
2013-10-11 21:44:55 +00:00
|
|
|
static const Status& UNKNOWN;
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Store the specified error in this Status object.
|
|
|
|
/// If "error_code == error::OK", error_message is ignored and a Status
|
|
|
|
/// object identical to Status::OK is constructed.
|
2013-10-11 21:44:55 +00:00
|
|
|
void SetError(error::Code error_code, const std::string& error_message) {
|
|
|
|
if (error_code == error::OK) {
|
|
|
|
Clear();
|
|
|
|
} else {
|
|
|
|
error_code_ = error_code;
|
|
|
|
error_message_ = error_message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// If "ok()", stores "new_status" into *this. If "!ok()", preserves
|
|
|
|
/// the current "error_code()/error_message()",
|
|
|
|
///
|
|
|
|
/// Convenient way of keeping track of the first error encountered.
|
|
|
|
/// Instead of:
|
|
|
|
/// if (overall_status.ok()) overall_status = new_status
|
|
|
|
/// Use:
|
|
|
|
/// overall_status.Update(new_status);
|
2013-10-11 21:44:55 +00:00
|
|
|
void Update(const Status& new_status) {
|
|
|
|
if (ok())
|
|
|
|
*this = new_status;
|
|
|
|
}
|
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Clear this status object to contain the OK code and no error message.
|
2013-10-11 21:44:55 +00:00
|
|
|
void Clear() {
|
|
|
|
error_code_ = error::OK;
|
|
|
|
error_message_ = "";
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:32:44 +00:00
|
|
|
bool ok() const { return error_code_ == error::OK; }
|
|
|
|
error::Code error_code() const { return error_code_; }
|
|
|
|
const std::string& error_message() const { return error_message_; }
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
bool operator==(const Status& x) const {
|
|
|
|
return error_code_ == x.error_code() && error_message_ == x.error_message();
|
|
|
|
}
|
2013-11-12 20:32:44 +00:00
|
|
|
bool operator!=(const Status& x) const { return !(*this == x); }
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return true iff this has the same error_code as "x", i.e., the two
|
|
|
|
/// Status objects are identical except possibly for the error
|
|
|
|
/// message.
|
2013-11-12 20:32:44 +00:00
|
|
|
bool Matches(const Status& x) const { return error_code_ == x.error_code(); }
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @return A combination of the error code name and message.
|
2013-10-11 21:44:55 +00:00
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
void Swap(Status* other) {
|
|
|
|
error::Code error_code = error_code_;
|
|
|
|
error_code_ = other->error_code_;
|
|
|
|
other->error_code_ = error_code;
|
|
|
|
error_message_.swap(other->error_message_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
error::Code error_code_;
|
|
|
|
std::string error_message_;
|
|
|
|
|
|
|
|
// Not using DISALLOW_COPY_AND_ASSIGN here intentionally to allow the compiler
|
|
|
|
// generated copy constructor and assignment operator.
|
|
|
|
};
|
|
|
|
|
|
|
|
extern std::ostream& operator<<(std::ostream& os, const Status& x);
|
|
|
|
|
|
|
|
// Status success comparison.
|
|
|
|
// This is better than CHECK((val).ok()) because the embedded
|
|
|
|
// error string gets printed by the CHECK_EQ.
|
|
|
|
#define CHECK_OK(val) CHECK_EQ(Status::OK, (val))
|
|
|
|
#define DCHECK_OK(val) DCHECK_EQ(Status::OK, (val))
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
|
|
|
|
#endif // MEDIA_BASE_STATUS_H_
|