美文网首页
代码迭代提议

代码迭代提议

作者: wnido | 来源:发表于2017-04-20 17:27 被阅读39次

引文

由于之前开发时间仓促,需求混乱、不明确 ,及公司内部、组内成员等多方面因素影响,我们的代码虽然满足了各方面的需求,实现了强大的功能;但代码本身还有较大优化的空间。为方便以后代码的迭代,和提高代码本身的质量 ,及提高 SDK 本身执行效率和性能,参考多方面资料,特提出此提议。

核心目标

  • MGPlayerController 重新定义
  • 代码规范约定
  • 性能优化
  • 代码优化

MGPlayerController 重新定义

原因

之前的 MGPlayerController 定义混乱 ,层次不够清晰,不仅会造成上层使用不方便 ,还会导致代码迭代困难。

建议

向 iOS 自带 MPMoviePlayerController 类靠近 ,重新定义 MGPlayerController 类 ;并在此基础上定义出更符合我们本身需求的类。主要涉及到以下几个方面:

  1. 分类规则
  2. 类扩展
  3. 协议使用
  4. 类继承
摘录 MPMoviePlayerController 类


#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayerDefines.h>
#import <MediaPlayer/MPMediaPlayback.h>
#import <UIKit/UIKit.h>

@class MPMovieAccessLog, MPMovieErrorLog;

// -----------------------------------------------------------------------------
// Types

typedef NS_ENUM(NSInteger, MPMovieScalingMode) {
 
typedef NS_ENUM(NSInteger, MPMoviePlaybackState) {
  
typedef NS_OPTIONS(NSUInteger, MPMovieLoadState) {

typedef NS_ENUM(NSInteger, MPMovieRepeatMode) {
 
typedef NS_ENUM(NSInteger, MPMovieControlStyle) {
 
typedef NS_ENUM(NSInteger, MPMovieFinishReason) {
  

// -----------------------------------------------------------------------------
// Movie Property Types

typedef NS_OPTIONS(NSUInteger, MPMovieMediaTypeMask) {
 
typedef NS_ENUM(NSInteger, MPMovieSourceType) {
  
// -----------------------------------------------------------------------------
// Movie Player
// 
// See MPMediaPlayback.h for the playback methods.

MP_API_IOS_DEPRECATED_WITH_REPLACEMENT_MACOS_TVOS_PROHIBITED("Use AVPlayerViewController in AVKit.", 2.0, 9.0, 10.12.2, 10.12.2, 2.0, 9.0)
@interface MPMoviePlayerController : NSObject <MPMediaPlayback>

- (instancetype)initWithContentURL:(NSURL *)url NS_DESIGNATED_INITIALIZER;

@property (nonatomic, copy) NSURL *contentURL;

// The view in which the media and playback controls are displayed.
@property (nonatomic, readonly) UIView *view;


@end

// -----------------------------------------------------------------------------
// Movie properties of the current movie prepared for playback.

@interface MPMoviePlayerController (MPMovieProperties)

@end

// -----------------------------------------------------------------------------
// Movie Player Notifications

// Posted when the scaling mode changes.
MP_EXTERN NSString * const MPMoviePlayerScalingModeDidChangeNotification MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(2.0, 9.0, 10.12.2, 10.12.2, 2.0, 9.0);


// -----------------------------------------------------------------------------
// Movie Property Notifications

// Calling -prepareToPlay on the movie player will begin determining movie properties asynchronously.
// These notifications are posted when the associated movie property becomes available.
MP_EXTERN NSString * const MPMovieMediaTypesAvailableNotification MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(3.2, 9.0, 10.12.2, 10.12.2, 3.2, 9.0);

// -----------------------------------------------------------------------------
// Thumbnails

typedef NS_ENUM(NSInteger, MPMovieTimeOption) {
    MPMovieTimeOptionNearestKeyFrame,
    MPMovieTimeOptionExact
} MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(3.2, 9.0, 10.12.2, 10.12.2, 3.2, 9.0);

@interface MPMoviePlayerController (MPMoviePlayerThumbnailGeneration)

@end

// Posted when each thumbnail image request is completed.
MP_EXTERN NSString * const MPMoviePlayerThumbnailImageRequestDidFinishNotification MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(3.2, 9.0, 10.12.2, 10.12.2, 3.2, 9.0);

// -----------------------------------------------------------------------------
// Timed Metadata

@interface MPMoviePlayerController (MPMoviePlayerTimedMetadataAdditions)

// Returns an array of the most recent MPTimedMetadata objects provided by the media stream.
@property (nonatomic, readonly) NSArray *timedMetadata MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.0, 9.0, 10.12.2, 10.12.2, 4.0, 9.0);

@end

MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.0, 9.0, 10.12.2, 10.12.2, 4.0, 9.0)
@interface MPTimedMetadata : NSObject

// A key which identifies a piece of timed metadata.
@property (nonatomic, readonly) NSString *key;

// The namespace of the identifying key.
@property (nonatomic, readonly) NSString *keyspace;

// The object value of the metadata.
@property (nonatomic, readonly) id value;

// The timestamp of the metadata, in the timebase of the media stream.
@property (nonatomic, readonly) NSTimeInterval timestamp;

// A dictionary containing all metadata information associated with this object, which may hold additional key-specific data (see below).
@property (nonatomic, readonly) NSDictionary *allMetadata;

@end

// Posted when new timed metadata arrives.
MP_EXTERN NSString * const MPMoviePlayerTimedMetadataUpdatedNotification MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.0, 9.0, 10.12.2, 10.12.2, 4.0, 9.0);
MP_EXTERN NSString * const MPMoviePlayerTimedMetadataUserInfoKey MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.0, 9.0, 10.12.2, 10.12.2, 4.0, 9.0);       // NSDictionary of the most recent MPTimedMetadata objects.

// -----------------------------------------------------------------------------

@interface MPMoviePlayerController (MPMovieLogging)

// Returns an object that represents a snapshot of the network access log. Can be nil.
@property (nonatomic, readonly) MPMovieAccessLog *accessLog MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0);

// Returns an object that represents a snapshot of the error log. Can be nil.
@property (nonatomic, readonly) MPMovieErrorLog *errorLog MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0);

@end

// -----------------------------------------------------------------------------
// An MPMovieAccessLog accumulates key metrics about network playback and presents them as a collection of MPMovieAccessLogEvent instances.
// Each MPMovieAccessLogEvent instance collates the data that relates to each uninterrupted period of playback.

MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0)
@interface MPMovieAccessLog : NSObject <NSCopying>

// Returns the webserver access log into a textual format that conforms to the W3C Extended Log File Format for web server log files.
// For more information see: http://www.w3.org/pub/WWW/TR/WD-logfile.html
@property (nonatomic, readonly) NSData *extendedLogData;

// Returns the string encoding of the extendedLogData property.
@property (nonatomic, readonly) NSStringEncoding extendedLogDataStringEncoding;

// An ordered collection of MPMovieAccessLogEvent instances that represent the chronological sequence of events contained in the access log.
@property (nonatomic, readonly) NSArray *events;

@end

// -----------------------------------------------------------------------------
// An MPMovieErrorLog provides data to identify if, and when, network resource playback failures occured.

MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0)
@interface MPMovieErrorLog : NSObject <NSCopying>

// Returns the webserver error log into a textual format that conforms to the W3C Extended Log File Format for web server log files.
// For more information see: http://www.w3.org/pub/WWW/TR/WD-logfile.html
@property (nonatomic, readonly) NSData *extendedLogData;

@end

// -----------------------------------------------------------------------------
// An MPMovieAccessLogEvent repesents a single access log entry.

MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0)
@interface MPMovieAccessLogEvent : NSObject <NSCopying>

// A count of media segments downloaded from the server to this client.
@property (nonatomic, readonly) NSUInteger numberOfSegmentsDownloaded;

@end

// -----------------------------------------------------------------------------
// An MPMovieErrorLogEvent repesents a single error log entry.

MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(4.3, 9.0, 10.12.2, 10.12.2, 4.3, 9.0)
@interface MPMovieErrorLogEvent : NSObject <NSCopying>

// The date and time when the error occured.
@property (nonatomic, readonly) NSDate *date;
@end

// -----------------------------------------------------------------------------
// Deprecated methods and properties
// These will be removed in a future release of iOS

@interface MPMoviePlayerController (MPMoviePlayerDeprecated)

// Indicates if the movie player should inherit the application's audio session instead of creating a new session (which would interrupt the application's session).
// Defaults to YES. Setting this property during playback will not take effect until playback is stopped and started again.
@property (nonatomic) BOOL useApplicationAudioSession MP_API_IOS_DEPRECATED_MACOS_TVOS_PROHIBITED(3.2, 6.0, 10.12.2, 10.12.2, 3.2, 6.0);

@end

从代码中,可以看出很多地方是和我们相似的,但他们的定义更合理,更清晰。

代码规范约定

原因

代码规范的重要性,是不言而喻的。好的代码规范,不仅仅能提高代码的可读性,也是团队协作的基础条件,另外也是体现写代码能力的一方面,更通俗点讲是我们 SDK 对外的脸面。

建议

向原生代码靠近,并结合主流代码规范。主要涉及方面:

  1. **命名规则 **
    常量、变量、静态变量、方法、枚举、结构体、宏定义、全局变量、局部变量、文件、文件、类、库等
  2. 代码注释规则
    使用 xcode 自带注释工具,对主要关键点注释
  3. 空行、分块规则
    属性、方法等之间的空行规则;分块主要是 #pragma mark - 的使用
代码摘录
/*********************************************************************
 * 版权所有    MiGu
 *
 * 文件名称:  MGPlayerExtraProperties.h
 * 文件标识:
 * 内容摘要:  播放器额外属性类
 * 其它说明:  sdk外部接口类
 * 作   者:  ciome
 * 创建日期:  2017/4/19
 **********************************************************************/

#import <Foundation/Foundation.h>

@interface MGPlayerExtraProperties : NSObject


/**
 是否使用硬解,默认不使用
 */
@property (nonatomic,assign)    BOOL isHWDecoderEnable;

/**
 http长链接,默认不是长连接
 */
@property (nonatomic,assign)    BOOL isHlsKeepAlive;

/**
 M3u8未读取完就播放,默认不播放
 */
@property (nonatomic,assign)    BOOL isHlsQuickStart;

/**
 是否静音播放,默认非静音
 */
@property (nonatomic,assign)    BOOL isMutePlay;

/**
 Rtmp低延时,默认不开启
 */
@property (nonatomic,assign)    BOOL isRtmpLowLatencyEnable;

/**
 是否自动请求gslb,默认不自动请求
 */
@property (nonatomic,assign)    BOOL isHlsAutoRequestGslb;

/**
 是否支持ipV6,默认不支持
 */
@property (nonatomic,assign)    BOOL isSupportIpV6;

/**
 Hls 超时时间,0~200000000 us,默认30000000 us
 */
@property (nonatomic,assign)    NSInteger hlsTimeoutUsTime;

/**
 Hls直播时,从哪个切片开始播放;[0,-nsegmentcount],默认 0
 */
@property (nonatomic,assign)    NSInteger hlsLiveStartIndex;

/**
 在seeking时缓冲多长时间开始播放,3秒~300秒 , 默认: 20s
 */
@property (nonatomic,assign)    NSInteger minCachedSeekingTime;

/**
 缓冲区时长, 单位 :s,默认 300s
 */
@property (nonatomic,assign)    NSInteger maxBufferCachedTime;

/**
 Flv探测视频大小,[0, 4M] ,单位:字节 , 默认 500 字节
 */
@property (nonatomic,assign)    NSInteger flvProbeSize;


/**
 Airplay激活超时时间 ,默认 12 s
 */
@property (nonatomic,assign)    NSInteger airplayActiveTimeoutTime;


/**
 这个是由域名:ip组成,比如cnclive.miguvideo.com:117.144.227.41,表示cnclive.miguvideo.com所对应的
 */
@property (nonatomic,copy)      NSString *domainAndIpStr;



@end


性能优化

原因

因为之前一直在抓需求,功能实现,并没有特别对性能优化一块做专门的精研,性能优化这一块,我们仍有很大的空间。

建议

影响性能主要表现在内存管理CPU使用率,具体表现在响应速度耗电量。如何高效合理的使用内存和高效率使用CPU,涉及的主要方面:

  • 修饰符的使用
    strong、retain、weak、assing、nonatomic、atomic、readwrite、readonly、copy、unsafe_unretained、synthesize、dynamic 等
  • 内存使用
    避免不必要的内存分配和销毁,减少必要的内存分配和销毁
  • 多线程使用
    在 CPU 空闲时,充分利用 CPU ,提高响应速度

代码优化

原因

因为开发初期的需求混乱、时间仓促等多方面原因,使得代码的结构上并没有一个很好的模块化。

建议

模块化、低耦合

结束语

限于个人对于 iOS 代码的理解,和本自身代码能力,上述可能写的并不完整,敬请谅解

相关文章

网友评论

      本文标题:代码迭代提议

      本文链接:https://www.haomeiwen.com/subject/reblzttx.html