• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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 #ifndef MEDIA_WRITER_H_
18 
19 #define MEDIA_WRITER_H_
20 
21 #include <utils/RefBase.h>
22 #include <media/stagefright/MediaSource.h>
23 #include <media/IMediaRecorderClient.h>
24 #include <media/mediarecorder.h>
25 
26 namespace android {
27 
28 struct MediaWriter : public RefBase {
MediaWriterMediaWriter29     MediaWriter()
30         : mMaxFileSizeLimitBytes(0),
31           mMaxFileDurationLimitUs(0) {
32     }
33 
34     // Returns true if the file descriptor is opened using a mode
35     // which meets minimum writer/muxer requirements.
isFdOpenModeValidMediaWriter36     static bool isFdOpenModeValid(int fd) {
37         // check for invalid file descriptor.
38         int flags = fcntl(fd, F_GETFL);
39         if (flags == -1) {
40             ALOGE("Invalid File Status Flags and/or mode : %d", flags);
41             return false;
42         }
43         // fd must be in read-write mode or write-only mode.
44         if ((flags & (O_RDWR | O_WRONLY)) == 0) {
45             ALOGE("File must be writable");
46             return false;
47         }
48         // Verify fd is seekable
49         off64_t off = lseek64(fd, 0, SEEK_SET);
50         if (off < 0) {
51             ALOGE("File descriptor is not seekable");
52             return false;
53         }
54         return true;
55     }
56 
57     virtual status_t addSource(const sp<MediaSource> &source) = 0;
58     virtual bool reachedEOS() = 0;
59     virtual status_t start(MetaData *params = NULL) = 0;
60     virtual status_t stop() = 0;
61     virtual status_t pause() = 0;
setCaptureRateMediaWriter62     virtual status_t setCaptureRate(float /* captureFps */) {
63         ALOG(LOG_WARN, "MediaWriter", "setCaptureRate unsupported");
64         return ERROR_UNSUPPORTED;
65     }
66 
setMaxFileSizeMediaWriter67     virtual void setMaxFileSize(int64_t bytes) { mMaxFileSizeLimitBytes = bytes; }
setMaxFileDurationMediaWriter68     virtual void setMaxFileDuration(int64_t durationUs) { mMaxFileDurationLimitUs = durationUs; }
setListenerMediaWriter69     virtual void setListener(const sp<IMediaRecorderClient>& listener) {
70         mListener = listener;
71     }
72 
dumpMediaWriter73     virtual status_t dump(int /*fd*/, const Vector<String16>& /*args*/) {
74         return OK;
75     }
76 
setStartTimeOffsetMsMediaWriter77     virtual void setStartTimeOffsetMs(int /*ms*/) {}
getStartTimeOffsetMsMediaWriter78     virtual int32_t getStartTimeOffsetMs() const { return 0; }
setNextFdMediaWriter79     virtual status_t setNextFd(int /*fd*/) { return INVALID_OPERATION; }
updateCVODegreesMediaWriter80     virtual void updateCVODegrees(int32_t /*cvoDegrees*/) {}
updatePayloadTypeMediaWriter81     virtual void updatePayloadType(int32_t /*payloadType*/) {}
updateSocketNetworkMediaWriter82     virtual void updateSocketNetwork(int64_t /*socketNetwork*/) {}
getSequenceNumMediaWriter83     virtual uint32_t getSequenceNum() { return 0; }
getAccumulativeBytesMediaWriter84     virtual uint64_t getAccumulativeBytes() { return 0; }
85 
86 protected:
~MediaWriterMediaWriter87     virtual ~MediaWriter() {}
88     int64_t mMaxFileSizeLimitBytes;
89     int64_t mMaxFileDurationLimitUs;
90     sp<IMediaRecorderClient> mListener;
91 
notifyMediaWriter92     void notify(int msg, int ext1, int ext2) {
93         if (msg == MEDIA_RECORDER_TRACK_EVENT_INFO || msg == MEDIA_RECORDER_TRACK_EVENT_ERROR) {
94             uint32_t trackId = (ext1 >> 28) & 0xf;
95             int type = ext1 & 0xfffffff;
96             ALOG(LOG_VERBOSE, "MediaWriter", "Track event err/info msg:%d, trackId:%u, type:%d,"
97                                              "val:%d", msg, trackId, type, ext2);
98         } else {
99             ALOG(LOG_VERBOSE, "MediaWriter", "Recorder event msg:%d, ext1:%d, ext2:%d",
100                                               msg, ext1, ext2);
101         }
102         if (mListener != nullptr) {
103             mListener->notify(msg, ext1, ext2);
104         }
105     }
106 private:
107     MediaWriter(const MediaWriter &);
108     MediaWriter &operator=(const MediaWriter &);
109 };
110 
111 }  // namespace android
112 
113 #endif  // MEDIA_WRITER_H_
114